Jump to content
Sign in to follow this  
Daz

Interrupts

Recommended Posts

Hi,

I've been away from the programming arena for some time now and I'm just on a personal re-fresher course.

 

Please find below a program that uses 1 switch input to turn on an led.

The program doesn't work when I run it in the sourceboost debugger. The breakpoint set in the ext int routine is never reached and therefore the led never lights, it may be obvious to you experienced guys out there any pointers would be apreciated.

Thanks in advance.

  
#include <system.h>

//Target PIC16F877 configuration word
#pragma DATA _CONFIG, _PWRTE_ON & _BODEN_OFF & _WDT_OFF & _LVP_OFF & _CPD_OFF & _DEBUG_OFF & _XT_OSC & _CP_OFF

//Set clock frequency
#pragma CLOCK_FREQ	4000000


#define	button1	portb.1
#define	led1	portd.0
unsigned char flag1;

void interrupt( void )
{

//Handle external interrupt
if( intcon & (1<<INTF) )
{
	if(button1){flag1 = 1;}
	clear_bit( intcon, INTF );
}

//Handle timer0 interrupt
if( intcon & (1<<T0IF) )
{
	clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit
}

//Handle timer1 interrupt
if( pir1 & (1<<TMR1IF) )
{
	clear_bit( pir1, TMR1IF ); //clear timer 1 interrupt bit
}

//Handle timer2 interrupt
if( pir1 & (1<<TMR2IF) )
{
	clear_bit( pir1, TMR2IF ); //clear timer 2 interrupt bit
}

}

void main( void )
{
//Configure port A
trisa = 0x00;
//Configure port B
trisb = 0x02;
//Configure port C
trisc = 0x00;
//Configure port D
trisd = 0x00;
//Configure port E
trise = 0x00;

//Configure A/D pins
adcon1 = 0x06;


//Initialize port A
porta = 0x00;
//Initialize port B
portb = 0x00;
//Initialize port C
portc = 0x00;
//Initialize port D
portd = 0x00;
//Initialize port E
porte = 0x00;
//Set Timer0 mode
clear_bit( option_reg, T0CS ); //configure timer0 as a timer
//Set prescaler assignment
clear_bit( option_reg, PSA ); //prescaler is assigned to timer0
//Set prescaler rate
clear_bit( option_reg, PS2 ); //prescaler rate 1:2
clear_bit( option_reg, PS1 );
clear_bit( option_reg, PS0 );
//Set timer0 source edge selection
set_bit( option_reg, T0SE ); //increment on high-to-low transition on RA4/T0CKI pin

//Set timer 1 prescaler rate
clear_bit( t1con, T1CKPS1 ); //prescaler rate 1:1
clear_bit( t1con, T1CKPS0 );
//Set timer 1 mode
clear_bit( t1con, TMR1ON ); //disable timer 1

//Set timer 2 prescaler rate
clear_bit( t2con, T2CKPS1 ); //prescaler rate 1:1
clear_bit( t2con, T2CKPS0 );
//Set timer 2 postscaler rate
clear_bit( t2con, TOUTPS3 ); //postscaler rate 1:1
clear_bit( t2con, TOUTPS2 );
clear_bit( t2con, TOUTPS1 );
clear_bit( t2con, TOUTPS0 );
//Set timer 2 mode (enable or disable)
clear_bit( t2con, TMR2ON ); //enable timer 2


//Enable interrupts (External, Timer0)
intcon = 0xB0;


//Endless loop
while( 1 )
{
	if(flag1)
	{
		led1 = 1;
	}
}//while
}//main

Share this post


Link to post
Share on other sites

There could be two problems:

 

  1. The source boost simulator does not support external interrupts
  2. I seem to recall that when you progam a pin to be an external interrupt input, it is no longer an input port pin.

The data sheet for the device you are using will give you the answer to #2. If this is the case, you know the state of the pin since you get an interrupt while the button is pushed and not when it is not. The problem is you don't get an interrupt when the button is released so the main code would need to clear flag after it notices that it is set (change led1 = 1; to led1 = 1; flag1 = 0;).

 

Another way to go is to just set up a timer interrupt and read the value of button in the interrupt routine and set flag accordingly. This will also give you a free debounce filter. If you don't want the overhead of the timer interrupt you could also use the interrupt on port B change feature of the PIC processors. I recall that this works in a group of 4 port B pins so if you have other inputs on those lines that you don't want an interrupt for you will get them. The data sheet has more details on this feature than I can remember off the top of my head.

 

It looks like you do have a timer interrupt but you set an external source for the clock which you may not be wiggling during your simulation. You could for the short term set the source to be FOSC (the internal core clock).

 

Sorry for the rambling message.

Share this post


Link to post
Share on other sites

In addition to the above regarding debouncing, toggling the LED etc. your code has a few basic errors for real life operation (I never use the simulator so I can't comment on that):

 

You have your switch on the wrong pin to use INTF - it should be on RB0 not RB1

Depending on how your switch is connected you will probably need to set the edge direction and possibly the internal pullup in OPTION_REG.

 

PEIE should also be set to enable the interrupts (INTCON = &d0)

 

Regards

 

davidb

Share this post


Link to post
Share on other sites

Thanks for your replies.

A fundamental error on my part using rb1 as a switch input instead of rb0, the simulator now works.

In the while loop this code amendment below works correctly.

I hope these posts will serve to help others with similar problems.

	//Endless loop
while( 1 )
{
	if(flag1)
	{
		led1 = !led1;	//on/off
		flag1 = !flag1; //reset
	}
}//while

Share this post


Link to post
Share on other sites
There could be two problems:

 

 

[*]I seem to recall that when you progam a pin to be an external interrupt input, it is no longer an input port pin.

 

Hi trossin, actually u can use the external interrupt pin as input pin but only in the ISR function of the interrupt (when the flag is not cleared).

Many spi device work that way.

Regards

Raghunathan.P

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...
Sign in to follow this  

×
×
  • Create New...