第10節(jié):一個用來學(xué)習(xí)C語言的模板程序
目前,幾乎所有的初學(xué)者在學(xué)習(xí)和上機(jī)練習(xí)C語言的時候,都是在電腦上安裝VC這個調(diào)試軟件,在源代碼里只要調(diào)用打印語句printf就可以觀察到不同的變量結(jié)果,挺方便的。但是現(xiàn)在我要提出另外一種方法,學(xué)習(xí)單片機(jī)的C語言,不一定非要用VC調(diào)試軟件,也可以直接在堅鴻51學(xué)習(xí)板上學(xué)習(xí)和上機(jī)練習(xí)的。我可以做一個調(diào)試模板程序給初學(xué)者使用,利用8位數(shù)碼管和16個LED燈來顯示不同的變量結(jié)果,利用3個按鍵來切換顯示不同的變量,這樣就能達(dá)到類似在VC平臺下用printf語句來觀察變量的效果。甚至我個人認(rèn)為這樣比用VC調(diào)試的效果還更加直觀?,F(xiàn)在重點(diǎn)介紹這個模板程序的使用。
本文引用地址:http://m.butianyuan.cn/article/201611/319820.htm在模板程序里,初學(xué)者只需要在主程序的初始化區(qū)域填入自己練習(xí)的C語言代碼,最后把需要觀察的變量賦值給窗口變量就可以了,其它部分的代碼屬于模板的監(jiān)控調(diào)試代碼,大家暫時不用讀懂它,直接復(fù)制過來就可以了。上述所謂的“賦值”,就是“=”這個語句,它表面上像我們平時用的等于號,實際上不是等于號,而是代表“給”的意思,把“=”符號右邊的數(shù)復(fù)制一份給左邊的變量,比如“a=36;”就是代表把36這個數(shù)值復(fù)制一份給變量a,執(zhí)行這條指令后,a就等于36了。這里的分號“;”代表一條程序指令的結(jié)束。窗口變量有幾個?有哪些?一共有10個,分別是GuiWdData0,GuiWdData1,GuiWdData2,GuiWdData3,GuiWdData4,GuiWdData5,GuiWdData6,GuiWdData7,GuiWdData8,GuiWdData9。這10個窗口變量是給大家調(diào)試專用的,8位數(shù)碼管可以切換顯示10個窗口變量,最左邊2位數(shù)碼管代表窗口變量號,剩下6位數(shù)碼管顯示十進(jìn)制的窗口變量數(shù)值,另外16個LED實時顯示此數(shù)據(jù)的二進(jìn)制格式。最左邊2位數(shù)碼管從“0-”到“9-”代表從第0個窗口變量到第9個窗口變量,也就是GuiWdData0依次到GuiWdData9。用S1和S5按鍵可以切換顯示不同的窗口變量,按住S9不放可以觀察到當(dāng)前窗口變量的十六進(jìn)制格式數(shù)據(jù),松開S9按鍵后,又自動返回顯示當(dāng)前窗口變量的十進(jìn)制數(shù)據(jù)。
該模板程序是基于堅鴻51學(xué)習(xí)板,現(xiàn)在跟大家分享這個程序,要讓這10個窗口變量分別顯示10,11,12,13,14,15,16,17,18,19這10個數(shù),用S1按鍵可以切換顯示從小往大的窗口變量號,用S5按鍵可以切換顯示從大往小的窗口變量號。再強(qiáng)調(diào)一次,大家只需要關(guān)注主程序main函數(shù)的初始化區(qū)域就可以了,其它的代碼請直接復(fù)制過來,不用理解。比如:
void main() //主程序
{
//...初始化區(qū)域
while(1)
{
}
}
詳細(xì)的源代碼如下:
#include "REG52.H"
#define const_voice_short 40
#define const_key_time1 20
#define const_key_time2 20
#define const_key_time3 20
void initial(void);
void delay_short(unsigned int uiDelayShort);
void dig_hc595_drive(unsigned char ucDigStatusTemp16_09,unsigned char ucDigStatusTemp08_01);
void display_drive(void);
void display_service(void);
void hc595_drive(unsigned char ucLedStatusTemp16_09,unsigned char ucLedStatusTemp08_01);
void T0_time(void);
void key_service(void);
void key_scan(void);
sbit beep_dr=P2^7;
sbit key_sr1=P0^0;
sbit key_sr2=P0^1;
sbit key_sr3=P0^2;
sbit key_gnd_dr=P0^4;
sbit led_dr=P3^5;
sbit dig_hc595_sh_dr=P2^0;
sbit dig_hc595_st_dr=P2^1;
sbit dig_hc595_ds_dr=P2^2;
sbit hc595_sh_dr=P2^3;
sbit hc595_st_dr=P2^4;
sbit hc595_ds_dr=P2^5;
unsigned char GucKeySec=0;
unsigned char GucKey3Sr=1;
unsigned int GuiVoiceCnt=0;
unsigned char GucVoiceStart=0;
unsigned char GucDigShow8;
unsigned char GucDigShow7;
unsigned char GucDigShow6;
unsigned char GucDigShow5;
unsigned char GucDigShow4;
unsigned char GucDigShow3;
unsigned char GucDigShow2;
unsigned char GucDigShow1;
unsigned char GucDisplayUpdate=1;
unsigned char GucWd=0;
unsigned int GuiWdData0=0;
unsigned int GuiWdData1=0;
unsigned int GuiWdData2=0;
unsigned int GuiWdData3=0;
unsigned int GuiWdData4=0;
unsigned int GuiWdData5=0;
unsigned int GuiWdData6=0;
unsigned int GuiWdData7=0;
unsigned int GuiWdData8=0;
unsigned int GuiWdData9=0;
code unsigned char dig_table[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40,
};
void main() //主程序
{
/*---C語言學(xué)習(xí)區(qū)域的開始---------------------------------------------------------------------------*/
GuiWdData0=10; //把10這個數(shù)值放到窗口變量0里面顯示
GuiWdData1=11; //把11這個數(shù)值放到窗口變量1里面顯示
GuiWdData2=12; //把12這個數(shù)值放到窗口變量2里面顯示
GuiWdData3=13; //把13這個數(shù)值放到窗口變量3里面顯示
GuiWdData4=14; //把14這個數(shù)值放到窗口變量4里面顯示
GuiWdData5=15; //把15這個數(shù)值放到窗口變量5里面顯示
GuiWdData6=16; //把16這個數(shù)值放到窗口變量6里面顯示
GuiWdData7=17; //把17這個數(shù)值放到窗口變量7里面顯示
GuiWdData8=18; //把18這個數(shù)值放到窗口變量8里面顯示
GuiWdData9=19; //把19這個數(shù)值放到窗口變量9里面顯示
/*---C語言學(xué)習(xí)區(qū)域的結(jié)束---------------------------------------------------------------------------*/
while(1)
{
initial();
key_service();
display_service();
}
}
void display_service(void)
{
static unsigned char SucLedStatus16_09=0;
static unsigned char SucLedStatus08_01=0;
static unsigned int SinWdDataTemp=0;
if(1==GucDisplayUpdate)
{
GucDisplayUpdate=0;
switch(GucWd)
{
case 0:
GucDigShow8=0;
SinWdDataTemp=GuiWdData0;
break;
case 1:
GucDigShow8=1;
SinWdDataTemp=GuiWdData1;
break;
case 2:
GucDigShow8=2;
SinWdDataTemp=GuiWdData2;
break;
case 3:
GucDigShow8=3;
SinWdDataTemp=GuiWdData3;
break;
case 4:
GucDigShow8=4;
SinWdDataTemp=GuiWdData4;
break;
case 5:
GucDigShow8=5;
SinWdDataTemp=GuiWdData5;
break;
case 6:
GucDigShow8=6;
SinWdDataTemp=GuiWdData6;
break;
case 7:
GucDigShow8=7;
SinWdDataTemp=GuiWdData7;
break;
case 8:
GucDigShow8=8;
SinWdDataTemp=GuiWdData8;
break;
case 9:
GucDigShow8=9;
SinWdDataTemp=GuiWdData9;
break;
}
GucDigShow7=17;
GucDigShow6=16;
if(1==GucKey3Sr)
{
if(SinWdDataTemp>=10000)
{
GucDigShow5=SinWdDataTemp/10000;
}
else
{
GucDigShow5=16;
}
if(SinWdDataTemp>=1000)
{
GucDigShow4=SinWdDataTemp%10000/1000;
}
else
{
GucDigShow4=16;
}
if(SinWdDataTemp>=100)
{
GucDigShow3=SinWdDataTemp%1000/100;
}
else
{
GucDigShow3=16;
}
if(SinWdDataTemp>=10)
{
GucDigShow2=SinWdDataTemp%100/10;
}
else
{
GucDigShow2=16;
}
GucDigShow1=SinWdDataTemp%10;
}
else
{
GucDigShow5=16;
if(SinWdDataTemp>=0x1000)
{
GucDigShow4=SinWdDataTemp/0x1000;
}
else
{
GucDigShow4=16;
}
if(SinWdDataTemp>=0x0100)
{
GucDigShow3=SinWdDataTemp%0x1000/0x0100;
}
else
{
GucDigShow3=16;
}
if(SinWdDataTemp>=0x0010)
{
GucDigShow2=SinWdDataTemp%0x0100/0x0010;
}
else
{
GucDigShow2=16;
}
GucDigShow1=SinWdDataTemp%0x0010;
}
SucLedStatus16_09=SinWdDataTemp>>8;
SucLedStatus08_01=SinWdDataTemp;
hc595_drive(SucLedStatus16_09,SucLedStatus08_01);
}
}
void key_scan(void)
{
static unsigned int SuiKeyTimeCnt1=0;
static unsigned char SucKeyLock1=0;
static unsigned int SuiKeyTimeCnt2=0;
static unsigned char SucKeyLock2=0;
static unsigned int SuiKey3Cnt1=0;
static unsigned int SuiKey3Cnt2=0;
if(1==key_sr1)
{
SucKeyLock1=0;
SuiKeyTimeCnt1=0;
}
else if(0==SucKeyLock1)
{
SuiKeyTimeCnt1++;
if(SuiKeyTimeCnt1>const_key_time1)
{
SuiKeyTimeCnt1=0;
SucKeyLock1=1;
GucKeySec=1;
}
}
if(1==key_sr2)
{
SucKeyLock2=0;
SuiKeyTimeCnt2=0;
}
else if(0==SucKeyLock2)
{
SuiKeyTimeCnt2++;
if(SuiKeyTimeCnt2>const_key_time2)
{
SuiKeyTimeCnt2=0;
SucKeyLock2=1;
GucKeySec=2;
}
}
if(1==key_sr3)
{
SuiKey3Cnt1=0;
SuiKey3Cnt2++;
if(SuiKey3Cnt2>const_key_time3)
{
SuiKey3Cnt2=0;
GucKey3Sr=1;
}
}
else
{
SuiKey3Cnt2=0;
SuiKey3Cnt1++;
if(SuiKey3Cnt1>const_key_time3)
{
SuiKey3Cnt1=0;
GucKey3Sr=0;
}
}
}
void key_service(void)
{
static unsigned char SucKey3SrRecord=1;
if(GucKey3Sr!=SucKey3SrRecord)
{
SucKey3SrRecord=GucKey3Sr;
GucDisplayUpdate=1;
}
switch(GucKeySec)
{
case 1:
GucWd++;
if(GucWd>9)
{
GucWd=9;
}
GucDisplayUpdate=1;
GuiVoiceCnt=const_voice_short;
GucVoiceStart=1;
GucKeySec=0;
break;
case 2:
GucWd--;
if(GucWd>9)
{
GucWd=0;
}
GucDisplayUpdate=1;
GuiVoiceCnt=const_voice_short;
GucVoiceStart=1;
GucKeySec=0;
break;
}
}
void display_drive()
{
static unsigned char SucDigShowTemp=0;
static unsigned char SucDisplayDriveStep=1;
switch(SucDisplayDriveStep)
{
case 1:
SucDigShowTemp=dig_table[GucDigShow1];
dig_hc595_drive(SucDigShowTemp,0xfe);
break;
case 2:
SucDigShowTemp=dig_table[GucDigShow2];
dig_hc595_drive(SucDigShowTemp,0xfd);
break;
case 3:
SucDigShowTemp=dig_table[GucDigShow3];
dig_hc595_drive(SucDigShowTemp,0xfb);
break;
case 4:
SucDigShowTemp=dig_table[GucDigShow4];
dig_hc595_drive(SucDigShowTemp,0xf7);
break;
case 5:
SucDigShowTemp=dig_table[GucDigShow5];
dig_hc595_drive(SucDigShowTemp,0xef);
break;
case 6:
SucDigShowTemp=dig_table[GucDigShow6];
dig_hc595_drive(SucDigShowTemp,0xdf);
break;
case 7:
SucDigShowTemp=dig_table[GucDigShow7];
dig_hc595_drive(SucDigShowTemp,0xbf);
break;
case 8:
SucDigShowTemp=dig_table[GucDigShow8];
dig_hc595_drive(SucDigShowTemp,0x7f);
break;
}
SucDisplayDriveStep++;
if(SucDisplayDriveStep>8)
{
SucDisplayDriveStep=1;
}
}
void dig_hc595_drive(unsigned char ucDigStatusTemp16_09,unsigned char ucDigStatusTemp08_01)
{
unsigned char i;
unsigned char ucTempData;
dig_hc595_sh_dr=0;
dig_hc595_st_dr=0;
ucTempData=ucDigStatusTemp16_09;
for(i=0;i<8;i++)
{
if(ucTempData>=0x80)dig_hc595_ds_dr=1;
else dig_hc595_ds_dr=0;
dig_hc595_sh_dr=0;
delay_short(1);
dig_hc595_sh_dr=1;
delay_short(1);
ucTempData=ucTempData<<1;
}
ucTempData=ucDigStatusTemp08_01;
for(i=0;i<8;i++)
{
if(ucTempData>=0x80)dig_hc595_ds_dr=1;
else dig_hc595_ds_dr=0;
dig_hc595_sh_dr=0;
delay_short(1);
dig_hc595_sh_dr=1;
delay_short(1);
ucTempData=ucTempData<<1;
}
dig_hc595_st_dr=0;
delay_short(1);
dig_hc595_st_dr=1;
delay_short(1);
dig_hc595_sh_dr=0;
dig_hc595_st_dr=0;
dig_hc595_ds_dr=0;
}
void hc595_drive(unsigned char ucLedStatusTemp16_09,unsigned char ucLedStatusTemp08_01)
{
unsigned char i;
unsigned char ucTempData;
hc595_sh_dr=0;
hc595_st_dr=0;
ucTempData=ucLedStatusTemp16_09;
for(i=0;i<8;i++)
{
if(ucTempData>=0x80)hc595_ds_dr=1;
else hc595_ds_dr=0;
hc595_sh_dr=0;
delay_short(1);
hc595_sh_dr=1;
delay_short(1);
ucTempData=ucTempData<<1;
}
ucTempData=ucLedStatusTemp08_01;
for(i=0;i<8;i++)
{
if(ucTempData>=0x80)hc595_ds_dr=1;
else hc595_ds_dr=0;
hc595_sh_dr=0;
delay_short(1);
hc595_sh_dr=1;
delay_short(1);
ucTempData=ucTempData<<1;
}
hc595_st_dr=0;
delay_short(1);
hc595_st_dr=1;
delay_short(1);
hc595_sh_dr=0;
hc595_st_dr=0;
hc595_ds_dr=0;
}
void T0_time(void) interrupt 1
{
TF0=0;
TR0=0;
if(1==GucVoiceStart)
{
if(GuiVoiceCnt!=0)
{
GuiVoiceCnt--;
beep_dr=0;
}
else
{
beep_dr=1;
GucVoiceStart=0;
}
}
key_scan();
display_drive();
TH0=0xfe;
TL0=0x0b;
TR0=1;
}
void delay_short(unsigned int uiDelayShort)
{
static unsigned int i;
for(i=0;i
}
void initial(void)
{
static unsigned char SucInitialLock=0;
if(0==SucInitialLock)
{
SucInitialLock=1;
key_gnd_dr=0;
led_dr=0;
beep_dr=1;
TMOD=0x01;
TH0=0xfe;
TL0=0x0b;
EA=1;
ET0=1;
TR0=1;
}
}
下節(jié)預(yù)告:三種類型變量的定義與賦值語句。目前,幾乎所有的初學(xué)者在學(xué)習(xí)和上機(jī)練習(xí)C語言的時候,都是在電腦上安裝VC這個調(diào)試軟件,在源代碼里只要調(diào)用打印語句printf就可以觀察到不同的變量結(jié)果,挺方便的。但是現(xiàn)在我要提出另外一種方法,學(xué)習(xí)單片機(jī)的C語言,不一定非要用VC調(diào)試軟件,也可以直接在堅鴻51學(xué)習(xí)板上學(xué)習(xí)和上機(jī)練習(xí)的。我可以做一個調(diào)試模板程序給初學(xué)者使用,利用8位數(shù)碼管和16個LED燈來顯示不同的變量結(jié)果,利用3個按鍵來切換顯示不同的變量,這樣就能達(dá)到類似在VC平臺下用printf語句來觀察變量的效果。甚至我個人認(rèn)為這樣比用VC調(diào)試的效果還更加直觀?,F(xiàn)在重點(diǎn)介紹這個模板程序的使用。
在模板程序里,初學(xué)者只需要在主程序的初始化區(qū)域填入自己練習(xí)的C語言代碼,最后把需要觀察的變量賦值給窗口變量就可以了,其它部分的代碼屬于模板的監(jiān)控調(diào)試代碼,大家暫時不用讀懂它,直接復(fù)制過來就可以了。上述所謂的“賦值”,就是“=”這個語句,它表面上像我們平時用的等于號,實際上不是等于號,而是代表“給”的意思,把“=”符號右邊的數(shù)復(fù)制一份給左邊的變量,比如“a=36;”就是代表把36這個數(shù)值復(fù)制一份給變量a,執(zhí)行這條指令后,a就等于36了。這里的分號“;”代表一條程序指令的結(jié)束。窗口變量有幾個?有哪些?一共有10個,分別是GuiWdData0,GuiWdData1,GuiWdData2,GuiWdData3,GuiWdData4,GuiWdData5,GuiWdData6,GuiWdData7,GuiWdData8,GuiWdData9。這10個窗口變量是給大家調(diào)試專用的,8位數(shù)碼管可以切換顯示10個窗口變量,最左邊2位數(shù)碼管代表窗口變量號,剩下6位數(shù)碼管顯示十進(jìn)制的窗口變量數(shù)值,另外16個LED實時顯示此數(shù)據(jù)的二進(jìn)制格式。最左邊2位數(shù)碼管從“0-”到“9-”代表從第0個窗口變量到第9個窗口變量,也就是GuiWdData0依次到GuiWdData9。用S1和S5按鍵可以切換顯示不同的窗口變量,按住S9不放可以觀察到當(dāng)前窗口變量的十六進(jìn)制格式數(shù)據(jù),松開S9按鍵后,又自動返回顯示當(dāng)前窗口變量的十進(jìn)制數(shù)據(jù)。
該模板程序是基于堅鴻51學(xué)習(xí)板,現(xiàn)在跟大家分享這個程序,要讓這10個窗口變量分別顯示10,11,12,13,14,15,16,17,18,19這10個數(shù),用S1按鍵可以切換顯示從小往大的窗口變量號,用S5按鍵可以切換顯示從大往小的窗口變量號。再強(qiáng)調(diào)一次,大家只需要關(guān)注主程序main函數(shù)的初始化區(qū)域就可以了,其它的代碼請直接復(fù)制過來,不用理解。比如:
void main() //主程序
{
//...初始化區(qū)域
while(1)
{
}
}
詳細(xì)的源代碼如下:
#include "REG52.H"
#define const_voice_short 40
#define const_key_time1 20
#define const_key_time2 20
#define const_key_time3 20
void initial(void);
void delay_short(unsigned int uiDelayShort);
void dig_hc595_drive(unsigned char ucDigStatusTemp16_09,unsigned char ucDigStatusTemp08_01);
void display_drive(void);
void display_service(void);
void hc595_drive(unsigned char ucLedStatusTemp16_09,unsigned char ucLedStatusTemp08_01);
void T0_time(void);
void key_service(void);
void key_scan(void);
sbit beep_dr=P2^7;
sbit key_sr1=P0^0;
sbit key_sr2=P0^1;
sbit key_sr3=P0^2;
sbit key_gnd_dr=P0^4;
sbit led_dr=P3^5;
sbit dig_hc595_sh_dr=P2^0;
sbit dig_hc595_st_dr=P2^1;
sbit dig_hc595_ds_dr=P2^2;
sbit hc595_sh_dr=P2^3;
sbit hc595_st_dr=P2^4;
sbit hc595_ds_dr=P2^5;
unsigned char GucKeySec=0;
unsigned char GucKey3Sr=1;
unsigned int GuiVoiceCnt=0;
unsigned char GucVoiceStart=0;
unsigned char GucDigShow8;
unsigned char GucDigShow7;
unsigned char GucDigShow6;
unsigned char GucDigShow5;
unsigned char GucDigShow4;
unsigned char GucDigShow3;
unsigned char GucDigShow2;
unsigned char GucDigShow1;
unsigned char GucDisplayUpdate=1;
unsigned char GucWd=0;
unsigned int GuiWdData0=0;
unsigned int GuiWdData1=0;
unsigned int GuiWdData2=0;
unsigned int GuiWdData3=0;
unsigned int GuiWdData4=0;
unsigned int GuiWdData5=0;
unsigned int GuiWdData6=0;
unsigned int GuiWdData7=0;
unsigned int GuiWdData8=0;
unsigned int GuiWdData9=0;
code unsigned char dig_table[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40,
};
void main() //主程序
{
/*---C語言學(xué)習(xí)區(qū)域的開始---------------------------------------------------------------------------*/
GuiWdData0=10; //把10這個數(shù)值放到窗口變量0里面顯示
GuiWdData1=11; //把11這個數(shù)值放到窗口變量1里面顯示
GuiWdData2=12; //把12這個數(shù)值放到窗口變量2里面顯示
GuiWdData3=13; //把13這個數(shù)值放到窗口變量3里面顯示
GuiWdData4=14; //把14這個數(shù)值放到窗口變量4里面顯示
GuiWdData5=15; //把15這個數(shù)值放到窗口變量5里面顯示
GuiWdData6=16; //把16這個數(shù)值放到窗口變量6里面顯示
GuiWdData7=17; //把17這個數(shù)值放到窗口變量7里面顯示
GuiWdData8=18; //把18這個數(shù)值放到窗口變量8里面顯示
GuiWdData9=19; //把19這個數(shù)值放到窗口變量9里面顯示
/*---C語言學(xué)習(xí)區(qū)域的結(jié)束---------------------------------------------------------------------------*/
while(1)
{
initial();
key_service();
display_service();
}
}
void display_service(void)
{
static unsigned char SucLedStatus16_09=0;
static unsigned char SucLedStatus08_01=0;
static unsigned int SinWdDataTemp=0;
if(1==GucDisplayUpdate)
{
GucDisplayUpdate=0;
switch(GucWd)
{
case 0:
GucDigShow8=0;
SinWdDataTemp=GuiWdData0;
break;
case 1:
GucDigShow8=1;
SinWdDataTemp=GuiWdData1;
break;
case 2:
GucDigShow8=2;
SinWdDataTemp=GuiWdData2;
break;
case 3:
GucDigShow8=3;
SinWdDataTemp=GuiWdData3;
break;
case 4:
GucDigShow8=4;
SinWdDataTemp=GuiWdData4;
break;
case 5:
GucDigShow8=5;
SinWdDataTemp=GuiWdData5;
break;
case 6:
GucDigShow8=6;
SinWdDataTemp=GuiWdData6;
break;
case 7:
GucDigShow8=7;
SinWdDataTemp=GuiWdData7;
break;
case 8:
GucDigShow8=8;
SinWdDataTemp=GuiWdData8;
break;
case 9:
GucDigShow8=9;
SinWdDataTemp=GuiWdData9;
break;
}
GucDigShow7=17;
GucDigShow6=16;
if(1==GucKey3Sr)
{
if(SinWdDataTemp>=10000)
{
GucDigShow5=SinWdDataTemp/10000;
}
else
{
GucDigShow5=16;
}
if(SinWdDataTemp>=1000)
{
GucDigShow4=SinWdDataTemp%10000/1000;
}
else
{
GucDigShow4=16;
}
if(SinWdDataTemp>=100)
{
GucDigShow3=SinWdDataTemp%1000/100;
}
else
{
GucDigShow3=16;
}
if(SinWdDataTemp>=10)
{
GucDigShow2=SinWdDataTemp%100/10;
}
else
{
GucDigShow2=16;
}
GucDigShow1=SinWdDataTemp%10;
}
else
{
GucDigShow5=16;
if(SinWdDataTemp>=0x1000)
{
GucDigShow4=SinWdDataTemp/0x1000;
}
else
{
GucDigShow4=16;
}
if(SinWdDataTemp>=0x0100)
{
GucDigShow3=SinWdDataTemp%0x1000/0x0100;
}
else
{
GucDigShow3=16;
}
if(SinWdDataTemp>=0x0010)
{
GucDigShow2=SinWdDataTemp%0x0100/0x0010;
}
else
{
GucDigShow2=16;
}
GucDigShow1=SinWdDataTemp%0x0010;
}
SucLedStatus16_09=SinWdDataTemp>>8;
SucLedStatus08_01=SinWdDataTemp;
hc595_drive(SucLedStatus16_09,SucLedStatus08_01);
}
}
void key_scan(void)
{
static unsigned int SuiKeyTimeCnt1=0;
static unsigned char SucKeyLock1=0;
static unsigned int SuiKeyTimeCnt2=0;
static unsigned char SucKeyLock2=0;
static unsigned int SuiKey3Cnt1=0;
static unsigned int SuiKey3Cnt2=0;
if(1==key_sr1)
{
SucKeyLock1=0;
SuiKeyTimeCnt1=0;
}
else if(0==SucKeyLock1)
{
SuiKeyTimeCnt1++;
if(SuiKeyTimeCnt1>const_key_time1)
{
SuiKeyTimeCnt1=0;
SucKeyLock1=1;
GucKeySec=1;
}
}
if(1==key_sr2)
{
SucKeyLock2=0;
SuiKeyTimeCnt2=0;
}
else if(0==SucKeyLock2)
{
SuiKeyTimeCnt2++;
if(SuiKeyTimeCnt2>const_key_time2)
{
SuiKeyTimeCnt2=0;
SucKeyLock2=1;
GucKeySec=2;
}
}
if(1==key_sr3)
{
SuiKey3Cnt1=0;
SuiKey3Cnt2++;
if(SuiKey3Cnt2>const_key_time3)
{
SuiKey3Cnt2=0;
GucKey3Sr=1;
}
}
else
{
SuiKey3Cnt2=0;
SuiKey3Cnt1++;
if(SuiKey3Cnt1>const_key_time3)
{
SuiKey3Cnt1=0;
GucKey3Sr=0;
}
}
}
void key_service(void)
{
static unsigned char SucKey3SrRecord=1;
if(GucKey3Sr!=SucKey3SrRecord)
{
SucKey3SrRecord=GucKey3Sr;
GucDisplayUpdate=1;
}
switch(GucKeySec)
{
case 1:
GucWd++;
if(GucWd>9)
{
GucWd=9;
}
GucDisplayUpdate=1;
GuiVoiceCnt=const_voice_short;
GucVoiceStart=1;
GucKeySec=0;
break;
case 2:
GucWd--;
if(GucWd>9)
{
GucWd=0;
}
GucDisplayUpdate=1;
GuiVoiceCnt=const_voice_short;
GucVoiceStart=1;
GucKeySec=0;
break;
}
}
void display_drive()
{
static unsigned char SucDigShowTemp=0;
static unsigned char SucDisplayDriveStep=1;
switch(SucDisplayDriveStep)
{
case 1:
SucDigShowTemp=dig_table[GucDigShow1];
dig_hc595_drive(SucDigShowTemp,0xfe);
break;
case 2:
SucDigShowTemp=dig_table[GucDigShow2];
dig_hc595_drive(SucDigShowTemp,0xfd);
break;
case 3:
SucDigShowTemp=dig_table[GucDigShow3];
dig_hc595_drive(SucDigShowTemp,0xfb);
break;
case 4:
SucDigShowTemp=dig_table[GucDigShow4];
dig_hc595_drive(SucDigShowTemp,0xf7);
break;
case 5:
SucDigShowTemp=dig_table[GucDigShow5];
dig_hc595_drive(SucDigShowTemp,0xef);
break;
case 6:
SucDigShowTemp=dig_table[GucDigShow6];
dig_hc595_drive(SucDigShowTemp,0xdf);
break;
case 7:
SucDigShowTemp=dig_table[GucDigShow7];
dig_hc595_drive(SucDigShowTemp,0xbf);
break;
case 8:
SucDigShowTemp=dig_table[GucDigShow8];
dig_hc595_drive(SucDigShowTemp,0x7f);
break;
}
SucDisplayDriveStep++;
if(SucDisplayDriveStep>8)
{
SucDisplayDriveStep=1;
}
}
void dig_hc595_drive(unsigned char ucDigStatusTemp16_09,unsigned char ucDigStatusTemp08_01)
{
unsigned char i;
unsigned char ucTempData;
dig_hc595_sh_dr=0;
dig_hc595_st_dr=0;
ucTempData=ucDigStatusTemp16_09;
for(i=0;i<8;i++)
{
if(ucTempData>=0x80)dig_hc595_ds_dr=1;
else dig_hc595_ds_dr=0;
dig_hc595_sh_dr=0;
delay_short(1);
dig_hc595_sh_dr=1;
delay_short(1);
ucTempData=ucTempData<<1;
}
ucTempData=ucDigStatusTemp08_01;
for(i=0;i<8;i++)
{
if(ucTempData>=0x80)dig_hc595_ds_dr=1;
else dig_hc595_ds_dr=0;
dig_hc595_sh_dr=0;
delay_short(1);
dig_hc595_sh_dr=1;
delay_short(1);
ucTempData=ucTempData<<1;
}
dig_hc595_st_dr=0;
delay_short(1);
dig_hc595_st_dr=1;
delay_short(1);
dig_hc595_sh_dr=0;
dig_hc595_st_dr=0;
dig_hc595_ds_dr=0;
}
void hc595_drive(unsigned char ucLedStatusTemp16_09,unsigned char ucLedStatusTemp08_01)
{
unsigned char i;
unsigned char ucTempData;
hc595_sh_dr=0;
hc595_st_dr=0;
ucTempData=ucLedStatusTemp16_09;
for(i=0;i<8;i++)
{
if(ucTempData>=0x80)hc595_ds_dr=1;
else hc595_ds_dr=0;
hc595_sh_dr=0;
delay_short(1);
hc595_sh_dr=1;
delay_short(1);
ucTempData=ucTempData<<1;
}
ucTempData=ucLedStatusTemp08_01;
for(i=0;i<8;i++)
{
if(ucTempData>=0x80)hc595_ds_dr=1;
else hc595_ds_dr=0;
hc595_sh_dr=0;
delay_short(1);
hc595_sh_dr=1;
delay_short(1);
ucTempData=ucTempData<<1;
}
hc595_st_dr=0;
delay_short(1);
hc595_st_dr=1;
delay_short(1);
hc595_sh_dr=0;
hc595_st_dr=0;
hc595_ds_dr=0;
}
void T0_time(void) interrupt 1
{
TF0=0;
TR0=0;
if(1==GucVoiceStart)
{
if(GuiVoiceCnt!=0)
{
GuiVoiceCnt--;
beep_dr=0;
}
else
{
beep_dr=1;
GucVoiceStart=0;
}
}
key_scan();
display_drive();
TH0=0xfe;
TL0=0x0b;
TR0=1;
}
void delay_short(unsigned int uiDelayShort)
{
static unsigned int i;
for(i=0;i
}
void initial(void)
{
static unsigned char SucInitialLock=0;
if(0==SucInitialLock)
{
SucInitialLock=1;
key_gnd_dr=0;
led_dr=0;
beep_dr=1;
TMOD=0x01;
TH0=0xfe;
TL0=0x0b;
EA=1;
ET0=1;
TR0=1;
}
}
下節(jié)預(yù)告:三種類型變量的定義與賦值語句。
評論