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 CCS Technical Support

PIC12f629 Timer0 Help

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



Joined: 18 Jul 2006
Posts: 103

View user's profile Send private message

PIC12f629 Timer0 Help
PostPosted: Mon May 07, 2012 2:04 pm     Reply with quote

Hello all,

I am using a pic12f629 and compiler version 4.103
I have written a simple program that toggles a "Heartbeat" LED on PIN_A4. I am trying to use the internal clock so I don’t have to add an external oscillator.

When I compile the code below and then run it the LED is pulsed at 1MHz.

Any ideas on what I am missing on this wonderful Monday afternoon.

Thanks
Code:

#include <12f629.h>

// This program is for the winch control

#fuses NOWDT,NOPROTECT,MCLR,PUT,NOBROWNOUT,INTRC
#use delay(clock = 4000000)

#define PULSE_OUT PIN_A4

int8   pulse_timer;      // used for pulse


#int_rtcc
void clock_isr()
{
   set_timer0(178);
   pulse_timer++;      // Used for Heartbeat
   disable_interrupts(GLOBAL);

   if (pulse_timer >=100)         // Toggle Heartbeat
   {
      OUTPUT_TOGGLE(PULSE_OUT);
      pulse_timer = 0;
   }
   enable_interrupts(GLOBAL);
}

void main()
{
   disable_interrupts(GLOBAL);
   setup_timer_0(T0_INTERNAL | RTCC_DIV_32);   
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   while (1==1)
   {
      delay_cycles(1);
//
//      Rest of code goes here
//
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19484

View user's profile Send private message

PostPosted: Mon May 07, 2012 2:13 pm     Reply with quote

_Never_ ever, ever, have the line 'enable_interrupts(GLOBAL)', inside an interrupt handler. This _will_ crash the chip.

On the PIC, recursion is not allowed (interrupts inside themselves, or function calls that call themselves). To _ensure_ that an interrupt cannot be called inside itself, the _hardware_ disables the global interrupt bit, when the interrupt handler is called, and automatically re-enables it, _the instruction after_ the code returns from the interrupt. Enabling it inside the interrupt _will_ result in the code continually looping, and never leaving the interrupt handler - especially since at the point where you enable the interrupt, the rest of the interrupt code (including the code that actually clears the interrupt, which the compiler automatically generates), has not been called.....

So you don't need to either disable or enable the global bit inside the handler. The former won't do any harm, but the latter is roughly equivalent to taking a hammer,and hitting yourself on the head with it.

Best Wishes
edbfmi1



Joined: 18 Jul 2006
Posts: 103

View user's profile Send private message

PostPosted: Mon May 07, 2012 2:18 pm     Reply with quote

Hi all,

I just saw the error of my ways.
Code:

#fuses NOWDT,NOPROTECT,MCLR,PUT,NOBROWNOUT,INTRC

should have been
Code:

#fuses NOWDT,NOPROTECT,MCLR,PUT,NOBROWNOUT,INTRC_IO


I was accidentally putting the clock out on PIN_A4.

Thanks for the heads up on the enable_interrupts(GLOBAL) Ttelmah.
I will take care of 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