Embedded C programs
For traffic light control system :
Overview :
Red, Yellow, Green lights are connected to P1.0, P1.1, and P1.2
respectively. You can connect LEDs to these pins.
LED state-
0 for OFF, 1 for ON.
Red light stays ON for 5 seconds.
Yellow light stays ON for 2 seconds.
Green light stays ON for 5 seconds.
After Green, Yellow is ON again before transitioning back to Red.
Delay is calculate in milliseconds (1000 milliseconds =1 second)
#include <reg51.h> // Include 8051 register definitions
// Define port pins for the traffic lights
#define RED P1_0 // Red light connected to P1.0
#define YELLOW P1_1 // Yellow light connected to P1.1
#define GREEN P1_2 // Green light connected to P1.2
void delay_ms(unsigned int ms); // Function prototype for delay
void main(void) {
// Initialize all lights to OFF
RED = 0;
YELLOW = 0;
GREEN = 0;
while (1) { // Infinite loop
// Red light ON, Yellow and Green OFF
RED = 1;
YELLOW = 0;
GREEN = 0;
delay_ms(5000); // Red light ON for 5 seconds
// Yellow light ON, Red and Green OFF
RED = 0;
YELLOW = 1;
GREEN = 0;
delay_ms(2000); // Yellow light ON for 2 seconds
// Green light ON, Red and Yellow OFF
RED = 0;
YELLOW = 0;
GREEN = 1;
delay_ms(5000); // Green light ON for 5 seconds
// Yellow light ON again (transition before Red)
RED = 0;
YELLOW = 1;
GREEN = 0;
delay_ms(2000); // Yellow light ON for 2 seconds
}
}
// Function to create a delay in milliseconds
void delay_ms(unsigned int ms) {
unsigned int i, j;
for (i = 0; i < ms; i++) {
for (j = 0; j < 120; j++); // Approximate delay for 1 ms (for 12 MHz
clock)
}
}
For DC Motor Speed Control:
Overview:
The motor is connected to P1.0 and This pin will output the
PWM signal to control the motor's speed.
The pwm() function generates a PWM signal with a specific duty
cycle (from 0% to 100%).
pwm() function with different duty cycles:
50% duty cycle: Medium speed.
75% duty cycle: Faster speed.
25% duty cycle: Slower speed.
The delay_ms() function is used to create delays in the program.
Program:
#include <reg51.h> // Include 8051 register definitions
// Define motor control pins
#define MOTOR_PIN P1_0 // Connect the motor to P1.0
void delay_ms(unsigned int ms); // Function prototype for delay
void pwm(unsigned int duty_cycle); // PWM function to control motor
speed
void main(void) {
while (1) {
pwm(50); // 50% duty cycle (medium speed)
delay_ms(2000); // Wait for 2 seconds
pwm(75); // 75% duty cycle (faster speed)
delay_ms(2000); // Wait for 2 seconds
pwm(25); // 25% duty cycle (slower speed)
delay_ms(2000); // Wait for 2 seconds
}
}
// Function to generate PWM signal
void pwm(unsigned int duty_cycle) {
unsigned int i, j;
// Calculate the number of iterations for ON and OFF times based on
duty cycle
unsigned int on_time = duty_cycle * 255 / 100; // ON time based on
duty cycle
unsigned int off_time = 255 - on_time; // OFF time
// Generate PWM signal with specified duty cycle
for (i = 0; i < 255; i++) {
if (i < on_time) {
MOTOR_PIN = 1; // Motor ON
} else {
MOTOR_PIN = 0; // Motor OFF
}
for (j = 0; j < 10; j++); // Small delay to create the PWM frequency
}
}
// Function to create a delay in milliseconds
void delay_ms(unsigned int ms) {
unsigned int i, j;
for (i = 0; i < ms; i++) {
for (j = 0; j < 120; j++); // Approximate delay for 1 ms (for 12 MHz
clock)
}
}