单片机C语言程序经典实例.doc
《单片机C语言程序经典实例.doc》由会员分享,可在线阅读,更多相关《单片机C语言程序经典实例.doc(25页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、发光二极管间隔1s流动,8位数码管间隔2s显示0-F#include#include#define uint unsigned int#define uchar unsigned charuchar temp,aa,num;sbit dula=P26;sbit wela=P27;uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;void delay(uint z);void main()num=0;aa=0;TMOD=0x01;TH0=(65536-5
2、0000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;temp=0xfe;P1=temp;dula=1;P0=table0;dula=0;wela=1;P0=0xc0;wela=0;while(1)delay(1000);temp=_crol_(temp,1);P1=temp;/*if(aa=40)aa=0;num+;if(num=16)num=0;dula=1;P0=tablenum;dula=0;*/void delay(uint z)uint x,y;for(x=z;x0;x-)for(y=110;y0;y-);void timer0() in
3、terrupt 1 TH0=(65536-50000)/256;TL0=(65536-50000)%256;aa+;if(aa=40)aa=0;num+;if(num=16)num=0;dula=1;P0=tablenum;dula=0;6位数码管显示123456:#include#include#define uint unsigned int#define uchar unsigned charuchar temp,aa,numdu,numwe;sbit dula=P26;sbit wela=P27;uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6
4、d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;uchar code tablewe=0xfe,0xfd,0xfb,0xf7,0xef,0xdf;/void delay(uint z);void main()numdu=0;numwe=0;aa=0;TMOD=0x01;TH0=(65536-5000)/256;TL0=(65536-5000)%256;EA=1;ET0=1;TR0=1;while(1)/delay(1000);/temp=_crol_(temp,1);/P1=temp;if(aa=1)aa=0;numdu+;if(numd
5、u=7)numdu=1;dula=1;P0=tablenumdu;dula=0;wela=1;P0=tablewenumwe;wela=0;numwe+;if(numwe=6)numwe=0;/*void delay(uint z)uint x,y;for(x=z;x0;x-)for(y=110;y0;y-);*/void timer0() interrupt 1 TH0=(65536-5000)/256;TL0=(65536-5000)%256;aa+;三位数码管间隔1s显示从000加到100,循环#include#define uint unsigned int#define uchar
6、unsigned charuchar temp,aa,bai,shi,ge;sbit dula=P26;sbit wela=P27;uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;void display(uchar bai,uchar shi,uchar ge);void delay(uint z);void init();void main()init();/初始化子程序while(1)if(aa=20) aa=0;temp+;if(temp=1
7、00) temp=0; bai=temp/100;shi=temp%100/10;ge=temp%10; display(bai,shi,ge);void delay(uint z)uint x,y;for(x=z;x0;x-)for(y=110;y0;y-);void display(uchar bai,uchar shi,uchar ge) dula=1;P0=tablebai;dula=0;P0=0xff;wela=1;P0=0xfe;wela=0;delay(1);dula=1;P0=tableshi;dula=0;P0=0xff;wela=1;P0=0xfd;wela=0;delay
8、(1);dula=1;P0=tablege;dula=0;P0=0xff;wela=1;P0=0xfb;wela=0;delay(1);void init()wela=0;dula=0;temp=0;TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;void timer0() interrupt 1TH0=(65536-50000)/256;TL0=(65536-50000)%256;aa+;Lesson5_1:#include #include #define uchar unsigned char#
9、define uint unsigned intsbit dula=P26;sbit wela=P27;uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x76,0x79,0x38,0x3f,0;uchar temp,t0,t1,bai,shi,ge,flag,flag1;uint shu;void init();void display(uchar,uchar,uchar,uchar,uchar,uchar);void delay(uint z)u
10、int t1,y;for(t1=z;t10;t1-)for(y=110;y0;y-);void main()init();while(1)if(flag1!=1)display(7,6,5,bai,shi,ge);elsedisplay(16,17,18,18,19,20);void init()shu=432;temp=0xfe;P1=temp;TMOD=0x11;TH0=(65536-50000)/256;TL0=(65536-50000)%256;TH1=(65536-50000)/256;TL1=(65536-50000)%256;EA=1;ET0=1;ET1=1;TR0=1;TR1=
11、1;void timer0() interrupt 1TH0=(65536-50000)/256;TL0=(65536-50000)%256;t0+;if(flag!=1)if(t0=10)t0=1;temp=_crol_(temp,1);P1=temp;elseif(t0%4=0)P1=P1;if(t0=60)TR0=0;P1=0xff;flag1=1;void timer1() interrupt 3TH1=(65536-50000)/256;TL1=(65536-50000)%256;t1+;if(t1=2)t1=0;shu-;bai=shu/100;shi=shu%100/10;ge=
12、shu%10;if(shu=398)TR0=0;TH0=(65536-50000)/256;TL0=(65536-50000)%256;TR0=1;flag=1;t0=0;P1=0xff;TR1=0;void display(uchar aa,uchar bb,uchar cc,uchar ba,uchar sh,uchar g) dula=1;P0=tableaa;dula=0;P0=0xff;wela=1;P0=0xfe;wela=0;delay(1); dula=1;P0=tablebb;dula=0;P0=0xff;wela=1;P0=0xfd;wela=0;delay(1); dul
13、a=1;P0=tablecc;dula=0;P0=0xff;wela=1;P0=0xfb;wela=0;delay(1); dula=1;P0=tableba;dula=0;P0=0xff;wela=1;P0=0xf7;wela=0;delay(1);dula=1;P0=tablesh;dula=0;P0=0xff;wela=1;P0=0xef;wela=0;delay(1);dula=1;P0=tableg;dula=0;P0=0xff;wela=1;P0=0xdf;wela=0;delay(1);Lesson5_2:键盘#include#define uint unsigned int#d
14、efine uchar unsigned charsbit d1=P10;sbit dula=P26;sbit wela=P27;sbit key1=P34;uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;uchar num;void delay(uint z) uint x,y;for(x=z;x0;x-)for(y=110;y0;y-);void main() wela=1;P0=0xfe;wela=0;P3=0xff;while(1) if(k
15、ey1=0)delay(5);if(key1=0)d1=0;num+;if(num=10)num=0;while(!key1);delay(5);while(!key1);elsed1=1;dula=1;P0=tablenum;dula=0;Lesson5_3:键盘#include#define uint unsigned int#define uchar unsigned charsbit dula=P26;sbit wela=P27;sbit key1=P34;uchar code table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6
16、f,0x77,0x7c,0x39,0x5e,0x79,0x71,0;uchar num,temp,num1;void delay(uint z)uint x,y;for(x=z;x0;x-)for(y=110;y0;y-);uchar keyscan();void display(uchar aa);void main()num=17;dula=1;P0=0;dula=0;wela=1;P0=0xc0;wela=0;while(1)display(keyscan();void display(uchar aa)dula=1;P0=tableaa-1;dula=0;uchar keyscan()
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 单片机 语言 程序 经典 实例
限制150内