Hc_Sr04_Part_2
short working video. . . .
https://youtu.be/5REL9pi0ZFs
Accordingly to datasheet making the trigger pin high at least 10 micro second shoots sound wave and the time taken is appeared on echo pin as a high pulse.So making 10 micro second on at trigger pin and waiting the raising edge of echo pin is the simplest thing.We need to turn on timer1 module after detecting rieing edge and turnning off after detecting falling edge. Getting the distance is reading TME1H and TMR1L value and doing some math.
//working ultrasonic code
//under test
//Input at RB0
//use timer1
#define Echo PORTB.F0
#define Trig PORTB.F1
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
float dist,time;
// End LCD module connections
char txt1[] = "Dist in cm";
char Space[]=" ";
unsigned char Num[15],Lcd[15],i,j,k;
int Pulse,Burst=1;
unsigned long int Begin,Beginl,Beginh;
void main()
{
OSCCON=0xff;
TRISB=0x01;
ADCON1 = 0x0F;
CMCON = 0x07; //need for portb config
T1CON.RD16=1;//16 bit mode
T1CON.T0CS=0; //system clock
T1CON.T1RUN=0;//clk derived from another osc
T1CON.T1CKPS0=0; // 1:1 ps
T1CON.T1CKPS1=0;
T1CON.T1OSCEN=0; //T1 OSC shutdown
T1CON.TMR1CS=0;//fosc/4
T1CON.TMR1ON=0;// stop TMR1
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt1);
while(1)
{
//T1CON.TMR0ON=1;
Begin=0;
Beginl=0;
Beginh=0;
// write TMR0l First
//TMR0H later
TMR1H=0;
TMR1L=0;
// delay_ms(60);
Trig=1;
delay_us(10);
Trig=0;
while(Echo==0);
T1CON.TMR1ON=1;
while(Echo==1);
T1CON.TMR1ON=0;
; //Read TMR0L first
//TMR0H later
Beginl=TMR1L;
Beginh=TMR1H<<8;
Begin=Beginl+Beginh;
time=0.5e-6*Begin; //no ps
dist=time*17000;
FloatToStr(dist,Num);
//IntToStr(Begin,Num);
j=0;
for(i=0;i<=3;i++) //j=15
{
if(Num[i]!=' ')
{
Lcd[j]=Num[i];
j++;
}
}
Lcd_Out(2,1,Lcd);
delay_ms(200);
Lcd_Out(2,1,Space);
}
}
https://youtu.be/5REL9pi0ZFs
Accordingly to datasheet making the trigger pin high at least 10 micro second shoots sound wave and the time taken is appeared on echo pin as a high pulse.So making 10 micro second on at trigger pin and waiting the raising edge of echo pin is the simplest thing.We need to turn on timer1 module after detecting rieing edge and turnning off after detecting falling edge. Getting the distance is reading TME1H and TMR1L value and doing some math.
//working ultrasonic code
//under test
//Input at RB0
//use timer1
#define Echo PORTB.F0
#define Trig PORTB.F1
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
float dist,time;
// End LCD module connections
char txt1[] = "Dist in cm";
char Space[]=" ";
unsigned char Num[15],Lcd[15],i,j,k;
int Pulse,Burst=1;
unsigned long int Begin,Beginl,Beginh;
void main()
{
OSCCON=0xff;
TRISB=0x01;
ADCON1 = 0x0F;
CMCON = 0x07; //need for portb config
T1CON.RD16=1;//16 bit mode
T1CON.T0CS=0; //system clock
T1CON.T1RUN=0;//clk derived from another osc
T1CON.T1CKPS0=0; // 1:1 ps
T1CON.T1CKPS1=0;
T1CON.T1OSCEN=0; //T1 OSC shutdown
T1CON.TMR1CS=0;//fosc/4
T1CON.TMR1ON=0;// stop TMR1
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,txt1);
while(1)
{
//T1CON.TMR0ON=1;
Begin=0;
Beginl=0;
Beginh=0;
// write TMR0l First
//TMR0H later
TMR1H=0;
TMR1L=0;
// delay_ms(60);
Trig=1;
delay_us(10);
Trig=0;
while(Echo==0);
T1CON.TMR1ON=1;
while(Echo==1);
T1CON.TMR1ON=0;
; //Read TMR0L first
//TMR0H later
Beginl=TMR1L;
Beginh=TMR1H<<8;
Begin=Beginl+Beginh;
time=0.5e-6*Begin; //no ps
dist=time*17000;
FloatToStr(dist,Num);
//IntToStr(Begin,Num);
j=0;
for(i=0;i<=3;i++) //j=15
{
if(Num[i]!=' ')
{
Lcd[j]=Num[i];
j++;
}
}
Lcd_Out(2,1,Lcd);
delay_ms(200);
Lcd_Out(2,1,Space);
}
}
Comments
Post a Comment