#include <REGX51.H>
bit stop = 0; //判斷暫停或開始
unsigned char Scan = 0, Mode = 0, Seg_scan = 0; //點矩陣掃描,選擇圖片,Seg掃描
unsigned int Cnt = 0; //延遲Cnt
unsigned char Cnt_2 = 15, Cnt_3 = 0; //兩組時間
//-----點矩陣要顯示的圖片(走動)-----//
code int Led[5][16] = { {0x0000, 0x0030, 0x0078, 0x0030, 0x0060, 0x01e0, 0x02e0, 0x04f0, 0x0588, 0x0180, 0x0280, 0x0440, 0x0820, 0x1018, 0x1000, 0x0000},
{0x0000, 0x0030, 0x0078, 0x0030, 0x0060, 0x01e0, 0x0260, 0x02e0, 0x0590, 0x0180, 0x0240, 0x0e20, 0x0820, 0x0640, 0x0070, 0x0000},
{0x0000, 0x0030, 0x0078, 0x0030, 0x0060, 0x01e0, 0x03e0, 0x07f0, 0x05c8, 0x01c0, 0x03c0, 0x05e0, 0x0b60, 0x1478, 0x1500, 0x0e00},
{0x0000, 0x0030, 0x0078, 0x0030, 0x0060, 0x01e0, 0x03c0, 0x03f0, 0x01c0, 0x01c0, 0x01c0, 0x01a0, 0x0340, 0x0470, 0x0500, 0x0e00},
{0x0000, 0x0030, 0x0078, 0x0030, 0x0060, 0x00e0, 0x00e0, 0x00c0, 0x01c0, 0x01c0, 0x03a0, 0x0280, 0x0380, 0x0200, 0x0280, 0x0000} };
//-----點矩陣要顯示的圖片(停止)-----//
code int Led_2[16] = { 0x0000, 0x03c0, 0x03c0, 0x03c0, 0x0c30, 0x0c30, 0x17e8, 0x37ec, 0x37ec, 0x37ec, 0x37ec, 0x2664, 0x2664, 0x0660, 0x0660, 0x0660};
code char Seg_com[4] = {0x70, 0xb0, 0xd0, 0xe0}; //七字節掃描
unsigned char Seg_data[4] = {1, 5, 0, 0}; //要顯示的Data
//七字節解碼
const char Seg_code[16] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xd8, 0x80, 0x98, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e};
void Delay( unsigned int Value ); //延遲副程式
void main(void)
{ while(1){
Cnt++;
if( Cnt>250 ){
Cnt = 0;
if( stop ){
Cnt_3--; //假如現在是紅燈,Cnt3開始到數
if( Cnt_3==0 ) stop = 0; //假如Cnt3變為0,變成綠燈
Cnt_2 = 15; //設定Cnt_2
Seg_data[0] = Cnt_2/10; //把Cnt_2十位數存到Seg_data[0]
Seg_data[1] = Cnt_2%10; //把Cnt_2個位數存到Seg_data[0]
Seg_data[2] = Cnt_3/10; //把Cnt_3十位數存到Seg_data[0]
Seg_data[3] = Cnt_3%10; //把Cnt_3個位數存到Seg_data[0]
}
else{ //假如是綠燈
Mode++; //切換圖片
if( Mode>=5 ) Mode = 0;
Cnt_2--; //Cnt_2倒數
if( Cnt_2==0 ) stop = 1; ////假如Cnt2變為0,變成紅燈
Cnt_3 = 15;
Seg_data[0] = Cnt_2/10; //把Cnt_2十位數存到Seg_data[0]
Seg_data[1] = Cnt_2%10; //把Cnt_2個位數存到Seg_data[0]
Seg_data[2] = Cnt_3/10; //把Cnt_3十位數存到Seg_data[0]
Seg_data[3] = Cnt_3%10; //把Cnt_3個位數存到Seg_data[0]
}
}
P0 |= 0xf0; 關閉七段顯示器
if(stop) P3 = Seg_code[Seg_data[Seg_scan+2]]; //假如紅燈,顯示Cnt_3
else P3 = Seg_code[Seg_data[Seg_scan]]; //假如綠燈,顯示Cnt_2
if( stop ){ //假如紅燈,顯示停止的圖片
P1 = ~(Led_2[Scan]&0xff); //P1送低8位元
P2 = ~(Led_2[Scan]>>8); //P2送高8位元
}
else{ //假如綠燈,顯示走路的圖片
P1 = ~(Led[Mode][Scan]&0xff); ; //P1送低8位元
P2 = ~(Led[Mode][Scan]>>8); //P2送高8位元
}
if(stop) P0 = Scan + Seg_com[Seg_scan+2]; //假如紅燈,顯示Cnt_3
else P0 = Scan + Seg_com[Seg_scan]; //假如綠燈,顯示Cnt_2
Scan++; //點矩陣掃描
if( Scan>15 ) Scan = 0;
Seg_scan++; //七字節掃描
if( Seg_scan>1 ) Seg_scan = 0;
Delay(80); //延遲 } }
//-----延遲副程式-----//
void Delay( unsigned int Value )
{
while( Value > 0 ) Value--;
} |