looking help on 8051 timer interrupt and switch input

Thread Starter

Parth786

Joined Jun 19, 2017
642
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.
You mean like this
upload_2017-9-10_1-38-5.png
Note: I Forgot to initialize LED at top
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
Your interrupt instructions enable/disable are inverted.
That's one problem.

Next, delete that INTERRUPT box and lines between the interrupt code and the main code. They run separately.
In main, change 'check interrupt ' to 'do nothing'. The interrupt happens automatically when the timer overflows. You don't check it.
Get rid of that END. Why?

Don't change anything else for now.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
That's one problem.

Next, delete that INTERRUPT box and lines between the interrupt code and the main code. They run separately.
In main, change 'check interrupt ' to 'do nothing'. The interrupt happens automatically when the timer overflows. You don't check it.
Get rid of that END. Why?

Don't change anything else for now.
upload_2017-9-10_5-45-57.png
 

JohnInTX

Joined Jun 26, 2012
4,787
Much better... strictly speaking, you're almost there
It is better, isn't it?
Not to keep beating on it but at the end of main, how does it ever get to 'Exit'?

Besides that, can you think of anything else that has to done at this conceptual level before moving on to more detail?
 

cmartinez

Joined Jan 17, 2007
8,253
It is better, isn't it?
Not to keep beating on it but at the end of main, how does it ever get to 'Exit'?

Besides that, can you think of anything else that has to done at this conceptual level before moving on to more detail?
I would've started the timer after enabling the interrupts. And I would've stopped the timer upon entering the interrupt routine, and then reset it and restart it just before exiting it... among other things...

Edit: a program such as this has no exit.
 
Last edited:

cmartinez

Joined Jan 17, 2007
8,253
On another note. For a very short routine, it's not necessary to stop the timer if the number of cycles spent inside the interrupt routine is larger than the number of cycles defined by the timer's reload register's complement. Doing things that way would insure perfectly spaced timing between interrupt calls.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
how does it ever get to 'Exit'?
that means come out from loop

If you don't mind. I want to show you some flow chart that I have made
1. if switch is ON, LED will ON , else LED will Off
2. while Switch is ON, LED will ON
3. turn ON, LED for 256 times

Please Look at flow chart and if there are any mistake let me know
 

Attachments

JohnInTX

Joined Jun 26, 2012
4,787
that means come out from loop
Gotcha!
Your 3 flow charts are better logic-wise but each contains the problem that we are going to address before going any further. Your interrupt flow chart has the same flaw. So let's get with it.

Consider the 'main' routine in all of your programs, both in the flow charts AND the code too:
  • The program starts at the top when you power on or after a reset. Check.
  • The program then performs various logic operations to manage an LED, do some counting etc. Check.
  • When the count is finished, or the switch is open or whatever, you say something like 'Exit from loop'. In the code, that means you reach that final bracket at the bottom of main. PROBLEM!

Exit to where? The processor keeps executing code forever so what does EXIT mean? It is true that in subroutines that are called by main, an 'exit' can mean the end of the subroutine and a return to main but main is the top level. Returning from main goes where???? The processor does not stop. A return from main is undefined in embedded systems.

When a program exits on a PC it returns to the operating system (Windows, Linux etc). Embedded systems have no operating system so I ask again, when you return from main at that last bracket or at 'Exit' in the flow chart, what exactly to you expect the processor to do and what makes you think it would do that?

A clue is in #26, the '1 loop' diamond at the end of main. That construct is essentially correct but has two logic flaws:
1) if you are testing 1 for TRUE how can it ever be FALSE?
2)and of course, even some condition could be FALSE, again, where does the program go from there if it has no operating system to return to???

We covered all of this in your previous threads. If you need more examples, go back to those threads and see how I handled the problem in the example flow charts and code. You will see no EXITs. Hint: look for 'while(1)'

This issue has been a problem for you from the beginning. We will stay right here until we get it resolved.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Gotcha!
Your 3 flow charts are better logic-wise but each contains the problem that we are going to address before going any further.
Have you seen flow chart for " if else statement " I don't think that there is any problem. Can you tell me whats wrong in that flow chart?
Now I am editing last two flow charts. i will post soon
 

JohnInTX

Joined Jun 26, 2012
4,787
Lets start with only flow charts if its look correct than I will write program.
Neither is correct.
Let's step back some more:
  • What is the one with the 'for' loop supposed to do?
  • What is the one with the 'while' loop supposed to do?
