22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Concept: I/O Ports and Bit Manipulation in Embedded C
I/O ports in microcontrollers enable communication with external devices like LEDs. Here,
we assume 8 LEDs are connected to Port 2 of an 8051 microcontroller, and our goal is to
alternately toggle even- and odd-numbered LEDs with a delay.
Real-Life Example:
Imagine a decorative lighting system where even-numbered bulbs turn on while odd-
numbered bulbs remain off, and then they switch states after a delay.
#include <reg51.h> // Include header file for 8051
void delay(); // Function prototype for delay
void main() {
while (1) {
P2 = 0xAA; // 10101010 - Turns ON even LEDs, OFF odd LEDs
delay(); // Call delay function
P2 = 0x55; // 01010101 - Turns ON odd LEDs, OFF even LEDs
delay(); // Call delay function
22bec0956
Sheik hussain beevi.A
}
// Delay function to create visible LED toggling effect
void delay() {
unsigned int i, j;
for (i = 0; i < 300; i++) { // Outer loop
for (j = 0; j < 1000; j++); // Inner loop
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
1. P2 = 0xAA; - Turns ON even-numbered LEDs (10101010).
2. delay(); - Waits before toggling.
3. P2 = 0x55; - Turns ON odd-numbered LEDs (01010101).
4. while (1) - Ensures continuous toggling.
5. delay() - Creates a visible blinking effect using nested loops.
This program continuously toggles LEDs in an alternating pattern.
22bec0956
Sheik hussain beevi.A
Concept: Accessing Individual Port Pins in Embedded C
Microcontrollers allow bit-level operations on I/O ports. Here, we will demonstrate:
1. Reading a pin (P1.2) and sending its status to another pin (P2.5).
2. Toggling a pin (P2.2) continuously with a delay.
Real-Life Example:
• (i) A sensor (e.g., temperature or motion sensor) connected to P1.2 controls an
output device (e.g., LED or buzzer) at P2.5.
• (ii) A blinking indicator (e.g., warning light) toggles on and off continuously.
Algorithm:
(i) Read P1.2 and send to P2.5
1. Configure P1.2 as an input pin.
2. Configure P2.5 as an output pin.
3. Continuously read P1.2 and set P2.5 accordingly.
(ii) Toggle P2.2 with delay
1. Configure P2.2 as an output pin.
2. Toggle P2.2 (change 1 to 0 and vice versa).
3. Add a delay for visibility.
4. Repeat indefinitely.
22bec0956
Sheik hussain beevi.A
Program (Embedded C - Keil)
#include <reg51.h> // Include header file for 8051
sbit input_pin = P1^2; // Define P1.2 as input
sbit output_pin = P2^5; // Define P2.5 as output
sbit toggle_pin = P2^2; // Define P2.2 as output
void delay(); // Function prototype for delay
void main() {
while (1) {
// (i) Read P1.2 and send to P2.5
output_pin = input_pin; // Copy input pin state to output pin
// (ii) Toggle P2.2 with delay
toggle_pin = ~toggle_pin; // Invert P2.2 state
delay(); // Call delay function
}
}
// Delay function to slow down toggling
void delay() {
unsigned int i, j;
for (i = 0; i < 300; i++) {
for (j = 0; j < 1000; j++);
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
1. Reading and Writing:
a. output_pin = input_pin; reads P1.2 and sends it to P2.5.
2. Toggling:
a. toggle_pin = ~toggle_pin; flips the state of P2.2 (1 → 0 or 0 → 1).
3. Delay Function:
a. Creates a visible blinking effect for P2.2.
This program runs continuously, updating P2.5 based on P1.2 and toggling P2.2 at regular
intervals.
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Question (2): Module 3: Programs on Timer/Counter Question (2)(a): Write a C program
using interrupts to perform two different tasks simultaneously. (i) Count the number of 1
Hz pulses applied to Timer input pin T1 and display it on P2 (LSByte of count) and P0
(MSbyte of count) (ii) Generate a square wave of 2 KHz on P1.0 with odd decimal multiplier
(i.e.10. 30 , 50 … etc ) count value or generate a square wave of 5 KHz on P1.0 with even
decimal multiplier (i.e.00. 20 , 40 … etc. ) count value.
Concept: Using Timers and Interrupts in Embedded C
Timers and counters in microcontrollers help in event counting and waveform generation.
Here, we use interrupts to:
1. Count 1 Hz pulses on T1 and display the count on P2 (LSB) and P0 (MSB).
2. Generate a 2 kHz or 5 kHz square wave on P1.0 based on the count value.
Real-Life Example:
• Event Counting: Counting customer entries at a store using a sensor.
• Wave Generation: Producing frequency signals for tone generation in buzzers.
Algorithm:
(i) Counting Pulses on T1:
1. Configure Timer 1 in counter mode.
2. Increment count whenever a 1 Hz pulse is detected.
3. Store the lower byte in P2 and higher byte in P0.
(ii) Generating Square Wave on P1.0:
1. Configure Timer 0 for square wave generation.
2. Use odd count values for 2 kHz and even count values for 5 kHz.
3. Toggle P1.0 on each Timer 0 overflow.
22bec0956
Sheik hussain beevi.A
Program (Embedded C - Keil)
#include <reg51.h>
sbit square_wave = P1^0; // Square wave output pin
unsigned int count = 0; // 16-bit count variable
void Timer0_ISR() interrupt 1 {
// Toggle square wave on P1.0 based on count value
if (count % 2 == 0) {
// Even count → Generate 5 kHz wave (delay for 100 µs)
TH0 = 0xFC; // Reload Timer0 for 5 kHz
TL0 = 0x66;
} else {
// Odd count → Generate 2 kHz wave (delay for 250 µs)
TH0 = 0xF8; // Reload Timer0 for 2 kHz
TL0 = 0xCC;
}
square_wave = ~square_wave; // Toggle P1.0
}
void Timer1_ISR() interrupt 3 {
count++; // Increment count on every 1 Hz pulse
P2 = count & 0xFF; // Lower byte to P2
P0 = (count >> 8) & 0xFF; // Upper byte to P0
}
void main() {
// Timer 0 Configuration (Square Wave)
TMOD = 0x12; // Timer0: Mode 2 (Auto-reload), Timer1: Mode 1 (16-
bit)
TH0 = 0xFC; // Load Timer0 for 5 kHz initially
TL0 = 0x66;
// Timer 1 Configuration (Pulse Counting)
TH1 = 0x00;
TL1 = 0x00;
22bec0956
Sheik hussain beevi.A
ET0 = 1; // Enable Timer0 Interrupt
ET1 = 1; // Enable Timer1 Interrupt
EA = 1; // Enable global interrupts
TR0 = 1; // Start Timer 0 (square wave)
TR1 = 1; // Start Timer 1 (counting pulses)
while (1); // Infinite loop
}
22bec0956
Sheik hussain beevi.A
Explanation:
1. Timer 1 ISR (Interrupt Service Routine) (1 Hz Pulse Counting):
a. Increments count when a 1 Hz pulse is received.
b. Stores lower byte in P2 and higher byte in P0.
2. Timer 0 ISR (Square Wave Generation):
a. If count is even: Generates 5 kHz wave (100 µs delay).
b. If count is odd: Generates 2 kHz wave (250 µs delay).
3. Main Function:
a. Configures Timer 0 in mode 2 (Auto-reload) for square wave.
b. Configures Timer 1 in mode 1 (16-bit counting).
c. Enables interrupts and starts timers.
22bec0956
Sheik hussain beevi.A
This program runs two tasks simultaneously using interrupts.
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
concept: Square Wave Generation Using Timers
Microcontrollers use timers to generate precise square waves by toggling output pins at
regular intervals.
• We generate 500 Hz on P1.0 and 10 kHz on P2.0 using Timer 0 and Timer 1.
• With a 12 MHz crystal, each machine cycle = 1 µs (12 MHz ÷ 12 = 1 MHz).
Real-Life Example:
• 500 Hz → Used in LED blinking applications.
• 10 kHz → Used for pulse width modulation (PWM) and sound generation.
Algorithm:
1. Configure Timer 0 to generate a 500 Hz wave on P1.0.
a. Time period = 1/500 sec = 2 ms → Half-cycle = 1 ms.
b. Timer delay = 1 ms = 1000 machine cycles.
c. Reload value = 65536 - 1000 = 64536 (0xFC18).
2. Configure Timer 1 to generate a 10 kHz wave on P2.0.
a. Time period = 1/10000 sec = 0.1 ms → Half-cycle = 50 µs.
b. Timer delay = 50 µs = 50 machine cycles.
c. Reload value = 65536 - 50 = 65486 (0xFFCE).
3. Use Timer Interrupts to toggle the pins (P1.0 and P2.0).
22bec0956
Sheik hussain beevi.A
Program (Embedded C – Keil)
#include <reg51.h>
sbit wave_500Hz = P1^0; // Define P1.0 for 500 Hz sbit wave_10kHz = P2^0; // Define P2.0
for 10 kHz
void Timer0_ISR() interrupt 1 {
TH0 = 0xFC; // Reload for 500 Hz (1 ms delay) TL0 = 0x18; wave_500Hz = ~wave_500Hz; //
Toggle P1.0 }
void Timer1_ISR() interrupt 3 {
TH1 = 0xFF; // Reload for 10 kHz (50 µs delay) TL1 = 0xCE; wave_10kHz = ~wave_10kHz; //
Toggle P2.0 }
void main() { // Timer 0 for 500 Hz TMOD = 0x11; // Timer 0 & Timer 1 in Mode 1 (16-bit
mode) TH0 = 0xFC; // Load Timer 0 for 500 Hz TL0 = 0x18;
// Timer 1 for 10 kHz
TH1 = 0xFF; // Load Timer 1 for 10 kHz
TL1 = 0xCE;
ET0 = 1; // Enable Timer 0 Interrupt
ET1 = 1; // Enable Timer 1 Interrupt
EA = 1; // Enable global interrupts
TR0 = 1; // Start Timer 0
TR1 = 1; // Start Timer 1
while (1); // Infinite loop
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
1. Timer 0 ISR (Interrupt Service Routine) for 500 Hz:
a. Generates a 1 ms delay and toggles P1.0.
2. Timer 1 ISR for 10 kHz:
a. Generates a 50 µs delay and toggles P2.0.
3. Main Function:
a. Timer 0 runs at 500 Hz, toggling P1.0.
b. Timer 1 runs at 10 kHz, toggling P2.0.
c. Interrupts handle pin toggling automatically.
This program generates accurate square waves at 500 Hz on P1.0 and 10 kHz on P2.0.
22bec0956
Sheik hussain beevi.A
3)(a): Write a program for the 8051 to transfer letter “A” serially at 4800
baud, continuously.
Concept: Serial Communication Using UART (8051)
The 8051 microcontroller has a Universal Asynchronous Receiver/Transmitter (UART)
for serial communication. To transmit 'A' continuously at 4800 baud, we configure Timer
1 to set the baud rate and use the serial buffer register (SBUF) to send data.
Real-Life Example:
• Serial communication is used in RS232-based devices, wireless modules
(Bluetooth, WiFi), and debugging via serial terminals.
Algorithm:
1. Set the baud rate to 4800 using Timer 1 in Mode 2 (Auto-reload).
2. Enable Serial Communication (Mode 1, 8-bit UART).
3. Send 'A' continuously using SBUF register.
4. Monitor the transmit flag (TI) and reset it before sending the next character.
Baud Rate Calculation:
For a 12 MHz crystal, baud rate formula:
Solving for TH1:
TH1=253=0xFDTH1 = 253 = 0xFDTH1=253=0xFD
Question:
Write an 8051 Embedded C program to transmit the letter 'A' serially at 4800 baud
continuously.
22bec0956
Sheik hussain beevi.A
Program (Embedded C - Keil)
#include <reg51.h>
void timer0_delay() {
TMOD |= 0x01; // Timer0 Mode 1 (16-bit)
TL0 = 0x66; // Load lower byte for delay (~50ms)
TH0 = 0xFC; // Load upper byte
TR0 = 1; // Start Timer0
while (TF0 == 0); // Wait for Timer0 overflow
TR0 = 0; // Stop Timer
TF0 = 0; // Clear Timer overflow flag
}
void main() {
TMOD = 0x20; // Timer1 Mode 2 (8-bit auto-reload)
TH1 = 0xE9; // Load value for 4800 baud (for 11.0592 MHz
crystal)
SCON = 0x50; // Serial Mode 1 (8-bit UART), Enable Receiver
TR1 = 1; // Start Timer 1
while (1) {
SBUF = 'A'; // Load 'A' into Serial Buffer
while (TI == 0); // Wait for transmission to complete
TI = 0; // Clear TI flag for next transmission
timer0_delay(); // Call timer-based delay
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
1. Baud Rate Setup:
a. Timer 1 in Mode 2 (Auto-reload).
b. Load TH1 = 0xFD to set 4800 baud.
2. Serial Communication Setup:
a. SCON = 0x50: 8-bit UART, Enable Transmission.
b. TR1 = 1: Start Timer 1 for baud rate generation.
3. Continuous Transmission:
a. SBUF = 'A'; → Load 'A' into the serial buffer.
b. Wait until TI (Transmit Interrupt) flag is set.
c. Reset TI and repeat.
This program transmits 'A' indefinitely at 4800 baud via UART.
22bec0956
Sheik hussain beevi.A
Question (3)(b): Write a program to transfer
the message “YES” serially at 9600 baud,
8-bit data, 1 stop bit. Do this continuously.
Concept: Serial Communication in 8051
Serial communication allows data transfer between devices using the UART (Universal
Asynchronous Receiver-Transmitter) module.
Real-Life Example
Sending a text message from a microcontroller to a PC via a serial terminal (e.g.,
HyperTerminal, Keil Serial Window).
Algorithm
1. Configure Timer1 for 9600 baud rate.
2. Set 8-bit data, 1 stop bit in serial mode (SM0=0, SM1=1).
3. Enable serial transmission (TI flag).
4. Send the characters 'Y', 'E', 'S' continuously.
Write an Embedded C program for 8051 to transmit the string "YES" serially at 9600 baud,
with 8-bit data and 1 stop bit, continuously.
22bec0956
Sheik hussain beevi.A
Program (Embedded C - Keil)
#include <reg51.h> // 8051 register definitions
void serial_init(); // Function to initialize serial communication
void serial_send(char ch); // Function to send a single character
void main() {
serial_init(); // Initialize serial communication
while (1) {
serial_send('Y'); // Send 'Y'
serial_send('E'); // Send 'E'
serial_send('S'); // Send 'S'
}
}
// Function to initialize serial communication at 9600 baud
void serial_init() {
TMOD = 0x20; // Timer1 in Mode 2 (8-bit auto-reload)
TH1 = 0xFD; // Load value for 9600 baud
SCON = 0x50; // Serial mode 1, 8-bit data, 1 stop bit
TR1 = 1; // Start Timer1
}
// Function to send a character serially
void serial_send(char ch) {
SBUF = ch; // Load character into serial buffer
while (TI == 0); // Wait for transmission to complete
TI = 0; // Clear transmit interrupt flag
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation
• Baud Rate Setting: TH1 = 0xFD; sets 9600 baud with 11.0592 MHz crystal.
• Character Transmission: "YES" is sent continuously.
• Polling Method: Waits for TI flag to ensure character is sent.
22bec0956
Sheik hussain beevi.A
Program the 8051 to receive bytes of data
serially, and put them in P1. Set the baud
rate at 4800, 8-bit data, and 1 stop bit.
Concept: Serial Data Reception in 8051
Serial communication allows the 8051 microcontroller to receive data byte by byte via its
UART (Universal Asynchronous Receiver-Transmitter) module. The received data is then
stored in Port 1 (P1) for further processing.
Real-Life Example
Receiving data from a PC terminal (e.g., Keil Serial Window or HyperTerminal) and
displaying it on LEDs connected to Port 1.
Algorithm
1. Configure Timer1 to generate a 4800 baud rate.
2. Set serial mode to 8-bit data, 1 stop bit (SM0=0, SM1=1).
3. Enable serial reception (REN=1).
4. Continuously check for received data (RI flag).
5. When data is received, store it in P1.
6. Clear RI flag and repeat the process.
Question
Write an Embedded C program for 8051 to receive bytes of data serially at 4800 baud,
with 8-bit data and 1 stop bit, and transfer them to Port 1 (P1).
22bec0956
Sheik hussain beevi.A
Program (Embedded C - Keil)
c
CopyEdit
#include <reg51.h> // 8051 register definitions
void serial_init(); // Function to initialize serial communication
void main() {
serial_init(); // Initialize serial communication
while (1) {
while (RI == 0); // Wait until a byte is received
P1 = SBUF; // Store received byte into Port 1
RI = 0; // Clear receive interrupt flag
}
}
// Function to initialize serial communication at 4800 baud
void serial_init() {
TMOD = 0x20; // Timer1 in Mode 2 (8-bit auto-reload)
TH1 = 0xFA; // Load value for 4800 baud (with 11.0592 MHz
crystal)
SCON = 0x50; // Serial mode 1, 8-bit data, 1 stop bit
TR1 = 1; // Start Timer1
REN = 1; // Enable reception
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation
• Baud Rate Setting: TH1 = 0xFA; sets 4800 baud with 11.0592 MHz crystal.
• Data Reception: Checks RI flag continuously.
• Storing Data in P1: Once received, the byte is stored in P1 (P1 = SBUF;).
• Clearing RI: Resets the receive interrupt flag (RI = 0;).
22bec0956
Sheik hussain beevi.A
Question (3)(d): Assume that the 8051 serial port is connected to the COM port of the
IBM PC, and on the PC we are using the HyperTerminal program to send and receive
data serially. P1 and P2 of the 8051 are connected to LEDs and switches, respectively.
Write an 8051 program to (a) send to the PC the message “We Are Ready”, (b) receive
any data sent by the PC and put it on LEDs connected to P1, and (c) get data on
switches connected to P2 and send it to the PC serially. The program should perform
part (a) once, but parts (b) and (c) continuously. Use the 4800 baud rate
Concept: Serial Communication Between 8051 and PC using
HyperTerminal
Microcontrollers communicate with a PC using serial communication (UART), where
data is transmitted and received through the serial port (COM port). The 8051's serial port
(SBUF) allows bidirectional communication with the PC’s HyperTerminal.
Real-Life Example
• A microcontroller-based control system where the 8051 receives commands
from a PC and controls devices (LEDs and switches).
• Similar to home automation, where a PC sends commands to control lights
(LEDs), and the microcontroller sends back sensor values.
Algorithm
Step 1: Initialize Serial Communication (4800 Baud Rate)
• Configure the serial port using Timer 1 for baud rate generation.
• Set the mode to 8-bit UART with 1 stop bit.
22bec0956
Sheik hussain beevi.A
Step 2: Send the message "We Are Ready" to the PC
• Transmit each character serially.
Step 3: Continuously Read Data from the PC and Display on LEDs (P1)
• Wait for incoming data from the PC.
• Store the received data into Port 1 (P1), which is connected to LEDs.
Step 4: Continuously Read Data from Switches (P2) and Send to PC
• Read the status of switches (P2).
• Transmit this status back to the PC serially
22bec0956
Sheik hussain beevi.A
Embedded C Program (Keil - 8051)
#include <reg51.h>
void Serial_Init(); // Function to initialize serial
communication
void Serial_Transmit(char); // Function to send data serially
char Serial_Receive(); // Function to receive data serially
void main() {
unsigned char receivedData;
Serial_Init(); // Initialize serial communication
// Send the message "We Are Ready" to PC once
char message[] = "We Are Ready";
for(int i = 0; message[i] != '\0'; i++) {
Serial_Transmit(message[i]);
}
while(1) {
// (b) Receive data from PC and put it on LEDs at P1
receivedData = Serial_Receive();
P1 = receivedData; // Display received data on LEDs (P1)
// (c) Read switches at P2 and send their status to PC
Serial_Transmit(P2);
}
}
// Initialize Serial Communication at 4800 Baud Rate
void Serial_Init() {
TMOD = 0x20; // Timer1 Mode 2 (8-bit auto-reload)
TH1 = 0xF3; // Baud rate 4800 for 11.0592 MHz
SCON = 0x50; // 8-bit UART mode
TR1 = 1; // Start Timer1
}
// Function to Send Data Serially
22bec0956
Sheik hussain beevi.A
void Serial_Transmit(char data) {
SBUF = data; // Load data into Serial Buffer
while(TI == 0); // Wait until transmission is complete
TI = 0; // Clear transmit interrupt flag
}
// Function to Receive Data Serially
char Serial_Receive() {
while(RI == 0); // Wait for reception to complete
RI = 0; // Clear receive interrupt flag
return SBUF; // Return received data
}
Explanation
1. Initialize Serial Communication at 4800 baud (Timer 1 in mode 2).
2. Send "We Are Ready" to the PC once when the program starts.
3. Continuously read data from the PC and display on LEDs (P1).
4. Continuously read switch inputs (P2) and send their status to the PC.
22bec0956
Sheik hussain beevi.A
Question (3)(e): Assume a switch is connected to pin P1.7. Write a program to monitor
its status and send two messages to serial port continuously as follows: SW=0 send
“NO” SW=1 send “YES” Assume XTAL = 11.0592 MHz, 9600 baud, 8-bit data, and 1 stop
bit
Concept
The 8051 microcontroller will monitor P1.7 for switch status and send "YES" if the switch is
pressed (1) and "NO" if it is not pressed (0) via serial communication at 9600 baud, 8-bit
data, and 1 stop bit.
Algorithm
1. Initialize the serial communication with 9600 baud rate.
2. Configure P1.7 as an input to read the switch status.
3. Check the switch state continuously:
a. If P1.7 = 1, transmit "YES".
b. If P1.7 = 0, transmit "NO".
4. Repeat continuously.
Embedded C Program (Keil)
#include <reg51.h>
sbit switch_pin = P1^7; // Define switch at P1.7
void Serial_Init(); // Function to initialize serial
22bec0956
Sheik hussain beevi.A
communication
void Serial_Transmit(char); // Function to send a character
void Send_String(char *); // Function to send a string
void main() {
Serial_Init(); // Initialize UART
while (1) {
if (switch_pin == 1) {
Send_String("YES\r\n"); // Send "YES" if switch is pressed
} else {
Send_String("NO\r\n"); // Send "NO" if switch is not
pressed
}
}
}
// Function to initialize serial communication
void Serial_Init() {
TMOD = 0x20; // Timer1 Mode 2 (8-bit auto-reload) for baud rate
generation
TH1 = 0xFD; // Load value for 9600 baud at 11.0592 MHz
SCON = 0x50; // 8-bit data, 1 stop bit, REN enabled
TR1 = 1; // Start Timer1
}
// Function to transmit a single character
void Serial_Transmit(char c) {
SBUF = c; // Load character into buffer
while (TI == 0); // Wait for transmission to complete
TI = 0; // Clear transmit flag
}
// Function to send a string via UART
void Send_String(char *str) {
while (*str) {
Serial_Transmit(*str++); // Send each character
}
22bec0956
Sheik hussain beevi.A
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation
1. Serial Initialization (Serial_Init())
a. Timer1 is set to Mode 2 (Auto-reload).
b. TH1 = 0xFD sets 9600 baud rate for an 11.0592 MHz crystal.
c. SCON = 0x50 enables 8-bit UART mode with 1 stop bit.
2. Reading the Switch (P1.7)
a. If P1.7 = 1, send "YES".
b. If P1.7 = 0, send "NO".
3. Transmitting Data
a. The Serial_Transmit() function sends one character at a time.
b. The Send_String() function sends the full string.
Expected Output in HyperTerminal
• If P1.7 = 1 → "YES"
• If P1.7 = 0 → "NO"
This program continuously monitors P1.7 and sends the correct message
22bec0956
Sheik hussain beevi.A
Question (3)(f): Write a program to send the message “The Earth is but One Country”
to serial port. Assume a SW is connected to pin P1.2. Monitor its status and set the
baud rate as follows: SW = 0, 4800 baud rate. SW = 1, 9600 baud rate Assume XTAL =
11.0592 MHz, 8-bit data, and 1 stop bit.
Concept
The 8051 microcontroller will:
• Monitor P1.2 to check the switch state.
• Set the baud rate dynamically:
o If P1.2 = 0, use 4800 baud.
o If P1.2 = 1, use 9600 baud.
• Send the message "The Earth is but One Country" via serial
communication.
Algorithm
1. Initialize serial communication with the baud rate depending on the switch status.
2. Monitor P1.2 continuously:
a. If P1.2 = 0, set baud 4800.
b. If P1.2 = 1, set baud 9600.
3. Send the message via UART.
4. Repeat continuously.
Embedded C Program (Keil)
#include <reg51.h>
sbit switch_pin = P1^2; // Define switch at P1.2
22bec0956
Sheik hussain beevi.A
void Serial_Init(unsigned int baud); // Function to initialize serial
communication
void Serial_Transmit(char c); // Function to send a character
void Send_String(char *str); // Function to send a string
void main() {
while (1) {
if (switch_pin == 1) {
Serial_Init(9600); // Set baud rate to 9600 if switch is
ON
} else {
Serial_Init(4800); // Set baud rate to 4800 if switch is
OFF
}
Send_String("The Earth is but One Country\r\n"); // Send
message
}
}
// Function to initialize serial communication based on baud rate
void Serial_Init(unsigned int baud) {
TMOD = 0x20; // Timer1 Mode 2 (8-bit auto-reload)
if (baud == 9600) {
TH1 = 0xFD; // Load value for 9600 baud
} else {
TH1 = 0xFA; // Load value for 4800 baud
}
SCON = 0x50; // 8-bit data, 1 stop bit, REN enabled
TR1 = 1; // Start Timer1
}
// Function to transmit a single character
void Serial_Transmit(char c) {
SBUF = c; // Load character into buffer
while (TI == 0); // Wait for transmission to complete
TI = 0; // Clear transmit flag
22bec0956
Sheik hussain beevi.A
}
// Function to send a string via UART
void Send_String(char *str) {
while (*str) {
Serial_Transmit(*str++); // Send each character
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation
1. Switch Monitoring (P1.2)
a. If P1.2 = 1, sets 9600 baud.
b. If P1.2 = 0, sets 4800 baud.
2. Dynamic Baud Rate Setting
a. TH1 = 0xFD for 9600 baud.
b. TH1 = 0xFA for 4800 baud.
3. Serial Communication Functions
a. Serial_Transmit() sends one character at a time.
b. Send_String() sends the full message.
Expected Output in HyperTerminal
• If P1.2 = 1 → "The Earth is but One Country" (9600 baud)
• If P1.2 = 0 → "The Earth is but One Country" (4800 baud)
This program dynamically adjusts the baud rate and transmits the message accordingly
Question (3)(g): Write a program for the second serial port of the DS89C4x0 to
continuously transfer the letter “A” serially at 4800 baud. Use 8-bit data and 1 stop bit.
Use Timer 1.
Concept
The DS89C4x0 microcontroller has two serial ports (UART0 and UART1).
• UART1 (Second Serial Port) → SCON1 Register
• Baud Rate Control → Timer 1
• Requirements:
o Transmit the character 'A' continuously.
o Baud Rate: 4800
o Data: 8-bit, Stop Bit: 1
22bec0956
Sheik hussain beevi.A
Algorithm
1. Configure Timer 1 for baud rate generation.
2. Set up Serial Port 1 (SCON1)
a. Mode 1 (8-bit UART).
b. Enable transmission.
3. Send character 'A' continuously.
4. Loop indefinitely.
Embedded C Program (Keil)
#include <reg51.h>
void Serial1_Init(); // Initialize Serial Port 1
void Serial1_Transmit(char c); // Transmit character via Serial 1
void main() {
Serial1_Init(); // Initialize UART1
while (1) {
Serial1_Transmit('A'); // Send 'A' continuously
}
}
// Function to initialize Serial Port 1 (UART1)
void Serial1_Init() {
SCON1 = 0x50; // Mode 1 (8-bit UART), REN1 enabled
TMOD |= 0x20; // Timer1 Mode 2 (8-bit auto-reload)
TH1 = 0xFA; // Load value for 4800 baud rate
TR1 = 1; // Start Timer1
}
// Function to transmit character via Serial1
void Serial1_Transmit(char c) {
SBUF1 = c; // Load character into Serial Buffer
while (TI1 == 0); // Wait for transmission complete
22bec0956
Sheik hussain beevi.A
TI1 = 0; // Clear transmit flag
}
22bec0956
Sheik hussain beevi.A
Explanation
1. Initialize Serial Port 1 (Serial1_Init())
a. SCON1 = 0x50 → Mode 1, Enable Receiver
b. TMOD |= 0x20 → Timer1 Mode 2
c. TH1 = 0xFA → 4800 baud
22bec0956
Sheik hussain beevi.A
d. TR1 = 1 → Start Timer1
2. Transmit 'A' continuously (Serial1_Transmit())
a. SBUF1 = c; → Load character into UART1.
b. while (TI1 == 0); → Wait for transmission.
c. TI1 = 0; → Clear flag for next character.
Question (3)(h): Write a program to send the text string “Hello” to Serial #1. Set the
baud rate at 9600, 8-bit data, and 1 stop bit.
Program to Send "Hello" to Serial Port #1 at 9600 Baud
This program initializes Serial Port 1 and transmits the string "Hello" continuously at 9600
baud, using 8-bit data and 1 stop bit.
Code (Embedded C - Keil)
c
CopyEdit
#include <reg51.h> // Include 8051 register definitions
void Serial_Init();
void Serial_Transmit(char *str);
void main() {
Serial_Init(); // Initialize Serial Port
while (1) {
Serial_Transmit("Hello"); // Send "Hello" continuously
}
22bec0956
Sheik hussain beevi.A
}
void Serial_Init() {
TMOD = 0x20; // Timer1 Mode 2 (8-bit Auto-reload)
TH1 = 0xFD; // Baud rate 9600 (for 11.0592 MHz)
SCON = 0x50; // Serial Mode 1 (8-bit, 1 stop bit)
TR1 = 1; // Start Timer 1
}
void Serial_Transmit(char *str) {
while (*str) {
SBUF = *str++; // Send each character
while (!TI); // Wait for transmission to complete
TI = 0; // Clear the transmit interrupt flag
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation
1. Serial_Init()
22bec0956
Sheik hussain beevi.A
a. Sets Timer 1 in Mode 2 (Auto-reload) for 9600 baud.
b. Configures the Serial Port for 8-bit data and 1 stop bit.
c. Starts Timer 1.
2. Serial_Transmit("Hello")
a. Sends each character of "Hello" one by one.
b. Waits for transmit interrupt (TI) before sending the next character.
c. Clears TI after sending.
Baud Rate Calculation
Using 11.0592 MHz crystal:
TH1=256−12×32×960011.0592×106 =0xFD
Question (3)(i): Program the second serial port of the DS89C4x0
to receive bytes of data serially and put them on P1. Set the baud
rate at 4800, 8-bit data, and 1 stop bit
1. Algorithm
1. Initialize Serial Port 2 with 4800 baud rate, 8-bit data, and 1
stop bit.
2. Continuously check the receive flag (S2RI) to detect incoming
data.
22bec0956
Sheik hussain beevi.A
3. Read the received byte from S2BUF.
4. Output the received byte to Port 1 (P1).
5. Repeat the process.
2. Concept
• The second serial port (S2) of DS89C4x0 is used for serial
communication.
• Baud Rate Generation: Timer 1 is used to set the baud rate to
4800.
• Polling Method: The program continuously checks S2RI for
received data.
• Data Transfer: The received data is moved from S2BUF to P1.
3. Code
#include <REG89C4XX.H>
void Serial2_Init(void) {
BDRCON = 0x1E; // Enable baud rate generator,
BRR=1
BRL = 253; // Set baud rate to 4800 for
11.0592 MHz
S2CON = 0x50; // Mode 1 (8-bit UART), Enable
Reception
}
void main(void) {
22bec0956
Sheik hussain beevi.A
Serial2_Init(); // Initialize Serial Port 2
while (1) {
while (!(S2CON & 0x01)); // Wait for received
data (S2RI flag)
S2CON &= ~0x01; // Clear S2RI flag
P1 = S2BUF; // Move received
byte to Port 1
}
}
22bec0956
Sheik hussain beevi.A
4. Explanation
1. Initialization:
a. BDRCON = 0x1E; enables the baud rate generator.
b. BRL = 253; configures the baud rate to 4800.
c. S2CON = 0x50; sets the serial port to 8-bit UART mode
and enables reception.
2. Data Reception:
a. The program waits for data by checking S2RI.
b. Once data is received, the S2RI flag is cleared.
3. Data Output:
a. The received byte is stored in P1, allowing LEDs or
external devices to display it.
This program ensures continuous reception and direct output to
Port 1. Let me know if you need modifications.
Question (4): Programs on Timer Interrupts Question 4(a): Write
a program that continuously gets 8-bit data from P0 and sends it
to P1 while simultaneously creating a square wave of 200 ms
period on pin P2.1. Use Timer 0 to create the square wave.
Assume that XTAL = 11.0592 MHz
C Algorithm
1. Initialize Timer 0 in Mode 1 (16-bit Timer)
22bec0956
Sheik hussain beevi.A
a. Set up Timer 0 to generate an interrupt every 100 ms.
b. Since one complete cycle is 200 ms, toggling P2.1 at 100
ms creates a 200 ms square wave.
2. Enable Interrupts
a. Enable Timer 0 interrupt (ET0) and global interrupt (EA).
3. Main Loop
a. Continuously read 8-bit data from P0 and write it to P1.
b. The timer interrupt toggles P2.1 every 100 ms.
Concept
Code
#include <REG51.H>
void Timer0_ISR() interrupt 1 {
TH0 = 0xDC; // Reload Timer High byte
TL0 = 0x00; // Reload Timer Low byte
P2_1 = ~P2_1; // Toggle P2.1 (Square Wave)
}
22bec0956
Sheik hussain beevi.A
void Timer0_Init() {
TMOD = 0x01; // Timer 0 Mode 1 (16-bit Timer)
TH0 = 0xDC; // Load Timer High byte
TL0 = 0x00; // Load Timer Low byte
ET0 = 1; // Enable Timer 0 Interrupt
EA = 1; // Enable Global Interrupt
TR0 = 1; // Start Timer 0
}
void main() {
Timer0_Init();
while (1) {
P1 = P0; // Continuously transfer P0 data to
P1
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation
1.Timer0_Init()
a. Configures Timer 0 in 16-bit mode.
b. Loads initial values for 100 ms delay.
c. Enables interrupts and starts Timer 0.
2.Timer0_ISR()
a. Interrupt Service Routine (ISR) for Timer 0.
b. Reloads Timer 0 to maintain 100 ms delay.
c. Toggles P2.1 to create 200 ms square wave.
3. Main Loop (while(1))
a. Reads P0 continuously and sends it to P1.
Expected Behavior
• P1 continuously receives data from P0.
22bec0956
Sheik hussain beevi.A
• P2.1 generates a 200 ms square wave.
Time (ms) P2.1
0 0
100 1
200 0
300 1
Next Steps
• Compile in Keil or Proteus.
• If P2.1 does not toggle, check interrupt enable (EA, ET0).
• If data transfer is slow, ensure P0-P1 connections are
correct.
22bec0956
Sheik hussain beevi.A
Question 4(b): Write a program that continuously gets 8-bit data from P0 and sends it to P1
while simultaneously creating a square wave of 200 ms period on pin P2.1. Create a square
wave that has a high portion of 1085 µs and a low portion of 15 µs. Assume XTAL = 11.0592
MHz. Use Timer 1.
ChatGPT said:
Algorithm
1. Initialize Timer 1 in Mode 1 (16-bit timer).
2. Set Timer 1 values for generating a square wave:
a. High portion: 1085 µs
b. Low portion: 15 µs
3. Configure Timer 1 Interrupt to toggle P2.1 at the specified timing.
4. Continuously transfer 8-bit data from P0 to P1.
5. Use ISR (Interrupt Service Routine) to toggle P2.1 at required intervals.
Concept
• XTAL = 11.0592 MHz, so the machine cycle = 1.085 µs.
• Timer 1 works in Mode 1 (16-bit Timer) to generate precise delays.
Code
#include <REG51.H>
sbit SquareWave = P2^1; // Define P2.1 for the square wave
void Timer1_ISR() interrupt 3 {
22bec0956
Sheik hussain beevi.A
static bit state = 0; // Toggle state
if (state == 0) {
TH1 = 0xFF; // Load High byte for 1085 µs
TL1 = 0xA4; // Load Low byte for 1085 µs
} else {
TH1 = 0xFF; // Load High byte for 15 µs
TL1 = 0xF2; // Load Low byte for 15 µs
}
state = ~state; // Toggle state
SquareWave = ~SquareWave; // Toggle P2.1
}
void Timer1_Init() {
TMOD |= 0x10; // Timer 1 in Mode 1 (16-bit Timer)
TH1 = 0xFF; // Load initial values
TL1 = 0xA4;
ET1 = 1; // Enable Timer 1 Interrupt
EA = 1; // Enable Global Interrupt
TR1 = 1; // Start Timer 1
}
void main() {
Timer1_Init();
while (1) {
P1 = P0; // Continuously transfer P0 to P1
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation
1. Timer 1 Interrupt toggles P2.1:
a. 1085 µs High: TH1 = 0xFF, TL1 = 0xA4
b. 15 µs Low: TH1 = 0xFF, TL1 = 0xF2
2. Main loop continuously transfers data from P0 to P1.
3. Interrupt toggles the square wave without affecting P0 → P1 transfer.
Question 4(c): Write a program to generate a square wave of 50 Hz
frequency on pin P2.3. it uses an interrupt for Timer 0.
22bec0956
Sheik hussain beevi.A
Algorithm
1. Initialize Timer 0 in Mode 1 (16-bit Timer).
2. Calculate Timer Reload Values for a 50 Hz square wave.
3. Enable Timer 0 Interrupt to toggle P2.3 at the required interval.
4. Inside the ISR, toggle P2.3 and reload Timer 0.
Concept
• XTAL = 11.0592 MHz, so machine cycle = 1.085 µs.
• Each cycle consists of 10 ms HIGH and 10 ms LOW.
22bec0956
Sheik hussain beevi.A
Code
#include <REG51.H>
sbit SquareWave = P2^3; // Define P2.3 for square wave
void Timer0_ISR() interrupt 1 {
SquareWave = ~SquareWave; // Toggle P2.3
TH0 = 0xD8; // Load high byte for 10 ms
TL0 = 0xF0; // Load low byte for 10 ms
}
void Timer0_Init() {
TMOD |= 0x01; // Timer 0 in Mode 1 (16-bit Timer)
TH0 = 0xD8; // Load Timer initial value
TL0 = 0xF0;
ET0 = 1; // Enable Timer 0 Interrupt
EA = 1; // Enable Global Interrupt
TR0 = 1; // Start Timer 0
}
void main() {
Timer0_Init();
while (1);
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation
1. Timer 0 runs in Mode 1 (16-bit Timer).
2. ISR toggles P2.3 every 10 ms, generating a 50 Hz square wave.
3. Timer values (TH0, TL0) are set to reload the 10 ms delay.
4. The main loop keeps the microcontroller running.
22bec0956
Sheik hussain beevi.A
L
Expected Output
• P2.3 toggles every 10 ms, forming a 50 Hz square wave.
• Interrupt ensures precise timing.
22bec0956
Sheik hussain beevi.A
Question 4(d): Assume that the INT1 pin is connected to a switch that is normally high.
Whenever it goes low, it should turn on an LED. The LED is connected to P1.3 and is
normally off. When it is turned on it should stay on for a fraction of a second. As long
as the switch is pressed low, the LED should stay on
Algorithm
1. Configure INT1 (P3.3) as an external interrupt with a falling edge trigger.
2. Set P1.3 as output for the LED, initially OFF.
3. Enable External Interrupt 1 (INT1) to detect when the switch goes LOW.
4. When INT1 triggers, turn ON the LED (P1.3 = 0).
5. Use a delay to keep the LED ON for a short time.
6. If the switch is still LOW, keep the LED ON.
7. When the switch is released (HIGH), turn OFF the LED.
Concept
• External interrupt INT1 (P3.3) is used to detect when the switch is pressed.
• LED (P1.3) is active LOW, meaning 0 = ON, 1 = OFF.
• Falling edge detection ensures the LED turns ON when the switch is pressed.
• A delay ensures the LED stays ON for a fraction of a second even if the switch is
released quickly.
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Code
#include <reg51.h>
sbit LED = P1^3; // LED connected to P1.3
void delay_ms(unsigned int ms) {
unsigned int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 1275; j++); // Approximate 1ms delay
void external_interrupt1(void) interrupt 2 { // ISR for INT1 (P3.3)
LED = 0; // Turn ON LED (Active LOW)
delay_ms(500); // Keep LED ON for 500ms
LED = 1; // Turn OFF LED
void main() {
LED = 1; // LED initially OFF
IT1 = 1; // Configure INT1 for falling edge trigger
22bec0956
Sheik hussain beevi.A
EX1 = 1; // Enable External Interrupt 1
EA = 1; // Enable Global Interrupts
while (1); // Infinite loop, waiting for interrupt
}
22bec0956
Sheik hussain beevi.A
Explanation
1. INT1 is configured for a falling edge trigger (IT1 = 1) to detect when the switch
goes LOW.
2. When the switch is pressed (INT1 triggered), the LED (P1.3) turns ON.
3. The program waits until the switch is released before turning OFF the LED.
4. Interrupt ensures that the LED turns ON instantly without delays in execution.
5. The main loop remains idle, waiting for interrupts.
Expected Output
• When the switch (P3.3) is pressed (LOW), the LED (P1.3) turns ON.
• As long as the switch remains pressed, the LED stays ON.
• When the switch is released, the LED turns OFF.
Question 4(e): Assuming that pin 3.3 (INT1) is connected to a pulse generator, write a
program in which the falling edge of the pulse will send a high to P1.3, which is
connected to an LED (or buzzer). In other words, the LED is turned on and off at the
same rate as the pulses are applied to the INT1 pin. This is an edge-triggered version
Algorithm
1. Configure INT1 (P3.3) as a Falling Edge Triggered Interrupt.
2. When a falling edge is detected on INT1, execute the ISR.
3. Toggle P1.3 (LED/Buzzer) every time a pulse is received on INT1.
4. Continue running indefinitely, responding to pulses on INT1.
Concepts Used
• 8051 External Interrupt (INT1 - P3.3)
• Interrupt Service Routine (ISR)
22bec0956
Sheik hussain beevi.A
• Edge-Triggered Mode
• Bit Addressing (sbit) for P1.3 (LED/Buzzer)
Code
#include <reg51.h>
sbit LED = P1^3; // LED/Buzzer connected to P1.3
void external_interrupt1(void) interrupt 2 { // ISR for INT1 (P3.3)
LED = ~LED; // Toggle LED state
}
void main() {
LED = 1; // LED initially OFF
IT1 = 1; // Configure INT1 for falling edge trigger
EX1 = 1; // Enable External Interrupt 1
EA = 1; // Enable Global Interrupts
while (1); // Infinite loop, waiting for interrupts
22bec0956
Sheik hussain beevi.A
}
Explanation
1. Interrupt Configuration
a. IT1 = 1; → Falling edge trigger for INT1 (P3.3).
b. EX1 = 1; → Enable External Interrupt 1.
c. EA = 1; → Enable Global Interrupts.
2. Interrupt Service Routine (ISR)
a. Every falling edge on INT1 toggles the LED/Buzzer.
b. If LED was OFF, it turns ON.
c. If LED was ON, it turns OFF.
22bec0956
Sheik hussain beevi.A
3. Main Loop
a. Runs infinitely, waiting for interrupts.
Question (5)(a): Write a program in which the 8051 reads data from P1 and
writes it to P2 continuously while giving a copy of it to the serial COM port
to be transferred serially. Assume that XTAL = 11.0592 MHz. Set the baud
rate at 9600
Algorithm
1. Initialize Serial Communication at 9600 baud, 8-bit data, 1 stop bit.
2. Continuously read data from Port 1 (P1).
3. Write the same data to Port 2 (P2).
4. Send a copy of the data through the serial COM port.
5. Repeat indefinitely.
Concepts Used
• 8051 Serial Communication using SBUF
• Polling for Serial Transmission (TI flag)
• Parallel Data Transfer from P1 to P2
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Code#include <reg51.h>
void serial_init() {
TMOD = 0x20; // Timer1 in Mode 2 (Auto-Reload) for Baud Rate
TH1 = 0xFD; // Baud Rate 9600 (for 11.0592 MHz XTAL)
SCON = 0x50; // 8-bit UART, 1 Stop Bit, REN Enabled
TR1 = 1; // Start Timer1
}
void serial_transmit(unsigned char data) {
SBUF = data; // Load data into Serial Buffer
while (!TI); // Wait until transmission completes
TI = 0; // Clear Transmit Interrupt Flag
}
void main() {
serial_init(); // Initialize Serial Communication
while (1) {
unsigned char data = P1; // Read from P1
P2 = data; // Write to P2
serial_transmit(data); // Send data serially
}
22bec0956
Sheik hussain beevi.A
}
Explanation
1. Serial Initialization (serial_init)
a. TMOD = 0x20; → Timer1 in Mode 2 (Auto-Reload).
b. TH1 = 0xFD; → 9600 baud rate at 11.0592 MHz.
c. SCON = 0x50; → 8-bit UART, 1 Stop Bit, REN enabled.
d. TR1 = 1; → Start Timer1 for Baud Rate Generation.
2. Main Function (main)
a. Continuously reads data from P1.
b. Writes the data to P2.
c. Transmits the same data serially.
22bec0956
Sheik hussain beevi.A
3. Seral Transmission (serial_transmit)
a. Loads data into SBUF.
b. Waits until the TI flag is set (indicating transmission complete).
c. Clears TI for the next byte transmission.
Question 5(b)
Concept:
• The 8051 microcontroller continuously reads data from P1 and sends it to P2.
• It also receives data from the serial port and sends it to P0.
• The serial communication operates at a baud rate of 9600.
• Uses 8-bit data and 1 stop bit configuration.
Algorithm:
1. Initialize serial communication at 9600 baud.
2. Configure P1 as input, P2 as output, and P0 as output for received serial data.
3. Continuously read data from P1 and send it to P2.
4. Continuously check if data is received via serial port and send it to P0.
Code:
#include <reg51.h>
void Serial_Init() {
TMOD = 0x20; // Timer1, Mode2 (8-bit auto-reload)
TH1 = 0xFD; // Baud rate 9600 for 11.0592MHz
SCON = 0x50; // 8-bit data, 1 stop bit
TR1 = 1; // Start Timer1
22bec0956
Sheik hussain beevi.A
}
char Serial_Receive() {
while (!RI); // Wait for reception to complete
RI = 0; // Clear RI flag
return SBUF; // Return received data
}
void Serial_Transmit(char data) {
SBUF = data; // Load data to serial buffer
while (!TI); // Wait until transmission completes
TI = 0; // Clear TI flag
}
void main() {
Serial_Init();
while (1) {
P2 = P1; // Send P1 data to P2
if (RI) {
char data = Serial_Receive();
P0 = data; // Send received data to P0
}
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
• Serial_Init() initializes the serial communication with a baud rate of 9600.
• Serial_Receive() waits for data reception and returns the received byte.
• Serial_Transmit() transmits a character via the serial port.
• Inside main(), data from P1 is continuously copied to P2, and any received data is
sent to P0.
Question 5(c)
Concept:
• The program uses interrupts to:
o Receive data serially and send it to P0.
o Read P1, transmit it serially, and send a copy to P2.
22bec0956
Sheik hussain beevi.A
o Generate a 5 kHz square wave on P0.1 using Timer 0.
• Baud rate: 4800.
Algorithm:
1. Initialize serial communication at 4800 baud.
2. Enable Serial Interrupt and Timer 0 Interrupt.
3. Use Timer 0 to generate a 5 kHz square wave.
4. Interrupt Routine (ISR):
a. If serial data is received, send it to P0.
b. If Timer 0 overflows, toggle P0.1 for square wave.
5. Continuously read P1, send it to P2, and transmit serially.
Code:
#include <reg51.h>
sbit SQWAVE = P0^1;
char receivedData;
void Serial_ISR() interrupt 4 {
if (RI) { receivedData = SBUF;
P0 = receivedData;
RI = 0;
}
if (TI) TI = 0;
}
void Timer0_ISR() interrupt 1 {
SQWAVE = ~SQWAVE;
TH0 = 0xF8; // Reload for 5kHz square wave
TL0 = 0xCD;
}
void Serial_Init() {
TMOD = 0x20;
TH1 = 0xFA; // Baud rate 4800
SCON = 0x50;
TR1 = 1;
22bec0956
Sheik hussain beevi.A
ES = 1; // Enable Serial Interrupt
}
void Timer0_Init() {
TMOD |= 0x01;
TH0 = 0xF8;
TL0 = 0xCD;
TR0 = 1;
ET0 = 1;
}
void main() {
EA = 1;
Serial_Init();
Timer0_Init();
while (1) {
Serial_Transmit(P1);
P2 = P1;
22bec0956
Sheik hussain beevi.A
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
• Serial ISR: Reads received data and sends it to P0.
• Timer ISR: Toggles P0.1 to generate a 5 kHz square wave.
• Main loop: Continuously reads P1, sends it to P2, and transmits serially.
Question 5(d)
Concept:
• The program performs three simultaneous operations:
o Read P1.0, send it to P2.0.
o Generate a 2.5 kHz square wave on P2.1.
o Send numbers '0' to '9' repeatedly to the serial port.
Algorithm:
1. Initialize Timer 0 for 2.5 kHz square wave.
2. Initialize Serial Communication.
3. Enable Timer Interrupt.
4. In Main Loop:
22bec0956
Sheik hussain beevi.A
a. Continuously send P1.0 to P2.0.
b. Send numbers '0' to '9' serially in a loop.
Code:
#include <reg51.h>
sbit SQWAVE = P2^1;
sbit DATA_IN = P1^0;
sbit DATA_OUT = P2^0;
void Timer0_ISR() interrupt 1 {
SQWAVE = ~SQWAVE;
}
void Serial_Init() {
TMOD = 0x20;
TH1 = 0xFD;
SCON = 0x50;
TR1 = 1;
}
void Timer0_Init() {
TMOD |= 0x01;
TH0 = 0xFA;
TL0 = 0xCD;
TR0 = 1;
ET0 = 1;
}
void main() {
char num = '0';
EA = 1;
Serial_Init();
Timer0_Init();
while (1) {
DATA_OUT = DATA_IN;
Serial_Transmit(num);
22bec0956
Sheik hussain beevi.A
num = (num == '9') ? '0' : num + 1;
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
• Timer ISR toggles P2.1 at 2.5 kHz.
• Serially transmits '0' to '9' continuously.
• Reads P1.0 and sends it to P2.0.
22bec0956
Sheik hussain beevi.A
Question 5(e)
Concept:
• This program performs three operations simultaneously:
o Reads P1.0, sends it to P2.0.
o Generates 2.5 kHz square wave on P2.1.
o Receives serial data and sends it to P0.
Code:
#include <reg51.h>
sbit SQWAVE = P2^1;
sbit DATA_IN = P1^0;
sbit DATA_OUT = P2^0;
void Serial_ISR() interrupt 4 {
if (RI) {
P0 = SBUF;
RI = 0;
}
}
void Timer0_ISR() interrupt 1 {
SQWAVE = ~SQWAVE;
}
void main() {
EA = 1;
ES = 1;
ET0 = 1;
while (1) {
DATA_OUT = DATA_IN;
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
• Timer ISR toggles P2.1 to generate 2.5 kHz.
• Serial ISR receives data and sends it to P0.
• Main loop reads P1.0 and sends it to P2.0.
22bec0956
Sheik hussain beevi.A
6a) Complement Port 1.7 on INT1 Interrupt
22bec0956
Sheik hussain beevi.A
Concept:
• INT1 (External Interrupt 1) is triggered on a falling edge.
• When an interrupt occurs, the logic of P1.7 is toggled (0 → 1 or 1 → 0).
Logic:
• P1.7 is initially in a default state.
• When an edge-triggered interrupt (INT1) occurs at P3.3, P1.7 toggles.
Algorithm:
1. Enable global interrupts (EA = 1).
2. Enable external interrupt INT1 (EX1 = 1).
3. Configure INT1 as edge-triggered (IT1 = 1).
4. On INT1 interrupt, toggle P1.7.
5. Repeat indefinitely.
22bec0956
Sheik hussain beevi.A
Code:
#include <reg51.h>
sbit LED = P1^7; // Define LED at P1.7
void external_ISR(void) interrupt 2 {
LED = ~LED; // Toggle P1.7
}
void main() {
22bec0956
Sheik hussain beevi.A
EA = 1; // Enable Global Interrupts
EX1 = 1; // Enable External Interrupt 1
IT1 = 1; // Configure INT1 as Edge Triggered
while(1); // Infinite loop
22bec0956
Sheik hussain beevi.A
}
22bec0956
Sheik hussain beevi.A
Explanation:
• EX1 = 1 enables INT1.
• IT1 = 1 sets INT1 as edge-triggered.
• When an interrupt occurs, P1.7 toggles between HIGH and LOW.
22bec0956
Sheik hussain beevi.A
(6b) Transfer P1 to P2 Using Interrupt While Timer 0
Generates 3 kHz Square Wave
Concept:
• Timer 0 is used to generate a 3 kHz square wave at P2.1.
• P1 data is transferred to P2 continuously.
Logic:
• Timer 0 is set in Mode 1 (16-bit mode).
• Timer 0 generates an interrupt every 166.67 µs, toggling P2.1.
• The main loop copies P1 to P2 continuously.
Algorithm:
1. Configure Timer 0 in Mode 1 (16-bit).
2. Load TH0, TL0 values for 3 kHz frequency.
3. Enable Timer 0 interrupt.
4. In ISR, toggle P2.1.
5. In main loop, copy P1 to P2.
Code:
#include <reg51.h>
sbit square_wave = P2^1; // Square wave output at P2.1
void timer0_ISR(void) interrupt 1 {
TH0 = 0xFA; // Reload for 3 kHz
TL0 = 0x33;
square_wave = ~square_wave; // Toggle P2.1
}
void main() {
TMOD = 0x01; // Timer 0 Mode 1 (16-bit)
TH0 = 0xFA;
TL0 = 0x33;
22bec0956
Sheik hussain beevi.A
TR0 = 1; // Start Timer 0
ET0 = 1; // Enable Timer 0 Interrupt
EA = 1; // Enable Global Interrupts
while(1) {
P2 = P1; // Transfer P1 data to P2
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
• Timer 0 generates 3 kHz wave by toggling P2.1 every 166.67 µs.
• P1 data is continuously copied to P2.
(6c) Transfer P1 to P2, Toggle LED on P0.4 Every Second
Using Timer 1
Concept:
• Timer 1 is used to generate a 1-second delay.
• P1 data is transferred to P2 continuously.
• P0.4 LED toggles every 1 second.
22bec0956
Sheik hussain beevi.A
Logic:
• Timer 1 is set in Mode 1 (16-bit).
• It overflows every 1 second, toggling P0.4.
Algorithm:
1. Configure Timer 1 in Mode 1 (16-bit).
2. Load TH1, TL1 values for 1-second delay.
3. Enable Timer 1 interrupt.
4. In ISR, toggle P0.4.
5. In main loop, copy P1 to P2.
Code:
#include <reg51.h>
sbit LED = P0^4; // LED at P0.4
void timer1_ISR(void) interrupt 3 {
TH1 = 0x3C; // Reload for 1 second
TL1 = 0xB0;
LED = ~LED; // Toggle LED
}
void main() {
TMOD = 0x10; // Timer 1 Mode 1 (16-bit)
TH1 = 0x3C;
TL1 = 0xB0;
TR1 = 1; // Start Timer 1
ET1 = 1; // Enable Timer 1 Interrupt
EA = 1; // Enable Global Interrupts
while(1) {
P2 = P1; // Transfer P1 data to P2
}
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
• Timer 1 overflows every second, toggling P0.4.
• P1 data is continuously transferred to P2.
(6d) Count INT1 Interrupts and Display on Port 2
Concept:
• Every time INT1 (P3.3) is triggered, a counter is incremented.
• The count is displayed on P2.
Logic:
• INT1 is edge-triggered.
• Each INT1 interrupt increases the counter.
• The counter is stored in P2.
Algorithm:
1. Enable global interrupts (EA = 1).
2. Enable external interrupt INT1 (EX1 = 1).
3. Configure INT1 as edge-triggered (IT1 = 1).
4. When INT1 occurs, increment count and store in P2.
Code:
#include <reg51.h>
unsigned char count = 0; // Interrupt counter
void external_ISR(void) interrupt 2 {
count++; // Increment count
P2 = count; // Display count on P2
}
void main() {
22bec0956
Sheik hussain beevi.A
EA = 1; // Enable Global Interrupts
EX1 = 1; // Enable External Interrupt 1
IT1 = 1; // Configure INT1 as Edge Triggered
P2 = 0; // Initialize P2 as 0
while(1); // Infinite loop
}
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
Explanation:
• When INT1 interrupt occurs, count increases.
• Count is continuously displayed on P2.
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A
22bec0956
Sheik hussain beevi.A