Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

relationship between FOSC/INTOSC and timing in general

Status
Not open for further replies.

mikeOJ

Member
Hi all,

Ive been using micros for some time now have written a number of different scripts but have recently come to realise that my comprehension of the way timing is managed in PICs is flawed, i am trying to create timing sequence of 15 seconds.

My reasoning was, OSC is running at 8MHz and if i set up timer 1 in 16-bit mode, it overflows after 65535 cycles.
instruction cycle is 8MHz / 4 = 2MHz ----> i think this is the flaw in my logic

therefore if i want an interrupt of 10ms its:

10ms *(2*10^6)=20000
65535-20000=45535

therefore if i load 45535 into the timer and initialise it i can get an interrupt every 10ms give or take the extra instruction it needs to overflow

therefore if i use nested loops, i can get a longer delay.

so my question is, is this logic correct, i've asked a few of my colleagues in the lab and there is some uncertainty. we cant reach an consensus, what i do know is that using this logic, my timer is only lasting half the time it should, 8 seconds. perhaps i am using the wrong oscillator?
 
Hi,

Yes , Fosc is 8mhz / 4 , via a 16 bit counter is 2mhz/65536 = 30.52hz.

To be able to produce 10Hz you will have to use the prescaler at 1:4 and then preload Timer1 with 15550 to produce an overflow.

The max delay you can produce with TMR1 with a Fosc of 2mhz and the Prescaler at 1:8 is 3.81hz
 
Check out this page, it has simple C routines to generate exact seconds (or any period you like).
https://www.romanblack.com/one_sec.htm

This code below will work fine for your PIC at 8MHz (2MHz timer1 ie 1:1 prescale) to generate exact average seconds. You will need to set up TMR1 to 1:1 prescale, and to enable the timer1 interrupt.

Alternatively, you don't have to use the interrupt you could instead manually check for the TMR1IF flag to be set, which occurs every 65536 TMR1 ticks.

Code:
// C code for a 1 second period with a 2MHz timer1 (8MHz xtal); 
// uses 1 variable; unsigned long bres

  // gets here every TMR1 int (every 65536 ticks)
  bres += 65536;         // add 65536 ticks to bresenham total

  if(bres >= 2000000)    // if reached 1 second!
  {
    bres -= 2000000;     // subtract 1 second, retain error
    do_1sec_event();     // update clock, etc
  }
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top