Don't redo the flow charts. Pick one only and state in plain English (sorry, I don't speak hindi, so you got me there :) ) and state the problem it is supposed to solve. I don't think you have that fully in mind yet. When that problem is fixed, we can consider the others but for now, pick one and we will drill down into it.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Neither is correct.
Let's step back some more:

  • What is the one with the 'while' loop supposed to do?
Don't redo the flow charts. Pick one only and state in plain English (sorry, I don't speak hindi, so you got me there :) ) and state the problem it is supposed to solve. I don't think you have that fully in mind yet. When that problem is fixed, we can consider the others but for now, pick one and we will drill down into it.
while switch is closed , turn ON LED (check only one condition again and again repeatedly and if true do the task)
while switch is open , turn OFF LED (check only one condition again and again repeatedly and if true do the task)

Program
C:
#include<REGX51.h>
#define LED_ON  (1)
#define LED_OFF  (0)
#define Switch_Closed  (1)
#define Switch_Open  (0)

/*set bit P1^7 to LED*/
sbit LED = P1^7;
/*set bit P2^0 to Switch*/
sbit Switch = P2^0;

void initialization ();
void main ()
{
  initialization ();
 
  while (Switch == Switch_Closed )
  {
  LED = LED_ON;
  }
  while (Switch == Switch_Open )
  {
  LED = LED_OFF;
  }   
}
void initialization (void)
{
  LED = LED_OFF;
  Switch = Switch_Open;
}
 

JohnInTX

Joined Jun 26, 2012
4,787
NO!
(Do I have your attention now?)

I did not ask for a program. I asked for a simple description of what the program is to do. In English. Complete. The reason that I did not ask for a program OR flow chart is that you still can not express what you want in programming terms. Here is what you wrote:
while switch is closed , turn ON LED (check only one condition again and again repeatedly and if true do the task)
while switch is open , turn OFF LED (check only one condition again and again repeatedly and if true do the task)
And here is my question. Are you only really only going to to that ONCE, or do you want the LED to follow the switch for as long as power is applied? If so, it should look something like this:

Do This Forever:
while switch is closed , turn ON LED (check only one condition again and again repeatedly and if true do the task)
while switch is open , turn OFF LED (check only one condition again and again repeatedly and if true do the task)
Do you see the difference? I said to do those tests forever. You have to say that in a program. Look at the end of your main in #37. It does NOT 'do this forever'. It does it once (and then blows up because embedded systems can never exit).

Before we go any further, do you see the difference in what you wrote and what I just said?

Are you taking this class at university?
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
NO!
(Do I have your attention now?)

I did not ask for a program. I asked for a simple description of what the program is to do. In English. Complete. The reason that I did not ask for a program OR flow chart is that you still can not express what you want in programming terms. Here is what you wrote:
And here is my question. Are you only really only going to to that ONCE, or do you want the LED to follow the switch for as long as power is applied? If so, it should look something like this:

Do This Forever:
while switch is closed , turn ON LED (check only one condition again and again repeatedly and if true do the task)
while switch is open , turn OFF LED (check only one condition again and again repeatedly and if true do the task)
Do you see the difference? I said to do those tests forever. You have to say that in a program. Look at the end of your main in #37. It does NOT 'do this forever'. It does it once (and then blows up because embedded systems can never exit).

Before we go any further, do you see the difference in what you wrote and what I just said?

Are you taking this class at university?
Now I understood the basic of if else , for and while loop
My program does this things
C:
main()
{
   while(condition 1)
    {
       statement 1;
    }
   while (condition 2)
    {
       statement 2;
    }
}
Your program does this things
C:
main()
{
while (1)
  {
         while(condition 1)
        {
          statement 1;
        }
        while (condition 2)
        {
          statement 2;
        }
   }
}
I am explaining for " for loop"
For loop
check the 10 conditions once and if its true do this task
C:
void main ()
{
   unsigned int loop;
   for ( loop = 0; loop <10; loop++ );
   LED=LED_ON;
}
check the 10 conditions once and if its true do this task repeat all time
C:
void main ()
{
   unsigned int loop;
    while(1)
    {
        for ( loop = 0; loop <10; loop++ );
         LED=LED_ON;
    }
}
 
Last edited:
Top