Subsection 10.3.10 Part 8b: SevenSeg_init(), and One Digit
You put a character on a digit yesterday, using a program that had the three setup commands written into it for you. Now you will package versions of these three commands into an initialization function, which is the first function of the device driver you need to write.
Three files are provided:
SevenSegPartial.c,
SevenSegPartial.h and
writeFirstDigit.c. The first two are the device driver for the display, and as the name suggests they are not complete.
SevenSegPartial.c contains four empty functions with their signatures provided. Start by completing the first; we’ll get to the other three afterwards (
Subsection 10.3.11).
#include "ES28.h"
#include "i2c.h"
#include "SevenSegPartial.h"
// Initialize the display: oscillator on, display on and steady, full brightness
void SevenSeg_init() {
// TODO 1 -- three one-byte writes, in this order: system setup with the
// oscillator on, display setup with the display on and not
// blinking, and dimming set to full brightness. Build each
// byte out of the names in SevenSegPartial.h.
}
// Change the display flashing
void SevenSeg_blink(uint8_t rate) {
// TODO 2 -- one byte: the display-setup command, the display-on bit, and
// the caller's rate masked to the two blink bits.
}
// Dim the display
void SevenSeg_dim(uint8_t brightness) {
// TODO 3 -- one byte: the dimming command with the caller's brightness
// masked to the four brightness bits.
}
// Write the display RAM
void SevenSeg_write(uint8_t *display_buffer) {
// display_buffer is size 2*HT16K33_NBUF (low byte, high byte)
// TODO 4 -- one call. Every byte in ONE transaction, starting at display
// RAM address HT16K33_ADDR_PTR. Do not touch an I2C register
// directly -- go through the i2c library.
}
To test your initialization code, you are given the third file,
writeFirstDigit.c. It puts one character into one digit.
#include "ES28.h"
#include "i2c.h"
#include "SevenSegPartial.h"
int main(void) {
uint8_t display_addr = HT16K33_ADDR;
uint8_t display_subaddr = 0; // 0, 2, 6, 8 - depending on the digit (4 is the colon)
uint8_t digit_data = 0b01111001; // 'E'
i2c1_init();
SevenSeg_init();
while(1) {
i2c1_memWrite(display_addr, display_subaddr, 1, &digit_data);
delay_ms(250);
}
return 0;
}
The arguments passed to
i2c1_memWrite() are first the address of the device to be talked to (
display_addr in this case), next where on the target the data should go (
display_subaddr in this case, the display RAM address the pointer starts at), how many bytes follow (
1 here, because we are only writing one digit), and finally the data itself (
&digit_data, where those bytes are stored). Here, the data
0b01111001 is an
E: bits 0, 3, 4, 5 and 6 are on, so that lights up LED segments
a d e f g.
Activity 10.3.6. Write One Digit, Then Another.
Three files are in the book’s starter files:
SevenSegPartial.c,
SevenSegPartial.h and
writeFirstDigit.c. Download all three, set up the project, then complete the below tasks.
(a)
Copy
TemplateProject to a new project named
SevenSegI2CSecondSteps.
(b)
Put
SevenSegPartial.c and
writeFirstDigit.c in that project’s
Src folder, and
SevenSegPartial.h in its
Inc folder: the
.c files in
Src, the
.h file in
Inc. Build and flash it before you change anything, then briefly disconnect power from the display: the display keeps its settings from
helloDisplay.c for as long as it has power. It should now do nothing.
(c)
Fill in TODO 1,
SevenSeg_init(), with the three commands you derived from the command table, written using the header’s names, not as 0x21, 0x81 and 0xEF. Build, flash, and confirm the first digit shows an
E.
(d)
Move it: change one line so the
E appears on the second digit, then the third, then the fourth. The digits are at
display_subaddr 0, 2, 6 and 8 (4 is the colon).
(e)
Change what it says: make the digit show an
S, then a
2, then an
8. Derive each pattern from
Figure 10.1.22.
Three transactions, not one: each is a separate command, and the HT16K33 wants each one addressed to it in its own right. The oscillator command is the one that has to come first: until the oscillator is running the chip drives nothing at all. Part 9 asks you what happens if this command goes last instead.
Nothing lights up? If
helloDisplay.c worked at the start of class, the fault is in your code, not your wiring: check that
SevenSeg_init() is actually called, and that the oscillator command is in it (see above).
To test your initialization code, you are given the third file,
writeFirstDigit.c. It puts one character into one digit.
#include "SevenSegPartial.h" // HT16K33_ADDR and the command names live here
int main(void) {
uint8_t display_addr = HT16K33_ADDR;
uint8_t display_subaddr = 0; // 0, 2, 6, 8 - depending on the digit (4 is the colon)
uint8_t digit_data = 0b01111001; // 'E'
i2c1_init();
SevenSeg_init();
while(1) {
i2c1_memWrite(display_addr, display_subaddr, 1, &digit_data);
delay_ms(250);
}
return 0;
}
-
The arguments passed to i2c1_memWrite() are first the address of the device to be talked to (display_addr in this case), next where on the target the data should go (display_subaddr in this case), how many bytes follow (1 here), and finally the data itself. Here, the data 0b01111001 is an E: bits 0, 3, 4, 5 and 6 are on, so that lights up LED segments a d e f g.
Three files are in the book’s starter files:
SevenSegPartial.c,
SevenSegPartial.h and
writeFirstDigit.c. The first two are the device driver for the display, and as the name suggests they are not complete.
SevenSegPartial.c contains four empty functions with their signatures provided. Start by completing the first; we’ll get to the other three afterwards.
void SevenSeg_init() {
// TODO 1 -- three one-byte writes, in this order: system setup with the
// oscillator on, display setup with the display on and not
// blinking, and dimming set to full brightness. Build each
// byte out of the names in SevenSegPartial.h.
}
The three commands you derived earlier today, now written using the header’s names rather than as 0x21, 0x81 and 0xEF.