收藏 分享(赏)

《单片机C语言程序设计》第 1篇 基础程序设计.doc

上传人:dreamzhangning 文档编号:2728463 上传时间:2018-09-26 格式:DOC 页数:76 大小:1.86MB
下载 相关 举报
《单片机C语言程序设计》第 1篇 基础程序设计.doc_第1页
第1页 / 共76页
《单片机C语言程序设计》第 1篇 基础程序设计.doc_第2页
第2页 / 共76页
《单片机C语言程序设计》第 1篇 基础程序设计.doc_第3页
第3页 / 共76页
《单片机C语言程序设计》第 1篇 基础程序设计.doc_第4页
第4页 / 共76页
《单片机C语言程序设计》第 1篇 基础程序设计.doc_第5页
第5页 / 共76页
点击查看更多>>
资源描述

1、1单片机 C 语言程序设计第 1 篇 基础程序设计01 闪烁的 LED/* 名称:闪烁的 LED说明:LED 按设定的时间间隔闪烁*/#include#define uchar unsigned char#define uint unsigned intsbit LED=P10;/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i#include#define uchar unsigned char#define uint unsigned int/延时void DelayMS(http:/ x)uchar i;2while(x-)for(i=0;

2、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 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,0

3、xff,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,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0

4、xff,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,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

5、,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,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

6、,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,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

7、,0xff,0xff,0xff,0xff,0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff;/延时void DelayMS(uint x)uchar i;4while(x-)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;sbit GREEN_B=P05;uchar Flash_Count=0,

