案例一:LED闪烁
电路:
代码:
#include sbit LED=P1^0; void delay(int i) { while(i--); } void main(void) { LED = 1; while(1) { delay(1000); LED = !LED; } } 案例二:按键控制LED的亮灭 电路: 代码: #include sbit K1=P3^0; sbit L1=P1^0; void main(void) { while(1) { if(K1==0) { L1=0; //灯亮 } else { L1=1; //灯灭 } } } 案例三:多按键控制多LED亮灭 电路 代码 #include unsigned char temp; void main(void) { while(1) { temp=P1>>4; temp=temp | 0xf0; P1=temp; } } 案例四:流水灯 电路 代码 #include unsigned char i; unsigned char temp; unsigned char a,b; void delay(void) { unsigned char m,n,s; for(m=20;m>0;m--) for(n=20;n>0;n--) for(s=248;s>0;s--); } void main(void) { while(1) { temp=0xfe; P1=temp; delay(); for(i=1;i<8;i++) { a=temp< b=temp>>(8-i); P1=a|b; delay(); } for(i=1;i<8;i++) { a=temp>>i; b=temp< P1=a|b; delay(); } } } 案例五:花样灯 电路 代码 #include unsigned char code table[]={0xfe,0xfd,0xfb,0xf7, 0xef,0xdf,0xbf,0x7f, 0xfe,0xfd,0xfb,0xf7, 0xef,0xdf,0xbf,0x7f, 0x7f,0xbf,0xdf,0xef, 0xf7,0xfb,0xfd,0xfe, 0x7f,0xbf,0xdf,0xef, 0xf7,0xfb,0xfd,0xfe, 0x00,0xff,0x00,0xff, 0x01}; unsigned char i; void delay(void) { unsigned char m,n,s; for(m=20;m>0;m--) for(n=20;n>0;n--) for(s=248;s>0;s--); } void main(void) { while(1) { if(table[i]!=0x01) { P1=table[i]; i++; delay(); } else { i=0; } } } 案例六:扬声器发声 电路 代码 #include #include bit flag; sbit P1_7=P1^7; sbit P1_0=P1^0; unsigned char count; void dely500(void) { unsigned char i; for(i=250;i>0;i--) { _nop_(); } } void main(void) { while(1) { if(P1_7==0) { for(count=200;count>0;count--) { P1_0=~P1_0; dely500(); } for(count=200;count>0;count--) { P1_0=~P1_0; dely500(); dely500(); } } } } 案例七:静态数码管循环显示0-9 电路 代码 #include unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char dispcount; void delay02s(void) { unsigned char i,j,k; for(i=20;i>0;i--) for(j=20;j>0;j--) for(k=248;k>0;k--); } void main(void) { while(1) { for(dispcount=0;dispcount<10;dispcount++) { P0=table[dispcount]; delay02s(); } } } 案例八:键控花样灯(按一下改变一个状态) 电路 代码 #include sbit P3_7=P3^7; unsigned char count; void delay10ms(void) { unsigned char i,j; for(i=20;i>0;i--) for(j=248;j>0;j--); } void main(void) { while(1) { if(P3_7==0) { delay10ms(); if(P3_7==0) { count++; if(count==16) { count=0; } P1=~count; while(P3_7==0); } } } } 案例九:键控花样灯(2) 电路 代码 #include sbit P1_0=P1^0; sbit P1_1=P1^1; sbit P1_2=P1^2; sbit P1_3=P1^3; sbit P3_7=P3^7; unsigned char ID; void delay10ms(void) { unsigned char i,j; for(i=20;i>0;i--) for(j=248;j>0;j--); } void delay02s(void) { unsigned char i; for(i=20;i>0;i--) {delay10ms(); } } void main(void) { while(1) { if(P3_7==0) {delay10ms(); if(P3_7==0) { ID++; if(ID==4) { ID=0; } while(P3_7==0); } } switch(ID) { case 0: P1_0=~P1_0; delay02s(); break; case 1: P1_1=~P1_1; delay02s(); break; case 2: P1_2=~P1_2; delay02s(); break; case 3: P1_3=~P1_3; delay02s(); break; } } } 案例十:键控花样灯(3) 电路 代码 #include sbit P3_7=P3^7; unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char Count; void delay10ms(void) { unsigned char i,j; for(i=20;i>0;i--) for(j=248;j>0;j--); } void main(void) { Count=0; P0=table[Count/10]; P2=table[Count%10]; while(1) { if(P3_7==0) { delay10ms(); if(P3_7==0) { Count++; if(Count==100) { Count=0; } P0=table[Count/10]; P2=table[Count%10]; while(P3_7==0); } } } } 案例十一:静态数码管计数(0-99) 电路 代码 #include unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f}; unsigned char Second; void delay1s(void) { unsigned char i,j,k; for(k=100;k>0;k--) for(i=20;i>0;i--) for(j=248;j>0;j--); } void main(void) { Second=0; P0=table[Second/10]; P2=table[Second%10]; while(1) { delay1s(); Second++; if(Second==60) { Second=0; } P0=table[Second/10]; P2=table[Second%10];
相关文章