//-------- 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 的接腳使用
#define BT1 14
#define BT2 15
#define BT3 16
#define BT4 17
byte pinout[]={SH_CP1,ST_CP1,DATA_SER1,SH_CP2,ST_CP2,DATA_SER2};
byte btpin[]={BT1,BT2,BT3,BT4};
void setup()
{
for(int i=0;i<(sizeof(pinout)/sizeof(byte));i++)
{
pinMode(pinout[i], OUTPUT);
}
for(int i=0;i<(sizeof(btpin)/sizeof(byte));i++)
{
pinMode(btpin[i], INPUT_PULLUP);
}
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,0x40};
byte Buff[]={0,0,0,0,0,0,0,0};//Buff[0]=1;
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, 0xFF);
digitalWrite(ST_CP2, HIGH);
}
byte no=0;
//-----------顯示副程式-------------------------
void show(void)
{
disp(Buff[no],no); // Buff[0]表示十位數 Buff[1]表示個位數
//no = (no+1) % 6; // 因為只有兩個七段 no 為 0~7 的變化
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;
byte cirdata[]={0x40,0x20,0x10,0x08,0x04,0x02};
int distime=0;
//----------------------------------------------------
void showdata()
{
if (millis() - showdataM >= 800)
{
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]=1;Buff[7]=0;
}
else if(distime>=5&&distime<10)
{
Buff[0]=17;Buff[1]=1;Buff[2]=2;Buff[3]=0;Buff[4]=8;Buff[5]=7;Buff[6]=6;Buff[7]=5;
}
else if(distime>=10&&distime<15){
Buff[0]=10;Buff[1]=11;Buff[2]=12;Buff[3]=16;Buff[4]=16;Buff[5]=13;Buff[6]=14;Buff[7]=15;
}
else{Buff[0]=1;Buff[1]=1;Buff[2]=18;Buff[3]=3;Buff[4]=5;Buff[5]=18;Buff[6]=1;Buff[7]=6; }
}
}
//-------------------------------------------------
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 == 3) {
}
if(newkey == 4) {
}
if(newkey == 8) {
}
oldkey = newkey;
}
}
//Serial.println("fun="+String(fun));
//Serial.println("start="+String(start));
}
//----------------------------------------------------------
void loop()
{
switchkey();
//1ms
showdata();
show();
}