《单片机实验程序(全)_1.docx》由会员分享,可在线阅读,更多相关《单片机实验程序(全)_1.docx(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、单片机实验程序(全)2基本输入输出实验蜂鸣器控制程序/*名称:基本输入输出I/O程序讲明:*/#include/包含头文件#defineuintunsignedint/宏定义#defineucharunsignedcharsbitbuzzer=P3A5;sbitKeyadd=P2A。;sbitKeydec=P2A1;sbitKeycom=P2A2;voidDelay(uintz)uintx;uchary;for(x=z;x0;x-)/延时for(y=250;y0;y-);voidmain(void)ucharvoice;voice=3;while(1)if(Keyadd=0)voice=1;/
2、按键被按下时为0if(Keydec=0)voice=2;if(Keycom=0)voice=3;if(voice=1)buzzer=buzzer;Delay(1);elseif(voice=2)buzzer=buzzer;Delay(20);elsebuzzer=1;3定时器中断实验/*名称:实验三作业讲明:*#include#defineuintunsignedint#defineucharunsignedcharucharcounter;ucharvoice;sbitbuzzer=P3A5;sbitKeyadd=P2A。;sbitKeydec=P2A1;sbitKeycom=P2A2;sb
3、itKeycan=P2A3;1400/定时时间us为单位(65536-(uint)(T0_TIME2*11.0592/12)%256)(65536-(uint)(T0_TIME2*11.0592/12)/256)*名称:主程序讲明:*voidmain(void)/-TMOD|=0x01;TH0=T0ReLoadH1;TL0=T0ReLoadL1;TR0=1;ET0=1;EA=1;/-counter=0;/包含头文件/宏定义/#defineT0_TIME1#defineT0ReLoadL1-/定时时间us为单位#defineT0ReLoadH1计算计数器初值254(65536-(uint)(T0
4、_TIME1*11.0592/12)%256)(65536-(uint)(T0_TIME1*11.0592/12)/256)/计算计数器初值/#defineT0_TIME2#defineT0ReLoadL2#defineT0ReLoadH2初始化TIMER0/定时器T0方式1/装载计数器初值/启动Timer0/Timer0中断使能/总开关使能大循环while(1)if(Keyadd=0)voice=1;if(Keydec=0)voice=2;if(Keycom=0)voice=3;/*名称:Timer0中断服务子程序功能:产生10ms定时中断*voidTimer0_Int(void)inter
5、rupt1if(voice=1)TH0=T0ReLoadH1;TL0=T0ReLoadL1;buzzer=buzzer;if(voice=2)TH0=T0ReLoadH2;TL0=T0ReLoadL2;buzzer=buzzer;if(voice=3)/按键被按下时为0/重装载计数器初值4LED数码管动态扫描显示实验#include#defineuintunsignedint#defineucharunsignedchar/-LED显示代码-/bit7bit6bit5bit4bit3bit2bit1bit0/dpGCDEFBAucharcodeseg10=0x3f,0x22,0x5b,0x73
6、,0x66,/0,1,2,3,40x75,0x7d,0x23,0x7f,0x77,;/5,6,7,8,9/第一行LED显示缓冲/第二行LED显示缓冲=P1A0;S3显示锁存74HC574脉冲=P1A1;S4显示锁存74HC574脉冲/定时时间us为单位(65536-(uint)(T0_TIME*11.0592/12)%256)(65536-(uint)(T0_TIME*11.0592/12)/256)*名称:数码管显示驱动子程序讲明:*voidDisplay(void)uchardatai,j,k,temp00;temp00=0x20;for(i=0;i=1;P1|=0x3C;/*名称:主程序
7、*ucharidatadis_buff_14;ucharidatadis_buff_24;ucharcounter;sbitU4_HC574_CPsbitU5_HC574_CP#defineT0_TIME10000#defineT0ReLoadL#defineT0ReLoadH/关所有扫描位/S3显示锁存74HC574脉冲上升沿/S4显示锁存74HC574脉冲上升沿/送扫描位/延时/扫描位移位/关所有扫描位TH0=T0ReLoadH;TL0=T0ReLoadL;counter+;/重装载计数器初值voidmain(void)uchardatabai,shi,ge;uintdataNum;uch
8、ardatatemp00;TMOD|=0x01;TH0=T0ReLoadH;TL0=T0ReLoadL;TR0=1;ET0=1;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_13=0;dis_buff_20=0;dis_buff_21=segbai;dis_buff_22=segshi;dis_buff_23=segge;if(counter=100)coun
9、ter=0;/定时器T0方式1/装载计数器初值/启动Timer0/Timer0中断使能/总开关使能Num-;if(Num=0)Num=999;Display();/调用LED数码管显示子程序voidTimer0_Int(void)interrupt15实时时钟与温度传感器实验只需main部分代码voidmainvoiducharHour,Min,Sec;/-初始化TIMER0TMOD|=0x01;/定时器T0方式1TH0=T0ReLoadH;/装载计数器初值TL0=T0ReLoadL;TR0=1;/启动Timer0ET0=1;/Timer0中断使能EA=1;/总开关使能counter=0;/-
10、大循环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_buff_10=segHour4;if(counter4;dis_buff_13=segMindis_buff_20=0;dis_buff_21=0;dis_buff_22=segSec4;dis_buff_23=segSecDisplay(
11、);674HC595串行输入实验#include#include#defineuintunsignedint#defineucharunsignedcharucharcounter;/-计算计数器初值-/ucharbdataHC595_buff2;sbitHC595RegCLK=P2A5;sbitHC595S_Data=卩2人6;sbitHC595ShiftCLK=P2A7;/交通灯控制1的位寻址变量/4hc595锁存时钟输出/74hc595串行数据输出/74hc595串行移位时钟输出#defineT0ReLoadL#defineT0ReLoadH(65536-(uint)(T0_TIME*1
12、1.0592/12)%256)(65536-(uint)(T0_TIME*11.0592/12)/256)#defineT0_TIME10000/定时时间us为单位/*名称:WR_74HC595讲明:向2片74HC595写入2字节数据(HC595_buff)*/voidWR_74HC595(void)uchari,j,k,temp00;HC595RegCLK=0;HC595ShiftCLK=0;/锁存线置“低/HC595.CLK=0for(i=0;i=1;/HC595.SDA/产生上升沿,移入数据/k右移一位HC595ShiftCLK=0;/HC595.CLK/use_nop_()function