单片机其他外围设备的使用
蜂鸣器的使用:
蜂鸣器粗略设定频率,以20ms即50hz发声
#include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit fen = P2^6; void delay(uint z); main() { while(1) { delay(1);//延时10毫秒注意这里输出的是20毫秒的方波 //因为方波要有正负的跳变才能算一个周期,这里? //每隔10毫秒跳变一次,因此周期是200ms,频率是50Hz fen = ~fen; } } void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); }
蜂鸣器精确控制发声:使用定时器
#include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit fen = P2^6; uchar num,ben; void init(); void main() { init(); while(1); } void init() { TMOD = 0x11; TH1 = (65536-50000)/256; TL1 = (65536-50000)%256; ET1 = 1; EA = 1; TR1 =1; } void time1()interrupt 3 { TH1 = (65536-5000)/256;//定时一次是50毫秒 TL1 = (65536-5000)%256; num++; if(num==2) { num = 0; fen = ~fen; } }
蜂鸣器精确发声(控制其发出100ms,200ms,300ms,400ms,500ms 的频率
错误代码:
//这个程序调试了一上午吧,感觉有点不对劲,问题是出在 //定时器的打断上,即两个定时器都在工作的时候,会不会打断原来的定时器 //这个程序实习实现的功能本来是使喇叭发声,发出 //以100ms,200,300,400,500 //本来应该听到5个频率,但是实际只是听到3个,说明中断发生了错位,中断之间产生了相互的影响 #include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit fen = P2^6; uchar num,flag,tt; void init(); void main() { init(); while(1); } void init() { TMOD = 0x11; TH1 = (65536-50000)/256; TL1 = (65536-50000)%256; TH0 = (65536-50000)/256; TL0 = (65536-50000)%256; ET1 = 1; ET0 =1; EA = 1; TR0 =1; TR1 =1; } void time0()interrupt 1 { TH0 = (65536-50000)/256; TL0 = (65536-50000)%256; tt++; switch(tt/20) { case 0: flag = 1; break; case 1: flag = 2; break; case 2: flag = 3; break; case 3: flag = 4; break; case 4: tt=0; flag = 5; break; } } void time1()interrupt 3 { TH1 = (65536-5000)/256;//定时一次是50毫秒 TL1 = (65536-5000)%256; num++; if(num==flag) { num = 0; fen = ~fen; } }
正确代码
/* 同时用两个定时器控制蜂鸣器发声, 定时器0控制频率,定时器1控制同个 频率持续的时间,间隔2s依次输出 1,10,50,100,200,400,800, 1k(hz)的方波? 设晶振频率为12MHz。 */ #include<reg52.h> #include<intrins.h> #define uint unsigned int #define uchar unsigned char sbit beep = P2^3; uchar tt; uint fre,flag; void main() { fre = 50000; beep = 0; TMOD = 0x11; TH0 = (65536-fre)/256; TL0 = (65536-fre)%256; TH1 = (65536- 50000)/256; TL1 = (65536-50000)%256; EA = 1; ET0 = 1; ET1 =1; TR0 =1; TR1 = 1; while(1); } void timer0()interrupt 1 { TR0 = 0;//进入中断函数后关闭定时器0,以防止内部程序中断丢失 TH0 = (65536-50000)/256; TL0 = (65536-50000)%256; tt ++; if(flag<40) if(tt==10) { tt = 0; fre = 50000;//分别用fre来选取频率定时50ms,所以中断产生的频率为10hz beep = ~beep; } if(flag>=40&&flag<80) { tt = 0; fre = 50000;//分别用fre来选取频率定时50ms,所以中断产生的频率为10hz beep =~beep; } if(flag>=80&&flag<120) { tt = 0; fre = 10000;//定时10ms,周期为20ms,频率为50hz beep =~beep; } if(flag>=120&&flag<160) { tt = 0; fre = 5000;//定时5毫秒,周期为10ms,频率为100zh beep =~beep; } if(flag>=160&&flag<200) { tt = 0; fre = 2500;//定时2.5,周期5ms,频率为200 beep =~beep; } if(flag>=200&&flag<240) { tt = 0; fre = 1250;//定时1.25,周期2.5ms,频率400 beep =~beep; } if(flag>=240&&flag<280) { tt = 0; fre = 625;//定时2.5,周期5ms,频率为800 beep =~beep; } if(flag>=280&&flag<320) { tt = 0; fre = 312;//频率为1600 beep =~beep; } if(flag>=320&&flag<360) { tt = 0; fre = 156;//频率为3200 beep =~beep; } TR0 = 1; } void timer1()interrupt 3 { TH1 = (65536 - 50000)/256; TL1 = (65536 - 50000)%256; flag++; if(flag == 360) { flag = 0; fre = 50000; } }