at89c51 – Deep Focus https://fazals.ddns.net/ Embrace it! Mon, 17 Aug 2020 07:30:30 +0000 en-US hourly 1 https://wordpress.org/?v=5.6.1 https://fazals.ddns.net/wp-content/uploads/2020/08/react-native-300-150x150.png at89c51 – Deep Focus https://fazals.ddns.net/ 32 32 173186184 Timers | Toggling in 8051 every 1 second using timer delay https://fazals.ddns.net/toggle-a-pin-in-8051-every-1-second-using-timers/ Tue, 19 May 2020 17:48:04 +0000 https://fazals.ddns.net/?p=1322 The 8051 or the AT89C51 is an 8-bit microcontroller. The 8051 microcontroller was designed long ago by Intel. It has a 40 pin DIP – Dual Inline Package. It has 4 kilobytes of ROM, 128 bytes of ram, two internal timers, and 5 interrupts. Namely, INT0, TF0, INT1, TF1 and RI/TI. The order of priority is same as the order they are written. It also has 1 non-maskable interrupt – RESET.

There are 2 timers available in the 8051. The timer0 and the timer1, and there are 4 modes in which both of these timers can be configured. And each of it can be configured as a Timer or as a Counter.

Difference between timers and counters

Timer in 8051 is used to provide timing or delays between two events in the controller. Whereas, a counter is used to count the number of events. The TMOD or the Timer MOD register, which is a byte addressable register, is used to configure the timer/counter.
Consider the TMOD register as shown below. The 0th and the 1st bit is used to configure the modes for timer0, and the 4th and 5th bit for timer1.
The 2nd and the 6th bit is used for configuring it in either timer mode or counter mode for timer0 and timer1 respectively. If the 2nd/6th bit is 0, its configured as timer, else it’s in counter mode.

The TMOD Register in 8051
  1. Gate: The gate bit decides whether the timer is controlled – RUN or STOP, with the internal interrupts, TR0 and TR1 or the external ones – INT0 & INT1.
  2. C/T: This bit decides whether to operate in timer mode or in counter mode
  3. M0, M1: These bits decides the mode of operation of the timers.

Timer modes in 8051

There are 4 timer modes in the 8051 microcontroller.

  • Mode 0
    Mode 0 is a 13-bit timer/counter. All the 8 bits from the upper byte are used with 5 most significant bit from the lower byte. Timer in this mode can be used by status check method. Maximum delay in this mode is 8.8 millisecond at a crystal frequency of 11.0592 MHz.
  • Mode 1
    When using mode 1, the timer/counter is configured in 16-bit mode. To be specific, it’s counts all the way from 0x0000 to 0xffff. And this mode gives a maximum delay of 71.106 millisecond, again at a crystal frequency of 11.0592 MHz.
  • Mode 2
    In mode 2 configuration, the timer/counter works in 8 bit mode but has a special feature this time. It has auto reload capacity. Meaning that, it can run continuously, all on its own, reloading the timer with the value stored in the higher byte. This mode is usually used for setting the baud rate for serial communication.
  • Mode 3
    Mode 3 is a split mode where in the timer or counter operates in the 8-bit mode. The TL0 uses the timer flag TF0, where as the TH0 uses the timer flag TF1. Yes, its timer0 using flags of both timer0 and timer1.

The concept of delays and timers

Now that you know the basic working of the timers in the 8051 microcontroller, let’s go through the concept of delays.
Delays are nothing but the cycles of operation with in the controller where the controller does nothing, literally nothing. If you know any programming language, its similar to a for or while loop that does nothing.

The most common clock frequency in the 8051 is 11.0592 MHz. The main reason to use this frequency is it compatibility with the most common baud rates in serial communication. Now, this clock frequency will give us a machine cycle time period of 1.085 microsecond. For a delay of 1 second, we need 921600 machine cycles.

We will be using timer0 in mode 1 configuration. Since it’s a 16-bit timer the maximum delay will be 71.11 millisecond. So, we need to run the timer for 14 complete cycles + 4096 machine cycles. I’ll leave the calculations to you.

So, now that we are done with the theory. Let’s have a look at the code.

The Assembly Code

				org 0000h
					mov p1, #00h
					mov tmod, #01h
loop:				cpl p1.5    ;compliment pin 1.5
					acall delay
					acall smallDelay
					sjmp loop

;the subroutine delay is to loop for 14 complete 16-bit timer cycles
;and the subroutine smallDelay is for looing for 4096 times.
;the rest of the code is self explainatory. 					
					
delay:				mov r0, #14
					mov tl0, #00h
					mov th0, #00h
					setb tr0
timerLoop:			jnb tf0, timerLoop
					clr tf0
					djnz r0, timerLoop
					clr tr0
					ret

smallDelay:			mov tl0, #00h
					mov th0, #0f0h
					setb tr0
timerLoop2:			jnb tf0, timerLoop2
					clr tr0
					clr tf0
					ret
					
					end

Download the code from here.
This code will result in toggling of pin 1.5 every second, only if operating at 11.0592 MHz. The C-equivalent code for the same program is also geven.

The embedded C Code | 8051 timer programming in C

#include<reg51.h>

sbit pin = P1^5;

void delay(){
    int i;
    TMOD = 0x01;
    for(i=0; i<1000; i++){
				TH0 = 0xFC;
				TL0 = 0x66;
				TR0 = 1;
        while(TF0 != 1){
            ;
        }
				TR0 = 0;
        TF0 = 0;
    }
}

void main(){
    while (1)
    {
        pin = ~pin;
        delay();
    }
    
}

