Home > other >  MCU using ultrasonic sensors
MCU using ultrasonic sensors

Time:11-01

Dear bosses, I recently playing with single chip microcomputer to do a ultrasonic ranging, has not been set, don't know where is wrong, the multimeter measuring ultrasonic module HC - SRO4 echo in the 200 m gear has voltage but microcontroller is not external interrupt, using external interrupt T1, input pin is P33, MCU chip is STC89C52RC, running the simulation program in open EX1=1 performed immediately after the next statement into the external interrupt function, don't know what problem, because only once, don't tube him, don't know if this problem, here is my program

# include//include the header file, generally do not need to change, the header file contains the definition of special function register # include & lt; Intrins. H> Unsigned char data dis_digit; # define uchar unsigned char//define a convenient use # define uint unsigned int# define ulong unsigned longvoid conversion (); Void isr_t0 (void);//the interrupt service function declarations void delay (z);//delay function declarations unsigned char counter, j.//used to define a variable counter count bit succeed_flag;//measuring success flag unsigned char ge, shi, bai, temp, flag, outcomeH, outcomeL, I;//custom register unsigned char ge_data, shi_data, bai_data, temp_data, x, y, z; Uint distance_data;/* sbit IN1=P3 ^ 1; Sbit IN2=P3 ^ 0; Sbit ENA=P3 ^ 2; */sbit IN3=p ^ 2; Sbit IN4 P1=^ 1; Sbit ENB P1=^ 0; Sbit VCC=P3 ^ 1; Sbit Trig=P3 ^ 2;//produce pulse pin sbit Echo=P3 ^ 3;//echo pin sbit GND=P3 ^ 4; Uchar code SEG7 [10]={xa4 xc0 0, 0 xf9, 0, 0 xb0, 0 x99, 0 x92, 0 x82, 0 xf8, 0 x80, 0 x90};//digital tube 0-9 sbit ENB=P3 ^ 2;//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * void main (void)//main function {uint distance_data; PX1=1; Counter=0; I=0; VCC=1; GND=0; Echo=0; Trig=0; IT1=1; TR0=0; TMOD=0 x11; XDC TH0=0; TL0=0 x00; EA=1;//the interrupt open ET0=1;//open the timer interrupt TR0=1; XFD dis_digit=0; While (1)//program loop {the if (succeed_flag==1) {distance_data=https://bbs.csdn.net/topics/outcomeH;//measurements of high eight distance_data <=8;//in a 16-bit high eight distance_data=distance_data | outcomeL;//a merger with low 8 become 16 result data distance_data *=12;//because the default timer for 12 points frequency distance_data/=58;//ms units divided by 58 equals centimeter conversion (); }//why divided by 58 equals cm, Y m=(X seconds * 344)/2//X seconds=(2) * Y m/344=="X seconds=0.0058 * Y m=="//* 58 cm=microseconds the if (succeed_flag==0) {distance_data=https://bbs.csdn.net/topics/0;//does not echo */reset}}}//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * void isr_t0 (void) interrupt 1//interrupt service function {TH0=0 XDC; TL0=0 x00; Counter++;/* 20 us a pulse, once every 1 s produce */while (counter==100) {TH1=0;//reset timer 1 TL1=0;//reset timer 1 Trig=1; Delay (3); Trig=0;//create a 20 us pulse, in Trig pin TR1=1;//start the timer 1 EX1=1;//open the external interrupt succeed_flag=0; Counter=0; }/* */P1=0 x00 digital tube display,//close all digital tube i++ first; {switch (I) in case 1: P0=bai; break; Case 2: P0=shi; break;//display code transmitted to P0 mouth case 3: P0=ge; break; } the if (I==3) I=0; P1=dis_digit;//dis_digit=_crol_ (dis_digit, 1);//a gate value shift to the left, the next time interrupt gate under a digital tube if dis_digit xef (dis_digit==0)=0 XFD; }//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//external interrupt 0, used to make a judgment echo level INTO_ () interrupt//external interrupt is {2, 2 outcomeH=TH1;//remove the value of the timer outcomeL=TL1;//remove the value of the timer succeed_flag=1;//to a sign of success measurement EX1=0;//off external interrupt}//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * void conversion () {temp_data=distance_data; Bai_data=temp_data/100; Temp_data=temp_data % 100;//take over operations shi_data=temp_data/10; Temp_data=temp_data % 10;//take over operations ge_data=temp_data; Bai_data=SEG7 [bai_data]; Shi_data=SEG7 [shi_data]; Ge_data=SEG7 [ge_data]; EA=0; Bai=bai_data; Shi=shi_data; Ge=ge_data; EA=1; }//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//* * * * * * * * * * * * * * * * * * * * * * * * * * void delay (z) {unsigned int x, y; For (x=z; X> 0; X -) for (y=10; Y> 0; y--); }//* * * * *

CodePudding user response:

Ultrasonic sensors of distance will output pulse of different interval, only need to statistics the pulse interval can calculate the distance, is not reached the level of trigger, do not have the right to return or interrupt

CodePudding user response:

My falling edge trigger mode is the trigger, should be as long as there is falling edge trigger, the Echo end of the ultrasonic module I have voltage measurement module should be no problem, so should be microcontroller is not into the external interrupt, I add a Echo to the program=1; Echo=0; Normally simulation execution after the two sentences should be into the external interrupt, but not go in, don't know if the interrupt is not defined well,
  • Related