1、单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 1入门资料单片机 C 语言程序设计实训 100 例基于 8051+ Proteus 仿真01 闪烁的 LED /* 名称:闪烁的 LED 说明:LED 按设定的时间间隔闪烁 */ #include #define uchar unsigned char #define uint unsigned int sbit LED=P10; /延时 void DelayMS(uint x) uchar i; while(x-) for(i=0;i #include #define uchar unsigned char
2、#define uint unsigned int /延时 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 2void DelayMS(uint x) uchar i; while(x-) for(i=0;i #include #define uchar unsigned char #define uint unsigned int /延时 void DelayMS(uint x) uchar i; while(x-) for(i=0;i #define uchar unsigned char #define uint unsigned int uchar
3、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, 0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f, 0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,
4、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,0xef,0xf7,0xfb,0xfd,0xfe, 0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0
5、x00,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,0xf3,0xe7,0xcf,0x9f,0x3f,0xff, 0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff, 0xaa,0x5
6、5,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,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f, 0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,
7、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, 0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff ; /延时 void DelayMS(uint x) uchar i; 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 4whi
8、le(x-) for(i=0;i #define uchar unsigned char #define uint unsigned int sbit RED_A=P00; /东西向灯 sbit YELLOW_A=P01; sbit GREEN_A=P02; sbit RED_B=P03; /南北向灯 sbit YELLOW_B=P04; sbit GREEN_B=P05; uchar Flash_Count=0,Operation_Type=1; /闪烁次数,操作类型变量 /延时 void DelayMS(uint x) uchar i; while(x-) for(i=0;i #inclu
9、de #define uchar unsigned char #define uint unsigned int uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff; /延时 void DelayMS(uint x) 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 6 uchar t; while(x-) for(t=0;t #include #define uchar unsigned char #define uint unsigned int uchar
10、code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; /延时 void DelayMS(uint x) uchar t; while(x-) for(t=0;t #include #define uchar unsigned char #define uint unsigned int uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; /延时 void DelayMS(uint x) uchar t; while(x-) for
11、(t=0;t #define uchar unsigned char #define uint unsigned int /段码表 uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90; /位码表 uchar code DSY_IDX=0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80; 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 8/延时 void DelayMS(uint x) uchar t; while(x-) for(t=0;t #
12、include #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 x) uchar t; while(x-) for(t=0;t #include #define uchar uns
13、igned char #define uint unsigned int /延时 void DelayMS(uint x) uchar i; while(x-) for(i=0;i #define uchar unsigned char #define uint unsigned int sbit 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-)
14、 for(i=0;i #define uchar unsigned char #define uint unsigned int /延时 void DelayMS(uint x) uchar i; while(x-) for(i=0;i4; k=0; /检查 1 所在位置,累加获取按键号 k while(Key_State!=0) k+; Key_State=1; /根据按键号 k 进行 4 种处理 switch(k) case 1: if(P0=0x00) P0=0xff; P0 #define uchar unsigned char #define uint unsigned int /段
15、码 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_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
16、_BufferKey_Counts-1=Key_Counts; break; case 0xfd: if(Key_Counts0)Display_Buffer-Key_Counts=10; break; case 0xfb: Display_Buffer0=0; for(i=1;i #include #define uchar unsigned char 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 14#define uint unsigned int /段码 uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x
17、92,0x82,0xf8,0x80,0x90,0xff; /待显示的 3 位缓冲 uchar Num_Buffer=0,0,0; /按键代码,按键计数 uchar Key_Code,Key_Counts=0; /延时 void DelayMS(uint x) uchar i; while(x-) for(i=0;i0) Key_Counts-; break; case 0xfb: Key_Counts=0; Key_Code=0xff; 16 4X4 矩阵键盘控 制条形 LED 显示 /* 名称:4X4 矩阵键盘控制条形 LED 显示 说明:运行本例时,按下 的 按 键 值 越 大点 亮 的
18、LED 越多。 */ #include #include #define uchar unsigned char #define uint unsigned int /矩阵键盘按键特征码表 uchar code 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;i=1; else P2_LED=1; P3=P3_LED; /点亮条形 LED P2=P2_
19、LED; 17 数码管显示 4X4 矩阵键盘按键号 /* 名称:数码管显示 4X4 矩阵键盘按键号 说明:按下任意键时,数码管都会显示其键的序号,扫描程序首先判断按键发生在哪一列,然后根据所发生的行附加不同的单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 17值,从而得到按键的序号。 */ #include #define uchar unsigned char #define uint unsigned int /段码 uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
20、, 0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00; sbit BEEP=P37; /上次按键和当前按键的序号,该矩阵中序号范围 015,16 表示无按键 uchar Pre_KeyNo=16,KeyNo=16; /延时 void DelayMS(uint x) uchar i; while(x-) for(i=0;i40x0f;/*按键后 f0 变成 XXXX0000,X 中有 1 个为 0,三个仍为 1;高 4 位转移到低 4 位并异或得到改变的值*/switch(Tmp) /对 03 行分别附加起始值 0,4,8,12 单片机 C 语言程序设计实训 100 例基于
21、8051+Proteus 仿真陈文亚 18case 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;i sbit S1=P10; sbit S2=P11; sbit LED1=P00; sbit LED2=P01; 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 19/主程序 void main() while(1) LED1=S1; LED2=S2; 19 继电器控制
22、照明设备 /* 名称:继电器控制照明设备 说明:按下 K1 灯点亮,再次按下时灯熄灭 */ #include #define uchar unsigned char #define uint unsigned int sbit K1=P10; sbit RELAY=P24; /延时 void DelayMS(uint ms) uchar t; while(ms-)for(t=0;t #include 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 20#define uchar unsigned char #define uint unsigned int
23、/各数字的数码管段码(共阴) uchar code DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f; /显示缓冲 uchar DSY_Buffer3=0,0,0; /延时 void DelayMS(uint ms) uchar t; while(ms-)for(t=0;t #define uchar unsigned char #define uint unsigned int sbit SPK=P10; sbit K1=P17; 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 21/发声
24、函数 void Alarm(uchar t) uchar i,j; for(i=0;i #define uchar unsigned char #define uint unsigned int sbit BEEP=P37; sbit K1=P14; sbit K2=P15; sbit K3=P16; sbit K4=P17; /延时 void DelayMS(uint x) uchar t; while(x-) for(t=0;t #define uchar unsigned char #define uint unsigned int sbit BEEP=P37; /生日快乐歌的音符频率表
25、,不同频率由不同的延时来决定 uchar code SONG_TONE=212,212,190,212,159,169,212,212,190,212,142,159, 212,212,106,126,159,169,190,119,119,126,159,142,159,0; /生日快乐歌节拍表,节拍决定每个音符的演奏长短 uchar code SONG_LONG=9,3,12,12,12,24,9,3,12,12,12,24, 9,3,12,12,12,12,12,9,3,12,12,12,24,0; /延时 void DelayMS(uint x) uchar t; while(x-)
26、for(t=0;t #define uchar unsigned char #define uint unsigned int /09 的段码 uchar code DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00; /计数值分解后各个待显示的数位 uchar DSY_Buffer=0,0,0; uchar Count=0; sbit Clear_Key=P36; /数码管上显示计数值 void Show_Count_ON_DSY() DSY_Buffer2=Count/100; /获取 3 个数 DSY_Buffe
27、r1=Count%100/10; DSY_Buffer0=Count%10; if(DSY_Buffer2=0) /高位为 0 时不显示 DSY_Buffer2=0x0a; 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 24if(DSY_Buffer1=0) /高位为 0,若第二位为 0 同样不显示 DSY_Buffer1=0x0a; P0=DSY_CODEDSY_Buffer0; P1=DSY_CODEDSY_Buffer1; P2=DSY_CODEDSY_Buffer2; /主程序 void main() P0=0x00; P1=0x00; P2=0x
28、00; IE=0x81; /允许 INT0 中断 IT0=1; /下降沿触发 while(1) if(Clear_Key=0) Count=0; /清 0 Show_Count_ON_DSY(); /INT0 中断函数 void EX_INT0() interrupt 0 Count+; /计数值递增 25 外部 INT0 中断控制 LED /* 名称:外部 INT0 中断控制 LED 说明:每次按键都会触发 INT0 中断,中断发生时将 LED 状态取反,产生 LED 状态由按键控制的效果 */ #include #define uchar unsigned char #define uint unsigned int sbit LED=P00; /主程序void main() LED=1; EA=1; EX0=1; IT0=1; while(1); /INT0 中断函数 void EX_INT0() interrupt 0 单片机 C 语言程序设计实训 100 例基于 8051+Proteus 仿真陈文亚 25 LED=LED; /控制 LED 亮灭