|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
Timer0 stubbornly not working
Hey all,
I'm embarrassed to have to ask, but we're using Timer0 in our code to give us a 60Hz cycle on a PID loop. And the darned thing refuses to work. After an hour or troubleshooting and fiddling with code and looking at a frequency counter, we were stuck at about 38.somethingHz. I finally realized that this was a full count from 0 to 65535 at a prescale of 1:4. So our pre-load values aren't pre-loading properly. Here's a code snippet of the initializing and the interrupt handler: Code:
//Initialize
T0CON= 0b00000001; ////1:4 Prescale
TMR0H= 0X5D; //Pre-load TMR0 to overflow at 60 Hz
TMR0L= 0X3D;
T0CONbits.TMR0ON= 1; //Turn timer on
T1CON= 0b10000110; //Set Timer 3 in async counter mode, 16-bit read
TMR1H=0x00;
TMR1L=0x00;
T1CONbits.TMR1ON = 1; //Turn Timer on
//Interrupt handler
else if (INTCONbits.TMR0IF && INTCONbits.TMR0IE) // timer 1 interrupt?
{
INTCONbits.TMR0IF = 0; // clear the timer 0 interrupt flag
TMR0H = 0X5D; //Pre-load TMR0 to overflow at 60Hz
TMR0L = 0X3D;
Timer1L = TMR1L;
Timer1H = TMR1H;
TMR1H = 0x00;
TMR1L = 0x00;
fMainTimer = 1;
}
I'll note that the commonality here is the Timer0 exclusively uses 16-bit read/write mode, and Timer1 wasn't working in 16-bit read/write mode. So. Does anyone know if there's some extra step I need to take to get things to work in 16-bit read/write mode? Is it just broken? Is there PIC errata about this too? Will I ever stop ending my sentences in question marks? |
|
#2
|
||||
|
||||
|
Re: Timer0 stubbornly not working
I might think you are missing the part where you enable the interrupt except
you say you are getting it but at a slower rate. Do you have any other interrupts happening? I think your ISR will pick up the timer if the ISR gets called for something else (even if the timer has not been enabled to generate an interrupt). I think the bits you need to set for timer 0 are in the INTCON register. Check the PIC data sheet for the exact bits. This is what we do when we set up timer1. However, IPR1bits.TMR1IP = 0; PIE1bits.TMR1IE = 1; INTCONbits.GIEL = 1; We are using timer 1 to provide a clock to run a number of different functions. We did our initial setup using the timer white paper that is published on the IFI web site. If you follow the instructions in the paper exactly, you will get timer1 up and running. http://www.ifirobotics.com/docs/time...004-jan-14.pdf we generalized it so we can can set an #define to the base HZ rate we want. We also use timer 2 via Kevin's ADC code, but that worked without modification. You may want to check that one out for a working example as well. Last edited by ericand : 09-03-2006 at 23:39. |
|
#3
|
||||
|
||||
|
Re: Timer0 stubbornly not working
I do believe that you have to write to the TMR0L then TMR0H, if memory serves, there's a hardware lock engaged when the low byte is accessed, doing it out of order can produce odd results. Try reversing those lines.
|
|
#4
|
|||||
|
|||||
|
Re: Timer0 stubbornly not working
Quote:
About the IFI whitepaper. I looked it over already, and they very specifically aren't enabling the 16-bit read/write mode. Which is just fine for using it as a timer like they do, but is problematic when using it as a counter. |
|
#5
|
||||
|
||||
|
Re: Timer0 stubbornly not working
Quote:
|
|
#6
|
|||||
|
|||||
|
Re: Timer0 stubbornly not working
Quote:
|
|
#7
|
|||||
|
|||||
|
Re: Timer0 stubbornly not working
As you can tell from Brian's post you need to delete ifi_library.lib from your MPLAB project and Add ifi_alltimers.lib in it's place.
Quote:
Last edited by Mark McLeod : 10-03-2006 at 22:01. |
|
#8
|
|||||
|
|||||
|
Re: Timer0 stubbornly not working
Quote:
Good Luck at GLR! ![]() |
|
#9
|
|||||
|
|||||
|
Re: Timer0 stubbornly not working
Success! Thanks to all in this thread. I should've known it was something obvious I was overlooking, but I was busy squashing all sorts of other bugs, mechanical and programmatic. The alltimers library fixed Timer0 right away. The Timer1 not reading properly is still happening, but there was some code in the PIC datasheet to get a proper read without using the 16-bit read/write mode. Also, the team managed to get bitten by the dreaded 8.2V bug in our first match. The linker patch appears to be working, so we'll see if that holds up.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Camera isn't working | Idaman323 | Programming | 13 | 15-02-2006 23:18 |
| What I'm Working On | Jared Russell | Programming | 2 | 18-01-2006 02:34 |
| CMUCam not working | Inverted | Programming | 10 | 04-02-2005 19:32 |
| IR Beacon/reciever not working... still... | Ferazel2001 | Programming | 23 | 04-02-2004 23:30 |
| I'm working :) | Jack | FIRST Scouting Network | 2 | 03-01-2004 01:26 |