0

I am trying to convert an integer to 3 bytes to send via the SPI protocol to control a MAX6921 serial chip. I am using spi.xfer2() but cannot get it to work as expected.

In my code, I can call spi.xfer2([0b00000000, 0b11101100, 0b10000000]) it displays the letter "H" on my display, but when I try and convert the int value for this 79616, it doesn't give the correct output:

val = 79616
spi.xfer2(val.to_bytes(3, 'little'))

My full code so far is on GitHub, and for comparison, this is my working code for Arduino.

More details

I have an IV-18 VFD tube driver module, which came with some example code for an ESP32 board. The driver module has a 20 output MAX6921 serial chip to drive the vacuum tube.

To sent "H" to the second grid position (as the first grid only displays a dot or a dash) the bits are sent to MAX6921 in order OUT19 --> OUT0, so using the LSB in my table below. The letter "H" has an int value 79616

I can successfully send this, manually, via SPI using:

spi.xfer2([0b00000000, 0b11101100, 0b10000000])

The problem I have is trying to convert other letters in a string to the correct bits. I can retrieve the integer value for any character (0-9, A-Z) in a string, but can't then work out how to convert it to the right format for spi.xfer() / spi.xfer2()

My Code

def display_write(val):
    spi.xfer2(val.to_bytes(3, 'little'))

# Loops over the grid positions
def update_display():
    global grid_index

    val = grids[grid_index] | char_to_code(message[grid_index:grid_index+1])

    display_write(val)

    grid_index += 1
    if (grid_index >= 9):
        grid_index = 0

The full source for my code so far is on GitHub

Map for MAX6921 Data out pins to the IV-18 tube pins:

BIT 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
IV-18 G9 G8 G7 G6 G5 G4 G3 G2 G1 A B C D E F G DP
MAX6921 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

IV-18 Pinout diagram

0andriy
  • 4,183
  • 1
  • 24
  • 37
Alexander Holsgrove
  • 1,795
  • 3
  • 25
  • 54
  • This is a general programming question, not a Pi specific question. – joan May 26 '21 at 15:02
  • @joan apologies - I didn't know if there was anything RPi specific in the way I use spidev. Can you flag to migrate this to SO please, or am I better to close this and open a new question there instead? – Alexander Holsgrove May 26 '21 at 15:18
  • Does this answer your question? [Reversing bits of Python integer](https://stackoverflow.com/questions/12681945/reversing-bits-of-python-integer) – 0andriy May 28 '21 at 18:04
  • `for i in map(lambda y: int('{:08b}'.format(y)[::-1], 2), int(79616).to_bytes(3, 'little')): print("%02x" % i)` --> `00 ec 80` which is what you want. – 0andriy May 28 '21 at 18:07

0 Answers0