// C++ code
#define delayT 200
//-------- 74595 IC 控制 8LED 元件接腳名稱控制參數設定 ----------------------------------------
#define SH_CP1 4 //SHift Register ClocK INPUT -> SRCLK,SCK;資料輸入序列時脈(前) 74595 PIN_11 接至 PD7,GPIO_7 需設為OUTPUT 模式
#define ST_CP1 3 //STorage Register ClocK INPUT -> RCLK,RCK;暫存器時脈(後) 74595 PIN_12 接至 PD6,GPIO_6 需設為OUTPUT 模式
#define DATA_SER1 2//SERrial Data INPUT -> SER,SI;資料序列輸入 74595 PIN_14 接至 PD5,GPIO_5 需設為OUTPUT 模式
//-------- 74595 IC 控制 七段顯示器 元件接腳名稱控制參數設定 ----------------------------------------
#define SH_CP2 7 //SHift Register ClocK INPUT -> SRCLK,SCK;資料輸入序列時脈(前) 74595 PIN_11 接至 PD7,GPIO_10 需設為OUTPUT 模式
#define ST_CP2 6 //STorage Register ClocK INPUT -> RCLK,RCK;暫存器時脈(後) 74595 PIN_12 接至 PD6,GPIO_9 需設為OUTPUT 模式
#define DATA_SER2 5//SERrial Data INPUT -> SER,SI;資料序列輸入 74595 PIN_14 接至 PD5,GPIO_8 需設為OUTPUT 模式
// 使用 74595 的優點 不需要在解碼使用 bitRead 指令控制周邊 直接將8bit
// 的資料並列輸出 輸出方式可以控制 MSB 或 LSB 先行輸出 使用3條輸入控制線
// 可以擴展到 8 個 GPIO 節省 GPIO 的接腳使用
void setup()
{
for(int i=2;i<=8;i++)
pinMode(i, OUTPUT);
for(int i=14;i<=17;i++)
pinMode(i, INPUT_PULLUP); // 設定成 INPUT_PULLUP 使用內建提升電阻 免去外接電阻
//pinMode(i, INPUT); //宣告成 INPUT 該腳位 GPIO 需要外接4.7K歐姆到 5V
Serial.begin(115200);
}
//---------------------------------------------------------
// 2 進制 轉成16進制 減少程式碼的版面
byte seg[]={0x3F,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x27,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x76};
byte Buff[]={0,0,0,0,0,0,0,0,0};//Buff[1]=0
byte scan[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
//---------------------------------------------------------
int leddis=0;
byte count=0;
//----------- 七段顯示副程式 ---------------------------
//------------6條掃描線控制6顆共陰極七段顯示 -------------
void disp(byte data7s,byte number7s ) // data7s 七段顯示器的編碼(byte) number7s 點亮第幾個七段顯示器 (0~1)
{
digitalWrite(ST_CP1, LOW);
shiftOut(DATA_SER1, SH_CP1, MSBFIRST, ~seg[data7s]);
digitalWrite(ST_CP1, HIGH);
digitalWrite(ST_CP2, LOW);
shiftOut(DATA_SER2, SH_CP2, MSBFIRST, scan[number7s]);
digitalWrite(ST_CP2, HIGH);
// delay(1); // 必須延遲 1ms 否則會看不到顯示
digitalWrite(ST_CP2, LOW);
shiftOut(DATA_SER2, SH_CP2, MSBFIRST, 0x00);
digitalWrite(ST_CP2, HIGH);
}
byte no=0;
//-----------顯示副程式-------------------------
void show(void)
{
disp(Buff[no],no); // Buff[0]表示十位數 Buff[1]表示個位數
//no = (no+1) % 6; // 因為只有兩個七段 no 為 0~5 的變化
no = (no+1) % 8;
delay(2);
}
//---------------------------------------------------------
unsigned long counterM = 0;
//----------------------------------------------------------
void counter()
{
// 每隔350ms執行一次底下的條件式內容
if (millis() - counterM >= 350)
{
counterM = millis();
count=(count+1)%16; // count=0 ~ (status-1)
}
}
unsigned long showdataM = 0;
int distime=0;
//----------------------------------------------------
void showdata()
{
if (millis() - showdataM >= 400)
{
showdataM = millis();
distime=(distime+1)%20;
if (distime<5)
{
Buff[0]=2;Buff[1]=0;Buff[2]=2;Buff[3]=4;Buff[4]=0;Buff[5]=8;Buff[6]=16;Buff[7]=1;
}
else if(distime>=5&&distime<10)
{
Buff[0]=1;Buff[1]=1;Buff[2]=5;Buff[3]=7;Buff[4]=8;Buff[5]=7;Buff[6]=16;Buff[7]=2;
}
else if(distime>=10&&distime<15){
Buff[0]=17;Buff[1]=1;Buff[2]=2;Buff[3]=3;Buff[4]=4;Buff[5]=5;Buff[6]=16;Buff[7]=3;
}
else{Buff[0]=10;Buff[1]=11;Buff[2]=12;Buff[3]=13;Buff[4]=14;Buff[5]=15;Buff[6]=16;Buff[7]=4; }
}
}
//-------------------------------------------------
char ReadKey(void)
{
char key=0;
if( digitalRead(14)==0 ) key |= 1; //當S3按下時為低電位,ReadKey()副程式傳回數值4
if( digitalRead(15)==0 ) key |= 2; //當S2按下時為低電位,ReadKey()副程式傳回數值2
if( digitalRead(16)==0 ) key |= 4; //當S1按下時為低電位,ReadKey()副程式傳回數值1
if( digitalRead(17)==0 ) key |= 8; //當S1按下時為低電位,ReadKey()副程式傳回數值1
return key;
}
//---------------------------------------------------
unsigned long switchkeyM = 0;
int newkey,oldkey;
//---------------------------------------------------------------------
void switchkey()
{
// 每隔20ms執行一次底下的條件式內容
if (millis() - switchkeyM >= 50)
{
switchkeyM = millis();
newkey = ReadKey();
if (newkey!=oldkey)
{
if(newkey == 1) {
}
if(newkey == 2) {
}
if(newkey == 4) {
}
if(newkey == 8) {
}
oldkey = newkey;
}
}
//Serial.println("fun="+String(fun));
//Serial.println("start="+String(start));
}
//----------------------------------------------------------
void loop()
{
switchkey();
//1ms
showdata();
show();
}