单片机实验程序(全)(共13页).doc
《单片机实验程序(全)(共13页).doc》由会员分享,可在线阅读,更多相关《单片机实验程序(全)(共13页).doc(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上2基本输入输出实验(蜂鸣器控制程序)/*名称:基本输入输出(I/O)程序说 明:*/#include<STC12c5A32s2.h>/包含头文件#define uint unsigned int /宏定义#define uchar unsigned char sbit buzzer=P35;sbit Keyadd=P20;sbit Keydec=P21;sbit Keycom=P22; void Delay(uint z)uint x;uchar y;for(x=z;x>0;x-)/延时 for(y=250;y>0;y-);void main(
2、void) uchar voice; voice=3; while(1) if(Keyadd=0)/按键被按下时为0voice=1;if(Keydec=0)voice=2;if(Keycom=0)voice=3;if(voice=1)buzzer=buzzer; Delay(1);else if(voice=2)buzzer=buzzer; Delay(20);elsebuzzer=1; 3定时器中断实验/*名称:实验三作业说 明:*/#include<STC12c5A32s2.h>/包含头文件#define uint unsigned int /宏定义#define uchar
3、unsigned char uchar counter;uchar voice;sbit buzzer=P35;sbit Keyadd=P20;sbit Keydec=P21;sbit Keycom=P22;sbit Keycan=P23;/-计算计数器初值-/#define T0_TIME1254 /定时时间us为单位#define T0ReLoadL1 (65536-(uint)(T0_TIME1*11.0592/12) % 256)#define T0ReLoadH1 (65536-(uint)(T0_TIME1*11.0592/12) / 256) /-计算计数器初值-/#define
4、 T0_TIME21400 /定时时间us为单位#define T0ReLoadL2 (65536-(uint)(T0_TIME2*11.0592/12) % 256)#define T0ReLoadH2 (65536-(uint)(T0_TIME2 *11.0592/12) / 256)/*名称:主程序说明:*/void main(void)/-初始化TIMER0 TMOD|=0x01;/定时器T0方式1TH0 =T0ReLoadH1;/装载计数器初值 TL0 =T0ReLoadL1;TR0 =1;/启动Timer0ET0 =1;/Timer0中断使能EA =1;/总开关使能/-大循环cou
5、nter=0;while(1)if(Keyadd=0)/按键被按下时为0voice=1;if(Keydec=0)voice=2;if(Keycom=0)voice=3;/*名称:Timer0中断服务子程序功能:产生10ms定时中断*/void Timer0_Int(void) interrupt1if(voice=1) TH0=T0ReLoadH1;/重装载计数器初值 TL0=T0ReLoadL1; buzzer=buzzer;if(voice=2) TH0=T0ReLoadH2; TL0=T0ReLoadL2; buzzer=buzzer;if(voice=3) 4 LED数码管动态扫描显示
6、实验#include<STC12c5A32s2.h>#define uint unsignedint#define uchar unsignedchar/-LED显示代码-/ bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0/ dp G C D E F B Auchar code seg10=0x3f,0x22,0x5b,0x73,0x66,/0,1,2,3,40x75,0x7d,0x23,0x7f,0x77,;/5,6,7,8,9ucharidatadis_buff_14;/第一行 LED显示缓冲ucharidatadis_buff_24;/第二行
7、LED显示缓冲ucharcounter;sbitU4_HC574_CP=P10;/S3 显示锁存74HC574脉冲sbitU5_HC574_CP=P11; /S4 显示锁存74HC574脉冲#define T0_TIME10000 /定时时间us为单位#define T0ReLoadL (65536-(uint)(T0_TIME*11.0592/12) % 256)#define T0ReLoadH (65536-(uint)(T0_TIME*11.0592/12) / 256)/*名称:数码管显示驱动子程序说明:*/void Display(void)uchardatai,j,k,temp0
8、0;temp00=0x20; for(i=0;i<4;i+)P1 |= 0x3C;/关所有扫描位P0=dis_buff_1i;U4_HC574_CP=0;U4_HC574_CP=1;/S3显示锁存74HC574脉冲 上升沿P0=dis_buff_2i;U5_HC574_CP=0;U5_HC574_CP=1;/S4显示锁存74HC574脉冲 上升沿P1 &= (temp00);/送扫描位for(j=0;j<20;j+)/延时 for(k=0;k<20;k+); temp00>>=1;/扫描位移位P1|=0x3C;/关所有扫描位/*名称:主程序*/void m
9、ain(void)uchardatabai,shi,ge;uintdataNum;uchardatatemp00;TMOD|=0x01;/定时器T0方式1TH0 =T0ReLoadH;/装载计数器初值 TL0 =T0ReLoadL;TR0 =1;/启动Timer0ET0 =1;/Timer0中断使能EA =1;/总开关使能counter=0;Num=999;for(;)bai=Num/100;temp00=Num-bai*100;shi=temp00/10;ge =temp00-shi*10;dis_buff_10=0;dis_buff_11=0;dis_buff_12=0;dis_buff_
10、13=0;dis_buff_20=0;dis_buff_21=segbai;dis_buff_22=segshi;dis_buff_23=segge;if(counter=100) counter=0;Num-;if(Num=0)Num=999;Display();/调用LED数码管显示子程序void Timer0_Int(void) interrupt1 TH0=T0ReLoadH;/重装载计数器初值 TL0=T0ReLoadL;counter+;5实时时钟与温度传感器实验(只需main()部分代码)void main(void)uchar Hour,Min,Sec;/-初始化TIMER0
11、TMOD|=0x01;/定时器T0方式1TH0 =T0ReLoadH;/装载计数器初值 TL0 =T0ReLoadL;TR0 =1;/启动Timer0ET0 =1;/Timer0中断使能EA =1;/总开关使能counter=0;/-大循环WR_DS1302(0x8e,0X00);WR_DS1302(0x84,0X14);WR_DS1302(0x82,0X43);WR_DS1302(0x80,0X00);WR_DS1302(0x8e,0X80);for(;) Hour=RD_DS1302(0x85); Min=RD_DS1302(0x83); Sec=RD_DS1302(0x81); dis_
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 单片机 实验 程序 13
限制150内