Saturday, June 23, 2012

Timer Programming in 8051 Micro Controller with Keil

Learn More about Interrupt, timer and Serial Programming in 8051 Micro Controller : Click here
      The 8051 has two timers/counters, they can be used either as
     Timers to generate a time delay
     or as Event counters to count events happening outside the microcontroller
      Both Timer 0 and Timer 1 are 16 bits wide.
      Since 8051 has an 8-bit architecture, each 16-bits timer is accessed as two separate registers of low byte and high byte.
     The low byte register is called TL0/TL1 and
     The high byte register is called TH0/TH1
The 8051 has two timers/counters, they can be used either as 

     Timers to generate a time delay
     or as Event counters to count events happening outside the microcontroller.
Both Timer 0 and Timer 1 are 16 bits wide.

      Since 8051 has an 8-bit architecture, each 16-bits timer is accessed as two separate registers of low byte and high byte.
     The low byte register is called TL0/TL1 and
     The high byte register is called TH0/TH1
Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the   various timer operation modes.

                             8051 Micro Controller Timer mode 1 programming

      The following are the characteristics and operations of mode1:
     It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH to be loaded into the timer’s register TL and TH
     After TH and TL are loaded with a 16-bit initial value, the timer must be started
     This is done by setting high TR0 for timer0 and  TR1 for timer1
     After the timer is started, it starts to count up
     It counts up until it reaches its limit of FFFFH
     When it rolls over from FFFFH to 0000, it sets high a flag bit called TF (timer flag).Each timer has its own timer flag: TF0 for timer 0, and TF1 for timer 1.
     When this timer flag is raised, one option would be to stop the timer.
     After the timer reaches its limit and rolls over, in order to repeat the process TH and TL must be reloaded with the original value, and TF must be reloaded to 0.


                             8051 Micro Controller Delay generation process

1.      Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which timer mode (0 or 1) is selected.
2.      Load registers TL and TH with initial count value.
3.      Start the timer.
4.      Keep monitoring the timer flag (TF). if it is raised,
5.      ƒStop the timer.
6.      Clear the TF flag for the next round
7.      Go back to Step 2 to load TH and TL again.

How to calculate values to be loaded into TH and TL 

Assume XTAL = 11.0592 MHz, we can use the following steps for finding the TH and TL registers’ values,
1.      Divide the desired time delay by 1.085 us.
2.      Calculate 65536 – n, where n is the decimal value we got in Step1.
3.      Convert the result of Step2 to hex, where yyxx is the initial hex value to be loaded into the timer’s register
4.      Set TL = xx and TH = yy.

Some Examples of 8051 Micro Controller Programming in Keil Micro Vision 4

To create a program in keil follow the following steps:

a.      Get the Keil MicroVision 4 from keil.com and install it on your computer.
b.      Click the project tab and create a new project.
c.      Select the microcontroller.
d.      Microcontroller here I will be using is AT 89C51
e.      Then Click create.
f.       From the Flash tab select Configure Flash Tools.
g.      Inside the new window, inside the Target Tab Check the Use the onchip Rom Option.
h.      Select Code Rom Size: less than 2k memory for Small programs.
i.        Give the Crystal Frequency 11.0592 mhz.
j.        Inside the Output Tab Check the Create a hex file to create a hex file.
k.      Add a C code file (.c file ) to your project by Rt Clicking the project.


/*
Write a program to Toggle (on/Off) bits of Port 2(P2) continuously forever with delay.
*/

# include <regx51.h>
 void main()
 {
            int i;
            P2=0x00;
            while(1)
            {
                        P2= 0x00;
                        for(i=0;i<15000;i++);
                        P2=0xFF;
                        for(i=0;i<15000;i++);
            }         
 }


/*
Write a program to on/off leds connected in port1 using Push Switches Sw1 (used as on switch) connected to p2.0 pin  and SW2 used as off switch connected to p2.1
 */
 # include <regx51.h>
 void main()
 {
            int i;
            P1=0x00;
            P2=0x00;
            while(1)
            {
                        if (P2_1==1 && P2_0==0)
                                    P1=0xFF;
                        else
                                    P1=0x00;
                        for(i=0;i<10000;i++);
            }
 }



/*
Write a program to display count 0 to 9 in 7-Segment display with the timing interval of 1 sec. Use Timer 0, 16 bit mode to generate the delay. 7 Segment display is connected to P1.
*/

#include <regx51.h>
void delay1ms(unsigned int i)
{                        int c=0;
            while(c<i)
            {
                          TMOD=0x01;
                          TH0=0xFC;
                          TL0=0x66;
                          TR0= 1;
                          while(!TF0);
                                    TR0=0;
                                    TF0=0;
                                     c++;
            }
}
void main()
{
            P1=0x00;
            while(1)
            {
                        P1=0xC0;
                        delay1ms(1000);
                        P1=0xF9;
                        delay1ms(1000);
                        P1=0xA4;
                        delay1ms(1000);
                        P1=0xB0;
                        delay1ms(1000);
                        P1=0x99;
                        delay1ms(1000);
                        P1=0x92;
                        delay1ms(1000);
                        P1=0x82;
                        delay1ms(1000);
                        P1=0xF8;
                        delay1ms(1000);
                        P1=0x80;
                        delay1ms(1000);
                        P1=0x98;
                        delay1ms(1000);
            }
}


4 comments:

  1. The Intel 8051 microcontrollers is one of the most popular general purpose microcontrollers in use today. The success of the Intel 8051 spawned a number of clones which are collectively referred to as the MCS-51 family of microcontrollers, which includes chips from vendors such as Atmel, Philips, Infineon.

    ReplyDelete
  2. Thank for the code! its really helpful. very clean and neat explanation..

    ReplyDelete
  3. Very nice post. Learn from the adaptable online videos, revision lectures and course materials on Computer Programming.

    ReplyDelete
  4. Sands Casino | $5000 Free Chip in Review & Official Review
    After winning the 1xbet Jackpot $15 Million jackpot at Sands Casino in Las Vegas in 샌즈카지노 December 2020, JackpotCity kadangpintar is giving you $500 no deposit bonus and

    ReplyDelete