930电子网

 找回密码
 立即注册
艾克姆科技推出最新产品STC32G12K128开发板
查看: 6756|回复: 2

用超声波测距显示,并且超过一定距离之后报警,触摸按键之后距离重新显示,并且蜂...

[复制链接]

1

主题

1

帖子

15

积分

新手上路

Rank: 1

积分
15
发表于 2019-5-10 22:52:52 | 显示全部楼层 |阅读模式


我想用超声波测距显示,并且超过一定距离之后报警,触摸按键之后距离重新显示,并且蜂鸣器停止,恢复初态

但是现在超过距离之后,触摸可以重新显示距离,但是蜂鸣器关不了。。。哪里错了








#define MAIN_Fosc                11059200L       
#include        "config.h"
#include        "PWM.h"
#include        "delay.h"
#include        "lcd1602.h"




sbit ECHO=P4^6;      
sbit TRIG=P4^7;
sbit beep = P3^7;
sbit KEY=P2^5;        
sbit LED_R=P0^7;


#define Distance_MAX    700            
uint8 code Cls[] =      {"                "};
uint8 code ASCII[15] =  {'0','1','2','3','4','5','6','7','8','9','.','-','M'};
uint8 code table[] =  {"the distance is:"};
                          
uint16  time=0;                        
uint32  S=0;                             
bit     flag =0;                        
uint8   disbuff[4]           ={ 0,0,0,0,};     




void feng()
{
                                beep=1;
        delay_ms(200);


        
}
      








void Conut(void)
{
         time=TH0*256+TL0;               
         TH0=0;                             
         TL0=0;                             
       
         S=(time*1.7)/100;                 
         if((S>=Distance_MAX))     
         {         
          flag=0;                           
       
          DisplayOneChar(0, 1, ASCII[11]);
          DisplayOneChar(1, 1, ASCII[10]);       
          DisplayOneChar(2, 1, ASCII[11]);
          DisplayOneChar(3, 1, ASCII[11]);
          DisplayOneChar(4, 1, ASCII[12]);       
         }
         else
         {
          disbuff[0]=S%1000/100;      
          disbuff[1]=S%1000%100/10;
          disbuff[2]=S%1000%10 %10;
          DisplayOneChar(0, 1, ASCII[disbuff[0]]);
          DisplayOneChar(1, 1, ASCII[10]);       
          DisplayOneChar(2, 1, ASCII[disbuff[1]]);
          DisplayOneChar(3, 1, ASCII[disbuff[2]]);
          DisplayOneChar(4, 1, ASCII[12]);       
         }
}














void Timer0Init(void)
{
        AUXR &= 0x7F;                  
        TMOD &= 0xF8;                  
        TMOD &= 0xF7;                  
        TMOD |= 0x01;                              
        TL0 = 0;                             
        TH0 = 0;                               
        ET0 = 1;                        
}




void timer0_int (void) interrupt TIMER0_VECTOR
{
  flag=1;                                                          
}
       


void  StartModule(void)                         
{
        TRIG=1;                                               
        delay_ms(5);                     
        TRIG=0;                           
}








void        PWM_config(void)
{
        PWMx_InitDefine                PWMx_InitStructure;                                       


        PWMx_InitStructure.PWMx_IO_Select       = PWM2_P37;                 
        PWMx_InitStructure.Start_IO_Level       = 0;                       
        PWMx_InitStructure.PWMx_Interrupt       = DISABLE;                                         
        PWMx_InitStructure.FirstEdge_Interrupt  = DISABLE;               
        PWMx_InitStructure.SecondEdge_Interrupt = DISABLE;               
        PWMx_InitStructure.FirstEdge            = 64;                       
        PWMx_InitStructure.SecondEdge           = 192;                       
        PWMx_Configuration(PWM2_ID, &PWMx_InitStructure);               
        P3n_standard(1<<7);                                                                               


        TH2 = (u8)((65536UL - MAIN_Fosc / 500000UL) / 256);       
        TL2 = (u8)((65536UL - MAIN_Fosc / 500000UL) % 256);
        AUXR = 0x14;               
        PWM_SourceClk_Duty(PwmClk_T2, 256);       
       
        PWMCR |= ENPWM;               
        PWMCR &= ~ECBI;               
}








/
int main(void)
{


        P0M1 = 0;        P0M0 = 0;                      
        P2M1  &= 0xE5;        P2M0 &= 0xE5;            
        P4M1 &= 0x3F;        P4M0 &= 0x3F;            
        delay_ms(100);                                          
       
  lcd_init();                        
  DisplayListChar(0, 0, table);      


        delay_ms(100);                    
        DisplayListChar(0, 1, Cls);               
       
  Timer0Init();                     
        EA=1;                                                                      
       
        while(1)
        {
             StartModule();              
             while(!ECHO);                           
             TR0=1;                                         
             while(ECHO);                                   
             TR0=0;                                                
   if(KEY==0)
{   
        delay_ms(10);
        if(KEY==0)
{       
         Conut();               
         delay_ms(50);
        if(S>=100)
        {               
               
                delay_ms(10);
            if(S>=100)             
        {
                flag=0;
                PWM_config();
                feng();
                while(!KEY);
                feng();
                beep=0;
                Conut();
               
       
        }  
        }
}
        }
                   delay_ms(80);                           
        }
}






               

回复

使用道具 举报

101

主题

332

帖子

7422

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7422
发表于 2019-5-11 08:40:36 | 显示全部楼层

回帖奖励 +1 金钱

蜂鸣器是PWM驱动的吧,注意还要把PWM对应的通道关掉才可。
回复 支持 反对

使用道具 举报

101

主题

332

帖子

7422

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7422
发表于 2019-5-11 13:46:32 | 显示全部楼层
PWMCR &= ~ENPWM;  使用这个语句关掉蜂鸣器
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|官方淘宝店|930电子网 ( 皖ICP备16000695号-2 )

Copyright © 合肥艾克姆电子科技有限公司
快速回复 返回顶部 返回列表