first working arduino test code for one display unit
|
@ -1,206 +1,136 @@
|
||||||
|
|
||||||
//Pin Assignments (You should change these)
|
#define CLK 2 //Connected to TPIC pin 13: SRCLK (aka Clock)
|
||||||
const int CLK = 9; //Connected to TPIC pin 13: SRCLK (aka Clock)
|
#define LATCH 3 //Connected to TPIC pin 12: RCLK (aka Latch/load/CS/SS...)
|
||||||
const int LATCH = 10; //Connected to TPIC pin 12: RCLK (aka Latch/load/CS/SS...)
|
#define DOUT 4 //Connected to TPIC pin 3: SER (aka MOSI)
|
||||||
const int OE = 11; //Connected to TPIC pin 9: OE (Output Enable)
|
#define SEG_0 A0
|
||||||
const int DOUT = 12; //Connected to TPIC pin 3: SER (aka MOSI)
|
#define SEG_1 A1
|
||||||
|
#define SEG_2 A2
|
||||||
|
#define SEG_3 A3
|
||||||
|
#define SEG_4 A4
|
||||||
|
#define SEG_5 A5
|
||||||
|
#define SEG_6 11
|
||||||
|
#define SEG_7 12
|
||||||
|
|
||||||
//Number Patterns (0-9)
|
#define FET_COUNT 8
|
||||||
//***Drains 0-7 must be connected to segments A-DP respectively***
|
#define BYTE_PER_FET 6
|
||||||
const byte numTable[] =
|
|
||||||
{
|
|
||||||
B11111100,
|
|
||||||
B01100000,
|
|
||||||
B11011010,
|
|
||||||
B11110010,
|
|
||||||
B01100110,
|
|
||||||
B10110110,
|
|
||||||
B10111110,
|
|
||||||
B11100000,
|
|
||||||
B11111110,
|
|
||||||
B11110110
|
|
||||||
};
|
|
||||||
|
|
||||||
//Global Variables
|
volatile uint8_t data[FET_COUNT][BYTE_PER_FET];
|
||||||
int numDevices = 1; //The number of x-digit display modules you plan to use
|
long dataMillis = 0;
|
||||||
int maxDisplays = 3; //The maximum displays that could be accommodated (see note 1)
|
uint8_t dataDemoStep = 0;
|
||||||
int maxDigits = 3; //The maximum digits you plan on displaying per display module (each SR can handle a max of 8 digits)
|
|
||||||
int SRData[3][3]; //The storage location for the digit information. We must specify a fixed array at compile time (see note 2)
|
|
||||||
boolean debug = true; //Change to true to print messages
|
|
||||||
int delayTime = 1000; //Optional (just for demonstrating multiplexing)
|
|
||||||
|
|
||||||
/*
|
void setup() {
|
||||||
Notes
|
Serial.begin(115200);
|
||||||
1. It is recommended to use an external power supply to avoid oversource/sinking the microcontroller
|
|
||||||
or if you need to power high voltage, high current displays. This code will turn on/off all segments in a digit for ***each*** display.
|
|
||||||
So, if using 2x 3-digit displays all displaying an 8 + DP, the max consumption will be:
|
|
||||||
20mA (desired forward current) * 8 (segments that are on) * 2 (displays showing identical info) = 320mA
|
|
||||||
2. The first dimension should equal maxDisplays. The second dimension should equal the number of digits
|
|
||||||
*/
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
//Set pin modes
|
|
||||||
pinMode(CLK,OUTPUT);
|
pinMode(CLK,OUTPUT);
|
||||||
pinMode(LATCH,OUTPUT);
|
pinMode(LATCH,OUTPUT);
|
||||||
pinMode(DOUT, OUTPUT);
|
pinMode(DOUT, OUTPUT);
|
||||||
pinMode(OE, OUTPUT);
|
pinMode(SEG_0, OUTPUT);
|
||||||
|
pinMode(SEG_1, OUTPUT);
|
||||||
|
pinMode(SEG_2, OUTPUT);
|
||||||
|
pinMode(SEG_3, OUTPUT);
|
||||||
|
pinMode(SEG_4, OUTPUT);
|
||||||
|
pinMode(SEG_5, OUTPUT);
|
||||||
|
pinMode(SEG_6, OUTPUT);
|
||||||
|
pinMode(SEG_7, OUTPUT);
|
||||||
|
|
||||||
//7-Segment Display Init
|
|
||||||
digitalWrite(OE,LOW); //Enables SR Operation
|
|
||||||
initializeSRData(); //Prepares SR and clears data on serial line
|
initializeSRData(); //Prepares SR and clears data on serial line
|
||||||
|
|
||||||
//Test
|
|
||||||
setDigit(0,0,4,true);
|
|
||||||
setDigit(0,1,5,true);
|
|
||||||
setDigit(0,2,6,true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop() {
|
||||||
{
|
|
||||||
|
if(millis() - dataMillis > 10) {
|
||||||
|
static uint8_t counterFet = 0;
|
||||||
|
static uint8_t counterDigit = 0;
|
||||||
|
static uint8_t counterBit = 0;
|
||||||
|
|
||||||
|
for(int fet=0; fet<FET_COUNT; fet++) {
|
||||||
|
|
||||||
|
for(int digit=0; digit<BYTE_PER_FET; digit++) {
|
||||||
|
|
||||||
|
if(fet == counterFet && digit == counterDigit) {
|
||||||
|
data[fet][digit] = _BV(counterBit);
|
||||||
|
} else {
|
||||||
|
data[fet][digit] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
counterBit++;
|
||||||
|
if(counterBit > 7) {
|
||||||
|
counterBit = 0;
|
||||||
|
counterDigit++;
|
||||||
|
|
||||||
|
if(counterDigit > 5) {
|
||||||
|
counterFet++;
|
||||||
|
counterDigit = 0;
|
||||||
|
counterFet %= FET_COUNT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
dataMillis = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
refreshDisplay(); //Cycles through all displays and digits
|
refreshDisplay(); //Cycles through all displays and digits
|
||||||
}
|
}
|
||||||
|
|
||||||
//==========BEGIN SR Functions==========
|
void initializeSRData() {
|
||||||
void initializeSRData()
|
|
||||||
{
|
digitalWrite(LATCH,HIGH); //Tells all SRs that uController is sending data
|
||||||
//Display Scanner (Iterates through each display module)
|
|
||||||
digitalWrite(LATCH,LOW); //Tells all SRs that uController is sending data
|
|
||||||
|
|
||||||
for(int dispID = 0; dispID < maxDisplays; dispID++)
|
for(int digit = 0; digit < BYTE_PER_FET; digit++) {
|
||||||
{
|
shiftOut(DOUT, CLK, LSBFIRST,0);
|
||||||
//Digit Scanner (Iterates through each SR (digit) in a display module)
|
|
||||||
for(int digit = 0; digit < maxDigits; digit++)
|
|
||||||
{
|
|
||||||
//Clears any garbage on the serial line
|
|
||||||
shiftOut(DOUT,CLK,LSBFIRST,0); //Shift out 0s to all displays
|
|
||||||
SRData[dispID][digit] = 0; //Stores a 0 for each digit so its completely off
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
digitalWrite(LATCH,HIGH); //Tells all SRs that uController is done sending data
|
|
||||||
|
digitalWrite(LATCH,LOW); //Tells all SRs that uController is done sending data
|
||||||
}
|
}
|
||||||
|
|
||||||
void printSRData()
|
|
||||||
{
|
|
||||||
if(!debug)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Serial.println("Printing SR Data...");
|
|
||||||
|
|
||||||
//Display Scanner
|
|
||||||
for(int dispID = 0; dispID < maxDisplays; dispID++)
|
|
||||||
{
|
|
||||||
Serial.print("Display # ");
|
|
||||||
Serial.println(dispID);
|
|
||||||
|
|
||||||
//Digit Scanner
|
void clearDisplay(int dispID) {
|
||||||
for(int digit = 0; digit < maxDigits; digit++)
|
|
||||||
{
|
|
||||||
Serial.print("Digit ");
|
|
||||||
Serial.print(digit);
|
|
||||||
Serial.print(": ");
|
|
||||||
Serial.println(SRData[dispID][digit],BIN);
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setDigit(int dispID, int digit, int value, boolean dp)
|
|
||||||
{
|
|
||||||
//Parameter checker
|
|
||||||
if(dispID < 0 || dispID >= numDevices)
|
|
||||||
{
|
|
||||||
Serial.println("dispID OoB!"); //OoB = Out of bounds
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(digit < 0 || digit > maxDigits)
|
|
||||||
{
|
|
||||||
Serial.println("digit OoB!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(value < 0 || value > 9)
|
|
||||||
{
|
|
||||||
Serial.println("Invalid value!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
value = numTable[value];
|
|
||||||
|
|
||||||
//Toggle dp if needed
|
|
||||||
if(dp)
|
|
||||||
value |= B00000001; //Turns on the first binary digit (segment) using an OR bitmask
|
|
||||||
|
|
||||||
//Store the digit
|
|
||||||
SRData[dispID][digit] = value;
|
|
||||||
|
|
||||||
if(debug)
|
|
||||||
printSRData();
|
|
||||||
}
|
|
||||||
|
|
||||||
void setSegments(int dispID, int digit, byte value)
|
|
||||||
{
|
|
||||||
//Parameter checker
|
|
||||||
if(dispID < 0 || dispID >= numDevices)
|
|
||||||
{
|
|
||||||
Serial.println("dispID OoB!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(digit < 0 || digit > maxDigits)
|
|
||||||
{
|
|
||||||
Serial.println("digit OoB!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(value < 0 || value > 255)
|
|
||||||
{
|
|
||||||
Serial.println("Invalid byte!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Store the digit
|
|
||||||
SRData[dispID][digit] = value;
|
|
||||||
|
|
||||||
if(debug)
|
|
||||||
printSRData();
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearDisplay(int dispID)
|
|
||||||
{
|
|
||||||
initializeSRData();
|
initializeSRData();
|
||||||
refreshDisplay();
|
refreshDisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
void refreshDisplay()
|
void refreshDisplay() {
|
||||||
{
|
|
||||||
//Digit Scanner
|
for(int fet=0; fet<FET_COUNT; fet++) {
|
||||||
for(int digit = 0; digit < maxDigits; digit++)
|
|
||||||
{
|
// switch fets
|
||||||
//Display Scanner
|
digitalWrite(SEG_0, LOW);
|
||||||
digitalWrite(LATCH,LOW);
|
digitalWrite(SEG_1, LOW);
|
||||||
for(int dispID = numDevices - 1; dispID >= 0; dispID--)
|
digitalWrite(SEG_2, LOW);
|
||||||
{
|
digitalWrite(SEG_3, LOW);
|
||||||
//Pre-Digit blanker (shifts out 0s to correct digits before sending segment data to desired digit)
|
digitalWrite(SEG_4, LOW);
|
||||||
for(int blanks = (maxDigits - 1 - digit); blanks > 0; blanks--)
|
digitalWrite(SEG_5, LOW);
|
||||||
shiftOut(DOUT,CLK,LSBFIRST,0);
|
digitalWrite(SEG_6, LOW);
|
||||||
|
digitalWrite(SEG_7, LOW);
|
||||||
shiftOut(DOUT,CLK,LSBFIRST,SRData[dispID][digit]);
|
|
||||||
|
digitalWrite(LATCH, HIGH);
|
||||||
//Post-Digit blanker (shifts out 0s to remaining digits)
|
|
||||||
for(int blanks = digit; blanks > 0; blanks--)
|
for(int digit = 0; digit < BYTE_PER_FET; digit++) {
|
||||||
shiftOut(DOUT,CLK,LSBFIRST,0);
|
shiftOut(DOUT, CLK, LSBFIRST, data[fet][digit]);
|
||||||
}
|
}
|
||||||
digitalWrite(LATCH,HIGH);
|
|
||||||
|
digitalWrite(LATCH, LOW);
|
||||||
|
|
||||||
//Demonstrates multiplexing operation
|
// switch fets
|
||||||
delay(delayTime);
|
digitalWrite(SEG_0, (fet == 0) ? HIGH : LOW);
|
||||||
delayTime -= 10;
|
digitalWrite(SEG_1, (fet == 1) ? HIGH : LOW);
|
||||||
if(delayTime <= 0)
|
digitalWrite(SEG_2, (fet == 2) ? HIGH : LOW);
|
||||||
delayTime = 0;
|
digitalWrite(SEG_3, (fet == 3) ? HIGH : LOW);
|
||||||
|
digitalWrite(SEG_4, (fet == 4) ? HIGH : LOW);
|
||||||
|
digitalWrite(SEG_5, (fet == 5) ? HIGH : LOW);
|
||||||
|
digitalWrite(SEG_6, (fet == 6) ? HIGH : LOW);
|
||||||
|
digitalWrite(SEG_7, (fet == 7) ? HIGH : LOW);
|
||||||
|
|
||||||
|
delayMicroseconds(500);
|
||||||
|
//delay(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Before Width: | Height: | Size: 368 B After Width: | Height: | Size: 368 B |
Before Width: | Height: | Size: 179 B After Width: | Height: | Size: 179 B |
Before Width: | Height: | Size: 712 B After Width: | Height: | Size: 712 B |
Before Width: | Height: | Size: 474 B After Width: | Height: | Size: 474 B |
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 1.0 KiB |
Before Width: | Height: | Size: 1006 B After Width: | Height: | Size: 1006 B |
Before Width: | Height: | Size: 530 B After Width: | Height: | Size: 530 B |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 904 B After Width: | Height: | Size: 904 B |
Before Width: | Height: | Size: 80 KiB After Width: | Height: | Size: 80 KiB |
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB |
Before Width: | Height: | Size: 52 KiB After Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 98 KiB After Width: | Height: | Size: 98 KiB |
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 81 KiB After Width: | Height: | Size: 81 KiB |
Before Width: | Height: | Size: 656 B After Width: | Height: | Size: 656 B |
Before Width: | Height: | Size: 986 B After Width: | Height: | Size: 986 B |
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 2.3 KiB |
Before Width: | Height: | Size: 494 B After Width: | Height: | Size: 494 B |
Before Width: | Height: | Size: 232 B After Width: | Height: | Size: 232 B |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |