looking help on 8051 timer interrupt and switch input

Parth786

Joined Jun 19, 2017
642
Hi all
I need help to understand 8051 timer interrupt. I have visited on some links and I find below code for 8051 timer interrupt. But I don't understand logically how does 8051 timer interrupt work. I am trying understand below code
C:
#include<reg51.h>
sbit LED = P1^0; //LED connected to P1.0

Void main()
{
TMOD = 0x01;                  // mode1 of Timer0
TH0 = 0xFC;                   // initial values loaded to timer
TL0 = 0x66;
IE = 0x82;                    // enable interrupt
TR0 = 1;                      //start timer
while(1);                     // do nothing
}

void timer(void) interrupt 1            //interrupt no. 1 for Timer 0

{
led=~led;                    //toggle LED on interrupt
TH0=0xFC;                   // initial values loaded to timer
TL0=0x66;
}
My attempt : at the beginning of main section, initially LED will be OFF, Time 0 and Timer 0 Interrupt will be SET and we need to Enable global interrupts
C:
main( )
{
LED = off               //  Set initial state (off) for LED
TR0 = 1;               //  Set the Timer 0
ET0 = 1;               //  Set Timer 0 Interrupt
EA  = 1;               //    Enable global interrupts on..
}
void timer (void) __interrupt (1)
{

}
How does 8051 timer interrupt work .logically and programmatically?

Last edited:

Papabravo

Joined Feb 24, 2006
12,878
1. While the Timer is counting your main() function executes normally in some kind of "do forever" loop which you have eliminated in your code fragment.
2. When the timer overflows from 0xFFFF to 0x0000, you should imagine a "phantom" call instruction inserted between the two instructions where the timer overflows to 0x0000
3. The "phantom" caal instruction takes you to the timer() interrupt function with interrupts disabled.
4. When the timer() function is finished it return to the instruction after the one that preceeded the "pahntom" call, with interrupts enabled again.
One cardinal rule of interrupt service routines is do not modify any registers or memory in an interrupt service routine and any code that runs at a non-interrupt level. You can modify stuff in one place or the other place, but not both places.

JohnInTX

Joined Jun 26, 2012
3,976
The code is incomplete.

The timer is not initialized as in the example and there is no interrupt service routine.
There is also no 'while (1)' loop in main so the program not work at all.

Again, it is pointless to post half written adaptations of someone else's code and expect that we can fix that.

