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

jpanhalt

Joined Jan 18, 2008
11,087
My current project does that. Pick a common denominator, in your case, 10 counts. Then do something after the first 3 interrupts, then after 2 do the other, and so forth.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Do you want one thing to happen at 30, then a second at 20 counts later? And then repeat?
Yes
My current project does that. Pick a common denominator, in your case, 10 counts. Then do something after the first 3 interrupts, then after 2 do the other, and so forth.
I did not understand how you did the calculation? What value have to load into the counter register?
 

MrChips

Joined Oct 2, 2009
30,823
If the counter is 8 bits, then the maximum count is 255.

If you want to count for 30 cycles, then subtract 30 from 256.
256 - 30 = 226

Preset the counter to 226. The counter will overflow after 30 counts.
 

jpanhalt

Joined Jan 18, 2008
11,087
Yes

I did not understand how you did the calculation? What value have to load into the counter register?
If you want an interrupt on the 10th count, load the TMR0 register with 246; if after the 10th count pre-load with 245 as described in post #2.

Remember, there is latency to an interrupt as well as time taken by the ISR itself, so you may have to adjust your preload number to work out exactly. That's one place where simulation helps. If you get to doing this in practice, you might want to consider using Timer 2, but let's not get into that now.
 
Last edited:

jpanhalt

Joined Jan 18, 2008
11,087
It's just an ordinary SFR (special function register) in Bank0

1596310511238.png

In Assembly, movlw d.245, movwf TMR0

In C, something link TMR0 = d.245 (or whatever radix it is in) That's just a wild guess.

BTW, if you expect to see 10 counts on an LED, it will probably not be visible.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
If the counter is 8 bits, then the maximum count is 255.

If you want to count for 30 cycles, then subtract 30 from 256.
256 - 30 = 226

Preset the counter to 226. The counter will overflow after 30 counts.
I'm just taking the hypothetical situation
First timer interrupt should happen at 30, then a second at 20 counts later And then repeat

But if first interrupt service routine take 15 counts time, Will second interrupt generate at 236.

It seems to me that second interrupt should not be generated I think because the two interrupt can not be active simultaneously.

BTW, if you expect to see 10 counts on an LED, it will probably not be visible.
As I said it's hypothetical situation. This question is just to clear concepts
 
Last edited:

jpanhalt

Joined Jan 18, 2008
11,087
I just re-read your question in post #26. The bits you set for an interrupt are in INTCON
1596312230233.png

Set bits 5 and 7, but be sure bit 2 is clear when you do that. You can access INTCON from any bank. It is one of the "Core" registers and can be accessed from any bank.

But if first interrupt service routine take 15 counts time, Will second interrupt generate at 236.
I don't understand that question. Your will interrupt at every 10 counts. Then in the ISR determine whether it is the 3rd time (30 counts) or 2nd (20 counts). Of course, your code must start at 3 interrupts, then accept the second interrupt, then the third, and so forth. Not hard to do in Assembly. In C, I have no clue.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
I don't understand that question.
If first interrupt happen and it is doing its job, can another interrupt be active during this time?

Interrupt 1
Do job 1
Interrupt 2
Do job 2

The first interrupt has been generarated and he is also doing his work in ISR. In the meantime, can second interrup happen?
 

MrChips

Joined Oct 2, 2009
30,823
Depends on what you want the timer to do.

There are two basic functions of a timer event interrupt.

1) an accurate time-base
2) a one-shot time delay

1) Accurate time-base
The easy way to create a delay in your program is to use a software loop. This method has serious limitations:
#1 - the MCU cannot do anything else
#2 - the delay is platform dependent, i.e. depends on processor speed and instruction clock cycles.

A better way is to use a hardware timer module.
#1 - the clock cycle is accurate and based on a crystal oscillator
#2 - the time period is independent of MCU instruction cycles and software running.

For a repetitive time-base, you set up the HW timer module to count and automatically recycle. There are no lost cycles from ISR.

2) One-shot delay
This is used to create a one-time delay of any desired time delay. You start the delay process and then continue with whatever processing is required. At the end of the delay an interrupt occurs. What you do in the ISR is your own business.

