|
我想用超声波测距显示,并且超过一定距离之后报警,触摸按键之后距离重新显示,并且蜂鸣器停止,恢复初态
但是现在超过距离之后,触摸可以重新显示距离,但是蜂鸣器关不了。。。哪里错了
#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);
}
}
|
|