Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problems running interrupts on PIC18F4620 on timer1/2/3

Status
Not open for further replies.

SrSul

Newbie level 3
Joined
Jan 24, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,303
Hello,

I am pretty much a noob when it comes to PIC's, i just started and i am obligated to use C18 as a compiler, also I'm having a rough time understanding the whole concept of interrupts on PIC's, but i think I'm good with those now.
Q:What do I want to achieve?
A:I want to generate an interrupt once every 1sec using timer1 or timer3

I am using a PIC18F4620 with 10.000.000Hz external clock, and writing code in C.
I have managed to generate interrupts using the timer0 every 1sec but these other timers are totally different and i don't really understand them.
If anyone could help me configure the registers i need, i would be very grateful.

Thank you
 

Hi,
The significant complexity of TMR1 on PIC18F4620 is due to attached Timer1-oscillator for it (using pins T1OS1 & T1OS0) and 16bit mode.

That means it has 3 clock sources(we have to select 1 of them for operation);
1. Internal-instruction cycle (ie Fosc/4)
2. External clock signal input to pin RC0/T1OSO/T13CKI (on the rising edge only)
3. Timer1 internal oscillator.

Decision to use Timer1 is configured via T1OSCEN bit of T1CON register;
T1OSCEN= 1 = Timer1 oscillator is enabled
T1OSCEN= 0 = Timer1 oscillator is shut off

If you don't use Timer1(ie T1OSCEN= 0), then we can select internal or external clock (ie TMR1CS=0 or 1) and can be treated nearly as TMR0.

Although TMR0 can work as a 8bit or 16bit modes; TMR1 only works as 16bit mode. Hence it has 2 registers for clock counting (TMR1H:TMR1L)

You may set RD16=1 for following;
When we read TMR1 register, we have to read them both; First TMR1L then TMR1H sequentially.(TMR1H should not be read first since only reading TMR1L will update the value for TMR1H). When we write reverse sequence should be followed.

Interrupt occurs (TMR1IF=1) when TMR1H:TMR1L registers overflow from FFFFh to 0000h.(note numbers are 16bit)

Hope this helps you to start,
 
  • Like
Reactions: SrSul

    SrSul

    Points: 2
    Helpful Answer Positive Rating
So when u say "If you don't use Timer1(ie T1OSCEN= 0)" you mean not using it as an oscillator but as a simple counter?
So if i set T1OSCEN=0 and TMR1CS=0 it should run like TMR0?

So with RD16=0 , T1RUN=0 , T1CKPS1:T1CKPS0 =11 , T1OSCEN=0 , T1SYNC=0 , TMR1CS=0 , TMR1ON=1;
Should give me a 200ms interrupt with a 10MHz osc?
 

Hi,

Yes, without Timer1.

Set RD16=1; for above described method of timer-register read/write.

T1RUN is a flag that indicate whether Timer1 is being used or not. We can't write it; so leave it alone.

T1CKPS1:T1CKPS0 =11 Ok, means 1:8 prescale

T1OSCEN=0 Ok, means Timer1 oscillator is shut off

T1SYNC=0 Ok, this is ignored since TMR1CS = 0

TMR1CS=0 Ok, means Internal-instruction clock (ie.FOSC/4)

TMR1ON=1 Ok, Start counting

We have not assigned values for TMR1H:TMR1L. Let's assume they are cleared, hence start from 0000h.

Now overflow occurs after FFFFh - 0000h = FFFFh (ie 65535 in decimal) counts of TMR1H:TMR1L registers;

Since 8 prescale required Internal-Instruction-clock-cycles = 65535 X 8
=524280

Time period for 1 Internal-instruction =4/Fosc =4/10MHz
=0.4 us

Total time for interrupt =0.4us X 524280
=209.712 ms

If you need exact 200ms, you should put some value to TMR1H:TMR1L before starting the timer. Then count start from that loaded value (not with 0000h like above).

You may calculate that initial value by assigning total time to 200ms and going backward in above calculation.

Hope this help you.
 
  • Like
Reactions: SrSul

    SrSul

    Points: 2
    Helpful Answer Positive Rating
Yes, thank you i know how to handle timer's i just didn't know how to set T1CON to run normally :) i got plenty experience with ATmel products.

Thank you again very much for clearing things up, I appreciate the time u took to write such a long and clear explanation.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top