View previous topic :: View next topic |
Author |
Message |
Nora
Joined: 23 Mar 2008 Posts: 50
|
Timer0 with PIC16F877A |
Posted: Thu Jul 14, 2011 7:43 pm |
|
|
I copied this code from another thread and am unsure as to why it doesn't work.
I have read a ton of stuff on this forum and in the F877A data sheet about timer0, but I must be missing something.
The LED turns on in the "welcome" toggle.
Thanks in advance for insight.
Nora
Code: |
#include <16f877A.h>
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP // changed to HS for 20 mHz
#use delay(clock=20000000) // one instruction=0.2us
#include <stdlib.h>
#use standard_io(a)
#use standard_io(b)
#use standard_io(c)
// global variables
int16 count=0;
#int_TIMER0
void TIMER0_isr()
{
if(count == 0)
output_high(pin_A5);
count++;
}
void main()
{
set_timer0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_256); //
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
output_high(PIN_A5); // Welcome
delay_ms(1000);
output_low(PIN_A5);
delay_ms(1000);
while(1){
if(count > 500) //
output_low(PIN_A5);
count = 0;
}
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 14, 2011 9:45 pm |
|
|
Change 'count' to an int8, and change the test to check for 50 instead of 500. |
|
|
Nora
Joined: 23 Mar 2008 Posts: 50
|
|
Posted: Fri Jul 15, 2011 6:57 am |
|
|
Thanks for the reply PCM. I did as suggested and no joy.
I eventually got it to work and for completeness, am posting the solution.
Turns out I was using the wrong function (I am definitely not sleeping enough!).
From the CCS manual,
Quote: |
setup_timer_0(mode) Sets the source, prescale etc for timer0
set_timer0(value) or
set_rtcc(value)
Initializes the timer0 clock/counter. Value may be a 8 bit or 16 bit
depending on the device |
So I changed the function to and all is well.
Nora |
|
|
|