0% found this document useful (0 votes)
12 views14 pages

8051 Timer and Interrupt Programming Guide

Uploaded by

14gunarajan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

8051 Timer and Interrupt Programming Guide

Uploaded by

14gunarajan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.8051 alp to generate 4 KHz square wave on pin 3 of port 1 using timer 0 in auto reload mode.

Explanation:

1. Frequency Calculation:

o Timer 0 operates in Mode 2 (8-bit auto-reload).

o 8051 runs on a 12 MHz clock (standard).

o Timer frequency = 12 MHz / 12 = 1 MHz.

o Time per timer tick = 1 µs.

o Period of 4 kHz wave = 1 / 4000 = 250 µs.

o Half period (to toggle pin) = 250 µs / 2 = 125 µs.

o Timer counts down from 256 - (125) = 131 (0x83 in HEX).

2. Program Workflow:

o Load TH0 = 0x83 (auto-reload value).

o Toggle P1.3 on every Timer 0 overflow.

o Repeat to maintain a square wave.

8051 Assembly Code (Keil / ASM)

assembly

CopyEdit

ORG 0000H ; Origin address

MOV TMOD, #02H ; Timer 0 in Mode 2 (8-bit auto-reload)

MOV TH0, #83H ; Load reload value for 4kHz wave

MOV TL0, #83H ; Load initial count value

SETB TR0 ; Start Timer 0

MAIN_LOOP:

SJMP MAIN_LOOP ; Infinite loop


ORG 000BH ; Timer 0 Interrupt Vector

CLR TF0 ; Clear Timer 0 Overflow Flag

CPL P1.3 ; Toggle P1.3 (Pin 3 of Port 1)

RETI ; Return from interrupt

END

Explanation of Code:

 TMOD = 0x02 → Timer 0 in Mode 2 (8-bit auto-reload).

 TH0 = 0x83 → Timer reload value for 4 kHz wave.

 TL0 = 0x83 → Initial count value.

 TR0 = 1 → Start Timer 0.

 Timer 0 ISR at ORG 000BH:

o Clears overflow flag.

o Toggles P1.3 to generate a square wave.

o Returns from the interrupt.

C Program (Keil C)#include <reg51.h>

sbit square_wave = P1^3; // Define P1.3 as output pin

void Timer0_ISR(void) interrupt 1 {

TF0 = 0; // Clear Timer 0 overflow flag

square_wave = ~square_wave; // Toggle P1.3

void main() {

TMOD = 0x02; // Timer 0 in Mode 2 (8-bit auto-reload)

TH0 = 0x83; // Load reload value for 4 kHz

TL0 = 0x83; // Load initial value

TR0 = 1; // Start Timer 0

ET0 = 1; // Enable Timer 0 Interrupt


EA = 1; // Enable global interrupt

while(1); // Infinite loop

Explanation of Code:

1. Pin Setup:

o sbit square_wave = P1^3; → Assigns P1.3 as the output pin.

2. Timer 0 Setup:

o TMOD = 0x02; → Timer 0 in Mode 2 (8-bit auto-reload).

o TH0 = 0x83; → Sets the reload value.

o TL0 = 0x83; → Initializes the timer count.

o TR0 = 1; → Starts Timer 0.

3. Interrupt Handling:

o ET0 = 1; → Enables Timer 0 interrupt.

o EA = 1; → Enables global interrupts.

o void Timer0_ISR(void) interrupt 1 → ISR toggles P1.3 on each timer overflow.

Simulation & Testing:

 Use Keil uVision or Proteus to simulate.

 Check P1.3 (Pin 3 of Port 1) for a 4 kHz square wave.

[Link] a ALP to monitor switch and perform the following using external hardware interrupt

I) if SW 1=0,flash port 0 with ((FF to 00)

II) is SW 2 =0,flash port 2 with ( 55 to AA)

Problem Statement

 SW1 (Connected to INT0, P3.2) = 0 → Flash Port 0 (P0) from FF to 00.

 SW2 (Connected to INT1, P3.3) = 0 → Flash Port 2 (P2) from 55 to AA.

 Use external hardware interrupts (INT0 and INT1) to detect switch presses.
Statements and Functionality

1. Interrupt Vector Table Setup:

o ORG 0000H: Program starts at memory address 0000H.

o SJMP MAIN: Jumps to the main program, avoiding execution of interrupt vectors initially.

o ORG 0003H: Defines the Interrupt Service Routine (ISR) for INT0 (SW1).

o CALL FLASH_P0: Calls the function to flash Port 0 (P0).

o RETI: Returns from the interrupt.

o ORG 0013H: Defines the Interrupt Service Routine (ISR) for INT1 (SW2).

o CALL FLASH_P2: Calls the function to flash Port 2 (P2).

o RETI: Returns from the interrupt.

2. Main Program Execution (Initialization & Infinite Loop):

o MOV IE, #85H: Enables external interrupts EX0 (INT0) and EX1 (INT1).

o SETB IT0: Configures INT0 for Edge-Triggered Mode.

o SETB IT1: Configures INT1 for Edge-Triggered Mode.

o MOV P0, #00H: Initializes Port 0 (P0) to 0.

o MOV P2, #00H: Initializes Port 2 (P2) to 0.

o SJMP LOOP: Infinite loop to keep the program running.

3. Subroutine: Flash Port 0 (SW1 Pressed)

o FLASH_P0: Flashing function for Port 0.

o MOV R0, #10: Sets loop counter to 10 iterations.

o MOV P0, #0FFH: Outputs FFH to Port 0.

o ACALL DELAY: Calls the DELAY subroutine.

o MOV P0, #00H: Outputs 00H to Port 0.

o ACALL DELAY: Calls the DELAY subroutine.

o DJNZ R0, FLASH_LOOP1: Repeats 10 times.

o RET: Returns from subroutine.

4. Subroutine: Flash Port 2 (SW2 Pressed)

o FLASH_P2: Flashing function for Port 2.


o MOV R1, #10: Sets loop counter to 10 iterations.

o MOV P2, #55H: Outputs 55H to Port 2.

o ACALL DELAY: Calls the DELAY subroutine.

o MOV P2, #0AAH: Outputs AAH to Port 2.

o ACALL DELAY: Calls the DELAY subroutine.

o DJNZ R1, FLASH_LOOP2: Repeats 10 times.

o RET: Returns from subroutine.

5. Delay Subroutine

o DELAY: Generates a time delay.

o MOV R2, #255: Loads R2 with 255.

o MOV R3, #255: Loads R3 with 255.

o DJNZ R3, DELAY_LOOP: Decrements R3 and loops until R3 = 0.

o DJNZ R2, DELAY_LOOP: Decrements R2 and loops until R2 = 0.

o RET: Returns from subroutine.

Full Assembly Language Program (ALP)

assembly

CopyEdit

ORG 0000H ; Start of Program

SJMP MAIN ; Jump to Main Program

ORG 0003H ; Interrupt Vector for INT0 (SW1)

CALL FLASH_P0 ; Call routine to flash P0

RETI ; Return from interrupt

ORG 0013H ; Interrupt Vector for INT1 (SW2)

CALL FLASH_P2 ; Call routine to flash P2

RETI ; Return from interrupt


MAIN:

MOV IE, #85H ; Enable External Interrupts (EX0 & EX1)

SETB IT0 ; Configure INT0 for Edge Triggered Mode

SETB IT1 ; Configure INT1 for Edge Triggered Mode

MOV P0, #00H ; Initialize Port 0

MOV P2, #00H ; Initialize Port 2

LOOP:

SJMP LOOP ; Infinite Loop, waiting for Interrupts

FLASH_P0:

MOV R0, #10 ; Loop Counter

FLASH_LOOP1:

MOV P0, #0FFH ; Output FF

ACALL DELAY ; Call Delay

MOV P0, #00H ; Output 00

ACALL DELAY ; Call Delay

DJNZ R0, FLASH_LOOP1 ; Repeat 10 times

RET

FLASH_P2:

MOV R1, #10 ; Loop Counter

FLASH_LOOP2:

MOV P2, #55H ; Output 55

ACALL DELAY ; Call Delay

MOV P2, #0AAH ; Output AA

ACALL DELAY ; Call Delay

DJNZ R1, FLASH_LOOP2 ; Repeat 10 times


RET

DELAY:

MOV R2, #255

MOV R3, #255

DELAY_LOOP:

DJNZ R3, DELAY_LOOP

DJNZ R2, DELAY_LOOP

RET

END

Conclusion

 When SW1 (INT0) is pressed, P0 flashes (FF to 00).

 When SW2 (INT1) is pressed, P2 flashes (55 to AA).

 Uses External Interrupts for event-driven execution.

 Efficient and power-saving, as it only executes on interrupts.

[Link] how traffic light control will be interfaced with 8051 with diagram and ALP.

Introduction

Traffic light control systems are essential for managing traffic flow efficiently. Using an 8051
microcontroller, we can implement a simple traffic light system that controls Red, Yellow, and Green
LEDs for two roads.

Interfacing Traffic Lights with 8051

The traffic light system consists of:

1. Microcontroller (8051)
2. LEDs for Red, Yellow, and Green lights

3. Resistors (330Ω)

4. Power Supply (5V DC)

Circuit Diagram

Below is a simple diagram showing how the 8051 is interfaced with traffic lights:

Connections

 Port 2 (P2.0 - P2.5) is used to control Red, Yellow, and Green LEDs for two roads.

 Each LED is connected in series with a 330Ω resistor to limit current.

 The microcontroller outputs HIGH (1) to turn ON an LED and LOW (0) to turn it OFF.

Working Principle

1. Road 1 gets a Green light while Road 2 gets a Red light (vehicles move on Road 1).
2. Yellow light turns ON for a few seconds before switching.

3. Road 2 gets a Green light, while Road 1 gets a Red light.

4. This cycle repeats continuously.

Assembly Language Program (ALP) for Traffic Light Control

ORG 0000H ;

Start of the program

MAIN:

MOV P2, #00000101B ; Road 1 Green, Road 2 Red

ACALL DELAY ; Wait for some time

MOV P2, #00000011B ; Road 1 Yellow, Road 2 Red

ACALL DELAY ; Short delay

MOV P2, #00001000B ; Road 1 Red, Road 2 Green

ACALL DELAY ; Wait for some time

MOV P2, #00001100B ; Road 1 Red, Road 2 Yellow

ACALL DELAY ; Short delay

SJMP MAIN ; Repeat the cycle

; Delay subroutine

DELAY:

MOV R7, #255

DELAY_LOOP1:

MOV R6, #255

DELAY_LOOP2:

DJNZ R6, DELAY_LOOP2

DJNZ R7, DELAY_LOOP1

RET
END

Explanation of Code

1. Traffic Light Cycle

o Road 1 Green (P2 = 00000101B)

o Road 1 Yellow (P2 = 00000011B)

o Road 2 Green (P2 = 00001000B)

o Road 2 Yellow (P2 = 00001100B)

2. Delays

o Long delays (~5 seconds) for Green and Red lights.

o Short delays (~2 seconds) for Yellow lights.

3. Continuous Loop

o The program continuously loops to maintain the traffic light cycle.

Conclusion

 This simple traffic light system allows alternate control of two roads.

 8051 controls the LEDs to simulate real-world traffic signals.

 Efficient and easy to implement in embedded systems.

[Link] how stepper motor is interfaced with 8051 with diagram and write ALP to control its
speed and direction:

Introduction

A stepper motor is a brushless DC motor that rotates in small steps. It is widely used in robotics,
CNC machines, and automation due to its precise control.

The 8051 microcontroller controls the stepper motor by sending signals to a motor driver
(ULN2003), which amplifies the current and voltage required to drive the motor.
Interfacing Stepper Motor with 8051

Components Required:

1. 8051 Microcontroller

2. Stepper Motor (Unipolar or Bipolar)

3. ULN2003 Motor Driver IC (Used for driving the motor)

4. 12V Power Supply

5. Connecting Wires

Circuit Diagram:

Here is the circuit diagram for interfacing a stepper motor with the 8051 microcontroller using the
ULN2003 motor driver IC. Let me know if you need any modifications or explanations! 😊

Working Principle

1. 8051 generates control pulses on Port 2 (P2.0 to P2.3).

2. These pulses are fed into the ULN2003 motor driver, which amplifies the signal.

3. The ULN2003 energizes the stepper motor coils in a sequence.

4. The stepper motor rotates clockwise or counterclockwise based on the sequence.

5. Speed is controlled by adjusting the delay between steps.


Assembly Language Program (ALP) to Control Stepper Motor Speed & Direction

assembly

ORG 0000H

MAIN:

MOV P2, #00000011B ; Step 1

ACALL DELAY

MOV P2, #00000110B ; Step 2

ACALL DELAY

MOV P2, #00001100B ; Step 3

ACALL DELAY

MOV P2, #00001001B ; Step 4

ACALL DELAY

SJMP MAIN ; Repeat continuously

; Delay Subroutine for Speed Control

DELAY:

MOV R7, #255

D1: MOV R6, #255

D2: DJNZ R6, D2

DJNZ R7, D1

RET

END

Explanation of Code

 The stepper motor moves in a 4-step sequence (Full-Step Mode).

 P2.0 to P2.3 are used to control the motor coils.


 The DELAY subroutine controls speed (increase/decrease values in R6, R7 to adjust speed).

 To reverse direction, swap the step sequence.

Conclusion

 Precise control of speed & direction can be achieved using an 8051 microcontroller.

 The ULN2003 motor driver protects the microcontroller and provides necessary current.

 By modifying the delay, we can increase or decrease the speed.

C Program for Stepper Motor Control

#include <reg51.h> // Include 8051 header file

#define STEPPER P2 // Stepper motor connected to Port 2

void delay(unsigned int); // Function prototype for dela

void main() {

while(1) {

// Full-Step Sequence (Clockwise)

STEPPER = 0x03; // Step 1

delay(500);

STEPPER = 0x06; // Step 2

delay(500);

STEPPER = 0x0C; // Step 3

delay(500);

STEPPER = 0x09; // Step 4

delay(500);

// Uncomment below to change direction (Counterclockwise)

/*

STEPPER = 0x09; // Step 1


delay(500);

STEPPER = 0x0C; // Step 2

delay(500);

STEPPER = 0x06; // Step 3

delay(500);

STEPPER = 0x03; // Step 4

delay(500);

*/

// Delay Function (Controls Speed)

void delay(unsigned int ms) {

unsigned int i, j;

for(i = 0; i < ms; i++)

for(j = 0; j < 1275; j++); // Roughly 1 ms delay

Explanation

1. Stepper Motor is connected to Port 2 (P2.0 - P2.3).

2. The program runs an infinite loop, continuously energizing the coils.

3. Clockwise rotation follows the sequence {0x03, 0x06, 0x0C, 0x09}.

4. Counterclockwise rotation (uncomment the second sequence).

5. Speed control is done by adjusting the delay function.

You might also like