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.

I am facing problem while working with 89s52 kindly give me suggestion

Tushar_27

Newbie
Joined
May 14, 2023
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
13
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:
    #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:
If one echo pulse from HC-SR04 is missing, code execution will hang forever.

You want to create debug features in the code to determine which parts are executing at all.
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top