8、Operation_Type=1; /闪烁次数,操作类型变量/延时void DelayMS(uint x)uchar i;while(x-) for(i=0;i#include#define uchar unsigned char#define uint unsigned int6uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t#include#define uchar unsigned cha

9、r#define 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;t#include#define uchar unsigned char#define uint unsigned intuchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/延时void DelayMS(uint x)u

10、char t;while(x-) for(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;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t#include#define uchar unsigned char

11、9#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 unsigned char10#define uint unsigned int/延时void

12、 DelayMS(uint x)uchar i;while(x-) for(i=0;i#define uchar unsigned char11#define 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 unsigned char#define uint unsigned

13、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#define uchar unsigned char#define uint unsigned int/段码uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/

14、位码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_BufferKey_Counts-1=Key_Counts;break;case 0xfd: if(Key_Counts0)Display_Buffer-Key_Count

15、s=10;break;case 0xfb: Display_Buffer0=0;for(i=1;i#include#define uchar unsigned char#define 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 Key_Code,Key_Counts=0;/延时void DelayMS(uint x)15uchar i;while(x-

16、) for(i=0;i0) Key_Counts-;break;case 0xfb: Key_Counts=0;Key_Code=0xff;1616 4X4 矩阵键盘控制条形 LED 显示/* 名称:4X4 矩阵键盘控制条形 LED 显示说明:运行本例时,按下的按键值越大点亮的LED 越多。*/#include#include#define uchar unsigned char#define uint unsigned int/矩阵键盘按键特征码表uchar code KeyCodeTable=0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x41,0x42

17、,0x44,0x48,0x81,0x82,0x84,0x88;/延时void DelayMS(uint x)uchar i;while(x-) for(i=0;i=1;elseP2_LED=1;P3=P3_LED; /点亮条形 LEDP2=P2_LED;17 数码管显示 4X4 矩阵键盘按键号/* 名称:数码管显示 4X4 矩阵键盘按键号说明:按下任意键时,数码管都会显示其键的序号,扫描程序首先判断按键发生在哪一列,然后根据所发生的行附加不同的值,从而得到按键的序号。*/#include#define uchar unsigned char#define uint unsigned int18

18、/段码uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,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 位并异或得到改变

19、的值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;isbit S1=P10;sbit S2=P11;sbit LED1=P00;sbit LED2=P01;/主程序void main()while(1)LED1=S1;LED2=S2;19 继电器控制照明设备20/* 名称:继电器控制照明设备说明:按下 K1 灯点亮,再次按下时灯熄灭*/#

20、include#define uchar unsigned char#define uint unsigned intsbit K1=P10;sbit RELAY=P24;/延时void DelayMS(uint ms)uchar t;while(ms-)for(t=0;t#include#define uchar unsigned char#define uint unsigned int/各数字的数码管段码(共阴)uchar code DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;/显示缓冲uchar DSY_Buff

21、er3=0,0,0;/延时void DelayMS(uint ms)21uchar t;while(ms-)for(t=0;t#define uchar unsigned char#define uint unsigned intsbit SPK=P10;sbit K1=P17;/发声函数void Alarm(uchar t)uchar i,j;for(i=0;i#define uchar unsigned char#define uint unsigned intsbit BEEP=P37;sbit K1=P14;sbit K2=P15;sbit K3=P16;sbit K4=P17;/延时

22、void DelayMS(uint x)uchar t;while(x-) for(t=0;t#define uchar unsigned char#define uint unsigned intsbit BEEP=P37;/生日快乐歌的音符频率表,不同频率由不同的延时来决定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

23、_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-) 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;uch

24、ar Count=0;sbit Clear_Key=P36;/数码管上显示计数值void Show_Count_ON_DSY()DSY_Buffer2=Count/100; /获取 3 个数DSY_Buffer1=Count%100/10;DSY_Buffer0=Count%10;if(DSY_Buffer2=0) /高位为 0 时不显示DSY_Buffer2=0x0a;if(DSY_Buffer1=0) /高位为 0,若第二位为 0 同样不显示DSY_Buffer1=0x0a;P0=DSY_CODEDSY_Buffer0;P1=DSY_CODEDSY_Buffer1;P2=DSY_CODED

25、SY_Buffer2;25/主程序void main()P0=0x00;P1=0x00;P2=0x00;IE=0x81; /允许 INT0 中断IT0=1; /下降沿触发while(1)if(Clear_Key=0) Count=0; /清 0Show_Count_ON_DSY();/INT0 中断函数void EX_INT0() interrupt 0Count+; /计数值递增25 外部 INT0 中断控制 LED/* 名称:外部 INT0 中断控制 LED说明:每次按键都会触发 INT0 中断,中断发生时将 LED 状态取反,产生 LED 状态由按键控制的效果*/#include#def

26、ine uchar unsigned char#define uint unsigned intsbit LED=P00;/主程序void main()LED=1;EA=1;EX0=1;IT0=1;while(1);/INT0 中断函数void EX_INT0() interrupt 0LED=LED; /控制 LED 亮灭2626 INT0 及 INT1 中断计数/* 名称:INT0 及 INT1 中断计数说明:每次按下第 1 个计数键时,第 1 组计数值累加并显示在右边 3 只数码管上, 每次按下第 2 个计数键时,第 2 组计数值累加并显示在左边 3 只数码管上,后两个按键分别清零。*/

27、#include#define uchar unsigned char#define uint unsigned intsbit K3=P34; /2 个清零键sbit K4=P35;/数码管段码与位码uchar code DSY_CODE=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;uchar code DSY_Scan_Bits=0x20,0x10,0x08,0x04,0x02,0x01;/2 组计数的显示缓冲,前 3 位一组,后 3 位一组uchar data Buffer_Counts=0,0,0,0,0,0;uint

28、Count_A,Count_B=0;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t#define uchar unsigned char#define uint unsigned intsbit LED=P00;uchar T_Count=0;/主程序void main()TMOD=0x00; /定时器 0 工作方式 028TH0=(8192-5000)/32; /5ms 定时TL0=(8192-5000)%32;IE=0x82; /允许 T0 中断TR0=1;while(1);/T0 中断函数void LED_Flash() interr

29、upt 1TH0=(8192-5000)/32; /恢复初值TL0=(8192-5000)%32;if(+T_Count=100) /0.5s 开关一次 LEDLED=LED;T_Count=0; 28 TIMER0 控制流水灯/* 名称:TIMER0 控制流水灯 说明:定时器控制 P0、P2 口的 LED 滚动显示,本例未使用中断函数。*/#include#include#define uchar unsigned char#define uint unsigned int/主程序void main()uchar T_Count=0;P0=0xfe;P2=0xfe;TMOD=0x01; /定

30、时器 0 工作方式 1TH0=(65536-40000)/256; /40ms 定时TL0=(65536-40000)%256;TR0=1; /启动定时器while(1)if(TF0=1)TF0=0;29TH0=(65536-40000)/256; /恢复初值TL0=(65536-40000)%256;if(+T_Count=5)P0=_crol_(P0,1);P2=_crol_(P2,1);T_Count=0;29 定时器控制 4 个 LED 滚动闪烁/* 名称:定时器控制 4 个 LED 滚动闪烁说明:4 只 LED 在定时器控制下滚动闪烁。*/#include#define uchar

31、unsigned char#define uint unsigned intsbit B1=P00;sbit G1=P01;sbit R1=P02;sbit Y1=P03;uint i,j,k;/主程序void main()i=j=k=0;P0=0xff;TMOD=0x02; /定时器 0 工作方式 2TH0=256-200; /200us 定时TL0=256-200;IE=0x82;TR0=1; /启动定时器while(1);/T0 中断函数void LED_Flash_and_Scroll() interrupt 1if(+k/主程序void main()TMOD=0x05; /定时器 0 为计数器,工作方式 1,最大计数值 65535TH0=0; /初值为 0TL0=0;TR0=1; /启动定时器while(1)P1=TH0;P2=TL0;31 TIMER0 与 TIMER1 控制条形 LED/* 名称:TIMER0 与 TIMER1 控制条形 LED说明:定时器 T0 定时控制上一组条形 LED,滚动速度较快定时器 T1 定时控制下一组条形 LED,滚动速度较慢*/#include#include

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 大学课件

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报