1、(完整word)单片机C语言程序设计实训100例单片机C语言程序设计实训100例基于8051+Proteus仿真案例第 01 篇 基础程序设计01闪烁的LED/* 名称:闪烁的LED说明:LED按设定的时间间隔闪烁*/includereg51。h#define uchar unsigned char#define uint unsigned intsbit LED=P10;/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i120;i+);/主程序void main()while(1)LED=LED;DelayMS(150);02 从左到右的流水灯
2、/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果/includereg51.hincludedefine uchar unsigned char#define uint unsigned int/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i120;i+);/主程序void main()P0=0xfe;while(1)P0=_crol_(P0,1); /P0的值向左循环移动DelayMS(150);03 8只LED左右来回点亮/* 名称:8只LED左右来回点亮说明:程序利用循环移位函数_crol_和_
3、cror_形成来回滚动的效果*/#includereg51.hincludeintrins.h#define uchar unsigned char#define uint unsigned int/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i120;i+);/主程序void main()uchar i;P2=0x01;while(1)for(i=0;i7;i+)P2=_crol_(P2,1); /P2的值向左循环移动DelayMS(150);for(i=0;i7;i+)P2=_cror_(P2,1); /P2的值向右循环移动DelayMS(
4、150);04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示/#includereg51.hdefine uchar unsigned char#define uint unsigned intuchar code Pattern_P0= 0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,
5、0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f, 0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff, 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0x
6、ef,0xf7,0xfb,0xfd,0xfe, 0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe, 0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff;uchar code Pattern_P2= 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0
7、xf3,0xe7,0xcf,0x9f,0x3f,0xff, 0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff, 0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f, 0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff, 0xff,0xff,0xff,0xff,0xff,0x
8、ff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f, 0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00, 0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0x0
9、0,0xff,0x00,0xff,0x00,0xff,0x00,0xff;/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i120;i+);/主程序void main()uchar i;while(1)/从数组中读取数据送至P0和P2口显示for(i=0;i#define uchar unsigned char#define uint unsigned intsbit RED_A=P00; /东西向灯sbit YELLOW_A=P01;sbit GREEN_A=P02;sbit RED_B=P03; /南北向灯sbit YELLOW_B=P04;
10、sbit GREEN_B=P05;uchar Flash_Count=0,Operation_Type=1; /闪烁次数,操作类型变量/延时void DelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);/交通灯切换void Traffic_Light()switch(Operation_Type)case 1:/东西向绿灯与南北向红灯亮RED_A=1;YELLOW_A=1;GREEN_A=0;RED_B=0;YELLOW_B=1;GREEN_B=1;DelayMS(2000);Operation_Type=2;break;case 2:/东西向黄
11、灯闪烁,绿灯关闭DelayMS(300);YELLOW_A=YELLOW_A;GREEN_A=1;if(+Flash_Count!=10) return; /闪烁5次Flash_Count=0;Operation_Type=3;break;case 3:/东西向红灯,南北向绿灯亮RED_A=0;YELLOW_A=1;GREEN_A=1;RED_B=1;YELLOW_B=1;GREEN_B=0;DelayMS(2000);Operation_Type=4;break;case 4:/南北向黄灯闪烁5次DelayMS(300);YELLOW_B=YELLOW_B;GREEN_B=1;if(+Fla
12、sh_Count!=10) return;Flash_Count=0;Operation_Type=1;/主程序void main()while(1) Traffic_Light();06 单只数码管循环显示09/名称:单只数码管循环显示09说明:主程序中的循环语句反复将09的段码送至P0口,使数字09循环显示/#includeincludeintrins.h#define uchar unsigned chardefine uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9
13、0,0xff;/延时void DelayMS(uint x)uchar t;while(x) for(t=0;t#includeintrins.hdefine uchar unsigned chardefine uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t120;t+);/主程序void main()uchar i,wei=0x80;while(1)for(i=0;i
14、#includeintrins.h#define uchar unsigned chardefine uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;tdefine uchar unsigned char#define uint unsigned int/段码表uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0x
15、f8,0x80,0x90;/位码表uchar code DSY_IDX=0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t120;t+);/主程序void main()uchar i,j;while(1)for(i=0;i30;i+)for(j=0;j8;j+)P0=0xff;P0=DSY_CODEj; /发送段码P2=DSY_IDXj; /发送位码DelayMS(2); P2=0x00; /关闭所有数码管并延时DelayMS(1000);10 8只数码管滚动显示数
16、字串电路如上图/名称:8只数码管滚动显示数字串说明:数码管向左滚动显示3个字符构成的数字串*/includereg51.hincludeintrins.h#define uchar unsigned char#define uint unsigned int/段码表uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/下面数组看作环形队列,显示从某个数开始的8个数(10表示黑屏)uchar Num=10,10,10,10,10,10,10,10,2,9,8;/延时void DelayMS(uint
17、 x)uchar t;while(x-) for(t=0;t120;t+);/主程序void main()uchar i,j,k=0,m=0x80;while(1)/刷新若干次,保持一段时间的稳定显示for(i=0;i15;i+)for(j=0;jincludeintrins。h#define uchar unsigned chardefine uint unsigned int/延时void DelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);/根据P1口的按键移动LEDvoid Move_LED()if ((P1&0x10)=0) P0=_c
18、ror_(P0,1); /K1else if(P10x20)=0) P0=_crol_(P0,1);/K2else if((P1&0x40)=0) P2=_cror_(P2,1); /K3else if(P10x80)=0) P2=_crol_(P2,1);/K4/主程序void main()uchar Recent_Key;/最近按键P0=0xfe;P2=0xfe;P1=0xff;Recent_Key=0xff;while(1)if(Recent_Key!=P1)Recent_Key=P1; /保存最近按键Move_LED();DelayMS(10);12 K1-K4 按键状态显示/*名称:
19、K1K4 按键状态显示说明:K1、K2按下时LED点亮,松开时熄灭, K3、K4按下并释放时LED点亮,再次按下并释放时熄灭;*/#includereg51。h#define uchar unsigned chardefine uint unsigned intsbit LED1=P00;sbit LED2=P01;sbit LED3=P02;sbit LED4=P03;sbit K1=P10;sbit K2=P11;sbit K3=P12;sbit K4=P13;/延时void DelayMS(uint x)uchar i;while(x) for(i=0;i#define uchar un
20、signed char#define uint unsigned int/延时void DelayMS(uint x)uchar i;while(x-) for(i=0;i4;k=0;/检查1所在位置,累加获取按键号kwhile(Key_State!=0)k+;Key_State=1;/根据按键号k进行4种处理switch(k)case 1:if(P0=0x00) P0=0xff;P0=1;DelayMS(200);break;case 2:P0=0xf0;break;case 3:P0=0x0f;break;case 4:P0=0xff;14 K1K4 控制数码管移位显示/*名称:K1K4
21、控制数码管移位显示说明:按下K1时加1计数并增加显示位, 按下K2时减1计数并减少显示位, 按下K3时清零。/#includedefine uchar unsigned char#define uint unsigned int/段码uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/位码uchar code DSY_Index=0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01;/待显示到各数码管的数字缓冲(开始仅在0位显示0,其他黑屏)uchar Display_
22、Buffer=0,10,10,10,10,10,10,10;/延时void DelayMS(uint x)uchar i;while(x) for(i=0;i8) Key_Counts=8;Display_BufferKey_Counts1=Key_Counts;break;case 0xfd:if(Key_Counts0)Display_Buffer-Key_Counts=10;break;case 0xfb:Display_Buffer0=0;for(i=1;i8;i+) Display_Bufferi=10;Key_Counts=0;/若键未释放则仅刷新显示,不进行键扫描while(P1
23、!=0xff) Show_Count_ON_DSY();15 K1-K4 控制数码管加减演示/*名称:K1-K4 控制数码管加减演示说明:按下K1后加1计数,按下K2后减1计数,按下K3后清零。/includeincludeintrins.h#define uchar unsigned chardefine uint unsigned int/段码uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/待显示的3位缓冲uchar Num_Buffer=0,0,0;/按键代码,按键计数uchar Ke
24、y_Code,Key_Counts=0;/延时void DelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);/显示函数void Show_Counts_ON_DSY()uchar i,j=0x01;Num_Buffer2=Key_Counts/100;Num_Buffer1=Key_Counts/10%10;Num_Buffer0=Key_Counts%10;for(i=0;i3;i+)j=_cror_(j,1);P0=0xff;P0=DSY_CODENum_Bufferi;P2=j;DelayMS(1);/主程序void main()uchar
25、 i;P0=0xff;P1=0xff;P2=0x00;Key_Code=0xff;while(1)Show_Counts_ON_DSY();P1=0xff;Key_Code=P1;/有键按下时,数码管刷新显示30次,该行代码同时起到延时作用if(Key_Code!=0xff)for(i=0;i30;i+) Show_Counts_ON_DSY();switch(Key_Code)case 0xfe:if(Key_Countsincludeintrins.h#define uchar unsigned char#define uint unsigned int/矩阵键盘按键特征码表uchar c
26、ode KeyCodeTable=0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42,0x44,0x48,0x81,0x82,0x84,0x88;/延时void DelayMS(uint x)uchar i;while(x-) for(i=0;i120;i+);/键盘扫描uchar Keys_Scan()uchar sCode,kCode,i,k;/低4位置0,放入4行P1=0xf0;/若高4位出现0,则有键按下if(P10xf0)!=0xf0)DelayMS(2);if(P10xf0)!=0xf0)sCode=0xfe;/行扫描码初值for(k
27、=0;k4;k+)/对4行分别进行扫描P1=sCode;if(P10xf0)!=0xf0)kCode=P1;for(i=0;i16;i+)/查表得到按键序号并返回if(kCode=KeyCodeTablei)return(i);elsesCode=_crol_(sCode,1); return(-1);/主程序void main()uchar i,P2_LED,P3_LED;uchar KeyNo=-1;/按键序号,-1表示无按键while(1)KeyNo=Keys_Scan();/扫描键盘获取按键序号KeyNo if(KeyNo!=1)P2_LED=0xff;P3_LED=0xff;for(
28、i=0;i=KeyNo;i+)/键值越大,点亮的LED越多if(i40x0f;/按键后f0变成XXXX0000,X中有1个为0,三个仍为1;高4位转移到低4位并异或得到改变的值switch(Tmp)/对03行分别附加起始值0,4,8,12case 1:KeyNo+=0;break;case 2:KeyNo+=4;break;case 4:KeyNo+=8;break;case 8:KeyNo+=12;/蜂鸣器void Beep()uchar i;for(i=0;i100;i+)DelayMS(1);BEEP=BEEP;BEEP=0;/主程序void main()P0=0x00;BEEP=0;w
29、hile(1)P1=0xf0;if(P1!=0xf0) Keys_Scan(); /获取键序号if(Pre_KeyNo!=KeyNo)P0=DSY_CODEKeyNo;Beep();Pre_KeyNo=KeyNo;DelayMS(100);18 开关控制LED/*名称:开关控制LED说明:开关S1和S2分别控制LED1和LED2。*/includereg51.hsbit S1=P10;sbit S2=P11;sbit LED1=P00;sbit LED2=P01;/主程序void main()while(1)LED1=S1;LED2=S2;19 继电器控制照明设备/名称:继电器控制照明设备说明:按下K1灯点亮,再次按下时灯熄灭*/includereg51。h#define uchar unsigned chardefine uint unsigned intsbit K1=P10;sbit RELAY=P24;/延时