cluelessNigerian
Newbie level 3
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:
lcd.h
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]);
}