Tushar_27
Newbie
I am trying to design simple toll system using 89s52. I am using hc-sro4 ultasonic sensorand sg90 servo motor and connction are done according to code
But whenever i am trying to test hardware it on hardware ultasonic sensor does not send trig signal as well as servo motor is not rotating. I have checked them whether they are working or not. But they working fine.
this is my code
[code tags added by moderator]
But whenever i am trying to test hardware it on hardware ultasonic sensor does not send trig signal as well as servo motor is not rotating. I have checked them whether they are working or not. But they working fine.
this is my code
Code:
#include <regx52.h> // Include 89S52 register definitions
#include <intrins.h>
#define trigPin P1_0 // Define trigPin as pin P1.0
#define echoPin P1_1 // Define echoPin as pin P1.1
#define servoPin P1_2 // Define servoPin as pin P1.2
sbit LED = P2^0; // Define LED pin as P2.0
unsigned long tmeduration;
int distance;
int servoAngle;
unsigned int pulseWidth;
int i;
void delay_us(unsigned int us) {
while (us--) {
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
void init() {
TMOD = 0x01; // Set timer0 as 16-bit timer
TH0 = 0x00; // Initialize timer0 high byte
TL0 = 0x00; // Initialize timer0 low byte
TR0 = 0; // Stop timer0
ET0 = 0; // Disable timer0 interrupt
}
void servoWrite(int angle) {
servoAngle = angle;
pulseWidth = (500 + (servoAngle * 10));
for(i=0;i<=50;i++)
{
servoPin = 1;
delay_us(pulseWidth);
servoPin = 0;
delay_us(20000 - pulseWidth);
}
}
void main() {
init();
while (1) {
LED = 0;
P3 &= ~(1 << 0); // Set trigPin low
delay_us(2);
P3 |= (1 << 0); // Set trigPin high
delay_us(10);
P3 &= ~(1 << 0); // Set trigPin low
while (!(P3 & (1 << 1))); // Wait for echoPin to go high
TH0 = 0; // Reset timer0 high byte
TL0 = 0; // Reset timer0 low byte
TR0 = 1; // Start timer0
while (P3 & (1 << 1)); // Wait for echoPin to go low
TR0 = 0; // Stop timer0
tmeduration = (TH0 << 8) | TL0; // Calculate pulse duration
distance = (0.034 * tmeduration) / 2; // Calculate distance in cm
if (distance <= 10) {
servoWrite(90);
LED = 1;
} else {
servoWrite(0);
LED = 0;
}
delay_us(50);
}
}
[code tags added by moderator]
Last edited by a moderator: