Embedded C Programming Module 6 Presentation

MarkkandanS 43 views 46 slides Jul 16, 2024
Slide 1
Slide 1 of 46
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46

About This Presentation

Embedded C Programming Module 6 Presentation


Slide Content

Module-6
Timer and Serial port programming
Dr. Markkandan S
School of Electronics Engineering (SENSE)
Vellore Institute of Technology
Chennai
Dr. Markkandan S (School of Electronics Engineering (SENSE)Vellore Institute of Technology Chennai)Module-6 Timer and Serial port programming 1 / 46

Module Objectives and Key Topics
Objectives:
Understand the basic architecture and capabilities of the 8051
microcontroller.
Learn how to program the 8051’s timers for various applications.
Master serial port programming and communication.
Develop practical skills through hands-on examples and case studies.
Key Topics:
8051 Architecture and Memory Organization
Timer Programming Techniques
Serial Communication Methods
Practical Applications and Case Studies
Dr. Markkandan S Module-6 Timer and Serial port programming 2/46

Understanding 8051 Architecture
The 8051 microcontroller features a Harvard architecture, separating
data and instruction buses.
Contains a central processing unit (CPU) capable of executing a wide
array of instructions with 12-clock cycles per instruction.
Equipped with an 8-bit ALU (Arithmetic Logic Unit) and
Accumulator.
Offers four parallel I/O ports, each 8-bit wide, alongside serial
communication.
Integral features include built-in timers, counters, and a versatile
interrupt system.
Dr. Markkandan S Module-6 Timer and Serial port programming 3/46

Memory Organization in 8051
The 8051 microcontroller has a complex memory structure,
comprising both on-chip and off-chip memory.
On-chip memory includes 128 bytes of internal RAM and up to 4 KB
of internal ROM.
RAM is divided into general-purpose RAM and special function
registers (SFRs).
Supports external memory interfacing, allowing the expansion of
ROM and RAM.
The distinct address spaces for program and data memory facilitate
efficient data processing and storage.
Dr. Markkandan S Module-6 Timer and Serial port programming 4/46

8051 Register Set Overview
The 8051 microcontroller consists of a set of registers that include the
Accumulator, B register, Program Status Word (PSW), Stack Pointer
(SP), Data Pointer (DPTR), and a series of R0 to R7 registers.
Accumulator (A): Central to most operations, especially arithmetic
and logic.
B Register: Used in multiply and divide operations.
Program Status Word (PSW): Contains status flags for arithmetic
operations and control flags.
Stack Pointer (SP): Points to the top of the stack, crucial for
subroutine and interrupt handling.
Data Pointer (DPTR): A 16-bit register, primarily used for external
memory addressing.
Dr. Markkandan S Module-6 Timer and Serial port programming 5/46

Introduction to Timer Programming in 8051
The 8051 microcontroller features two 16-bit timers/counters: Timer
0 and Timer 1.
These timers are versatile and can be operated in four different
modes.
Key uses: event counting, generating time delays, baud rate
generation for serial communication, and producing PWM signals.
Each timer is controlled by two registers: a control register (TMOD
for mode and TRx for run control) and a count register (THx and
TLx).
Dr. Markkandan S Module-6 Timer and Serial port programming 6/46

Timer Register
Dr. Markkandan S Module-6 Timer and Serial port programming 7/46

Mod 0 and Mod 1 Timer / Counter
Figure:Figure:
Dr. Markkandan S Module-6 Timer and Serial port programming 8/46

Mod 2 and Mod 3 Timer/ Counter
Figure:Figure:
Dr. Markkandan S Module-6 Timer and Serial port programming 9/46

Accessing Timer Registers in 8051
Timer Mode Register (TMOD): A single byte register used to set the
timer mode and operation type (timer or counter).
Timer Control Register (TCON): Contains control bits for the timers,
including run control and overflow flags.
Timer High (THx) and Timer Low (TLx) Registers: Two 8-bit
registers that together form a 16-bit count for each timer.
Example: Configuring Timer 0 in mode 1 (16-bit timer mode).
TMOD = 0x01; // Set Timer 0 in mode 1
TR0 = 1; // Start Timer 0
Dr. Markkandan S Module-6 Timer and Serial port programming 10/46

Advanced Scenarios in Accessing Timer Registers
Exploring edge cases and advanced configurations in timer register
access.
Scenario 1: Configuring Timer 0 in Mode 3 (Split Timer Mode).
Scenario 2: Cascading Timer 0 and Timer 1 for extended duration
timing.
// Timer 0 in Mode 3
void Timer0_Mode3_Init() {
TMOD |= 0x0C; // Set Timer 0 in Mode 3
// Timer 0 now operates as two separate 8-bit timers/counters
}
// Cascading Timers for Extended Duration
void CascadeTimers_Init() {
TMOD |= 0x11; // Timer 0 and Timer 1 in Mode 1
// Configure and start Timer 0
// On Timer 0 overflow, increment a software counter
// Use this software counter along with Timer 1 for extended timing
}
Note: These advanced configurations provide more flexibility in timing
applications.
Dr. Markkandan S Module-6 Timer and Serial port programming 11/46

Timer Delays in Mode 1 and Mode 2
Mode 1 (16-bit timer mode): Suitable for longer time delays.
Mode 2 (8-bit auto-reload mode): Useful for consistent, repeatable
delays.
Code example: Implementing a 1ms delay using Timer 0 in Mode 1.
Set TMOD for Timer 0, Mode 1.
Load TH0 and TL0 with values calculated for 1ms delay.
Start the timer and wait for the TF0 (timer overflow) flag to set, then
stop the timer and clear TF0.
Dr. Markkandan S Module-6 Timer and Serial port programming 12/46

Accessing Timer Registers in 8051
Timers in the 8051 are controlled by specific registers: TMOD for
mode setting, TCON for control bits, and THx/TLx for timer values.
Code Example: Initializing Timer 0 in Mode 1 (16-bit mode).
void Timer0_Init() {
TMOD &= 0xF0; // Clear lower 4 bits of TMOD
TMOD |= 0x01; // Set Timer 0 in Mode 1
TH0 = 0xFC; // Load high byte for 1ms delay
TL0 = 0x66; // Load low byte for 1ms delay
TR0 = 1; // Start Timer 0
}
Note: TH0 and TL0 values depend on system clock frequency.
Dr. Markkandan S Module-6 Timer and Serial port programming 13/46

Timer Delay in Mode 1
Mode 1 is a 16-bit timer mode, allowing for a wider range of delay.
Code Example: Generating a 1ms delay using Timer 0 in Mode 1.
void Delay1ms() {
TMOD |= 0x01; // Timer 0 in Mode 1
TH0 = 0xFC; // Values for 1ms delay
TL0 = 0x66;
TR0 = 1; // Start Timer 0
while (TF0 == 0); // Wait until Timer 0 overflows
TR0 = 0; // Stop Timer 0
TF0 = 0; // Clear overflow flag
}
Note: Adjust TH0 and TL0 for your clock frequency.
Dr. Markkandan S Module-6 Timer and Serial port programming 14/46

Timer Delay in Mode 2
Mode 2 configures the timer as an 8-bit auto-reload timer, useful for
consistent time intervals.
Code Example: Setting up Timer 0 in Mode 2 for a fixed delay.
void DelayUsingTimer0Mode2(unsigned char delayValue) {
TMOD &= 0xF0; // Clear lower 4 bits for Timer 0
TMOD |= 0x02; // Set Timer 0 in Mode 2
TH0 = TL0 = delayValue; // Set auto-reload value
TR0 = 1; // Start Timer 0
while (TF0 == 0); // Wait until Timer 0 overflows
TR0 = 0; // Stop Timer 0
TF0 = 0; // Clear overflow flag
}
Note: The delay is dependent on the value loaded into TH0/TL0 and the
system clock.
Dr. Markkandan S Module-6 Timer and Serial port programming 15/46

Generating Clock Frequencies with 8051 Timers
Timers in 8051 can be used to generate various clock frequencies,
particularly using modes 1 and 2.
Example: Generating a 1kHz clock using Timer 1 in Mode 2.
Configure TMOD for Timer 1, Mode 2.
Load TH1 with a value that yields a 1kHz frequency upon overflow.
Enable Timer 1 and use its overflow (TF1) to toggle a port pin,
creating a square wave.
Dr. Markkandan S Module-6 Timer and Serial port programming 16/46

Generating Clock Frequencies: Mathematical
Approach
Understanding the relationship between the microcontroller’s
oscillator frequency, timer settings, and the desired output frequency.
Timer Tick Frequency:
Formula:
Oscillator Frequency
Prescaler Value
Example: For a 12 MHz oscillator,
12,000,000
12
= 1,000,000 Hz
Timer Ticks per Cycle for Desired Frequency:
Formula:
Timer Tick Frequency
Desired Frequency
Example: For 1 kHz,
1,000,000
1,000
= 1,000 ticks
Reload Value Calculation:
16-bit Mode (Mode 1): 65536−Timer Ticks per Cycle
8-bit Auto-Reload Mode (Mode 2): 256−(Timer Ticks per Cycle
mod 256)
Example for 1 kHz in Mode 2: 256−(1,000
mod 256) = 256−232 = 24
Implementation:
Configuring Timer 1 in Mode 2 with the calculated reload value for
generating the desired frequency.
Dr. Markkandan S Module-6 Timer and Serial port programming 17/46

Generating Various Clock Frequencies with Timers
Timers in the 8051 can be used to generate a range of clock
frequencies, crucial for tasks like PWM generation.
Code Example: Generating a 500 Hz square wave using Timer 1 in
Mode 2.
void Generate500Hz() {
TMOD &= 0x0F; // Clear higher 4 bits for Timer 1
TMOD |= 0x20; // Set Timer 1 in Mode 2 (8-bit auto-reload)
TH1 = TL1 = 256 - (Oscillator_Frequency / (12 * 2 * 500));
// Load TH1 and TL1 for 500 Hz
TR1 = 1; // Start Timer 1
// Connect P3.5 (T1 output) to observe the waveform
}
Note: OscillatorFrequency should be replaced with the actual frequency
of your 8051’s oscillator.
Dr. Markkandan S Module-6 Timer and Serial port programming 18/46

Generating Clock Frequencies for Different
Applications
Advanced applications of frequency generation using 8051 timers.
Example 1: Generating a PWM signal for motor speed control.
Example 2: Creating a variable frequency output for communication
protocols.
// PWM Signal Generation
void PWM_Init() {
TMOD |= 0x02; // Timer 1 in Mode 2 (Auto-Reload)
TH1 = PWM_Value; // Set PWM frequency
TR1 = 1; // Start Timer 1
// Connect P1.0 to PWM output (use T1 overflow to toggle P1.0)
}
// Variable Frequency Generation
void VariableFrequency_Init(uint8_t frequency) {
TMOD |= 0x02; // Timer 1 in Mode 2 (Auto-Reload)
TH1 = 256 - (Oscillator_Frequency / (12 * frequency));
TR1 = 1; // Start Timer 1
// Frequency can be adjusted by changing TH1
}
Note: Adjust the frequency and PWM values based on specific application
needs.
Dr. Markkandan S Module-6 Timer and Serial port programming 19/46

Using Timers as Counters in 8051
The 8051 timers can function as event counters by counting external
pulses.
Example: Using Timer 0 as an event counter.
Configure TMOD to set Timer 0 as a 16-bit counter.
Clear TF0 and start Timer 0.
Timer 0 now increments its count with each external pulse applied to
the T0 pin.
Dr. Markkandan S Module-6 Timer and Serial port programming 20/46

Programming of Timers as Counters
The 8051’s timers can also function as event counters, incrementing
not on internal clock pulses but on external events.
Code Example: Using Timer 0 as an event counter in Mode 1.
void SetupTimer0AsCounter() {
TMOD &= 0xF0; // Clear lower 4 bits for Timer 0
TMOD |= 0x05; // Timer 0 in Mode 1 as counter
TH0 = TL0 = 0; // Clear Timer 0
TR0 = 1; // Start Timer 0
// Timer 0 now counts external events on T0 pin (P3.4)
}
Note: External events should be applied to the T0 pin (P3.4) to see the
count increment.
Dr. Markkandan S Module-6 Timer and Serial port programming 21/46

Introduction to Serial Port Programming in 8051
The 8051 microcontroller features a serial port (UART) for data
communication.
The serial port is controlled by the SCON register, which sets the
mode of operation and framing.
Baud rate for serial communication is typically set using Timer 1 in
Mode 2.
Serial communication in 8051 can be interrupt-driven or polling-based.
Applications include data transfer between the microcontroller and a
computer, sensor, or another microcontroller.
Dr. Markkandan S Module-6 Timer and Serial port programming 22/46

Introduction to Serial Port Programming in 8051
8051 includes a full-duplex UART for serial communication.
Serial Control Register (SCON): Sets the mode of operation, framing,
and control bits for serial communication.
Baud rate in 8051 is usually set using Timer 1 in Mode 2.
Example: Configuring the 8051 for serial communication.
SCON = 0x50; // Mode 1, 8-bit data, 1 stop bit, REN enabled
Set Timer 1 for the desired baud rate.
Enable serial interrupt (optional) for asynchronous communication.
Dr. Markkandan S Module-6 Timer and Serial port programming 23/46

Configuring the Serial Port in 8051
To use the serial port in 8051, we need to configure the Serial Control
(SCON) register and set the baud rate.
SCON Register: Controls the mode of operation, reception,
transmission, and framing.
Baud Rate: Typically set by Timer 1 in Mode 2.
Code Example: Configuring Serial Port for Mode 1, 8-bit data, and
variable baud rate.
void SerialPort_Init() {
SCON = 0x50; // 8-bit data, 1 stop bit, REN enabled
TMOD |= 0x20; // Timer 1 in Mode 2
TH1 = 0xFD; // 9600 baud rate with 11.0592 MHz Crystal
TR1 = 1; // Start Timer 1
}
Note: Adjust TH1 value based on your crystal frequency.
Dr. Markkandan S Module-6 Timer and Serial port programming 24/46

Transmitting Data via Serial Port
Data transmission in the 8051 is done by writing data to the SBUF
register.
After loading data into SBUF, the TI flag is monitored to check for
completion of transmission.
Code Example: Transmitting a string via Serial Port.
void UART_Transmit_String(char* str) {
while (*str) {
SBUF = *str++; // Load character into SBUF
while (!TI); // Wait for transmission to complete
TI = 0; // Clear TI flag
}
}
Note: Ensure UART is initialized before calling this function.
Dr. Markkandan S Module-6 Timer and Serial port programming 25/46

Receiving Data via Serial Port
Data reception is handled by reading the SBUF register when the RI
flag is set.
The RI flag is set by hardware once a complete character is received.
Code Example: Receiving a character via UART.
char UART_Receive() {
while (!RI); // Wait for data to be received
RI = 0; // Clear RI flag
return SBUF; // Return the received character
}
Note: This function will wait (block) until a character is received.
Dr. Markkandan S Module-6 Timer and Serial port programming 26/46

Serial Port Interrupt Programming in 8051
Interrupt-driven serial communication can greatly enhance efficiency
in data handling.
The 8051 provides interrupts for both transmission (TI) and reception
(RI).
Code Example: Using Interrupts for Serial Communication.
void Serial_ISR(void) interrupt 4 {
if (TI) {
TI = 0; // Clear transmit interrupt flag
// Handle transmission completion (e.g., load next character)
}
if (RI) {
char receivedData = SBUF; // Read received data
RI = 0; // Clear receive interrupt flag
// Handle received data (e.g., store or process it)
}
}
Note: This ISR is triggered by both RI and TI flags.
Dr. Markkandan S Module-6 Timer and Serial port programming 27/46

Serial Port Interrupt Programming in 8051
Figure:
Dr. Markkandan S Module-6 Timer and Serial port programming 28/46

Combining Timer and Serial Operations in 8051
Timers and serial port can be combined for sophisticated applications
like time-stamped data logging or synchronized data transmission.
Example: Using a timer to timestamp incoming serial data.
Overview of how timers can provide precise time intervals for
managing serial communication tasks.
Dr. Markkandan S Module-6 Timer and Serial port programming 29/46

Practical Application: Timer-Based LED Blinking
Demonstrate a simple application using a timer to blink an LED at
regular intervals.
Code Example: Using Timer 0 in Mode 1 to blink an LED connected
to a port pin.
void Timer0_ISR(void) interrupt 1 {
TF0 = 0; // Clear Timer 0 overflow flag
TH0 = reload_value_high; // Reload high byte
TL0 = reload_value_low; // Reload low byte
P1 ^= 0x01; // Toggle P1.0 (LED pin)
}
void main() {
TMOD |= 0x01; // Timer 0 in Mode 1
// Calculate and set reload_value_high and reload_value_low based on desired delay
EA = 1; // Enable global interrupts
ET0 = 1; // Enable Timer 0 interrupt
TR0 = 1; // Start Timer 0
while(1);
}
Note: Adjust reload values based on the system clock frequency.
Dr. Markkandan S Module-6 Timer and Serial port programming 30/46

Practical Application: Serial Data Logging
Using the serial port to log data, such as sensor readings, to a
computer or external storage.
Code Example: Sending sensor data over the serial port.
void LogData(char* data) {
SerialPort_Init(); // Initialize serial port
UART_Transmit_String("Sensor Data: ");
UART_Transmit_String(data);
UART_Transmit_String("Ω");
}
void main() {
char sensorData[10];
while(1) {
// Assuming GetSensorData() reads data from a sensor
GetSensorData(sensorData);
LogData(sensorData);
Delay1s(); // Delay between readings
}
}
Note: Ensure that the UART initialization matches the baud rate of your
receiver.
Dr. Markkandan S Module-6 Timer and Serial port programming 31/46

Case Study: Real-Time Clock Implementation
Explore a case study where timers and serial communication are used
to implement a real-time clock (RTC) in the 8051.
Discuss how a timer can be used to maintain time, and serial port to
display or log time data.
Overview of integrating an external RTC module via serial
communication for accurate timekeeping.
Note: Provide a conceptual overview or flowchart of the RTC system,
highlighting the role of the timer and serial interface.
Dr. Markkandan S Module-6 Timer and Serial port programming 32/46

Interrupt Programming in 8051
Interrupts are critical for responsive and efficient microcontroller
applications.
The 8051 microcontroller features five interrupt sources: two external
interrupts, two timer interrupts, and a serial communication interrupt.
Code Example: Setting up an external interrupt.
void External0_ISR(void) interrupt 0 {
// Handle external interrupt 0 (INT0)
}
void main() {
IT0 = 1; // Configure INT0 for falling edge trigger
EX0 = 1; // Enable external interrupt 0
EA = 1; // Enable global interrupts
while(1);
}
Note: The external interrupt can be used for a variety of purposes like
triggering an action when an external event occurs.
Dr. Markkandan S Module-6 Timer and Serial port programming 33/46

Power Saving Modes and Timer Usage in 8051
The 8051 offers power saving modes like Idle and Power Down modes.
Idle Mode: The CPU is halted while the timers, serial port, and
interrupt system remain operational.
Power Down Mode: Most of the microcontroller’s functions are
turned off for maximum power saving, but the external interrupts
remain enabled.
Discussion on how timers can be used efficiently in power saving
modes, particularly in battery-operated applications.
Dr. Markkandan S Module-6 Timer and Serial port programming 34/46

Best Practices in Timer and Serial Programming
Accurate Timer Configurations:
Understand the clock source of your 8051 variant. Timer intervals
depend on the oscillator frequency.
Always calculate and double-check timer reload values for accuracy.
Use prescalers effectively to extend timer capabilities, especially for
long-duration timing.
Optimizing Serial Communication:
Match baud rates accurately between your 8051 system and the
external device to prevent data corruption.
Use buffer systems for receiving data to handle variable-length or
high-speed data efficiently.
When using interrupts, ensure that the ISR (Interrupt Service Routine)
is concise to minimize overhead.
Robust Error Handling:
Implement error-checking mechanisms, such as parity bits or
checksums, in your serial communication protocol.
In timer routines, account for possible scenarios like timer overflow or
missing timer interrupts.
Testing and Validation:
Test timer routines in both normal and edge-case scenarios.
For serial communication, test with different data lengths, speeds, and
start-stop conditions.
Effective Documentation:
Maintain clear documentation, especially for complex timer setups and
custom serial communication protocols.
Include comments in code for key functions and routines for better
understanding and maintenance.
Dr. Markkandan S Module-6 Timer and Serial port programming 35/46

External Interface of the Standard 8051
The 8051 microcontroller can interface with a variety of external
devices like sensors, memory, and displays.
Utilizing I/O ports for digital input/output: Interfacing with LEDs,
buttons, and other digital components.
Memory interfacing: Connecting external RAM and ROM to expand
the 8051’s memory capabilities.
Interfacing with serial devices: Using UART for communication with
modules like GPS receivers, GSM modems, etc.
Special considerations for driving loads and reading sensors: Signal
conditioning, level shifting, and use of external drivers.
Dr. Markkandan S Module-6 Timer and Serial port programming 36/46

Debugging Tips for 8051 Programs
Debugging is a crucial part of development with the 8051
microcontroller.
Use a logic analyzer or oscilloscope to trace and debug timer and
serial communication issues.
Implement serial print statements for debugging purposes, especially
in the absence of advanced debugging tools.
Be vigilant about common pitfalls: timer overflows, serial buffer
overruns, and incorrect interrupt service routines.
Simulators can be used for preliminary testing, but always validate
with real hardware.
Dr. Markkandan S Module-6 Timer and Serial port programming 37/46

Project Idea: Home Automation System
Implementing a basic home automation system using 8051.
Use timers for controlling device operation duration and serial
communication for receiving control commands.
// Example: Timer for device operation
void Timer1_Init() {
TMOD |= 0x10; // Timer 1, Mode 1
// Set timer duration for device operation
TH1 = 0xFC; TL1 = 0x66;
TR1 = 1; // Start Timer 1
}
// Example: Serial Command Reception
void ReceiveCommand() {
char cmd;
if (RI) {
cmd = SBUF; // Read received command
RI = 0; // Clear RI flag
// Process command (e.g., turn on/off a device)
}
}
Note: Expand this with full device control logic and serial communication
setup.
Dr. Markkandan S Module-6 Timer and Serial port programming 38/46

Solution to Memory Limitation Challenge
A common challenge in embedded systems is managing limited
memory. Here’s how you can address it:
// Example: Memory-efficient data processing
void ProcessSensorData() {
uint8_t sensorData[10]; // Limited local buffer
ReadSensorData(sensorData);
// Process and transmit data immediately
TransmitData(sensorData);
}
Note: Focus on using local buffers and processing data in small chunks.
Dr. Markkandan S Module-6 Timer and Serial port programming 39/46

Transmitting and Receiving Data at Different Baud
Rates
Code Example
void Serial_Init(uint16_t baud) {
SCON = 0x50; // 8-bit UART, REN enabled
TMOD &= 0x0F; // Clear Timer 1 settings
TMOD |= 0x20; // Timer 1 in Mode 2
TH1 = 256 - (Osc \_ Freq / (384 * baud));
TR1 = 1; // Start Timer 1
}
void Transmit(char data) {
SBUF = data; // Load data into buffer
while(!TI); // Wait for transmission
TI = 0; // Clear TI flag
}
Baud rate depends on Timer 1 overflow rate, which is set based on the oscillator frequency.
For reception, the same Timer 1 settings must be used to match baud rates.
Can handle different rates by reinitializing Timer 1 as needed.
Dr. Markkandan S Module-6 Timer and Serial port programming 40/46

Transmitting Data at Different Baud Rates
Baud rate is crucial in serial communication. Different baud rates are
set by configuring Timer 1.
Code Example: Transmitting data at 9600 and 19200 baud.
void Serial_Init(uint16_t baud) {
SCON = 0x50; // Mode 1, 8-bit UART, REN enabled
TMOD &= 0x0F; // Clear Timer 1 settings
TMOD |= 0x20; // Timer 1 in Mode 2 (auto-reload)
TH1 = 256 - (Oscillator_Frequency / (384 * baud));
TR1 = 1; // Start Timer 1
}
void Transmit_Data(char data) {
SBUF = data; // Load data into SBUF
while(!TI); // Wait for transmission to complete
TI = 0; // Clear TI flag
}
Note: Modify OscillatorFrequencyandbaudvaluesasperrequirement.
Dr. Markkandan S Module-6 Timer and Serial port programming 41/46

Receiving Data at Different Baud Rates
Adjusting the baud rate for receiving data is similar to transmission.
The same Timer 1 settings are used.
Code Example: Receiving data with UART setup for different baud
rates.
char Receive_Data() {
while(!RI); // Wait for data to be received
RI = 0; // Clear RI flag
return SBUF; // Return the received data
}
void main() {
Serial_Init(9600); // Initialize for 9600 baud rate
char received = Receive_Data();
Serial_Init(19200); // Switch to 19200 baud rate
// ... further processing ...
}
Note: Ensure that the transmitting device matches the baud rate.
Dr. Markkandan S Module-6 Timer and Serial port programming 42/46

Timer and Serial Communication Interrupts
Using interrupts for efficient timer and serial operations.
Code Example: Implementing Timer and Serial Interrupts.
void Timer0_ISR(void) interrupt 1 {
TF0 = 0; // Clear Timer 0 overflow flag
// Timer 0 specific code...
}
void Serial_ISR(void) interrupt 4 {
if (TI) {
TI = 0; // Clear transmit interrupt flag
// Handle transmission completion
}
if (RI) {
char receivedData = SBUF; // Read received data
RI = 0; // Clear receive interrupt flag
// Handle received data
}
}
void main() {
// Timer and Serial initialization code...
EA = 1; // Enable global interrupts
ET0 = 1; // Enable Timer 0 interrupt
ES = 1; // Enable Serial interrupt
// ...
}
Note: This setup allows for efficient handling of both timer and serial
operations.
Dr. Markkandan S Module-6 Timer and Serial port programming 43/46

Timer and Serial Interrupt Code
Code Example
void Timer0_ISR(void) interrupt 1 {
TF0 = 0; // Clear overflow flag
// Timer 0 code
}
void Serial_ISR(void) interrupt 4 {
if (TI) {
TI = 0; // Clear transmission interrupt
// Handle transmission completion
}
if (RI) {
// Process received data
RI = 0; // Clear reception interrupt
}
}
Enables asynchronous, event-driven serial and timer handling through interrupts
instead of polling.
Dr. Markkandan S Module-6 Timer and Serial port programming 44/46

Advanced Serial Port Operations in 8051
Exploring advanced operations like multi-device communication and
handling different data formats.
Code Example: Handling different types of data via the serial port.
void HandleSerialData() {
char data;
if (RI) {
data = SBUF; // Read received data
RI = 0; // Clear RI flag
switch (data) {
case ’A’: // Handle ASCII character
// Process ASCII character
break;
case 0x01: // Handle specific control byte
// Process control byte
break;
// Add more cases as needed
}
}
}
Note: This function demonstrates handling different data types received
through UART.
Dr. Markkandan S Module-6 Timer and Serial port programming 45/46

Error Handling in Serial Communication
Discussing strategies for detecting and managing errors in serial data
transmission.
Techniques include parity checking, checksums, and timeout
mechanisms.
Emphasizing the importance of implementing error handling to ensure
reliable data communication.
Example: Describe a basic checksum implementation for data
integrity checks.
Note: Provide a conceptual overview rather than a specific code example,
focusing on the importance and methods of error handling in serial
communication.
Dr. Markkandan S Module-6 Timer and Serial port programming 46/46
Tags