第十节:一个用来学习C语言的模板程序。
目前,几乎所有的初学者在学习和上机练习C语言的时候,都是在电脑上安装VC这个调试软件,在源代码里只要调用打印语句printf就可以观察到不同的变量结果,挺方便的。但是现在我要提出另外一种方法,学习单片机的C语言,不一定非要用VC调试软件,也可以直接在坚鸿51学习板上学习和上机练习的。我可以做一个调试模板程序给初学者使用,利用8位数码管和16个LED灯来显示不同的变量结果,利用3个按键来切换显示不同的变量,这样就能达到类似在VC平台下用printf语句来观察变量的效果。甚至我个人认为这样比用VC调试的效果还更加直观。现在重点介绍这个模板程序的使用。
在模板程序里,初学者只需要在主程序的初始化区域填入自己练习的C语言代码,最后把需要观察的变量赋值给窗口变量就可以了,其它部分的代码属于模板的监控调试代码,大家暂时不用读懂它,直接复制过来就可以了。上述所谓的“赋值”,就是“=”这个语句,它表面上像我们平时用的等于号,实际上不是等于号,而是代表“给”的意思,把“=”符号右边的数复制一份给左边的变量,比如“a=36;”就是代表把36这个数值复制一份给变量a,执行这条指令后,a就等于36了。这里的分号“;”代表一条程序指令的结束。窗口变量有几个?有哪些?一共有10个,分别是GuiWdData0,GuiWdData1,GuiWdData2,GuiWdData3,GuiWdData4,GuiWdData5,GuiWdData6,GuiWdData7,GuiWdData8,GuiWdData9。这10个窗口变量是给大家调试专用的,8位数码管可以切换显示10个窗口变量,最左边2位数码管代表窗口变量号,剩下6位数码管显示十进制的窗口变量数值,另外16个LED实时显示此数据的二进制格式。最左边2位数码管从“0-”到“9-”代表从第0个窗口变量到第9个窗口变量,也就是GuiWdData0依次到GuiWdData9。用S1和S5按键可以切换显示不同的窗口变量,按住S9不放可以观察到当前窗口变量的十六进制格式数据,松开S9按键后,又自动返回显示当前窗口变量的十进制数据。
该模板程序是基于坚鸿51学习板,现在跟大家分享这个程序,要让这10个窗口变量分别显示10,11,12,13,14,15,16,17,18,19这10个数,用S1按键可以切换显示从小往大的窗口变量号,用S5按键可以切换显示从大往小的窗口变量号。再强调一次,大家只需要关注主程序main函数的初始化区域就可以了,其它的代码请直接复制过来,不用理解。比如:
void main() //主程序
{
//...初始化区域
while(1)
{
}
}
详细的源代码如下:
- #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语言学习区域的开始---------------------------------------------------------------------------*/
- GuiWdData0=10; //把10这个数值放到窗口变量0里面显示
- GuiWdData1=11; //把11这个数值放到窗口变量1里面显示
- GuiWdData2=12; //把12这个数值放到窗口变量2里面显示
- GuiWdData3=13; //把13这个数值放到窗口变量3里面显示
- GuiWdData4=14; //把14这个数值放到窗口变量4里面显示
- GuiWdData5=15; //把15这个数值放到窗口变量5里面显示
- GuiWdData6=16; //把16这个数值放到窗口变量6里面显示
- GuiWdData7=17; //把17这个数值放到窗口变量7里面显示
- GuiWdData8=18; //把18这个数值放到窗口变量8里面显示
- GuiWdData9=19; //把19这个数值放到窗口变量9里面显示
- /*---C语言学习区域的结束---------------------------------------------------------------------------*/
- 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<uiDelayShort;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;
- }
- }
复制代码
下节预告:三种类型变量的定义与赋值语句。
好东西,先收藏,谢谢分享!
很好的资料
感谢,希望继续支持哦