Jump to content
Baixue

Timer0 Interrupt Problem

Recommended Posts

I am trying to learn timer0 interrupt on 16f690, but seems like what my code does is stay in the ISR when the first interrupt happens and never go back to main routine. Could someone help me? Appreciate a lot!!!!!

 

#include <system.h>
#pragma DATA 0x2007, _HS_OSC & _WDT_OFF

int counter=0;
void main()
{

trisc=0x00;// set portc as output
portc=0x00;// initialize portc


option_reg=0b10000111; // set prescaler as 1:256
set_bit(intcon,7); // set GIE to enable all the interrupts
clear_bit(intcon,6); // clear PEIE to disable all peripheral interrupts
set_bit(intcon,5); //set T0IE to enable the timer0 interrupt
clear_bit(intcon,4); // clear INTE to disable the RA2/INT external interrupt
clear_bit(intcon,3); // clear RABIE to disable PORTA/PORTB change interrupt

while(1);
}

void interrupt()
{
clear_bit(intcon,2); // clear TOIF, the timer0 overflow interrupt flag
if (counter==25)// when the 25th interrupt happens
{
	if (portc.0==1)
	{
		portc=0b00000000; // when the led was on ,turn it off then
	}
	else
	{
		portc=0b00000001; // when the led was off, turn it on then
	}

	counter=0;
}
else
{
	counter++;
}

}

Share this post


Link to post
Share on other sites

My post about timer0 may help you in this post:

http://forum.sourceboost.com/index.php?showtopic=3717

 

Also, at the end of your interrupt routine, you need to set the GIE bit again. It is automatically cleared in hardware when an interrupt is triggered so the currently running interrupt routine can't itself be interrupted. (Page 208 of the data sheet).

 

- Bill

Share this post


Link to post
Share on other sites

Your code runs OK using the simulator.

 

The GIE bit is set (enabled again) automatically when the return from interrput instruction is executed so you have to do nothing.

 

If you have the equipment check your oscillator is running correctly and other hardware connections.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
...

The GIE bit is set (enabled again) automatically when the return from interrput instruction is executed so you have to do nothing.

...

 

I missed that little fact. I guess I can save myself another instruction whenever I put an interrupt into my project.

 

- Bill

Share this post


Link to post
Share on other sites
Your code runs OK using the simulator.

 

The GIE bit is set (enabled again) automatically when the return from interrput instruction is executed so you have to do nothing.

 

If you have the equipment check your oscillator is running correctly and other hardware connections.

 

Cheers

 

Reynard

 

The hardware I use is Low Pin Count Demo Board. I didn't put on any extra stuff on the board and the board is very simple, just has a pic16f690 and four leds on it.

I tested the my Timer0 interrupt code today(the one I posted), it still doesn't work ;)

Share this post


Link to post
Share on other sites

Baixue,

 

Here is something that I should have picked up on straight away. The analogue adc channel are selected on chip reset. Try setting ANSEL = 0 and ANSELH = 0 to turn all I/O ports into digital signals.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
Baixue,

 

Here is something that I should have picked up on straight away. The analogue adc channel are selected on chip reset. Try setting ANSEL = 0 and ANSELH = 0 to turn all I/O ports into digital signals.

 

Cheers

 

Reynard

 

Yeah! I got it work now! Thank you so much Reynard! And thank you all guys! But I wonder do ANSEL and ANSELH have to be treated when you want to make the ports deal with digital signals? Because when I first learn microcontroller to make the port as digital output, I didn't deal with ANSELH stuff and it worked. Thank you!!

 

Regards,

Baixue

Share this post


Link to post
Share on other sites

Many learn the PIC using kits at college which have the PIC16F84 or similar basic device. These basic devices of course do not have fancy peripherals such as the ADC module, hence you do not learn how to use them.

 

I can only presume the ADC inputs are default so the PIC resets into the lowest possible current mode (with perhaps the exception of the oscillator). A lot of PICs are used in portable, lithium coin cell powered devices. These devices usually sit in sleep mode waiting for someone to press the ON button to wake them up. Before entering sleep mode, the program shuts down all peripherals (internal and external), drops the clock rate to LC, makes output pins inputs. Only port pins that are required to wake the device up remain active (ON button for instance using an interrupt). Switching input pins to analogue mode also saves input pin current making the batteries last longer.

 

I too have forgotten to turn off analogue mode several times, but you soon learn about it. Reading the PIC data sheet about the ports is always helpful. I/O ports are not all equal. Some pins are input only, some open drain, some have pull-ups, some have interrupts, SOME HAVE ANALOGUE INPUTS.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
Many learn the PIC using kits at college which have the PIC16F84 or similar basic device. These basic devices of course do not have fancy peripherals such as the ADC module, hence you do not learn how to use them.

 

I can only presume the ADC inputs are default so the PIC resets into the lowest possible current mode (with perhaps the exception of the oscillator). A lot of PICs are used in portable, lithium coin cell powered devices. These devices usually sit in sleep mode waiting for someone to press the ON button to wake them up. Before entering sleep mode, the program shuts down all peripherals (internal and external), drops the clock rate to LC, makes output pins inputs. Only port pins that are required to wake the device up remain active (ON button for instance using an interrupt). Switching input pins to analogue mode also saves input pin current making the batteries last longer.

 

I too have forgotten to turn off analogue mode several times, but you soon learn about it. Reading the PIC data sheet about the ports is always helpful. I/O ports are not all equal. Some pins are input only, some open drain, some have pull-ups, some have interrupts, SOME HAVE ANALOGUE INPUTS.

 

Cheers

 

Reynard

 

Thank you so much Reynard!!!!! That make a lot sense!!! ;)

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...