Notifications
Clear all

Example Sketch: Seeeduino xiao as Serial Backpack for HT16K33-7 Seg Display

2 Posts
1 Users
0 Likes
1,335 Views
 dw
(@dw)
Member
Joined: 3 years ago
Posts: 15
Topic starter  

Hello all,

I'm experimenting with the xiao these days - and thought I would dabble with using one as a possible slave for running an HT16K33 7-segment display via Serial control.   Sometimes I play with PICs and having a Serial controlled display makes things easier for PIC development.

Anyway - here is my first working draft.

I'd be happy to entertain any suggestions about how to make it more compact or efficient - if anyone is so included.  Otherwise - here is a working sketch for the Seeeduino xiao

------------------------------------------------------------------------------------

/*
Controlling the HT16K33 4 digit 7 segment driver
For any given number to display, the system must send a sequence of 17 bytes

Byte (2 of 17) contains the value for the 1000ths place digit
If the number to display is less than 4 digits, this byte = 0

Byte (4 of 17) contains the value for the 100ths place digit
If the number to display is less than 3 digits, this byte = 0

Byte (8 of 17) contains the value for the 10sth place digit
If the number to display is less than 2 digits, this byte = 0

Byte (10 of 17) contains the value for the 1s place digit

The following bytes represent the value of 1010
0x00 0x06 0x00 0x3F 0x00 0x00 0x00 0x06 0x00 0x3F 0x00 0x00 0x00 0x00 0x00 0x00 0x00
*
//NUMERIC VALUES
0x3F 63 = 0
0x06 6 = 1
0x5B 91 = 2
0x4F 79 = 3
0x66 102 = 4
0x6D 109 = 5
0x7D 125 = 6
0x07 7 = 7
0x7F 127 = 8
0x6F 111 = 9
*/

#include <Wire.h>
#include "Adafruit_LEDBackpack.h" // Seven Segment display library

int incomingNumber;
char input[5]; // to hold incoming number
static uint8_t digits;
char incomingcharacter;
Adafruit_7segment matrix = Adafruit_7segment();
byte segment_address = 0x70; //This is hex address of the 7 segment display

//INIT the display
void setBrightness(uint16_t b, byte segment_address) {
if (b > 16) b = 16; //Max brightness on this display is 16.
if (b < 1) b = 1; // Brightness of 0 is too dim so make 1 the min.
Wire.beginTransmission(segment_address);
Wire.write(0xE0 | b); // write the brightness value to the hex address.
Wire.endTransmission();
}

void setup() {
Wire.begin();
Serial.begin(9600);
Serial1.begin(9600);
matrix.begin(segment_address);
setBrightness(8, segment_address);
matrix.println(0);
matrix.writeDisplay();
}

void loop() {
// Software Serial RX incoming pin = A7

if (Serial1.available()) {
incomingcharacter = Serial1.read ();

if (incomingcharacter != '\n' && digits < 15 ){ // \n is the line feed ending the incoming number
input[digits] = incomingcharacter;
digits++;
}
else
{

switch (digits) {
case 1:
incomingNumber = input[0] - 48;
break;
case 2:
incomingNumber = input[1] - 48;
incomingNumber = incomingNumber + (input[0] - 48) * 10;
break;
case 3:
incomingNumber = input[2] - 48;
incomingNumber = incomingNumber + (input[1] - 48) * 10;
incomingNumber = incomingNumber + (input[0] - 48) * 100;
break;
case 4:
incomingNumber = input[3] - 48;
incomingNumber = incomingNumber + (input[2] - 48) * 10;
incomingNumber = incomingNumber + (input[1] - 48) * 100;
incomingNumber = incomingNumber + (input[0] - 48) * 1000;
}
// Serial1.write(incomingNumber); //for debugging
// Serial.println(incomingNumber);

// Display incoming number to 7 Segment
matrix.println(incomingNumber);
//Serial.println(incomingNumber); //for debugging

matrix.writeDisplay(); // Write the value to the 7 segment display.
input[0] = '\0'; //resets the char array
digits = 0; //reset digits
incomingNumber = 0;

}// End of else
}// End of if Serial1.available
}// end of MAIN loop


   
Quote
 dw
(@dw)
Member
Joined: 3 years ago
Posts: 15
Topic starter  

   
ReplyQuote