Download this code from my Github.
That completes the part of toggling any pin with a delay of exactly 1 second.

Read more posts on 8051 from here.

FAQs

How do I change the pin that has to be toggled?

You can do that by simply changing the line cpl p1.5 or the line sbit pin = P1^5; in embedded C to the required pin. You can also modify the code to toggle an entire port instead of a single pin.

What’s the difference between a Timer and a Counter?

I suggest you read this again.

How do I change the time for toggling?

Simply change the delay function, by changing the value in the TH0 and TL0.

What are the different timer/counter modes in 8051

]]>
1322
How to connect seven segment display with 8051| Easiest method | Proteus Simulation https://fazals.ddns.net/seven-segment-display-with-8051-proteus-simulation/ Sat, 02 May 2020 10:21:25 +0000 https://fazals.ddns.net/?p=745 How to connect seven segment display with 8051| Easiest method | Proteus Simulation Read More »

]]>
In this post you will learn how to interface seven segment display with the 8051 microcontroller (AT89C51) in multiplexed method, alternatively rolling seven segment display in a simulation software called Proteus.

The basic concept behind multiplexed seven segment display is to turn on one display at a time, at a rate so fast that it seems to our eyes that all of the displays are turned on. I will be using 8 such displays and show you how to use them in the right way.

Introduction to seven segment display

As it can be seen from the picture beside that the digit 8 is made up of 7 segments which can be indivisually turned on or off to display a character. Also, make a note that it can be used to display all the numbers, but not all the letters. For instance, letters like Z, N, G, J, K, M, Q, R, and so on can’t be displayed. While letters like O, B, D cause a lot of confusion when interpreting.

The displays can be used in 2 modes or 2 configurations.
1. Common Anode (common +ve terminal)
2. Common Cathode (common ground)
We will be using the 2nd one, the common cathode or the common ground because it causes less confusions in logics.

We will be using the entire 8 bit port. So we have to connect led A to the MSB, led B to next LSB and so on up till the last one. This will make the led G connect to the last but one bit. The last bit will have to be connected to the DP, or the point.

So inorder to display the letter A, you must turn on led E, F, A, B, C, G and turn off the rest. That combination would give us 11101110 in binary that has to be sent to the required port. Like wise, to display the letter E, would have to send 10011110 in binary to the required port. This covers the basics.

The Hardware

Indeed the first thing you need to do is to set up the hardware. Connect the seven segment displays as shown in the schematic. If you have indivisual displays and not blocks of them. Just connect them in parallel and then connect them to the microcontroller.

Seven Segment Display
The 8051 μController

The assembly code

In order to use the seven segment display, we need to use the lookup table method. Lookup table method is a way to access the data based on the addresses or in programming terminology, the indexed. So we will write the table starting from location 500h on the ROM as follows.

org 500h
numbers: db 11111100b, 01100000b, 11011010b, 11110010b, 01100110b, 10110110b, 10111110b, 11100000b, 11111110b, 11110110b
_H: db 01101110b
_E: db 10011110b
_L: db 00011100b
_O: db 11111100b
_A: db 11101110b
_F: db 10001110b
_Z: db 11011010b
_C: db 10011100B
_: db 00000001b

The 1st bit from the right, in other words, the LSB, corresponds to the point (DP) led on the seven segment display. So sending the value 00000001b to the displays will turn on the point. The MSB bit corresponds to the segment A, the next bit to B, the next one to C and so on up to G.

The main program

org 0000h
mov p1, #0ffh
mov p2, #00h

Port 1 is set to turn off all the displays, and Port 2 is cleared to configure it as output port.

startOver:mov dptr, #_H
acall display1
mov dptr, #_E
acall display2
mov dptr, #_L
acall display3
mov dptr, #_L
acall display4
mov dptr, #_O
acall display5
mov dptr, #_
acall display6
mov dptr, #_
acall display7
mov dptr, #_
acall display8
sjmp startOver

the command mov dptr, #_H loads the hex value of code from the lookup table. Since we have used 8 displays in multiplexed mode, the subroutine acall display1 send the data to 1st display and turns the display ON for some time and then turns it OFF. Similarly, acall display2 sends the data to 2nd display, turns it ON for some time and then turns it OFF. The rest of the commands similar to acall display* send the data to corresponding display, turns it ON for some time and then turns it OFF again.

Since this process is repeated at a very high rate, our eyes perceives them as if all the displays are turned ON simultainously.

acall routine:clr a
movc a, @a+dptr
mov p2, a
acall lessDelay                   ;CAN BE OMITTED
ret

display1:clr p1.7
acall routine
setb p1.7
ret

display2:clr p1.6
acall routine
setb p1.6
ret

display3:clr p1.5
acall routine
setb p1.5
ret

display4:clr p1.4
acall routine
setb p1.4
ret

display5:clr p1.3
acall routine
setb p1.3
ret

display6:clr p1.2
acall routine
setb p1.2
ret

display7:clr p1.1
acall routine
setb p1.1
ret

display8:clr p1.0
acall routine
setb p1.0
ret

Now the delay function is as follows. You can omit the delay part if you want

lessDelay:mov r0, #0fh
loop5:mov r1, #127
loop4:djnz r1, loop4
djnz r0, loop5
ret

Download the assembly language codes, .asm file as well as the Proteus file from here.

You can customize the code and workaround to have your content displayed.

Visit my github github icon page for more codes like this.

Intrested in creating a website for free ? Or setting up your own Blynk server for IoT ?

Care to share.😄

]]>
745