《单片机C语言程序设计实训99例(共77页).doc》由会员分享,可在线阅读,更多相关《单片机C语言程序设计实训99例(共77页).doc(77页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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;i120;i+);/主程序void main()while(1)if(Signal)BEEP=BEEP;Dela
2、yMS(3);10 BCD译码数码管显示数字/*名称:BCD译码数码管显示数字说明:BCD码经4511译码后输出数码管段码,实现数码管显示(4511驱动数码管)。*/#include#define uchar unsigned char#define uint unsigned int/数码管位码uchar code DSY_Index=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f;/待显示数字(10为不显示)uchar code BCD_CODE=2,0,1,0,10,3,10,5;/延时void DelayMS(uint ms)uchar i;while(
3、ms-) for(i=0;i120;i+);/主程序void main()uchar k;while(1)for(k=0;k8;k+)P2=DSY_Indexk;P1=BCD_CODEk;DelayMS(1);11 MAX7221控制数码管动态显示/*名称:MAX7221控制数码管动态显示说明:本例用MAX7221控制8只数码管动态显示,这样大大减少了单片机引脚和机器时间的占用。*/#include#include#define uchar unsigned char#define uint unsigned intsbit DIN=P20;sbit CSB=P21;sbit CLK=P22;
4、uchar Disp_Buffer=2,0,1,5,10,5,10,9;/显示缓冲,10为“-”/延时void DelayMS(uint ms)uchar i;while(ms-) for(i=0;i120;i+);/写数据void Write(uchar Addr,uchar Dat)uchar i;CSB=0;for(i=0;i8;i+)CLK=0;Addr=1;DIN=CY;CLK=1;_nop_();_nop_();CLK=0;for(i=0;i8;i+)CLK=0;Dat=1;DIN=CY;CLK=1;_nop_();_nop_();CLK=0;CSB=1;/MAX7221初始化vo
5、id Initialise()Write(0x09,0xff);/编码模式地址0x09 0x000xff,为1的则位选通Write(0x0a,0x07);/亮度地址0x0a 0x000x0f,0x0f最亮Write(0x0b,0x07);/扫描数码管个数地址0x0b,最多扫描8只数码管Write(0x0c,0x01);/工作模式地址0x0c 0x00:关闭;0x01:正常/主程序void main()uchar i;Initialise();/初始化DelayMS(1);for(i=0;i8;i+)/显示8个数码管Write(i+1,Disp_Bufferi);while(1);12 LCD1
6、602字符液晶滚动演示程序/main.c/*名称:LCD1602字符液晶滚动演示程序说明:K1K3按钮分别实现液晶垂直或水平滚动显示及暂停与继续控制。*/#include#include#define uchar unsigned char #define uint unsigned intvoid Initialize_LCD();void DelayMS(uint ms);void ShowString(uchar,uchar,uchar *);sbit K1=P30;sbit K2=P31;sbit K3=P32;uchar code Prompt=Press K1 - K3 To St
7、art 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 PCB layout now offering automation of both component ,can often
8、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_Display()uchar i,j,k=0;uchar *p=Msg0;uchar *q=MsgLine_Count+strlen(MsgLine_C
9、ount);/以下仅使用显示缓冲的前16字节空间while(pq)for(i=0;i16&pLine_Count) break;p=Msgk;/p指向下一串的首地址Disp_Bufferi=*p+;/不足16个字符时空格补充for(j=i;j16;j+) Disp_Bufferj= ;/垂直滚动显示while(F0) DelayMS(5);ShowString(0,0, );DelayMS(150);while(F0) DelayMS(5);ShowString(0,1,Disp_Buffer);DelayMS(150);while(F0) DelayMS(5);ShowString(0,0,
10、Disp_Buffer);ShowString(0,1, );DelayMS(150);/最后清屏ShowString(0,0, );ShowString(0,1, );/水平滚动显示void H_Scroll_Display()uchar i,j,k=0,L=0;uchar *p=Msg0;uchar *q=MsgLine_Count+strlen(MsgLine_Count);/将32个字符的显示缓冲前16个字符设为空格for(i=0;i16;i+) Disp_Bufferi= ;while(pq)/忽略缓冲中首尾可能出现的空格if(i=16|i=31)&*p= ) p+;for(i=16
11、;i32&pLine_Count) break;p=Msgk;/p指向下一串的首地址Disp_Bufferi=*p+;/不足32个字符时空格补充for(j=i;j32;j+) Disp_Bufferj= ;/水平滚动显示for(i=0;i=16;i+)while(F0) DelayMS(5);ShowString(0,L,Disp_Buffer+i);while(F0) DelayMS(5);DelayMS(20);L=(L=0)?1:0;/行号在0,1间交替DelayMS(300);/如果显示结束时停留在第0行,则清除第1行的内容if(L=1) ShowString(0,1, );/外部中断
12、0,由K3控制暂停与继续显示void EX_INT0() interrupt 0F0=!F0;/暂停与继续显示控制标志位/主程序void main()uint Count=0;IE=0x81;/允许外部中断0IT0=1;/下降沿触发F0=0;/暂停与继续显示控制标志位Initialize_LCD();ShowString(0,0,Prompt);ShowString(0,1,Prompt+16);while(1)if(K1=0)V_Scroll_Display();DelayMS(300);elseif(K2=0)H_Scroll_Display();DelayMS(300);/LCD1602
13、.c/*名称:液晶控制与显示程序说明:本程序是通用的1602液晶控制程序。*/#include#include#define 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;i120;i+);/忙检查uchar Busy_Check()uchar LCD_Status;RS=0;/寄存器选择RW=1;/读状态寄存器EN=1;/开始读DelayMS(1);LCD_Status=P0;E
14、N=0;return LCD_Status;/写LCD命令void Write_LCD_Command(uchar cmd)while(Busy_Check()&0x80)=0x80);/忙等待RS=0;/选择命令寄存器RW=0;/写EN=0;P0=cmd;EN=1;DelayMS(1);EN=0;/发送数据void Write_LCD_Data(uchar dat)while(Busy_Check()&0x80)=0x80);/忙等待RS=1;RW=0;EN=0;P0=dat;EN=1;DelayMS(1);EN=0;/LCD初始化void Initialize_LCD()Write_LCD
15、_Command(0x38);DelayMS(1);Write_LCD_Command(0x01);DelayMS(1);/清屏Write_LCD_Command(0x06);DelayMS(1);/字符进入模式:屏幕不动,字符后移Write_LCD_Command(0x0c);DelayMS(1);/显示开,光标关/显示字符串void ShowString(uchar x,uchar y,uchar *str)uchar i=0;if(y=0) Write_LCD_Command(0x80|x);/设置显示起始位置if(y=1) Write_LCD_Command(0xc0|x);for(i
16、=0;i16;i+)/输出字符串Write_LCD_Data(stri);1319 用ADC0808控制PWM输出/*名称:用ADC0808控制PWM输出说明:使用数模转换芯片ADC0808,通过调节可变电阻RV1来调节脉冲宽度,运行程序时,通过虚拟示波器观察占空比的变化。*/#include#define uchar unsigned char#define uint unsigned intsbit CLK=P24;/时钟信号sbit ST=P25;/启动信号sbit EOC=P26;/转换结束信号sbit OE=P27;/输出使能sbit PWM=P30;/PWM输出/延时void De
17、layMS(uint ms)uchar i;while(ms-) for(i=0;i40;i+);/主程序void main()uchar Val;TMOD=0x02;/T1工作模式2TH0=0x14;TL0=0x00;IE=0x82;TR0=1;while(1)ST=0;ST=1;ST=0;/启动A/D转换while(!EOC);/等待转换完成OE=1;Val=P1;/读转换值OE=0;if(Val=0)/PWM输出(占空比为0%)PWM=0;DelayMS(0xff);continue;if(Val=0xff) /PWM输出(占空比为100%)PWM=1;DelayMS(0xff);con
18、tinue;PWM=1; /PWM输出(占空比为0%100%)DelayMS(Val);PWM=0;DelayMS(0xff-Val);/T0定时器中断给ADC0808提供时钟信号void Timer0_INT() interrupt 1CLK=CLK;20 ADC0809数模转换与显示/*名称:ADC0809数模转换与显示说明:ADC0809采样通道3输入的模拟量,转换后的结果显示在数码管上。*/#include#define uchar unsigned char#define uint unsigned int/各数字的数码管段码(共阴)uchar code DSY_CODE=0x3f,
19、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)uchar i;while(ms-) for(i=0;i120;i+);/显示转换结果void Display_Result(uchar d)P2=0xf7;/第4个数码管显示个位数P0=DSY_CODEd%10;DelayMS(5);P2=0xfb;/第3个数码管显示十位数P0=DSY_CODEd%100/10;D
20、elayMS(5);P2=0xfd;/第2个数码管显示百位数P0=DSY_CODEd/100;DelayMS(5);/主程序void main()TMOD=0x02;/T1工作模式2TH0=0x14;TL0=0x00;IE=0x82;TR0=1;P1=0x3f;/选择ADC0809的通道3(0111)(P1.4P1.6)while(1)ST=0;ST=1;ST=0;/启动A/D转换while(EOC=0);/等待转换完成OE=1;Display_Result(P3);OE=0;/T0定时器中断给ADC0808提供时钟信号void Timer0_INT() interrupt 1CLK=CLK;
21、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;i120;i+);/主程序void main()while(1)LED=LED;DelayMS(150);02 从左到右的流水灯/* 名称:从左到右的流水灯说明:接在P0口的8个LED从左到右循环依次点亮,产生走马灯效果*/#include#include#define uchar
22、 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_和_cror_形成来回滚动的效果*/#include#include#define uchar unsigned char#define uint unsigned
23、 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(150);04 花样流水灯/* 名称:花样流水灯说明:16只LED分两组按预设的多种花样变换显示*/#include#define uchar unsigned char#define ui
24、nt 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, 0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f, 0x7f,0x3f,0x1f,0x8f
25、,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,0xef,0xf7,0xfb,0xfd,0xfe, 0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0
26、x00, 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,0xf3,0xe7,0xcf,0x9f,0x3f,0xff, 0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,
27、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,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f, 0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0
28、xff,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, 0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff;/延时void DelayMS(uint x)uchar i;while(x-)for(i=0;i120;i+);/主程序vo
29、id main()uchar i;while(1)/从数组中读取数据送至P0和P2口显示for(i=0;i136;i+)P0=Pattern_P0i;P2=Pattern_P2i;DelayMS(100);05 LED模拟交通灯/*名称:LED模拟交通灯说明:东西向绿灯亮若干秒,黄灯闪烁5次后红灯亮, 红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复。*/#include#define uchar unsigned char#define uint unsigned intsbit RED_A=P00; /东西向灯sbit YELLOW_A=P01;s
30、bit 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;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;GREE
31、N_B=1;DelayMS(2000);Operation_Type=2;break;case 2:/东西向黄灯闪烁,绿灯关闭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:/南北
32、向黄灯闪烁5次DelayMS(300);YELLOW_B=YELLOW_B;GREEN_B=1;if(+Flash_Count!=10) return;Flash_Count=0;Operation_Type=1;/主程序void main()while(1) Traffic_Light();06 单只数码管循环显示09/*名称:单只数码管循环显示09说明:主程序中的循环语句反复将09的段码送至P0口,使数字09循环显示*/#include#include#define uchar unsigned char#define uint unsigned intuchar code DSY_COD
33、E=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff;/延时void DelayMS(uint x)uchar t;while(x-) for(t=0;t120;t+);/主程序void main()uchar i=0;P0=0x00;while(1) /* for(;i11;i+) P0=DSY_CODEi; DelayMS(300); /注:另一方案 */P0=DSY_CODEi;i=(i+1)%10;DelayMS(300);07 8只数码管滚动显示单个数字/*名称:8只数码管滚动显示单个数字说明:数码管从左到右依次滚动显示07
34、,程序通过每次仅循环选通一只数码管*/#include#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)uchar t;while(x-) for(t=0;t120;t+);/主程序void main()uchar i,wei=0x80;while(1)for(i=0;i8;i+)P2=0xff; /关闭显示wei=_crol_(wei,1)
35、;P0=DSY_CODEi; /发送数字段码P2=wei; /发送位码DelayMS(300);08 8只数码管动态显示多个不同字符电路如上图/*名称:8只数码管动态显示多个不同字符说明:数码管动态扫描显示07。*/#include#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)uchar t;while(x-) for(t=0;t120;
36、t+);/主程序void main()uchar i,wei=0x80;while(1)for(i=0;i8;i+)P2=0xff;P0=DSY_CODEi; /发送段码wei=_crol_(wei,1);P2=wei; /发送位码DelayMS(2);09 8只数码管闪烁显示数字串电路如上图/*名称:8只数码管闪烁显示数字串说明:数码管闪烁显示由07构成的一串数字 本例用动态刷新法显示一串数字,在停止刷新时所有数字显示消失。*/#include#define uchar unsigned char#define uint unsigned int/段码表uchar code DSY_CODE
37、=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;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只数码管滚动显示数字串电路如上图/*名称:8只数码管滚动显示数字串说明:数码管向左滚动显示3个字符构成的数字串*/#include#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,
限制150内