|
|
View previous topic :: View next topic |
Author |
Message |
MAKInnovation
Joined: 16 Nov 2010 Posts: 61
|
timer 1 interrupt |
Posted: Sat Jan 18, 2014 3:59 am |
|
|
Dear Friends;
Code: |
#define USE_WITH_PC 1
#include <18F452.h>
#fuses HS, NOWDT, NOLVP, PROTECT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, errors)
//RTC variables
#define XTAL_FREQUENCY 20000000
#define TIMER1_FREQUENCY (XTAL_FREQUENCY / 4) // 1 clock tick = 1 instr. cycle = crystal frequency / 4
int32 Ticker;
int8 Seconds=0;
////////////////////////////////////////////////////////////////////////////////
// Initialize RTC
////////////////////////////////////////////////////////////////////////////////
void Initialize_RTC(void)
{
Ticker = TIMER1_FREQUENCY; // initialize clock counter to number of clocks per second
setup_timer_1( T1_INTERNAL | T1_DIV_BY_1 ); // initialize 16-bit Timer1 to interrupt
// exactly every 65536 clock cycles
// (about 76 times per second)
enable_interrupts( INT_TIMER1 ); // Start RTC
}
#org DEFAULT
// Timer 1
#org 0x1000, 0x1FFF DEFAULT
#int_TIMER1
void TIMER1_isr()
{
Ticker -= 65536;// Decrement ticker by clocks per interrupt
if ( Ticker < 65536 ) // If second has expired
{ Ticker += TIMER1_FREQUENCY; // Increment ticker by clocks per second
seconds++; // Increment number of seconds
}
//--------- Some code -----
if (Seconds == 30) {Seconds=0;}
}
////////////////////////////////////////////////////////////////////////////////
// MAIN Program
////////////////////////////////////////////////////////////////////////////////
void main()
{
int8 prev_second;
SET_TRIS_C( 0x8F );
Initialize_RTC();
enable_interrupts( GLOBAL );
while(1)
{
if (seconds != prev_second)
{
prev_second = seconds;
// -------- some code ----------
delay_ms(10);
}
}
}
|
I am using a very common timer code for my project. this code generates interrupt about 76 times per second.
which is slow for some of my calculation.
I want to generat interrupt about 800 times per second. How can i do this. Kindly resolve my issue.
Regards;
Ashraf |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat Jan 18, 2014 4:19 am |
|
|
Use timer2.
Unlike the other timers, this one can be programmed for a specific count, rather than 65536.
setup_timer_2(T2_DIV_BY_4, 155, 10);
Will give an interrupt at 801Hz.
(((20000000/4)/4)/156)/10 = 801.28
However a much nicer number to work with is 1000Hz, since this divides evenly into the master clock/4.
setup_timer_2(T2_DIV_BY_4,249,5);
Remember though that it takes typically 60 instructions to get into and out of the interrupt handler, so the chip will be spending about 1.5% of it's time just handling the interrupt.
Best Wishes |
|
|
|
|
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
|