Hello,
I am trying to display the number 1234 on the display in the link below:
I am converting the value into 4 separate bytes and send them one at a time. Also I am getting an error on where I am shifting the bytes in the transfer. The display does not display the number.
What part am I missing?
Thanks
// Segment labelling:
// A
// ----
// F | | B
// ---- G
// E | | C
// .DP ----
// D
#include <SPI.h> // include the SPI library
byte valuetobyte(); // function valuetobyte
int num=0; // set value to be initial zero
int value;
int dig1 = 0;
int dig2 = 0;
int dig3 = 0;
int dig4 = 0;
//G F A B C D E DP
byte zero = 0b01111110; // every value on display to binary
byte one = 0b00011000;
byte two = 0b10110110;
byte three = 0b10111100;
byte four = 0b11011000;
byte five = 0b11101100;
byte six = 0b11101110;
byte seven = 0b00111000;
byte eight = 0b11111110;
byte nine = 0b11111100;
byte dash = 0b10000000;
byte blank = 0b00000000;
const int SlaveSelectPin = 10; // set pin 10 as the slave select pin (SS)
void setup() {
pinMode(SS, OUTPUT); // set SS as an output
digitalWrite(SS, HIGH); // set the SS pin to high
SPI.begin (); // start the SPI
SPI.beginTransaction(SPISettings (1500000, LSBFIRST, SPI_MODE1));
}
void loop()
{
int num; // define variables
byte displaydigit1;
byte displaydigit2;
byte displaydigit3;
byte displaydigit4;
num = 1234;
dig1 = num / 1000; // the thousandths digit
num = num - (dig1 * 1000);
dig2 = num / 100; // the hundredths digit
num = num - (dig2 * 100);
dig3 = num / 10; // the tenths digit
dig4 = num - (dig3 *10); // the ones digit
Serial.begin (9600);
displaydigit1 = Serial.println(valuetobyte(dig1), BIN);
displaydigit2 = Serial.println(valuetobyte(dig2), BIN);
displaydigit3 = Serial.println(valuetobyte(dig3), BIN);
displaydigit4 = Serial.println(valuetobyte(dig4), BIN);
digitalWrite(SS, LOW); // set SS low to send data
SPI.transfer(displaydigit1>>24); // transfer the highest 8 bits
SPI.transfer(displaydigit2>>16);
SPI.transfer(displaydigit3>>8);
SPI.transfer(displaydigit4);
digitalWrite(SS, HIGH);
delay(1000);
//return(0);
}
byte valuetobyte(int value) { // function that takes a value and converts it into a byte
byte bytevalue;
//int value;
switch (value) {
case 0: // if the value is 0, then return the byte value of zero defined above
if(value == 0){
bytevalue = zero;
return(bytevalue);
break; }
case 1:
if(value == 1) {
bytevalue = one;
return(bytevalue);
break; }
case 2:
if(value == 2) {
bytevalue = two;
return(bytevalue);
break; }
case 3:
if(value == 3) {
bytevalue = three;
return(bytevalue);
break; }
case 4:
if(value == 4) {
bytevalue = four;
return(bytevalue);
break; }
case 5:
if(value == 5) {
bytevalue = five;
return(bytevalue);
break; }
case 6:
if(value == 6) {
bytevalue = six;
return(bytevalue);
break; }
case 7:
if(value == 7) {
bytevalue = seven;
return(bytevalue);
break; }
case 8:
if(value == 8) {
bytevalue = eight;
return(bytevalue);
break; }
case 9:
if(value == 9) {
bytevalue = nine;
return(bytevalue);
break; }
default:
bytevalue = blank;
return(bytevalue);
break;
}
}