It's OK to look at other's code if your point is to study each and every function in it until you understand how it works (or why it doesn't). That is a great learning tool. But what I see here, as in your other threads, is that you are trying to take some code you haven't invested the time to understand, stir it around a bit then ask for help when it doesn't work. That isn't a good way to learn programming.

I am not scolding you nor trying to discourage other members from helping you. What I am saying is that this thread is like your others and those did not achieve success, despite receiving lots of help and advice on how to proceed with design from senior members. But that did not seem to change anything, including your approach to learning this stuff. If you expect this time to be any different, well.. I hope so.

Good luck and I hope someone can figure out a way to help get you there.

Last edited:

Papabravo

Joined Feb 24, 2006
12,878
There is also no 'while (1)' loop in main so the program work at all.
Yup, but there is one in the program under study. There is not one in the OP's. It's probably missing because he did not understand the purpose.

JohnInTX

Joined Jun 26, 2012
3,976
Yup, but there is one in the program under study. There is not one in the OP's. It's probably missing because he did not understand the purpose.
Indeed but not because it hasn't been a feature of several previous threads by TS. I was adding to my advice to TS when you posted.

Papabravo

Joined Feb 24, 2006
12,878
Indeed but not because it hasn't been a feature of several previous threads by TS. I was adding to my advice to TS when you posted.
I did think it was odd that he said: "I'm studying this...", and "this is what I've come up with" and they don't really resemble each other.

JohnInTX

Joined Jun 26, 2012
3,976
I did think it was odd that he said: "I'm studying this...", and "this is what I've come up with" and they don't really resemble each other.
I don't want to be beating on someone who is trying to learn and certainly not in the threads but @Parth786 eventually needs to accept that his approach to learning this stuff is faulty. If we left it at that, we would not be helping. But an inspection of his other threads shows that the pattern that isn't working is his and until there is some change in that - he's gotten lots of advice in that regard - he will not be successful. Simple as that. I sincerely hope that he figures it out or that someone new can lead him to that 'aha' moment.

Take a shot.

Papabravo

Joined Feb 24, 2006
12,878
I don't want to be beating on someone who is trying to learn and certainly not in the threads but @Parth786 eventually needs to accept that his approach to learning this stuff is faulty. If we left it at that, we would not be helping. But an inspection of his other threads shows that the pattern that isn't working is his and until there is some change in that - he's gotten lots of advice in that regard - he will not be successful. Simple as that. I sincerely hope that he figures it out or that someone new can lead him to that 'aha' moment.

Take a shot.
I didn't even notice. I thought I could recognize and avoid getting entangled, but this one caught me not looking. If nothing else the questions are seeming somehow -- more reasonable.

shteii01

Joined Feb 19, 2010
4,647
They are on my ignore list so I don't even see their threads.

Parth786

Joined Jun 19, 2017
642
The code is incomplete.

Again, it is pointless to post half written adaptations of someone else's code and expect that we can fix that.

It's OK to look at other's code if your point is to study each and every function in it until you understand how it works (or why it doesn't). That is a great learning tool. But what I see here, as in your other threads, is that you are trying to take some code you haven't invested the time to understand, stir it around a bit then ask for help when it doesn't work. That isn't a good way to learn programming.
.
do you think there is any difference between both programs. have you read the comments in both codes. have you noticed that I have initialize LED in main function. that is not initialize in his program
I was trying to understand someone others code. I said in my post that I don't understand logic. My code was incomplete because I wrote the statement that I understand, I left statement which I don't understand. I was not asking for completing program. I was asking help to understand logic. and I think without having basic understanding its impossible to write our own program.

I don't want to be beating on someone who is trying to learn and certainly not in the threads but @Parth786 eventually needs to accept that his approach to learning this stuff is faulty. If we left it at that, we would not be helping. But an inspection of his other threads shows that the pattern that isn't working is his and until there is some change in that - he's gotten lots of advice in that regard - he will not be successful. Simple as that. I sincerely hope that he figures it out or that someone new can lead him to that 'aha' moment.

Take a shot.
I know, you have helped me a lot in my previous thread's. In some threads like keypad interfacing I couldn't get success but still I am working on it. I will post the program with proper understanding. my mistake was I started with tough tasks. so I am just trying to understand easy tasks

I don't want to hurt you, you are doing great job. if you think that my approach is wrong, I will change my learning styles but I don't understand what's wrong in current approach.

Last edited:

Parth786

Joined Jun 19, 2017
642
8051 has timer0 16 bit wide
TH0= D0.....D7
TL0= D8.....D15
TH0 = 0xvalue;
TL0 = 0xvalue;

what is use of below two lines
TH0 = 0xFC;
TL0 = 0x66;

JohnInTX

Joined Jun 26, 2012
3,976
I said in my post that I don't understand logic. My code was incomplete because I wrote the statement that I understand, I left statement which I don't understand. I was not asking for completing program. I was asking help to understand logic. and I think without having basic understanding its impossible to write our own program...
You have identified your main problem in the first text I highlighted - you don't understand logic. The advice you've been given in your other threads told you how to begin to understand logic (flow charts, step-by-step inspection, tracing with your finger etc.) until you have solved the problem on paper AND understand that solution. Several members offered variations on that basic truth but they all say the same thing i.e. if you can't understand it and solve it on paper and see how the solution to your problem unfolds step-by-step (which is how the computer runs), then you have no hope whatsoever of getting something running on a chip. None. Ever.

The second text I highlighted, as well as your post itself plainly says that you reject that advice. Instead of drilling down into the problem breaking it into smaller and smaller parts that you could manage, you instead ran off and got some code off the internet and tried to monkey with it in hopes that it would work somehow. That is what's wrong with your current approach. You can't do that. It doesn't work. That's why I said your approach is faulty and you would continue to fail until you changed the approach. This thread confirms that you still think you can get away with some random effort and have success.

Look, at any given time on this forum you have access to members with literally centuries of combined experience both academically and in the real world - free for the asking. Many of these members, and I personally account for about 40 years of those centuries, know how to do this stuff and have told you, step by step, literally, how to do this stuff and have told you over and over. But you just won't do it. Instead, you open yet another thread in some vain hope that somewhere along the line, the rules have changed. After awhile, even the most generous members will get frustrated and just drop out. The lack of participation in your recent threads is an indicator of that.

That's really about all I have to say. I am not trying to be a hard-case or a scold, I just don't have any idea how to proceed from here. Maybe someone else has.

EDIT: this was posted in reply to #10.

Last edited:

cmartinez

Joined Jan 17, 2007
6,758
8051 has timer0 16 bit wide
TH0= D0.....D7
TL0= D8.....D15
TH0 = 0xvalue;
TL0 = 0xvalue;

what is use of below two lines
TH0 = 0xFC;
TL0 = 0x66;
Parth, where are you located? What is your native language?

Parth786

Joined Jun 19, 2017
642
Parth, where are you located? What is your native language?
I am from india. my native language is hindi

cmartinez

Joined Jan 17, 2007
6,758
Sometimes things are lost in translation and it's difficult to understand when one studies things in a foreign language. Especially when reading manuals and datasheets.

I don't understand logically how does 8051 timer interrupt work.
I'm under the impression that you do not understand the concept of "Timer overflow" which is the principle under which an 8051 timer works.

Since you're using 16 bit mode for your timer application, it means that you can specify a timer lapse of up to 2^16 - 1 = 65,535 cycles before an interrupt is activated. This 16 bit number is defined by two 8 bit bytes named TL0 and TH0. TL0 is timer 0 low byte, and TH0 is timer 0 high byte. You could say that the 16 bit timer startup value is equal to 256*TH0 + TL0

Let's say you want to specify a timer startup value of 10,000. In this case, TH0 would be equal to the integer result of 10,000/256 = 39 (or 27H) and TL0 would be 10,000 - 39*256 = 16 (or 10H)

Finally, a very important thing to understand is that these are the startup values of the timer. That is, the timer will start counting up from that point until the maximum allowed value of 65,535 is reached. After that, the timer counter will overflow to 0 and that's exactly when the interrupt routine will be called. The higher the value defined by TL0 and TH0, the shorter the time elapsed before the interrupt routine is called.

I've attached a document by Atmel regarding the architecture of the 8051 MCU. I suggest you read pages 81 to 83 if you want to learn more about the working principle of timers.

Has this explanation been helpful to you?

Attachments

• 2 MB Views: 3
Last edited:

Parth786

Joined Jun 19, 2017
642
Sometimes things are lost in translation and it's difficult to understand when one studies things in a foreign language. Especially when reading manuals and datasheets.

I'm under the impression that you do not understand the concept of "Timer overflow" which is the principle under which an 8051 timer works.
Has this explanation been helpful to you?
Thanks for your explanations. you have pointed right thing. sometime its happen with me. that's reason I couldn't write long explanation. sometime I couldn't figure out what they are saying to me but I am working on it.
You have identified your main problem in the first text I highlighted - you don't understand logic. The advice you've been given in your other threads told you how to begin to understand logic (flow charts, step-by-step inspection, tracing with your finger etc.) until you have solved the problem on paper AND understand that solution. Several members offered variations on that basic truth but they all say the same thing i.e. if you can't understand it and solve it on paper and see how the solution to your problem unfolds step-by-step (which is how the computer runs), then you have no hope whatsoever of getting something running on a chip. None. Ever.

The second text I highlighted, as well as your post itself plainly says that you reject that advice. Instead of drilling down into the problem breaking it into smaller and smaller parts that you could manage, you instead ran off and got some code off the internet and tried to monkey with it in hopes that it would work somehow. That is what's wrong with your current approach. You can't do that. It doesn't work. That's why I said your approach is faulty and you would continue to fail until you changed the approach. This thread confirms that you still think you can get away with some random effort and have success.
.
I understood what you are saying. I have to make flow chart before writing program and now I will remember it. many time I have tried to complete flow chart but I couldn't complete it. whenever I create new thread I have fear in mind I think what will happen.. what will other say
I am struggling with basic due to lack of my hardware knowledge. I am just trying to understand how does timer interrupt work, and after that I try to make flow chart. any way as my understanding. I made below flow chart

if do you find any mistake let me know I will try to solve one by one

JohnInTX

Joined Jun 26, 2012
3,976
OK, I'll bite.
First, look at the boxes from LED Off through Start Timer. That code initializes the system. Now look at what happens after Start Timer.
• Where does the program go?
• Do you think it is a good idea to initialize the system over and over?
• How would you fix that? i.e. Where should the edge (arrow) that comes out of Start Timer go? It can't just end there because the processor keeps executing code, it never stops.
• Hint: after initializing, does main have anything else to do?
Next, look at the edge (arrow) into your interrupt routine.
• If you are interrupting when the timer runs out, what makes you think it would happen right there? The very nature of interrupts is that they can happen anywhere. How would you represent that?
• Where would the arrows into and from the interrupt routine go - if shown at all?
Those are problems that I see by tracing the flow chart with my finger (eyes, actually). Can you see the problems in what you have drawn?

The good news is that the init steps make sense as does what you do in the interrupt routine. It is the program flow that needs work.

Chew on that, fix the flow chart and post the results. Don't guess at it. Trace and modify it until it makes sense.

JohnInTX

Joined Jun 26, 2012
3,976
And note that most of my advice has nothing to do with understanding interrupts. That will have to wait while we figure out how to design a program flow.

Parth786

Joined Jun 19, 2017
642
And note that most of my advice has nothing to do with understanding interrupts. That will have to wait while we figure out how to design a program flow.
after spending some time in reading about timer interrupt. I made this flow chart.I don't see any error in below flow chart. so again if you find any error please let me know?

JohnInTX

Joined Jun 26, 2012
3,976
Not quite.
The interrupt routine must be COMPLETELY SEPARATE from the main routine. There are no edges (arrows) between your main routine and the interrupt routine. . That connection is done by the hardware, Your first interrupt routine was essentially correct.