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.

[SOLVED] Timer0 interrupt using PIC18f4680 in CCS c

Status
Not open for further replies.

SJ2013

Newbie level 3
Newbie level 3
Joined
Aug 22, 2013
Messages
3
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Visit site
Activity points
19
Hello all,
I am trying to generate timer0 based interrupt at a rate of 360Hz (2.777ms). This is the CODE I am using. But when I monitor Pin_b1, the ON time is 2.20ms, shouldnt it be 2.777ms?

Code:
#include <18F4680.h> 
#fuses HS, NOPROTECT, PUT, BROWNOUT, NOWDT, NOLVP 
#use delay(clock=20000000) 
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, ERRORS) 

#INT_TIMER0
void timer_irq()
{
set_timer0(58591);   //360Hz = 20*10^6/(4*(65536-n)*2)
output_toggle(pin_b1);
}

void main()
{
   setup_timer_0(RTCC_INTERNAL | RTCC_DIV_2);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   
   while(TRUE)
   {
      //TODO: User Code
   }

}
 
Last edited:

adjust set_timer0=58592. what did you get?

adjusting set_timer0= 58592, pin_b1 still toggles every 2.24ms. Is there any factor I need to take into consideration when I am calculating "n"?
thanks for your reply
 
Last edited:
With my experience so far, assembly language give accurate time more than c language that is why I said you should adjust loaded value.
note: when the interrupt occur, this line was executed "set_timer0(58591);" if I would ask you what time does it takes it to run? Even before the toggle will occur, some time are also spent before the effect will be seen on the port.

However, for you to archive such time accuracy, adjust continuously the set_timer0 value or recalibrate your prescaler value.

Run and observed to see the accuracy.....
Code:
void timer_irq()
{
output_toggle(pin_b1);
set_timer0(58591);   //360Hz = 20*10^6/(4*(65536-n)*2)
}
 
Last edited by a moderator:

Is Timer0 a 16 bit Timer?

Try this configuration in CCS C. Clock is 8 MHz.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Timer0
//Prescaler 1:1; TMR0 Preload = 59981; Actual Interrupt Time : 2.777 ms
 
//Place/Copy this part in declaration section
void InitTimer0(){
  T0CON  = 0x88;
  TMR0H  = 0xEA;
  TMR0L  = 0x4D;
  GIE_bit    = 1;
  TMR0IE_bit     = 1;
}
 
void Interrupt(){
  if (TMR0IF_bit){ 
    TMR0IF_bit = 0;
    TMR0H    = 0xEA;
    TMR0L    = 0x4D;
    //Enter your code here
  }
}

 

The feature of the controller pic18f4520 make timer0 mode selectable, either 8bit or 16bit mode.
 

Thanks for all your replies.
I made a silly mistake, my clock frequency is supposed to be 25Mhz since i am using an external crystal clock.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top