If you want to repeat the process with the same delay or a different delay you simply reset and restart the delay process.
 

jpanhalt

Joined Jan 18, 2008
11,087
If first interrupt happen and it is doing its job, can another interrupt be active during this time?
Not with an enhanced mid-range device like the 12F1840. There is no ability to prioritize interrupts. If you are in an ISR, another interrupt cannot interrupt. BUT, another interrupt flag (IF) can occur that will produce an interrupt immediately on exiting the first interrupt. The 18F's provide for priorities, but that may be more bother than it is worth for simple work.

Interrupt 1
Do job 1
Interrupt 2
Do job 2

The first interrupt has been generated and he is also doing his work in ISR. In the meantime, can second interrupt happen?
You do not need two different interrupts. THINK about it. What if you have an interrupt after two rollovers and another after three rollovers. How will the first know that the second type is yet to come? An MCU cannot see into the future. The problem as you stated it does not require that, but it does require discipline. There will be first a 30-count process, then a 20-count process, then a 30-count process and so forth. It is basically no different than flashing an LED on for 30 us, off for 20 us, on for 30 ms and so forth.

If you want to do something different, please describe.

Do you happen to know a user @Gajyamadake? He asked similar questions. My suggestion to you, and don't take it lightly, is to get the processor we are discussing, program it, and see what happens with it in real life or simulation.

I took piano through the 4th grade. I can read music, and presumably know how to play a violin ( in theory). That doesn't mean I can play a violin. I have never had a violin to my chin and am quite sure that whatever sound I made would have little relation to the notes on the musical score. This thread is much the same. You seem to be trying to learn how to program MCU's in a vacuum Get one. Do it. And then let's work on solving real problems. If you have a decent simulator, that is OK too. But doing it in the abstract is fruitless.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
If you want to do something different, please describe.

Do you happen to know a user @Gajyamadake? He asked similar questions. My suggestion to you, and don't take it lightly, is to get the processor we are discussing, program it, and see what happens with it in real life or simulation.
I have read many old threads and they are providing me a great help. I think I am not a stage where I can do anything practical don't know basics so i want to clear basics.

I just want to learn important protocol. I want to work in automotive industry.
I would like to do something practically with the ARM controller Because I have heard that ARM controllers are used a lot in company and it is advanced version
 
Last edited:

MrChips

Joined Oct 2, 2009
30,823
I have read many old threads and they are providing me a great help. I think I am not a stage where I can do anything practical don't know basics so i want to clear basics.
Learning to program interrupts is not basic. This is beyond basics.
Get an MCU, an IDE and start programming. Flash an LED without copying someone else's code.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Learning to program interrupts is not basic. This is beyond basics.
Get an MCU, an IDE and start programming. Flash an LED without copying someone else's code.
Yes, I want to buy a physical ARM board in which I can practice for the automotive domain. I'm not a very good guy in programming but I can manage. I have studied C programming.

I am having hard time to understanding the microcontroller's peripheral. For example, I do not know which registers should be used for timer interrupt and why and which bit should be set . That's why I asked you to see post #26. I want to see how you find out required information in the datasheet
 
Last edited:

jpanhalt

Joined Jan 18, 2008
11,087
I am having hard time to understanding the microcontroller's peripheral. For example, I do not know which registers should be used for timer interrupt and why and which bit should be set . That's why I asked you to see post #26. I want to see how you find out required information in the datasheet
That has been told to you at least twice by me. More by others. What do you still not understand?

If you are going to ARM, why are you wasting time with PIC's?
 

MrChips

Joined Oct 2, 2009
30,823
Programming an ARM chip is at least two levels beyond a basic MCU chip.
I suggest you learn to program a simpler MCU first. My recommendation would be an MSP430 MCU.
 

jpanhalt

Joined Jan 18, 2008
11,087
Here's some code to do what I suggested in post #33 in response to your post #30. Note timings, as expected, will be off due to latency and time in the ISR, but counts seem OK.

Code:
;code
     list        p=12f1840      ; list directive to define processor
    #include    <p12f1840.inc> ; processor specific variable definitions
    errorlevel -302, -305
     list st=off
     radix dec
    __CONFIG _CONFIG1, _FOSC_ECL & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
    __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF 


     cblock    0x20
     toggle
     endc

     org  0x0000
     bra  Start
