[Solved]Could you help me to understand the timer interrupt

MrChips

Joined Oct 2, 2009
30,813
It seems that the PIC microcontroller would be better to start at beginning because a lot of information is available on the internet about it. Here also so many peoples discuss about PIC microcontroller. I would also start with suitable PIC microcontroller.
Take your pic. I am no fan of PIC architecture. I teach Freescale HC11, Atmel AVR, TI MSP430. I would never want to teach PIC ASM to anyone.

And you might as well forget about learning and using ARM ASM. It is ten times more complex.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Can timer interrupts be useful in following conditions?

There are three LED lights, Red, Green and Yellow

1. I want RED LED to turn on 1000ms, then after turn off Red LED

2. While Red LED is on, I want Green LED to turn on 500 ms, then turn off Green LED

3. While Green LED is on , I want Yellow LED to turn on 200ms, then turn off Yellow LED

4. Repeat process

IMG_20200812_001651.jpg

Does program flow achieve the given requirements ?
 

hexreader

Joined Apr 16, 2011
581
What a mess....

Your flow diagram shows no form of delay, so the light sequence will run very fast, assuming sensible processor clock.

At what point during Red-on does green go on?

At what point during green-on does yellow go on?

What happens after red goes off?

Here is my lazy version of a flow diagram ( I am too lazy to draw it out and scan it)

1) Start
2) Initialise
3) red on, green off, yellow off
4) wait 500 ms
5) green on
6) wait 300 ms
7) yellow on
8) wait 200 ms
9) all off
10) wait ??? ms - you did not specify
11) go to start of step 3

Can timer interrupts be useful in following conditions?
I see no need for interrupt here, unless the "wait xxx ms function" uses it
 
Last edited:

MrChips

Joined Oct 2, 2009
30,813
The entire purpose of using timer interrupts is so that the computer main code does not have to do any processing.

Green ON for 500ms -> Yellow ON for 200ms -> Red ON for 1000ms -> repeat

What is the common factor? It is 100ms. Set your timer to interrupt every 100ms.
Now set up your phase counter:
0-1-2-3-4-5 Green OFF/Yellow ON-6-7 Yellow OFF/Red ON-8-9-10-11-12-13-14-15-16-17 Red OFF/Green ON,0...
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Now set up your phase counter:
0-1-2-3-4-5 Green OFF/Yellow ON-6-7 Yellow OFF/Red ON-8-9-10-11-12-13-14-15-16-17 Red OFF/Green ON,0...
Thank you for great help
I don't understand how three Lights goes on, off after interrupt generates

IMG_20200812_013857.jpg

We have to compare time but it is not understood where to do it in the while loop or in the interrupt routine.
 

MrChips

Joined Oct 2, 2009
30,813
Timer interrupts every 100ms.
When timer interrupts, increment a software counter.
Reset counter at 17.

Counter
0
1
2
3
4
5 Green OFF, Yellow ON
6
7 Yellow OFF, Red ON
8
9
10
11
12
13
14
15
16
17 = 0 Red OFF, Green ON
 

MrChips

Joined Oct 2, 2009
30,813
Pseudo code

Timer Interrupt Service Routine (ISR)

increment counter
if counter = 5, green = OFF, yellow = ON
if counter = 7, yellow = OFF, red = ON
if counter >= 17, red = OFF, green = ON, counter = 0
 

jpanhalt

Joined Jan 18, 2008
11,087
This is like deja vu all over again.* Discussed in posts #28 and following using common denominator or serial delay (alternating in that case). Code (Assembly) in post #40.

*It's a famous quote from sports.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
This is like deja vu all over again.* Discussed in posts #28 and following using common denominator or serial delay (alternating in that case). Code (Assembly) in post #40.

*It's a famous quote from sports.
I am sorry but the code is in assembly language and assembly language goes over my head. I can understand the C code
 

jpanhalt

Joined Jan 18, 2008
11,087
I am sorry but the code is in assembly language and assembly language goes over my head. I can understand the C code
I also described the process and commented the code. Are you now asking for the same thing in C?

If you had just said that 20 posts ago, I suspect someone would have provided the answer. My question, if this is not for a real device or MCU, why does it have to be coded at all?
 

MrChips

