CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

timer0 interrupt function takes 4ms?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jjabi



Joined: 24 Mar 2018
Posts: 4

View user's profile Send private message

timer0 interrupt function takes 4ms?
PostPosted: Sat Mar 24, 2018 7:23 am     Reply with quote

Hi, im new here and fairly new to PIC microcontrollers.

I am trying to set up a 10ms interrupt on timer0 on 31kHz internal oscillator frequency, and have almost succeeded. I am using PIC16LF1574.

I'm testing the code on Proteus 8 simulator and also on breadboard. I am trying to get a LED to blink 10sec ON 10sec OFF. Just a simple test program.

Well I set all the timer stuff properly like this:

Timer0 clock is 31kHz so instruction clock is 31/4 = 7.75kHz so one period is 129.032 us.

When prescaler is 1, one count takes129 us when INTERRUPT using 8-bit timer comes every: 129.032 us * 256 = 33 032 us = 33 ms.

To get it to be 10 ms: 129.032 us counts fit into 10ms is: 10 000 / 129.032 = 77.5 so we have to skip 256 - 77.5 = 178.5 = 179 counts.



Code:
#include <16LF1574.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,PROTECT,NOBROWNOUT,NOPUT,NOWRT,NOMCLR
#use delay(clock=31000)

int16 increment = 0;
//Interrupt called when TMR0
//flips from 255 to 0
//Timerin pitäisi interruptaa kerran 10 ms
#int_TIMER0                                 
void clock_isr()                             

   output_high(PIN_C3); //for the oscilloscope simulation
   increment++;
   set_timer0(179);                                       
     
}

void main()
{
   
   setup_oscillator(OSC_31kHZ);
   setup_timer_0( T0_INTERNAL | T0_DIV_1 );
   enable_interrupts( INT_TIMER0 );
   enable_interrupts( GLOBAL );
   set_timer0(179); //pyörähtää kun 256
   while(1)
   {
      output_low(PIN_C3);
      if (increment > 1000)
      {
      output_high(PIN_C4);
      }
   
      if (increment > 2000)
      {
      output_low(PIN_C4);
      increment = 0;
     
      }
   }
}



NOW the problem is that the led blinks in every 14 secs instead of 10secs. I investigated this using Proteus 8 oscilloscope, and I noticed that the program takes 4ms in the interrupt function. So every interrupt takes 14ms to get through instead of 10ms. Any idea why is this happening and how to fix this?

Thanks!
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Sat Mar 24, 2018 7:32 am     Reply with quote

You realize that it takes 130us per instruction with your 31kHz clock. So 4ms gives a total of 30 instructions. About what it takes to get in/out of interrupt with say 7 instructions in interrupt.

Regards
jjabi



Joined: 24 Mar 2018
Posts: 4

View user's profile Send private message

PostPosted: Sat Mar 24, 2018 7:35 am     Reply with quote

alan wrote:
You realize that it takes 130us per instruction with your 31kHz clock. So 4ms gives a total of 30 instructions. About what it takes to get in/out of interrupt with say 7 instructions in interrupt.

Regards


Oh I see, but I only have 3 instructions in my interrupt though? So I should try raising my frequency. The reason I wanted to use 31kHz to save power as much as possible in my design I'm making. I have it working in 4MHz at the moment perfectly, it just takes 0.5mA more current.
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 24, 2018 7:53 am     Reply with quote

hmm saving power implies a battery operated device.
if so , check out an application note on Microchips website involving calculating power used. Simple hardware a small PIC, an ADC chip and sending data to a PC. There's LOTS of interesting info in the apnote and going slower is NOT necessarily going to save energy !
I don't have the appnote #, though an667 (?) rings a bell as well my Microchip books are at the office and I'm not. It shouldn't be too hard to find though, look for an appnote from about 15 years ago.
These days using a higher capacity battery only costs pennies compared to back then.

And

As alan said there is a LOT of 'overhead' code in doing ISRs. Dump the listing of your program and you'll see it all.
Jay
jjabi



Joined: 24 Mar 2018
Posts: 4

View user's profile Send private message

PostPosted: Sat Mar 24, 2018 8:39 am     Reply with quote

I measured the current in my design to be about 3mA when using 4MHz clock, and around 2.5mA when using 31kHz clock. While it is not much of a difference I thought it would be a nice plus if I got it to work.

So now that I adjusted the blink to interrupt count (around 700 and 1400) according to 14ms interrupt cycle I got it blinking in 10sec ON/OFF. But it becomes too complex so perhaps it is best to just raise the frequency for now.


Last edited by jjabi on Sat Mar 24, 2018 11:40 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat Mar 24, 2018 11:20 am     Reply with quote

jjabi wrote:
alan wrote:
You realize that it takes 130us per instruction with your 31kHz clock. So 4ms gives a total of 30 instructions. About what it takes to get in/out of interrupt with say 7 instructions in interrupt.

Regards


Oh I see, but I only have 3 instructions in my interrupt though? So I should try raising my frequency. The reason I wanted to use 31kHz to save power as much as possible in my design I'm making. I have it working in 4MHz at the moment perfectly, it just takes 0.5mA more current.


Don't confuse C instructions with machine instructions.
A single C instruction, like *, can involve a thousand or more machine instructions.
In your case, not this bad, but each C instruction is several machine instructions.

When you think it is doing over 100 times more per second at 4MHz than 30KHz, it is actually much more efficient to clock faster. There is usually an 'optimum speed' where the processor does the most per uW. Typically around 1 to 4Mhz.
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 24, 2018 5:09 pm     Reply with quote

OK, it took me awhile but I FINALLY found the appnote...
It's AN606, circa 1997, and should be a must read for anyone doing battery powered devices.
Yes, it's old, same age as my pickup, BUT it's a great resource.
Still available from the Microchip website.
Jay
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Mar 25, 2018 4:37 am     Reply with quote

Simply increasing the value you use for this line (from ISR) will allow you decrease the on/off periods from 14s to 10s.
Quote:
set_timer0(179);

I'll let you work out what the value needs to be.

The down side is that if (when) you edit your ISR you may have to change the value used.
A better approach is to increase the timer0 value rather than set it to a fixed one.

Like Mr T says, read the AP note. You may find a reason why there is so little difference in current when you go from 31kHz to 4MHz.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sun Mar 25, 2018 11:38 am     Reply with quote

I think were going to have to start answering to the "two T's"... Smile

Half of the comments come from Jay (Temtronic), and the rest from me.

Nice is that we are both singing from the same sheet (maybe the "Two T's" could be a group.....).
jjabi



Joined: 24 Mar 2018
Posts: 4

View user's profile Send private message

PostPosted: Tue Mar 27, 2018 6:46 am     Reply with quote

Thanks for your help guys!

Quote:
OK, it took me awhile but I FINALLY found the apnote...
It's AN606, circa 1997, and should be a must read for anyone doing battery powered devices.
Yes, it's old, same age as my pickup, BUT it's a great resource.
Still available from the Microchip website.
Jay


I will read the appnote thank you for this.

Quote:
The down side is that if (when) you edit your ISR you may have to change the value used.
A better approach is to increase the timer0 value rather than set it to a fixed one.

I'll look into this also.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group