View previous topic :: View next topic |
Author |
Message |
BartDelboo
Joined: 26 Feb 2018 Posts: 5
|
Timer 0 not running on PIC18F47K42 |
Posted: Mon Feb 26, 2018 4:32 am |
|
|
Hello,
On the PIC18F47K42, i am using timer 0 to update a display every 300ms.
I am upgrading from a PIC18F46K20 were it did work, but the timer was called RTCC. I changed the names to timer0, but now the timer is not working.
Following is the code where i set up the timer:
Code: |
setup_timer_0(T0_INTERNAL|T0_DIV_256|T0_8_BIT);//Timer 0 instellen
set_timer0(0); //Timer 0 starten
|
And enabling interrupts:
Code: |
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0); //Interrupts enable timer 0
|
What am i doing wrong?
Thanks! |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9174 Location: Greensville,Ontario
|
|
Posted: Mon Feb 26, 2018 5:52 am |
|
|
Without seeing a complete program, I'd think it's because you do not have a 'handler' for the timer0 interrupt. Whenever you enable any interrupt you MUST have an ISR (Interrupt Service Routine) for it otherwise the PIC will 'crash', sooner of later. |
|
![](templates/subSilver/images/spacer.gif) |
BartDelboo
Joined: 26 Feb 2018 Posts: 5
|
Timer 0 not running on PIC18F47K42 (small test program) |
Posted: Mon Feb 26, 2018 7:36 am |
|
|
Thank you for your quick answer.
I made a small test program, that i think should work. But it does not work.
Code: |
/*
* File: main.c
* Author: bartd
*
* Created on 26 februari 2018, 14:18
*/
// system parameters ----------------------------------------------------------
#if defined(__PCH__)
#include <18F47K42.h>
#include <stdlib.h>
#ifdef _WDT_
#fuses HS,WDT,PROTECT,NOLVP,BROWNOUT,NOPBADEN
#else
#fuses HS,NOWDT,PROTECT,NOLVP,BROWNOUT//,NOPCADEN
#endif
#OCS 4 MHz
#use delay(clock=4000000) // xtal-frequency
#define OUTPUT1 PIN_a2 //Output pin 1
int Memory = 0;
int Main_Execution_Allowed = 0;
#INT_TIMER0
void RTCC_isr(void) //interrupt timer 0
{
if (Memory == 1)
{
output_bit(OUTPUT1, 0);
Memory = 0;
}
else
{
output_bit(OUTPUT1, 1);
Memory = 1;
}
}
void main(void) {
setup_timer_0(T0_INTERNAL|T0_DIV_256|T0_8_BIT|T0_INPUT_SYNCRONIZED);//Timer 0 instellen
set_timer0(0); //Timer 0 starten
Main_Execution_Allowed = 0; //Set to 1 to test output
while(TRUE)
{
//While loop
if(Main_Execution_Allowed == 1)
{
if (Memory == 1)
{
output_bit(OUTPUT1, 0);
Memory = 0;
}
else
{
output_bit(OUTPUT1, 1);
Memory = 1;
}
delay_ms(1000);
}
}
}
|
This should clarify things up. I have an external 4 MHz crystal.
I expect to have my output toggled every 300ms by the ISR. But it does nothing.
To test my output, i do the same in the main loop, but use delay_ms function. I can enable this by setting Main_Execution_Allowed to 1.
Can you find what is going wrong? Has somebody already used this PIC with CCS ? Because i am also having trouble with the I2C, i am thinking the header file of the PIC may have faults in it?
Thanks in advance
Bart Delboo |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19372
|
|
Posted: Mon Feb 26, 2018 9:33 am |
|
|
You are not enabling the interrupt at any point. Not going to do anything.... |
|
![](templates/subSilver/images/spacer.gif) |
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Mon Feb 26, 2018 11:17 am |
|
|
On PIC18F46K40 I had to add the following to get TIMER0 to work with interrupts.
Not sure if the K42 Have the same problem. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19372
|
|
Posted: Mon Feb 26, 2018 12:19 pm |
|
|
The internal clock does not support sync or async. Since it is coming from the same clock source it is always running synchronised.
He just needs to enable the Timer1 interrupt, and the global interrupt.... |
|
![](templates/subSilver/images/spacer.gif) |
BartDelboo
Joined: 26 Feb 2018 Posts: 5
|
|
Posted: Tue Feb 27, 2018 1:22 am |
|
|
Ok, my fault. My small example program could not work without enabling interrupts . Now it's working.
Thanks!
I am going to pull my large program that worked for the PIC18F46K20 into pieces, so that i can know where the problem is.
Very quick response. I appreciate that! |
|
![](templates/subSilver/images/spacer.gif) |
|