View previous topic :: View next topic |
Author |
Message |
anishpsla
Joined: 31 Dec 2014 Posts: 9
|
Timer0 Interrupt not working |
Posted: Wed Dec 31, 2014 1:49 pm |
|
|
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
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 31, 2014 4:25 pm |
|
|
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 ? |
|
![](templates/subSilver/images/spacer.gif) |
anishpsla
Joined: 31 Dec 2014 Posts: 9
|
|
Posted: Wed Dec 31, 2014 11:18 pm |
|
|
I am new to CCS. I just testing the new compiler. So please help me by posting the full code. Thanks |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 31, 2014 11:25 pm |
|
|
Add this line to your #fuses:
|
|
![](templates/subSilver/images/spacer.gif) |
anishpsla
Joined: 31 Dec 2014 Posts: 9
|
|
Posted: Thu Jan 01, 2015 12:38 am |
|
|
Thanks for your reply. I will post the result after checking the code. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Thu Jan 01, 2015 3:45 am |
|
|
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). |
|
![](templates/subSilver/images/spacer.gif) |
anishpsla
Joined: 31 Dec 2014 Posts: 9
|
|
Posted: Thu Jan 01, 2015 4:33 am |
|
|
Very good explanation. Thanks for your reply. |
|
![](templates/subSilver/images/spacer.gif) |
anishpsla
Joined: 31 Dec 2014 Posts: 9
|
|
Posted: Thu Jan 01, 2015 7:41 am |
|
|
the interrupt code still not working. I am simulating the code in Proteus. But the blink led working |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Thu Jan 01, 2015 7:55 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
anishpsla
Joined: 31 Dec 2014 Posts: 9
|
|
Posted: Thu Jan 01, 2015 10:10 am |
|
|
Finally the code worked with minor changes. Thanks everyone for the help. |
|
![](templates/subSilver/images/spacer.gif) |
|