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

200 ms Timer Interrupt?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Izzy



Joined: 27 May 2007
Posts: 106

View user's profile Send private message

PostPosted: Thu Apr 03, 2008 8:00 pm     Reply with quote

Hello Inservi,

I tried it your way, but it does not work. Timer 0 is an 256 bit timer. I just checked it with a small test program.

Any one has any other opinion?

inservi wrote:
Hello Izzy.

For get interrupt for just 200mS, you can set your timer0 for interrupt every 209.7 as decribed Ttelmah. Then, simply set the timer0 counter to the right value (greater than 0) after each interrupt.

If 65536=209.7ms then :
9.7/200 = 0.485
65536*0.485 = 3178.4 = 9.7ms
65536-3178= 62358 = 200ms

Code:


#INT_RTCC
void T0_isr ()
  {
     set_timer0(62358) // for 200 ms
     ...
     your code
     ...
  }


void main() {
  ...
  setup_timer_0(T0_INTERNAL | T0_DIV_BY_32) = 209.7152 ms

  set_timer0(62358) // for 200ms

  enable_INTerrupts ( INT_TIMER0 );
  enable_INTerrupts ( global );
  ...





That is very simple.

Best regards,
dro
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 12:54 am     Reply with quote

Izzy wrote:
I tried it your way, but it does not work. Timer 0 is an 256 bit timer.
According to the PIC18F4685 datasheet timer0 can be configured as 8 or 16-bit timer. The CCS compiler defaults to 16 bit unless you specify it to be 8 bit.

Quote:
I just checked it with a small test program.
Can you post this program?
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 1:18 am     Reply with quote

Hello Izzy,

As ckielstra write, PIC18F4685's timer0 is 16-bit default. (If you use it as 8-bit timer with the same prescaler, the overflow occurs after only 819µs.)

Also, it's a good idea to use the very good ckielstra's enhancement
Code:
set_timer0( get_timer0() + 62358 ) // for 200 ms


as :
Code:

                      
#use delay(clock=40000000)

#INT_RTCC
void T0_isr ()
  {
     set_timer0( get_timer0() + 62358 ) // for 200 ms
     ...
     your code
     ...
  }


void main() {
  ...
  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32) // = 209.7152 ms

  set_timer0( get_timer0() + 62358 ) // for 200 ms

  enable_INTerrupts ( INT_TIMER0 );
  enable_INTerrupts ( global );
  ...



The 62358 value is not 'exact', you must correcting it for more accuracy. It's probaly about 62400 or something so.

I'm also waiting for your 'test' program?

Best regards,
dro
_________________
in médio virtus
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 4:31 am     Reply with quote

Just one litle more comment for test,

Make your program for 209.7ms. When that's working, simply add the line
Code:
set_timer0( get_timer0() + 62358 ) // for 200 ms
at the start (first line) of your interrupt function.

Best regards,
dro.
_________________
in médio virtus
Izzy



Joined: 27 May 2007
Posts: 106

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 4:47 am     Reply with quote

Hello guys thanks for your reply..
Here is my small test code



Code:
#include <18F4685.h>
#device ICD=TRUE
#fuses HS,NOLVP,NOWDT
#use delay(clock=40,000,000)
#use rs232(stream=debug, DEBUGGER)
#include <stdlib.h>

int time1 =0;

#INT_RTCC
void read_analog_pin()
{
      set_timer0(0);
      delay_us(819);  //I am trying to see Where the counter is at , at 819 us.
      time1 = get_timer0();

      fprintf(debug,"%u\n", time1);
}


void main()
{
set_timer0(0);
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_32);  enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);

While(true)
   {
   }

}





If I use 819us delay it will display 255. Which makes sense
(4 / 40000000) * 3.2 * 256 = ~ 819us

Now if I increase the delay to 825us then the counter will display back to 1. it is only counting from 0-255??

WHy is it? Or am I doing some thing wrong?
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 5:24 am     Reply with quote

Hello Izzy,

note that please, in ccs c the int type is 8 bit by default.
try with
Code:
int16 time1 =0;


then
Code:
fprintf(debug,"%lu\n", time1);
must be changed to for unsigned decimal long.
dro.
_________________
in médio virtus
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 5:34 am     Reply with quote

Also, are you sure that your clock is correctly set ?

I think that for run at 40Mhz, if you use a 10Mhz cristal, you need define
Code:
#FUSES H4                       //High speed osc with HW enabled 4X PLL

and not HS

dro.
_________________
in médio virtus
Guest








PostPosted: Fri Apr 04, 2008 6:33 am     Reply with quote

Hello Inservi,

Thanks for your reply.

That int16 might be the case, I will try it when I go back home.

And I am using a 40Mz crystal oscillator. So shouldnt that just be
#FUSE HS?
Ttelmah
Guest







PostPosted: Fri Apr 04, 2008 3:41 pm     Reply with quote

Are you using a 40MHz _oscillator_ (IC sized block, needing power), or a 40MHz crystal?.
The former will work, but would be better suited to using 'EC' as the fuse. Otherwise you are wasting a little power running the internal high gain amplifier designed for the internal oscillator. If you are using a crystal, then you need to read the data sheet. The highest frequency the internal oscillator is designed to run at, for most PICs, is 25MHz. Haven't checked the data sheet for the 4685, but I'd expect it to be the same (only a very few of the latest chips support higher than this).

Best Wishes
Izzy



Joined: 27 May 2007
Posts: 106

View user's profile Send private message

PostPosted: Fri Apr 04, 2008 3:50 pm     Reply with quote

Sorry I mean 40Mhz oscillator needing power.

Whats the difference between crystal and the power needing oscillator? Embarassed
Ttelmah
Guest







PostPosted: Sat Apr 05, 2008 2:22 am     Reply with quote

OK.
A complete crystal oscillator, needs several bits. The crystal itself, a couple of capacitors to provide the correct loading, and phase shift, and an amplifier that provides the rest of the phase shift (to give a 360 degree 'total'), and the power gain to drive the whole circuit. The external 'modules', needing power, have all this internally, and just produce out the end the required signal. The alternative is just to use a crystal and capacitors with the PIC itself, in which case the PIC, has to provide the last part. Internally the PIC has two gain stages (selectable by XT, or HS), which are designed to give the gain, and phase shift required to build oscillators for up to 4MHz, and to 25MHz respectively. Operating a PIC over 25MHz(except for a few versions), requires you to use _either_ a complete external oscillator, or to use the internal oscillator at a lower frequency, and the PLL circuit internally to give the required final frequency.
In your case, with the external oscillator, you really should be using the EC, or ECIO fuses, rather than HS. The problem is that the HS amplifier in the PIC, is not rated to handle above 25MHz, and though the chip will work, with an external oscillator module, this results in wasted power (probably as much as 5 to 6mA). Seeing HS, which is not designed for 40MHz, had the 'suspicion' modes going, that you were not running at the frequency you thought (if wired up with just a 40Mhz crystal, what tends to happen is that the circuit runs on an 'undertone' of the actual rate...).

Best Wishes
Izzy



Joined: 27 May 2007
Posts: 106

View user's profile Send private message

PostPosted: Sat Apr 05, 2008 1:46 pm     Reply with quote

Thanks a lot Tlemah.

It worked after I changed it to int16. I will remove Fuse HS if it is wasting power. I will also try to do some reading on EC, or ECIO fuses.
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Sun Apr 06, 2008 4:59 am     Reply with quote

Oh yes, Thanks a lot Tlemah for the int16 solution. Smile

dro
_________________
in médio virtus
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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