Joined Oct 2, 2009
30,813
Your problem is not about ASM, C, or interrupts.
Your problem is about problem solving.
You have been given a problem. Your task is to find a solution which can be formulated without the need to use computer jargon.

Use plain native language to define the problem and to layout a method of solving the problem.
 

jpanhalt

Joined Jan 18, 2008
11,087
Could you help me to understand the timer interrupt'

That has been explained to you by many people on at least 4 different forums. We can explain it to you, but it seems you are trying very hard not to understand. Is there any solution? What?
 

MrChips

Joined Oct 2, 2009
30,813
Also this has nothing to do with timer interrupts.

What you did not see is that the solution to the problem is a "finite state machine".

Green, yellow, and red are three lamps that each have binary states.
You need to map out how the lamps transition from one state to the next.
But that is not enough to solve your problem.

You do not solve the problem by using timer delays. You solve the problem by considering time as another element besides three lamps in a state machine.

Time is also an element with states. Time = 0 is one state. Timer = 14 is another state. I have given you a solution where time has 18 states.
 

hexreader

Joined Apr 16, 2011
581
This is how I would code 3 LEDs to flash in the way described in post #50....

Code:
// simple LED flash program for PIC12F1822 on EasyPIC7
// enable PLL in configuration settings, select intosc, disable watchdog
// Written using mikroC pro for PIC C compiler - free version

// connect RA0 through 1k Ohm resistor through red LED to ground
// connect RA1 through 1k Ohm resistor through green LED to ground
// connect RA2 through 1k Ohm resistor through yellow LED to ground

void main() {

     OSCCON = 0xF0;           // internal 8MHz x4 PLL gives 32 MHz clock
    
     TRISA = 0;               // all output
     LATA = 0;                // get compiler to correct bank for LATA, so it doesn't have to do it in the while loop

     while(1){
          LATA = 0x07;        // red, green, yellow LEDs on
          Delay_ms(200);      // Yellow LED on for 200ms
          LATA = 0x05;        // red, green LEDs on, yellow off
          Delay_ms(300);      // 300 ms (green on for 500 ms)
          LATA = 0x01;        // red LED only
          Delay_ms(500);      // 500ms (red on for 1000ms)
          LATA = 0x00;        // all LEDs off
          Delay_ms(500);      //       ... for this many ms  - actual off time is not specified
     }
}
You can download mikro C pro demo version for free, then use simulator to step through C code

Note - this is my preferred solution - Mr Chips has a very different way in mind
 

MrChips

Joined Oct 2, 2009
30,813
That is absolutely terrible. The processor is locked into doing that particular task and cannot do anything else.
With the scheme that I have suggested, the processor is free to proceed with some other task. The lights will sequence merrily on their own.
 

hexreader

Joined Apr 16, 2011
581
That is absolutely terrible.
No need to be mean about it. I did not say anything nasty about your posts

I believe that this code fully meets the OPs requirement (if I understand the requirement) in a nice simple way that is easy to understand. Why complicate with interrupts?

The processor is locked into doing that particular task and cannot do anything else.
If there is a requirement for something else, maybe I missed it?

I am perfectly capable of using interrupts when the requirement calls for it, and I am happy to provide the OP with such code when I see a need, and if the OP happens to choose mikro C compiler. I may be able to provide MPLAB-X code as an alternative.

Until the OP settles on a particular MCU and compiler - it is guesswork and speculation to provide any sample C code, but I gave it a try anyway.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,813
Green, yellow, red lights sounds very much like a traffic flow control system.
TS is just using this as an example. You cannot anticipate that this is the end all of what he wants.
Traffic at an intersection has cross traffic. How are you going to include that in your code?
There are pedestrian lights to think about. A pedestrian steps up and presses a button while the light is still GREEN. How are you going to accommodate that in a Delay_ms(500)?

Forward thinking means that you do not lock yourself into a design that cannot accommodate other possibilities.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
What you did not see is that the solution to the problem is a "finite state machine".
If you mean that I have to make a state diagram, then I can make a state diagram . Post number #32 you can see at the link given below. I have created for another problem
https://forum.allaboutcircuits.com/threads/light-controller-state-diagram.171902/page-2

Sure I will make one state diagram for given problem in this thread

But it is very important for me to know why you think that this problem can be solved by the state machine.
 
Top