|
|
View previous topic :: View next topic |
Author |
Message |
mike88
Joined: 03 Apr 2014 Posts: 2
|
Asynchronous Timer1 / 3 on 18F25K50 Interrupt Problem |
Posted: Thu Apr 03, 2014 1:41 pm |
|
|
Hi everyone,
I am Mike. I have been reading you for quite a long time but I have never posted anything on this forum. I am having a problem which is giving me quite of an headache and I am hoping somebody will help me with it.
I am trying to make a simple USB counter with a USB-enabled PIC 18F25K50, using the internal 48 MHz oscillator as time base.
I make use of two timers: TIMER0 and TIMER3. I use TIMER0 as time-base to decide the integration time of my counts while TIMER3 is used with the external input as 16 bit counter (originally in ASYNCHRONOUS MODE). Here is a sample of the code. It's not the actual code but should pretty much give an idea:
Code: | #include <18F25k50.h>
#fuses INTRC, PLLEN, PLL3X, NOCPUDIV, NOWDT, NOPROTECT, DEBUG, NOBROWNOUT, NOMCLR,T3CKB5
#use delay(clock=48000000)
#include <stdlib.h>
#include <string.h>
#include <usb_cdc.h>
// prototypes
void serial_tx(void);
void init(void);
// global variables
unsigned int16 int_time=100; //integration time of 10 ms x 100 = 1 sec
unsigned int32 tx_counts=0;
unsigned int16 timer0_overflow=0;
unsigned int16 timer3_overflow=0;
int1 transmit_flag=0;
void serial_tx(void)
{
printf(usb_cdc_putc, "%lu\r\n",tx_counts);
}
void init(void)
{
setup_timer_0(T0_INTERNAL | T0_DIV_2);
setup_timer_3(T3_EXTERNAL | T3_GATE_INVERTED | T3_DIV_BY_1);
clear_interrupt(INT_TIMER0);
clear_interrupt(INT_TIMER3);
set_timer3(0);
set_timer0(5465);
output_high(PIN_C1); //connected to T3 GATE pin!
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER3);
enable_interrupts(GLOBAL);
}
void main()
{
usb_init_cs();
init();
while(true)
{
usb_task();
if (transmit_flag==1)
{
transmit_flag=0;
printf(usb_cdc_putc, "%lu\r\n",tx_counts);
}
}
}
#INT_TIMER0
void timer0_event(void)
{
timer0_overflow++; //this happens every 10 ms
if (timer0_overflow>=int_time) //with int_time = 100 this will give 1 sec integration time
{
output_low(PIN_C1); //gate OFF TIMER3
transmit_flag=1;
timer0_overflow=0;
tx_counts=make32(timer3_overflow,get_timer3());
timer3_overflow=0;
set_timer3(0);
output_high(PIN_C1); //gate ON TIMER3
}
set_timer0(5465);
}
#INT_TIMER3
void timer3_event(void)
{
timer3_overflow++; //Count number of times TIMER3 overflows
} |
Now the problem is:
The TIMER3 interrupt should fire only when TIMER3 overflows. In such way I can count how many times TIMER3 has rolled-over and compute the total number of counts. Unfortunately I seldomly get a spurious TIMER3 interrupt resulting in my integrated counts to be off by 65535.
It is a completely random behaviour!!! It happens even if I feed to the PIC a 10 Hz square wave (that will never saturate the counter in a reasonable time).
If I switch the counter to SYNC MODE, everything works fine but then I am limited by the F/4 for Ton Toff an period of my input signal. Since I would like non-periodic signals I would like to keep it ASYNC.
Do you know what might be going on?
Thank you so much.
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 03, 2014 7:11 pm |
|
|
Quote: | I seldomly get a spurious TIMER3 interrupt |
Did you read the Errata about the false overflow event ?
http://ww1.microchip.com/downloads/en/DeviceDoc/80000547C.pdf
This section here:
Quote: | 2. Module: Timer1/3 Module with Gate Control |
Anytime something weird happens, look at your erratas.
Go to Google. Type in the full name of your PIC, such as PIC18F25K50.
The first hit will take you to the Microchip page for that PIC. All PICs
have their own page. Then click on the Documentation and Software
button. Then you'll see the Errata sheet. |
|
|
mike88
Joined: 03 Apr 2014 Posts: 2
|
|
Posted: Fri Apr 04, 2014 1:39 am |
|
|
Thank you so much. Problem solved!
I completely forgot to have a look at the Errata. I thought it could be a firmware or compiler error.... |
|
|
|
|
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
|