echo '' ;

8051 Tutorials – 10hz Square wave generator using Timer

In this tutorial, you can learn how to generate a 10Hz square wave using a timer in an 8051 microcontroller. I chose the AT89S51 microcontroller (You can select any other Keil-supported microcontroller) and demonstrated it. Generating a 10Hz square wave using a timer in an 8051 microcontroller is a common task. Here’s a basic tutorial on how to do it:

 

Generating a square wave is a fundamental task in electronics, often required for various purposes such as clock signals, digital communication, and waveform testing. One common way to generate a square wave is by using an astable multivibrator circuit, which typically consists of two transistors or integrated circuits like the 555 timer.

Required software

Required components and Programmer

  • 1x AT89S51 Controller
  • 1x 4Mhz Crystal
  • 2x 22pf capacitor
  • ISP AVR USB programmer

Note

Remember to configure the microcontroller’s clock frequency appropriately and adjust the timer values accordingly. Additionally, the exact implementation might vary depending on the specific variant of the 8051 microcontroller and the assembler you are using.

Code of 8051 Generating a 10Hz Square Wave

Assembly Code

  1. Configure Timer 0: Timer 0 is an 8-bit timer in the 8051 microcontroller. You need to configure it in mode 1 (16-bit mode) for generating square waves.
  2. Calculate Timer Value: Calculate the timer value needed to generate a 10Hz square wave. Timer value can be calculated using the formula: [ TimerValue = \frac{2^{16} – (RequiredCounts)}{CrystalFrequency} ] For a 10Hz square wave:
    [ RequiredCounts = \frac{CrystalFrequency}{10} ]
  3. Load Timer Value: Load the calculated timer value into Timer 0.
  4. Generate Square Wave: Configure Timer 0 in such a way that when it overflows, it generates an interrupt. Toggle a GPIO pin inside the interrupt service routine to generate a square wave.
  5. Repeat: The timer will continuously overflow, generating a continuous square wave.
ORG 0H         ; Start of the program
MOV TMOD, #01H ; Timer 0, Mode 1 (16-bit mode)
MOV TH0, #0CH  ; Load initial value for 10Hz frequency
MOV TL0, #0B0H ; (Assuming 11.0592 MHz crystal)
SETB TR0       ; Start Timer 0

MAIN: JMP MAIN  ; Infinite loop

TIMER0_ISR:
    CLR TF0     ; Clear Timer 0 overflow flag
    CPL P1.0    ; Toggle GPIO pin P1.0
    RETI        ; Return from interrupt
  • ORG 0H sets the origin of the code to address 0.
  • TMOD is configured for Timer 0 in mode 1.
  • Initial values for Timer 0 are loaded into TH0 and TL0.
  • TR0 is set to start Timer 0.
  • The main program is an infinite loop.
  • TIMER0_ISR is the Timer 0 interrupt service routine. It toggles the GPIO pin connected to P1.0.

C code

// www.aruneworld.com/
/// Tested By   : Arun(20170227)
// Example Name : AEW_Blink_LED.lua

// Program to 10Hz_Square Wave generator at Port pin P1.0 (physical pin 1 of IC) using timer

#include <reg51.h>              // special function register declarations for 89s51        
#include<stdio.h>                // prototype declarations for I/O functions
sbit pin = P1^0;                // decleare a variable type sbit for P1.0 

void main(void)
{
    P1 = 0x00;                  // clear port
    TMOD = 0x09;                // (0x90->0b00001001) initialize timer 0 as 16 bit timer
loop:   TL0 = 0xAF;             // load valur 15535 = 3CAFh so after 
    TH0 = 0x3C;                 // 50000 counts timer 0 will be overflow
    pin = 1;                    // send high logic to P1.0
    TR0 = 1;                    // start timer
    while(TF0 == 0)  {}         // wait for first overflow for 50 ms

    TL0 = 0xAF;                 // again reload count
    TH0 = 0x3C;
    pin = 0;                    // now send 0 to P1.0
    while(TF0 == 0)  {}         // wait for 50 ms again  
goto loop;                      // continue with the loop  
}

NEXT

8051 – Introduction
8051 – Program Methods
8051 – Flash HEX into 8051
8051 – USB ISP Programmer
8051 – Simulators
8051 Interface
8051 Interface – LED
8051 Interface – LCD
8051 Interface – 7 Segment
8051 Interface – Keypad
8051 Interface – Servo
8051 Protocol Interface
8051 – UART Bit banking
8051 – I2C Bit banking (Add Soon)
8051 Tutorials
8051 – 10Khz Square Wave
Others
8051 – Interview Questions

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading