Skip to main content

Subsection 10.3.11 Part 8c: The Rest of the Driver

SevenSegPartial.c has three functions left in it, and their prototypes are already in the header. Two use the one-byte write style you used in SevenSeg_init() in order to send commands to the display. SevenSeg_write() puts all ten display bytes into the display RAM in one transaction.
void SevenSeg_init();                           // Initialize the display
void SevenSeg_blink(uint8_t rate);              // Blink the display
void SevenSeg_dim(uint8_t brightness);          // Dim the display
void SevenSeg_write(uint8_t *display_buffer);   // Write the display RAM
void SevenSeg_number(uint16_t num, uint8_t *display_buffer);
                                                // Convert a number to segments
SevenSeg_blink() and SevenSeg_dim() are each one call to i2c1_byteWrite(), sending the display-setup and dimming commands you derived earlier, with the caller’s option bits ORed in. Mask the argument to the bits the command actually has (0x6 for the two blink bits, 0xf for the four brightness bits), so that a caller passing something out of range cannot corrupt the command itself.
SevenSeg_write() is where the entire display RAM is written. The function receives a pointer to a ten-byte buffer which holds the desired LED pattern, and it has to get all ten bytes into the display RAM starting at address 0, in one transaction. Everything needed for that is in i2c1_memWrite()’s four arguments; the function is one line.

Activity 10.3.7. Finish the Driver and Show ES.28.

You’ll work in SevenSegPartial.c. Its header file defines everything you need. HT16K33_ADDR is the display’s I2C address, and the command names are the ones we derived from the command table. HT16K33_ADDR_PTR is the address of the first byte of the display RAM, which is where a full write starts. HT16K33_NBUF is the number of digit-and-colon positions on the display, which is five; since each position takes two bytes, the buffer you send is 2*HT16K33_NBUF bytes long.

(a)

Write SevenSeg_blink() and SevenSeg_dim(). Each should be one line, namely one call to i2c1_byteWrite(), sending the display-setup and dimming commands you derived earlier with the desired dimming/blink rate option bits ORed in. Mask the argument to the bits the command actually has (0x6 for the two blink bits, 0xf for the four brightness bits), so an out-of-range argument cannot corrupt the command itself.

(c)

In a new main() (you might start a new file for this and exclude writeFirstDigit.c from the resource configuration), declare uint8_t display_buffer[2*HT16K33_NBUF];, fill it so the display reads ES.28, and call SevenSeg_write(). For the 2 and the 8 use numbertable[].
Three things about that listing. The odd-numbered bytes are the zeros from the RAM map. They carry nothing; they are sent because the pointer advances on every byte that arrives, so skipping them would land digit 2’s pattern at address 1. 0b11101101 is an S with bit 7 set, which is the decimal point: one buffer entry produces both. And this main() has no while(1): it runs once, sets the display, and stops. The display keeps showing ES.28 afterwards, because the HT16K33 is still sweeping its RAM whether or not the STM32C031C6 is doing anything at all. That is the whole argument for the backpack, visible on your desk.
Nothing, or the wrong thing, on the display? Work down this list:
  1. Does writeFirstDigit.c still work? If yes, the bus, the init and the RAM addressing are all fine and the fault is in SevenSeg_write() or in how the buffer was filled.
  2. A display showing four garbled characters could mean that your buffer is shifted: first check whether you sent 2*HT16K33_NBUF bytes and started at HT16K33_ADDR_PTR, before checking whether you got the segment patterns wrong.
  3. Nothing at all, and writeFirstDigit.c also shows nothing: put the AD2 on SDA and SCL in oscilloscope mode, as yesterday, arm a single sweep and reset the board. No activity at all means the scope is not seeing the bus or the program never reaches an I2C call: check the probes (channel 1 on SCL, channel 2 on SDA), the trigger (channel 2, falling, 1 V), that the board was flashed, and that i2c1_init() runs. Nine clock pulses with SDA HIGH on the ninth means nobody answered, so check that the display is properly wired and powered and check the address you passed. SDA LOW on every ninth pulse means the display is responding, so the fault is in your code: check that SevenSeg_init() is called (use the debugger to step through your code) and that its three commands are the ones you derived.
  4. Still stuck? We’re here to help!