蜂鸣器的驱动程序---吴坚鸿

*爱你&永不变心* 提交于 2019-12-05 17:56:48

#include "REG52.H"
#define const_time_05s 222
#define const_time_1s 444
#define const_time_3s 1332
#define const_time_6s 2664
#define const_voice_short 40
#define const_voice_long 200
void initial_myself();
void initial_peripheral();
void delay_long(unsigned int uiDelaylong);
void led_flicker();
void alarm_run();
void T0_time();
sbit led_dr=P3^5;
sbit beep_dr=P1^5;
unsigned char ucLedStep=0;
unsigned int uiTimeLedCnt=0;
unsigned char ucAlarmStep=0;
unsigned int uiTimeAlarmCnt=0;
unsigned int uiVoiceCnt=0;
void main()
{
 initial_myself();
 delay_long(100);
 initial_peripheral();
 while(1)
 {
  led_flicker();
  alarm_run();
 }
}
void led_flicker()  //第三区  LED闪烁程序
{
 switch(ucLedStep)
 {
  case 0:
   if(uiTimeLedCnt>=const_time_05s)
   {
    uiTimeLedCnt=0;
    led_dr=0;
    ucLedStep=1;
   }
   break;
  case 1:
   if(uiTimeLedCnt>=const_time_05s)
   {
    uiTimeLedCnt=0;
    led_dr=1;
    ucLedStep=0;
   }  
   break; 
 }
}
void alarm_run()
{
 switch(ucAlarmStep)
 {
  case 0:
   if(uiTimeAlarmCnt>=const_time_3s)
   {
    uiTimeAlarmCnt=0;
    uiVoiceCnt=const_voice_short;
    ucAlarmStep=1;
   }
   break;
  case 1:
   if(uiTimeAlarmCnt>=const_time_6s)
   {
    uiTimeAlarmCnt=0;
    uiVoiceCnt=const_voice_long;
    ucAlarmStep=0;
   }
   break;
 }
}
void T0_time() interrupt 1
{
 TF0=0;
 TR0=0;
 if(uiTimeLedCnt<0xffff)
  uiTimeLedCnt++;
 if(uiTimeAlarmCnt<0xffff)
  uiTimeAlarmCnt++;
 
 if(uiVoiceCnt!=0)
 {
  uiVoiceCnt--;
  beep_dr=0;
 }
 else
 {
  ;
  beep_dr=1;
 }
 
 TH0=0xf8;
 TL0=0x2f;
 TR0=1;
}
void delay_long(unsigned int uiDelaylong)
{
 unsigned int i;
 unsigned int j;
 for(i=0;i<uiDelaylong;i++)
  for(j=0;j<500;j++)
   ;
}
void initial_myself()
{
 TMOD=0x01;
 TH0=0xf8;
 TL0=0x2f;
 led_dr=1;
 beep_dr=1;
}
void initial_peripheral()
{
 EA=1;
 ET0=1;
 TR0=1;
}
 
 
 
 
 
 
 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!