Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[51] Interfacing HC-SR04 with a C51 CAN Extension Board

Status
Not open for further replies.

cluelessNigerian

Newbie level 3
Joined
Dec 28, 2016
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
38
I have a 8051 based CAN Demo board which can be found at http://datasheet.elcodis.com/pdf/13/1/130172/can-demoboard1.pdf . I've been trying to interface the popular HC-SR04 Sensors with the board for a while now and I've had no luck at all.

It also doesn't help that the interrupt pins on the 8051 are used by other components connected to the board. However, I can't get my the sensor to display the distance to an obstacle on the LCD screen.

I know my circuit works as I've tested with an arduino. I've probed my echo signal on a scope and the amplitude of my signal varies from 20mV to 600mV. Could it be that the amplitude of the signal is too small to be detected by the 8051?

Here's my code:

Code:
      #include <REG51CC01.h>                           
       #include <lcd.h>
      # include <intrins.h>
       sbit Echo = P2^7;
      sbit Trig = P2^2;

void delay_ms(unsigned int ms)
{
    unsigned int i, j;
    for( i =0; i < ms; i++)
       for( j = 0; j < 127; j++);
}

void start_pulse()
{

  Trig=1;
 _nop_();_nop_();_nop_();_nop_();_nop_();   //each _nop_() generates 1u sec of delay 
 _nop_();_nop_();_nop_();_nop_();_nop_();
 Trig=0;
}


void main()
{

    unsigned int Count,Time,Distance; 
    Lcd4_Init();
    Echo = 1;
    Trig = 0;
    TMOD |= 0x01;//Timer 0 in 16-bit mode
    do{
    while(Echo == 0);//Wait for Rising edge at Echo pin
                Lcd4_Write_String("In loop 1");
            Lcd4_Clear();
                TR0=1;//Start Timer
                TL0=TH0=0;//Clear timer count register
    while(Echo == 1)//Wait for Falling edge at Echo pin
    {
        if(TF0 == 1)//timer over if no obstacle is detected
            break;
        Lcd4_Write_String("In this loop");
        Lcd4_Clear();
  }
                TR0=0;//Stop Timer.
                TF0 = 0;//clear Timer Over Flow Flag
                Count = TL0 + TH0*256;//Calculate number of count
                Time = Count*1.085;//Calculate total time in uS.
                Distance = Time/58;

    delay_ms(2000);
  }while(1);
}

lcd.h
Code:
#include "REG51CC01.h"

    sbit EN = P3^4;
    sbit EN1= P3^2;
    sbit D4 = P1^0;
    sbit D5 = P1^1;
    sbit D6 = P1^2;
    sbit D7 = P1^3;
    sbit RW = P1^6;
    sbit RS = P1^7;


    void Lcd_delay(unsigned int a)
    {
        int j;
        int i;
        for(i=0;i<a;i++)
        {
            for(j=0;j<100;j++);
        }
    }

void Lcd4_Port(char a)
{
    if(a & 1)
        D4 = 1;
    else 
        D4 = 0;

    if(a & 2)
        D5 = 1;
    else
        D5 = 0;

    if(a & 4)
        D6 = 1;
    else
        D6 = 0;

    if(a & 8)
        D7 = 1;
    else
        D7 = 0;
}
void Lcd4_Cmd(unsigned char a)
{ 

    RS = 0;                             // => RS = 0
    RW = 0; 

    Lcd4_Port(a);
    EN  = 0;
    EN1 = 0;                            // => E = 1
    Lcd_delay(5);
  EN  = 1;
    EN1 = 1;                            // => E = 0

}

void Lcd4_Clear()
{
    Lcd4_Cmd(0);
    Lcd4_Cmd(1);
}

void Lcd4_Set_Cursor(char a, char b)
{
    char temp,z,y;
    if(a == 1)
    {
      temp = 0x80 + b;
        z = temp>>4;
        y = temp & 0x0F;
        Lcd4_Cmd(z);
        Lcd4_Cmd(y);
    }
    else if(a == 2)
    {
        temp = 0xC0 + b;
        z = temp>>4;
        y = temp & 0x0F;
        Lcd4_Cmd(z);
        Lcd4_Cmd(y);
    }
}

void Lcd4_Init()
{
    Lcd4_Port(0x00);
    Lcd_delay(200);
    ///////////// Reset process from datasheet /////////
  Lcd4_Cmd(0x03);
    Lcd_delay(50);
  Lcd4_Cmd(0x03);
    Lcd_delay(110);
  Lcd4_Cmd(0x03);
  /////////////////////////////////////////////////////
  Lcd4_Cmd(0x02);    
    Lcd4_Cmd(0x02);
  Lcd4_Cmd(0x08);   
    Lcd4_Cmd(0x00); 
    Lcd4_Cmd(0x0C);     
  Lcd4_Cmd(0x00);    
  Lcd4_Cmd(0x06);   
}

void Lcd4_Write_Char(char a)
{
   char temp,y;
   temp = a&0x0F; 
   y = a&0xF0;  
     RS = 1;                                             // => RS = 1
     RW = 0;
   Lcd4_Port(y>>4);             //Data transfer
     EN = 0;
     EN1= 0;
     Lcd_delay(5);
     EN = 1;
     EN1= 1;
     Lcd4_Port(temp);
     EN = 0;
     EN1= 0;    
     Lcd_delay(5);
     EN = 1;
     EN1= 1;
}


void Lcd4_Write_String(char *a)
{
    int i;
    for(i=0;a[i]!='\0';i++)
    Lcd4_Write_Char(a[i]);
}
 

Hi,
I didn't go through your code.
from 20mV to 600mV. Could it be that the amplitude of the signal is too small to be detected by the 8051?
It depends how you input it to the 8051.

600mV surely is too low for a valid logic high level. --> Read datasheet about valid logic levels. VIH, VIL.
But for sure it can be detected with a comparator or an ADC.

Klaus
 

Hi,
I didn't go through your code.

It depends how you input it to the 8051.

600mV surely is too low for a valid logic high level. --> Read datasheet about valid logic levels. VIH, VIL.
But for sure it can be detected with a comparator or an ADC.

Klaus

Hey Klaus, The trigger and echo pins are connected directly to Port 2.7 and Port 2.2 on the 8051. The issue with using an ADC is that I think the reflected signal on the echo pin will be too quick for an ADC. I've explored that area and the consensus is that it's pretty much impossible

Circuit.PNG
 

Hi,

Instead of relative textual descriptions you could use values and units.
If an ADC is suitable or could be verified with it's "sample rate".

Klaus
 

Hey, my apologies. Could you please elaborate? I'm not sure I follow..
 

Hi,

I think....will be too quick
..are no values one can calculate with, ...or can be compared with datasheet values..

Only you know what "too quick" means.

Klaus
 

From https://www.quora.com/Can-we-connec...f-yes-how-do-we-get-the-distance-measurements

But we can't use the ADC on arduino to anything good with the data we receive from its echo pin.
That's because the echo pin of HC-SR04 receives a pulse of very short duration,a pulse that has a value of 5v (or maybe less, but mostly greater than the threshold for 0 and 5v) . Also, even if the value returned was in some proportion with the distance from the object it is to detect,you never know when it will return!So you never know when to check the status of a pin for a value!
 

Part of the thread sounds confused. Any datasheet clarifies that HC-SR04 has digital TTL level in- and outputs. Using an ADC to interface it is just off-topic. If you see interface high levels below 2.4 V, there's something wrong with your circuit.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top