first working arduino test code for one display unit

This commit is contained in:
Lucas Pleß 2018-01-07 02:33:01 +01:00
parent a533a4d3fe
commit 196986fb89
33 changed files with 104 additions and 174 deletions

View file

@ -1,206 +1,136 @@
//Pin Assignments (You should change these)
const int CLK = 9; //Connected to TPIC pin 13: SRCLK (aka Clock)
const int LATCH = 10; //Connected to TPIC pin 12: RCLK (aka Latch/load/CS/SS...)
const int OE = 11; //Connected to TPIC pin 9: OE (Output Enable)
const int DOUT = 12; //Connected to TPIC pin 3: SER (aka MOSI)
#define CLK 2 //Connected to TPIC pin 13: SRCLK (aka Clock)
#define LATCH 3 //Connected to TPIC pin 12: RCLK (aka Latch/load/CS/SS...)
#define DOUT 4 //Connected to TPIC pin 3: SER (aka MOSI)
#define SEG_0 A0
#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)
//***Drains 0-7 must be connected to segments A-DP respectively***
const byte numTable[] =
{
B11111100,
B01100000,
B11011010,
B11110010,
B01100110,
B10110110,
B10111110,
B11100000,
B11111110,
B11110110
};
#define FET_COUNT 8
#define BYTE_PER_FET 6
//Global Variables
int numDevices = 1; //The number of x-digit display modules you plan to use
int maxDisplays = 3; //The maximum displays that could be accommodated (see note 1)
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)
volatile uint8_t data[FET_COUNT][BYTE_PER_FET];
long dataMillis = 0;
uint8_t dataDemoStep = 0;
/*
Notes
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(115200);
void setup()
{
Serial.begin(9600);
//Set pin modes
pinMode(CLK,OUTPUT);
pinMode(LATCH,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
//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
}
//==========BEGIN SR Functions==========
void initializeSRData()
{
//Display Scanner (Iterates through each display module)
digitalWrite(LATCH,LOW); //Tells all SRs that uController is sending data
void initializeSRData() {
digitalWrite(LATCH,HIGH); //Tells all SRs that uController is sending data
for(int dispID = 0; dispID < maxDisplays; dispID++)
{
//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
}
for(int digit = 0; digit < BYTE_PER_FET; digit++) {
shiftOut(DOUT, CLK, LSBFIRST,0);
}
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
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)
{
void clearDisplay(int dispID) {
initializeSRData();
refreshDisplay();
}
void refreshDisplay()
{
//Digit Scanner
for(int digit = 0; digit < maxDigits; digit++)
{
//Display Scanner
digitalWrite(LATCH,LOW);
for(int dispID = numDevices - 1; dispID >= 0; dispID--)
{
//Pre-Digit blanker (shifts out 0s to correct digits before sending segment data to desired digit)
for(int blanks = (maxDigits - 1 - digit); blanks > 0; blanks--)
shiftOut(DOUT,CLK,LSBFIRST,0);
shiftOut(DOUT,CLK,LSBFIRST,SRData[dispID][digit]);
//Post-Digit blanker (shifts out 0s to remaining digits)
for(int blanks = digit; blanks > 0; blanks--)
shiftOut(DOUT,CLK,LSBFIRST,0);
void refreshDisplay() {
for(int fet=0; fet<FET_COUNT; fet++) {
// switch fets
digitalWrite(SEG_0, LOW);
digitalWrite(SEG_1, LOW);
digitalWrite(SEG_2, LOW);
digitalWrite(SEG_3, LOW);
digitalWrite(SEG_4, LOW);
digitalWrite(SEG_5, LOW);
digitalWrite(SEG_6, LOW);
digitalWrite(SEG_7, LOW);
digitalWrite(LATCH, HIGH);
for(int digit = 0; digit < BYTE_PER_FET; digit++) {
shiftOut(DOUT, CLK, LSBFIRST, data[fet][digit]);
}
digitalWrite(LATCH,HIGH);
digitalWrite(LATCH, LOW);
//Demonstrates multiplexing operation
delay(delayTime);
delayTime -= 10;
if(delayTime <= 0)
delayTime = 0;
// switch fets
digitalWrite(SEG_0, (fet == 0) ? HIGH : LOW);
digitalWrite(SEG_1, (fet == 1) ? HIGH : LOW);
digitalWrite(SEG_2, (fet == 2) ? HIGH : LOW);
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);
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

Before

Width:  |  Height:  |  Size: 368 B

After

Width:  |  Height:  |  Size: 368 B

View file

Before

Width:  |  Height:  |  Size: 179 B

After

Width:  |  Height:  |  Size: 179 B

View file

Before

Width:  |  Height:  |  Size: 712 B

After

Width:  |  Height:  |  Size: 712 B

View file

Before

Width:  |  Height:  |  Size: 474 B

After

Width:  |  Height:  |  Size: 474 B

View file

Before

Width:  |  Height:  |  Size: 1 KiB

After

Width:  |  Height:  |  Size: 1 KiB

View file

Before

Width:  |  Height:  |  Size: 1,006 B

After

Width:  |  Height:  |  Size: 1,006 B

View file

Before

Width:  |  Height:  |  Size: 530 B

After

Width:  |  Height:  |  Size: 530 B

View file

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View file

Before

Width:  |  Height:  |  Size: 904 B

After

Width:  |  Height:  |  Size: 904 B

View file

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 80 KiB

View file

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

View file

Before

Width:  |  Height:  |  Size: 98 KiB

After

Width:  |  Height:  |  Size: 98 KiB

View file

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View file

Before

Width:  |  Height:  |  Size: 81 KiB

After

Width:  |  Height:  |  Size: 81 KiB

View file

Before

Width:  |  Height:  |  Size: 656 B

After

Width:  |  Height:  |  Size: 656 B

View file

Before

Width:  |  Height:  |  Size: 986 B

After

Width:  |  Height:  |  Size: 986 B

View file

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View file

Before

Width:  |  Height:  |  Size: 494 B

After

Width:  |  Height:  |  Size: 494 B

View file

Before

Width:  |  Height:  |  Size: 232 B

After

Width:  |  Height:  |  Size: 232 B

View file

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB