收藏 分享(赏)

单片机C语言程序设计实训99例new.doc

上传人:dreamzhangning 文档编号:2299979 上传时间:2018-09-10 格式:DOC 页数:77 大小:1.92MB
下载 相关 举报
单片机C语言程序设计实训99例new.doc_第1页
第1页 / 共77页
单片机C语言程序设计实训99例new.doc_第2页
第2页 / 共77页
单片机C语言程序设计实训99例new.doc_第3页
第3页 / 共77页
单片机C语言程序设计实训99例new.doc_第4页
第4页 / 共77页
单片机C语言程序设计实训99例new.doc_第5页
第5页 / 共77页
点击查看更多>>
资源描述

1、 单片机 C 语言程序设计实训 99 例青岛大学1单片机 C 语言程序设计实训 99 例案例第 01 篇 基础程序设计09 555 定时器实验/* 名称:555 定时器实验说明:调节外部电阻 RV1 可改变延时值,从而影响灯点亮延时和发声延时。*/#include#define uchar unsigned char#define uint unsigned intsbit Signal=P10;sbit BEEP=P37;/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i#define uchar unsigned char#define

2、 uint unsigned int/数码管位码uchar code DSY_Index=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f;单片机 C 语言程序设计实训 99 例青岛大学2/待显示数字(10 为不显示)uchar code BCD_CODE=2,0,1,0,10,3,10,5;/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i#include#define uchar unsigned char#define uint unsigned intsbit DIN=P20;sbit CSB=P21;

3、sbit CLK=P22;uchar Disp_Buffer=2,0,1,5,10,5,10,9;/显示缓冲,10 为“-”/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i#include#define uchar unsigned char 单片机 C 语言程序设计实训 99 例青岛大学4#define uint unsigned intvoid Initialize_LCD();void DelayMS(uint ms);void ShowString(uchar,uchar,uchar *);sbit K1=P30;sbit K2=

4、P31;sbit K3=P32;uchar code Prompt=“Press K1 - K3 To Start Demo Prog“;/待滚动显示的信息段落,每行不超过 80 个字符,共 6 行uchar const Line_Count=6;uchar code Msg80=“Many CAD users dismiss schematic capture as a necessary evil in the “,“process of creating PCB layout but we have always disputed this point “,“of view. With

5、PCB layout now offering automation of both component “,“can often be the most time consuming element of the exercise.“,“And if you use circuit simulation to develop your ideas, “,“you are going to spend even more time working on the schematic.“;/显示缓冲(2 行)uchar Disp_Buffer32;/垂直滚动显示void V_Scroll_Disp

6、lay()uchar i,j,k=0;uchar *p=Msg0;uchar *q=MsgLine_Count+strlen(MsgLine_Count);/以下仅使用显示缓冲的前 16 字节空间while(pLine_Count) break;p=Msgk; /p 指向下一串的首地址Disp_Bufferi=*p+;/不足 16 个字符时空格补充单片机 C 语言程序设计实训 99 例青岛大学5for(j=i;jLine_Count) break;p=Msgk; /p 指向下一串的首地址Disp_Bufferi=*p+;/不足 32 个字符时空格补充for(j=i;j#include#defi

7、ne uchar unsigned char#define uint unsigned intsbit RS=P20;sbit RW=P21;sbit EN=P22;/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i#define uchar unsigned char#define uint unsigned intsbit CLK=P24; /时钟信号sbit ST=P25; /启动信号sbit EOC=P26; /转换结束信号sbit OE=P27; /输出使能sbit PWM=P30; /PWM 输出/延时void DelayMS(

8、uint ms)uchar i;while(ms-) for(i=0;i#define uchar unsigned char#define uint unsigned int单片机 C 语言程序设计实训 99 例青岛大学10/各数字的数码管段码(共阴)uchar code DSY_CODE=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;sbit CLK=P13; /时钟信号sbit ST=P12; /启动信号sbit EOC=P11; /转换结束信号sbit OE=P10; /输出使能/延时void DelayMS(uint ms)uch

9、ar i;while(ms-) for(i=0;i#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(uint x)单片机 C 语言程序设计实训 99 例青岛大学12uchar i;while(x-)for(i=0;i#include#define uchar unsig

10、ned 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,0xff,0xe7,0xc3,0x81,0x00,0x81

11、,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,0xff,0xff,0xff,0xff,0xff,0x7f

12、,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,0xff,0xff,0xff,0xff,0xfe,0x

13、fc,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,0xff,0xff,0xff,0xff,0xff,0x

14、ff,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,0xff,0xff,0xff,0xff,0xff,0x

15、00,0xff,0x00,0xff,0x00,0xff,0x00,0xff;单片机 C 语言程序设计实训 99 例青岛大学14/延时void DelayMS(uint x)uchar i;while(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,Oper

16、ation_Type=1; /闪烁次数,操作类型变量/延时void DelayMS(uint x)单片机 C 语言程序设计实训 99 例青岛大学15uchar i;while(x-) for(i=0;i#include#define uchar unsigned char#define uint unsigned intuchar 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#defin

17、e uchar unsigned char#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;/延时v

18、oid DelayMS(uint x)uchar 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

19、 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 unsigned char#define uint un

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

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

22、x80,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_BufferKey_Counts-1=Key_Counts;break;case 0xfd: if(Key_Counts0)Display_B

23、uffer-Key_Counts=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 位缓冲单片机 C 语言程序设计实训 99 例青岛大学25uchar Num_Buffer=0,0,0;/按键代码,按键计数uchar Key_Code,Key_Counts=0;/

24、延时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 显示说明:运行本例时,按下的按键值越大点亮的LED 越多。*/#include#include#define uchar unsigned char#define uint unsigned int/矩阵键盘按键特征码表uchar code KeyCodeTable=0x11,0x12,0x1

25、4,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;elseP2_LED=1;P3=P3_LED; /点亮条形 LEDP2=P2_LED;17 数码管显示 4X4 矩阵键盘按键号/* 名称:数码管显示 4X4 矩阵键盘按键号单片机 C 语言程序设计实训 99 例青岛大学28说明:按下任意键时,数码管都会显示其键的序号,扫描程序首先判断按键发生在哪一列,然后根据所发生的行附加不同的值,从而得到按键的序号。*/#

26、include#define uchar unsigned char#define uint unsigned int/段码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;

27、/按键后 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;单片机 C 语言程序设计实训 99 例青岛大学29/蜂鸣器void Beep()uchar i;for(i=0;isbit S1=P10;sbit S2=P11;sbit LED1=P00;sbit LED2=P01;/主程序void

28、 main()单片机 C 语言程序设计实训 99 例青岛大学30while(1)LED1=S1;LED2=S2;19 继电器控制照明设备/* 名称:继电器控制照明设备说明:按下 K1 灯点亮,再次按下时灯熄灭*/#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;t120;t+);/主程序void main()P1=0xff;RELAY=1;while(1)if(K1=0)while(K1=0);RELAY=RELAY;DelayMS(20);20 数码管显示拨码开关编码/* 名称:数码管显示拨码开关编码说明:系统显示拨码开关所设置的编码 000255*/

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

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

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


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

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

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