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

Timer0 Interrupt not working

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



Joined: 31 Dec 2014
Posts: 9

View user's profile Send private message

Timer0 Interrupt not working
PostPosted: Wed Dec 31, 2014 1:49 pm     Reply with quote

I am new to CCS compiler. The following code to test Timer0 simply does not working. Please help me.

Code:
#include <new_int.h>

int interrupt_counter= 0;

#INT_TIMER0
void TIMER0_isr()
{
   interrupt_counter++;
   if(interrupt_counter==61)
      output_high(LED);
}

void main()
{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);
   set_timer0(0);
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_TIMER0);
   

   while(TRUE)
   {
      //TODO: User Code
   }

}


new_int.h

Code:
#include <16F877A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(clock=20000000)
#use FIXED_IO( A_outputs=PIN_A0 )
#define LED   PIN_A0
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 31, 2014 4:25 pm     Reply with quote

Quote:
#include <16F877A.h>

#FUSES NOWDT //No Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(clock=20000000)
#use FIXED_IO( A_outputs=PIN_A0 )
#define LED PIN_A0

Where is your oscillator fuse ?
anishpsla



Joined: 31 Dec 2014
Posts: 9

View user's profile Send private message

PostPosted: Wed Dec 31, 2014 11:18 pm     Reply with quote

I am new to CCS. I just testing the new compiler. So please help me by posting the full code. Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 31, 2014 11:25 pm     Reply with quote

Add this line to your #fuses:
Code:
#FUSES HS
anishpsla



Joined: 31 Dec 2014
Posts: 9

View user's profile Send private message

PostPosted: Thu Jan 01, 2015 12:38 am     Reply with quote

Thanks for your reply. I will post the result after checking the code.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Jan 01, 2015 3:45 am     Reply with quote

As a comment, this is nothing to do with the compiler, but the _chip_.

There are certain basic settings needed to get the chip to work. You have set some of them (NOLVP for instance), but have not selected an oscillator.

Now PCM_programmer has assumed that you do 'know what you are doing' to some extent, and since you are specifying the clock at 20MHz, that you have a 20Mhz crystal attached to the oscillator pins on the chip. Do you?.
He then gives the extra fuse that is needed to use this crystal.

If you look at the chip's data sheet, you will find that the 'oscillator selection' bits default to '11', which is the setting to use an external RC oscillator. Without the required RC network, this is not going to work at all. This then comes to the second comment. There is a general 'rule' here that if a chip does not work, _start_ by verifying that the chip is actually running. Forget your code, instead run the basic 'flash an LED' test. This is:
Code:

#include <16F877A.h>

#FUSES NOWDT, NOLVP, HS

#use delay(clock=20000000)

void main(void)
{
   while(TRUE)
   {
      output_toggle(PIN_A0); //choose your own pin
      delay_ms(1000);
   }
}

This when working properly should have the LED flashing at a nice steady 1second on, 1second off.
If it doesn't start at all, then you have a hardware problem.
If it flashes at the wrong rate, then you have a clock problem.
If it comes on, and then stops, then you probably don't have a suitable current limiting resistor on the PIC.
etc. etc..

You can learn more from simple 'one step at a time' operations than starting by trying to fiddle with interrupts.

Always when you get stuck, _simplify_ remove the dross till you can narrow down exactly where the problem lies.

Then there is the comment, what do you expect the LED to do in your code?.

As written, the timer is running off Fosc/(4*256), and will interrupt every 256 counts. So the interrupt will be called 76.2* per second. You count 62 times, then turn the LED on. It never goes off again, and the counter never resets. So 0.81 sec after the code starts, the LED goes 'on' and that is all it does. Every 3.3 seconds after this it is turned on again (since the counter wraps at 256).
anishpsla



Joined: 31 Dec 2014
Posts: 9

View user's profile Send private message

PostPosted: Thu Jan 01, 2015 4:33 am     Reply with quote

Very good explanation. Thanks for your reply.
anishpsla



Joined: 31 Dec 2014
Posts: 9

View user's profile Send private message

PostPosted: Thu Jan 01, 2015 7:41 am     Reply with quote

the interrupt code still not working. I am simulating the code in Proteus. But the blink led working
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Jan 01, 2015 7:55 am     Reply with quote

Read the post at the head of the forum.

Forget Proteus for trying to actually learn anything about PIC's. It will often take code that works perfectly, and tell you it doesn't work, and conversely take code that cannot run in a real chip and tell you it is fine.

Then get rid of your fixed_io settings. You should _only_ use fixed I/O, or fast_io, once you know what you are doing, and really understand 'why' these are used. Leave the compiler to handle the I/O till this is the case.

Now, I took your code, with the necessary modifications:
Code:

#include <16F877A.h>
#FUSES NOWDT, NOLVP, HS
#use delay(clock=20000000)

int interrupt_counter=0;
#define LED   PIN_A0

#INT_TIMER0
void TIMER0_isr()
{
   interrupt_counter++;
   if(interrupt_counter==61)
      output_high(LED);
}

void main()
{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);
   set_timer0(0);
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_TIMER0);
   while(TRUE)
   {
      //TODO: User Code
   }
}

I put this into a PIC16F877A, with a 20MHz crystal, and the LED merrily lit 0.8seconds after power was applied. Exactly as expected.
anishpsla



Joined: 31 Dec 2014
Posts: 9

View user's profile Send private message

PostPosted: Thu Jan 01, 2015 10:10 am     Reply with quote

Finally the code worked with minor changes. Thanks everyone for the help.
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