CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Timer 0 not running on PIC18F47K42

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BartDelboo



Joined: 26 Feb 2018
Posts: 5

View user's profile Send private message

Timer 0 not running on PIC18F47K42
PostPosted: Mon Feb 26, 2018 4:32 am     Reply with quote

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!
temtronic



Joined: 01 Jul 2010
Posts: 6332
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Feb 26, 2018 5:52 am     Reply with quote

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.
BartDelboo



Joined: 26 Feb 2018
Posts: 5

View user's profile Send private message

Timer 0 not running on PIC18F47K42 (small test program)
PostPosted: Mon Feb 26, 2018 7:36 am     Reply with quote

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
Ttelmah



Joined: 11 Mar 2010
Posts: 13788

View user's profile Send private message

PostPosted: Mon Feb 26, 2018 9:33 am     Reply with quote

You are not enabling the interrupt at any point. Not going to do anything....
alan



Joined: 12 Nov 2012
Posts: 280
Location: South Africa

View user's profile Send private message

PostPosted: Mon Feb 26, 2018 11:17 am     Reply with quote

On PIC18F46K40 I had to add the following to get TIMER0 to work with interrupts.

Code:
    T0ASYNC = TRUE;


Not sure if the K42 Have the same problem.
Ttelmah



Joined: 11 Mar 2010
Posts: 13788

View user's profile Send private message

PostPosted: Mon Feb 26, 2018 12:19 pm     Reply with quote

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....
BartDelboo



Joined: 26 Feb 2018
Posts: 5

View user's profile Send private message

PostPosted: Tue Feb 27, 2018 1:22 am     Reply with quote

Ok, my fault. My small example program could not work without enabling interrupts Embarassed. 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!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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