使用NodeMCu
S1 = +1000
S2 = +100
S3 = +10
S4 = +1
S5 清除設定的計數值
S6 Start /Stop
S7 在Stop下 設定 上數 或 下數
S8 在Stop下 清除計數值
當計數值與設定值 相同時 計數值變成0000 後開始計數
const int strobe = 4; //D2
const int clock = 5; //D1
const int data = 16; //D0
void sendCommand(uint8_t value)
{
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, value);
digitalWrite(strobe, HIGH);
}
void reset()
{
sendCommand(0x40); // set auto increment mode
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, 0xc0); // set starting address to 0
for(uint8_t i = 0; i < 16; i++)
{
shiftOut(data, clock, LSBFIRST, 0x00);
}
digitalWrite(strobe, HIGH);
}
void setup()
{
pinMode(strobe, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(data, OUTPUT);
sendCommand(0x8f); // activate
reset();
}
int number=9500;
int DispAddr2=0xc8;
int DispAddr1=0xc0;
int SetNumber= 0;
boolean StartStop=false;
boolean UpDn=false;
const int interval=100;
unsigned long previousmillis=0;
uint8_t buttons=0;
void loop()
{
Dis4digit(number,DispAddr2);
Dis4digit(SetNumber,DispAddr1);
unsigned long currentmillis =millis();
if (currentmillis -previousmillis >= interval)
{
previousmillis=currentmillis;
buttons = readButtons();
if (bitRead(buttons,0))
{
SetNumber=SetNumber+1000;
}
else if (bitRead(buttons,1))
{
SetNumber=SetNumber+100;
}
else if (bitRead(buttons,2))
{
SetNumber=SetNumber+10;
}
else if (bitRead(buttons,3))
{
SetNumber=SetNumber+1;
}
else if (bitRead(buttons,4))
{
SetNumber=0;
}
else if (bitRead(buttons,5))
{
StartStop=!StartStop ;
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, 0xcb); // 3st LED
if (StartStop==true)
{
shiftOut(data, clock, LSBFIRST, 0x01);
}
else
{
shiftOut(data, clock, LSBFIRST, 0x00);
}
digitalWrite(strobe, HIGH);
}
else if (bitRead(buttons,6))
{
if (StartStop==false)
{
UpDn=!UpDn;
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, 0xcd); // 2st LED
if (UpDn==true)
{
shiftOut(data, clock, LSBFIRST, 0x01);
}
else
{
shiftOut(data, clock, LSBFIRST, 0x00);
}
digitalWrite(strobe, HIGH);
}
}
else if (bitRead(buttons,7))
{
if (StartStop==false)
{
number=0;
}
}
if (StartStop==0)
{
number = number;
}
if (UpDn==false && StartStop==true)
{
number = number+1;
}
else if (UpDn==true && StartStop==true)
{
if (number==0)
{
number =9999;
}
else
{
number = number-1;
}
}
if (number>=10000)
{
number=0;
}
else if (SetNumber>=9999)
{
SetNumber=0;
}
else if (SetNumber==number)
{
number=0;
}
}
}
void Dis4digit(int num1,int addr1)
{
/*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/ /*8*/ /*9*/
uint8_t digits[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f ,0x00};
int number1=num1;
int n3=number1/1000;
number1=number1%1000;
int n2=number1/100;
number1=number1%100;
int n1=number1/10;
int n0=number1%10;
//0xc0 第8個 七段 0xc2 第7個 七段 0xc4 第6個 七段 0xc6 第5個 七段 0xc8 第4個 七段
//0xc1 第8個 LED 0xc3 第7個 LED 0xc5 第6個 LED 0xc7 第5個 LED 0xc9 第4個 LED
sendCommand(0x44); // set single address
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, addr1); // 4st digit 7-segment display
shiftOut(data, clock, LSBFIRST, digits[n3]);
digitalWrite(strobe, HIGH);
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, addr1+2); // 3st digit 7-segment display
shiftOut(data, clock, LSBFIRST, digits[n2]);
digitalWrite(strobe, HIGH);
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, addr1+4); // 2st digit 7-segment display
shiftOut(data, clock, LSBFIRST, digits[n1]);
digitalWrite(strobe, HIGH);
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, addr1+6); // 1st digit 7-segment display 最左邊
shiftOut(data, clock, LSBFIRST, digits[n0]);
digitalWrite(strobe, HIGH);
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, addr1+7); // 1st LED
shiftOut(data, clock, LSBFIRST, n0%2);
digitalWrite(strobe, HIGH);
}
uint8_t readButtons(void)
{
uint8_t buttons = 0;
digitalWrite(strobe, LOW);
shiftOut(data, clock, LSBFIRST, 0x42);
pinMode(data, INPUT);
for (uint8_t i = 0; i < 4; i++)
{
uint8_t v = shiftIn(data, clock, LSBFIRST) << i;
buttons |= v;
}
pinMode(data, OUTPUT);
digitalWrite(strobe, HIGH);
return buttons;
}
沒有留言:
張貼留言