標(biāo)題: 這個單片機(jī)程序,為什么蜂鳴器不能正常響 求幫助 [打印本頁]

作者: zbyshen    時間: 2019-7-30 14:12
標(biāo)題: 這個單片機(jī)程序,為什么蜂鳴器不能正常響 求幫助
#include <reg52.h>

#define uint unsigned int
#define uchar unsigned char
//定義字符
typedef bit BOOL; //定義關(guān)鍵字
uchar i1=45,i2=45;
uchar boom=0;
sbit S1=P3^7;
sbit S2=P3^6;        
sbit io = P2^0;
sbit rs = P2^6;  
sbit rw = P2^5;
sbit ep = P2^7;
sbit beep=P1^5; //位定義
uchar data_byte;
uchar RH,RL,TH,TL;
/********延時模塊***********/
void delay(uchar ms)// 延時子程序
{   
uchar i;
while(ms--)
{
  for(i = 0;i<100;i++);  
}
}
void delay1()//延時8us
{
uchar i;
for(i=0;i<1;i++);
}
//延時10us
void delay2(uint i)
{
while(i--);
}
BOOL lcd_bz()//測試LCD忙碌狀態(tài)
{
BOOL result=0x80;
P0=0x80;
rs = 0;  
rw = 1;
ep = 1;
result = (BOOL)(P0 & 0x80);
ep = 0;  
return result;  
}
void write_cmd(uchar cmd)// 寫指令
{  
while(lcd_bz());
rs = 0;
rw = 0;
ep = 0;
P0 = cmd;
delay(1);
ep = 1;
delay(1);
ep = 0;
}
void write_addr(uchar addr)//寫地址
{
write_cmd(addr|0x80); //寫入第一行的首地址
}
void write_byte(uchar dat)//寫字節(jié)
{  
while(lcd_bz());
rs = 1;
rw=0;
ep = 0;
P0 = dat;
delay(1);         
ep = 1;
delay(1);
ep = 0;
}
void lcd_init()// lcd初始化
{
rw=0;

write_cmd(0x38);//設(shè)置16*2顯示
delay(1);
write_cmd(0x08);
delay(1);
write_cmd(0x01); //清零
delay(1);
write_cmd(0x06); //地址指針自加1
delay(1);  
write_cmd(0x0c);
delay(1);
}
void display(uchar addr, uchar q)//addr為地址數(shù)值,q為寫入數(shù)字
{
delay(10);
write_addr(addr);
write_byte(q);
delay(20);
}




//dht初始化
void start()//開始信號
{
io=1;
delay1();
io=0;
delay(25);//>18ms  
io=1;  //檢測開始信號
delay1();//20-40us
delay1(); //4*8
delay1();
delay1();

}
uchar receive_byte()//接收一個字節(jié)
{
uchar i,temp,count;
for(i=0;i<8;i++)
{
   count=2;  
  while((!io)&&count++)//等待50us低電平結(jié)束
  delay1();
  delay1();
  delay1();
  delay1();
   temp=0;//如果時間為26~28,數(shù)據(jù)為0
   if(io==1)
  temp=1;// 數(shù)據(jù)為1
  count=2;
  while((io)&&count++);
  if(count==1) break;
data_byte<<=1;
  data_byte|=temp;
}
return data_byte;
}

//接受數(shù)據(jù)//
void receive()
{
uchar T_H,T_L,R_H,R_L,check,num_check,i;
   uchar count;
start();//開始信號
io=1;
if(!io)//判斷從機(jī)的響應(yīng)信號,讀取DHT11響應(yīng)信號
{
  count=2;
  while((!io)&&count++);//DHT11高電平80us是否結(jié)束
  count=2;
  while((io)&&count++); //若響應(yīng)信號結(jié)束,進(jìn)入數(shù)據(jù)接收狀態(tài)
  R_H=receive_byte(); //濕度數(shù)據(jù)
  R_L=receive_byte();
  T_H=receive_byte();//溫度數(shù)據(jù)
  T_L=receive_byte();
  check=receive_byte();//校驗(yàn)位
  io=0;//數(shù)據(jù)接收完畢,將從機(jī)拉低延時50us
  for(i=0;i<7;i++)
  delay1();
  io=1;        //從機(jī)電平拉高,進(jìn)入空閑狀態(tài)
  num_check=R_H+R_L+T_H+T_L;
  if(num_check==check) //判斷數(shù)據(jù)與校驗(yàn)位是否相同
  {
   RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
check=num_check;
  }
}
}
//蜂鳴器報警程序
void sound()
{
   
  beep=~beep;
  delay2(10);
  delay(100);
display(0x0d,'o');
display(0x0e,'n');
}
void stop()
{
beep=1;
display(0x0d,'o');
display(0x0e,'f');
}
//主程序
void main()
{
lcd_init();//初始化lcd
delay(10);
while(1)
{
  receive();
  delay(100);
  display(0x00,'R');//第一行顯示
  display(0x01,':');
  display(0x02,RH/10+0x30);
   display(0x03,RH%10+0x30);//濕度整數(shù)顯示
  display(0x04,'%');
  display(0x40,'T');  //lcd第二行顯示
  display(0x41,':');                                 
  display(0x42,TH/10+0x30);
  display(0x43,TH%10+0x30);
display(0x44,0xdf);
  display(0x45,0x43);
  if(RH>40)
  {beep=~beep;
  display(0x0d,'o');
  display(0x0e,'n');}
  else
  beep=1;
}
}







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1