;*****************************INTERRUPT SERVICE ROUTINE************************
     org      0X0004          ;ISR checks status of toggle,0, branches to
                              ;appropriate subroutine, toggles between 20
                              ;and 30 counts.
Alternate
     movlw     1
     xorwf     toggle,f       ;set toggle for next interrupt
     btfss     toggle,0
     bra       Do20           ;alternate procedure after 20 counts
     bra       Do30           ;goes to Do30 on first interrupt
Do20
;<insert code for Do20>
     movlw     256-30         ;calculate counts for Do30
     bra       ExitISR
Do30
;<insert code for Do30>
     movlw     256-20         ;calculate counts for Do20
ExitISR
     bcf       INTCON,TMR0IF  ;clear interrupt flag
     movwf     TMR0           ;pre-set TMR0 for next period
     retfie
;*********************************END OF ISR***********************************
Start
     movlb     1              ;setup MCU
     movlw     b'01101000'
     movwf     OSCCON         ;OSCCON = 4 MHz
     movlw     b'00001000'
     movwf     OPTION_REG     ;TMR0 = system clock,no prescale
     movlb     0
     movlw     b'10100000'
     movwf     INTCON         ;enable TMR0 interrupt,clear TMR0IF
     clrf      toggle         ;
     movlw     256-30         ;calculate conts for Do30
     movwf     TMR0           ;first period is 30 counts
     nop                      ;idle here
     bra       $-1            ;idle here

     end
EDIT: Since the first delay is 30 counts, change line 24 btfss to btfsc so second delay is 20 counts. Missed that in cleaning the code.
EDIT2: Code can be shortened slightly by replacing lines 46-50 with this:
Code:
     clrf      toggle
;    movlw     256-30         ;calculate conts for Do30
;    movwf     TMR0           ;first period is 30 counts
     movlw     b'10100100'  
     movwf     INTCON         ;enable TMR0 interrupt,clear TMR0IF
Wasn't sure how forcing and immediate interrupt would work. Seems OK in simulation.
 
Last edited:

jpanhalt

Joined Jan 18, 2008
11,087
Better for what? PIC's so far can do whatever I have wanted to do, and I am comfortable with their instruction sets. With which instruction sets are you most comfortable?

If I am offered the choice between learning C and switching to ARM Assembly, I will probably do the latter. But, that is a long ways into the future for me.
 

MrChips

Joined Oct 2, 2009
30,823
You cannot compare PIC with ARM in any reasonable fashion. They are very different architectures with hundreds of different chip models to compare.

You might have a better chance comparing Atmel AVR with Microchip PIC and TI MSP430. Again, there are thousands of chips to compare. In order to do a valid comparison you need to select specific MCU models with similar capabilities.

For example,
Atmel ATtiny48
TI MSP430G2553
Microchip PIC16F628A

(I have intentionally selected low-end MCUs in order to keep the comparison simple. I have chosen parts off the top of my head without examining each MCU in detail. There are literally thousands of MCU from which one can make a comparative selection. You can do so on your own if you want to take the time.)
 

jpanhalt

Joined Jan 18, 2008
11,087
The TI MSP430 is a 16-bit core; the PIC 16F628 is just 14-bit. A more even comparison would be the 8-bit, 16-bit core PIC 18F devices. However, the enhanced mid-range and newer (e.g., 16F1n(x) ) chips, which are still just 14-bit core have several advantages to make them more comparable: 1) stack can be accessed; 2) movlb makes bank changes easier; 3) linear memory that allows access to the entire user RAM in a linear fashion; arithmetic functions that include carry/borrow like the TI chip does, and some niceties that facilitate indirect addressing.

I did not look at the ATtiny48 chip. But any fair comparison should not be made to the 16F628A and similar chips as they are effectively legacy chips. Of course, in some areas, they seem to be the only chips available.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
You cannot compare PIC with ARM in any reasonable fashion. They are very different architectures with hundreds of different chip models to compare.
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.
 
Top