View previous topic :: View next topic |
Author |
Message |
POPE19
Joined: 27 Jun 2017 Posts: 71
|
rtcc not working |
Posted: Tue Sep 05, 2017 9:08 am |
|
|
hello
I am trying to count seconds using timer0 of pic16f1939, but it is not working could not figure out where the problem is.
Code: |
#include <16F1939.h>
#fuses HS ,NOVCAP, MCLR, NOWDT, PUT, NOIESO, BROWNOUT, NOFCMEN,PLL_SW
#use delay(crystal=20MHz, clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define INTS_PER_SECOND 76 // (20000000/(4*256*256))
int int_count=INTS_PER_SECOND;
long seconds=0;
//============================================================================//
#INT_TIMER0 //This function is called
void clock_isr()
{ //every time the RTCC (timer0)
//overflows (255->0)
//For this program this is apx
//31 times per second.
if(--int_count==0)
{
seconds++;
int_count=INTS_PER_SECOND;
}
}
//============================================================================//
void main()
{
set_rtcc(0);
setup_counters (RTCC_INTERNAL, RTCC_DIV_256);
setup_comparator (NC_NC_NC_NC);
enable_interrupts (INT_TIMER0);
enable_interrupts(GLOBAL);
while (TRUE)
{
printf("%Lu \n\r ",seconds);
delay_ms(1000);
if(seconds >=999)
{
seconds = 0;
}
}
}
|
The serial monitor just displays 0 all the time.
I am using ccs PCWH compiler version 5.074. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Tue Sep 05, 2017 9:50 am |
|
|
Have you tried a '1Hz LED' program and confirmed the PIC is running ?
This will confirm the PIC and PCB are functional....
What caps are you using for the 20MHz xtal ?
Typically 15 to 22 puffs is OK.
Have a look at the software RTC 'sticky' in the code library if you want precise timing.
When displaying data it's always nice to say xx seconds, not just xx. This self confirms the serial is operating at correct speed.
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 05, 2017 10:46 am |
|
|
Look at the .LST file for your program. Notice the archaic setup_counters()
function doesn't generate any ASM code. Get rid of it.
Use setup_timer_0() instead.
Quote: | ................... {
.................... set_rtcc(0);
0139: MOVLB 00
013A: CLRF TMR0
.................... setup_counters (RTCC_INTERNAL, RTCC_DIV_256);
.................... setup_comparator (NC_NC_NC_NC);
013B: MOVLB 02
013C: CLRF CM1CON1
013D: CLRF CM1CON0
013E: CLRF CM2CON1
013F: CLRF CM2CON0
....................
|
The setup_timer_0() function does actually produce code. Use it:
Code: |
........... setup_timer_0(T0_INTERNAL | T0_DIV_256 | T0_8_BIT);
013B: MOVLB 01
013C: MOVF OPTION_REG,W
013D: ANDLW C0
013E: IORLW 07
013F: MOVWF OPTION_REG |
|
|
|
POPE19
Joined: 27 Jun 2017 Posts: 71
|
|
Posted: Tue Sep 05, 2017 11:48 am |
|
|
Thanks pcm programmer it is working now. |
|
|
|