Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Timer0 and INT interrupt in PIC16F628A

Status
Not open for further replies.

shoeb.eee

Member level 1
Joined
Mar 28, 2009
Messages
36
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,553
I want to program PIC mc to get a delayed output after external interrupt occurs. i.e. whenever INT pin get triggered the timer0 will start counting and when the timer overflow occurs it will turn a LED.

The logic seems very simple. But I could not achieve the goal!
Steps are as below -
1. enable external interrupt INT
2. Then Timer0 will be enabled
3. TMR0 will start counting
4. TMR0 overflows occurs and RA0 will be high
5. LED will be on
5. After 1micro sec LED will be again off.

I did it using Flowcode for simplicity. Could anybody please help me to do this?

Code:
#define MX_PIC

//Defines for microcontroller
#define P16F628A
#define MX_EE
#define MX_EE_TYPE1
#define MX_EE_SIZE 128
#define MX_UART
#define MX_UART_B
#define MX_UART_TX 2
#define MX_UART_RX 1
#define MX_PWM
#define MX_PWM_CNT 1
#define MX_PWM_TRIS1 trisb
#define MX_PWM_1 3

//Functions
#define MX_CLK_SPEED 4000000
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic.h>
#endif

//Configuration data
#ifdef _BOOSTC
#pragma DATA 0x2007, 0x3f38
#endif
#ifdef HI_TECH_C
__CONFIG(0x3f38);
#endif

//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode V4\FCD\internals.h"

//Macro function declarations
void FCM_tint();  // this macro is called when timer0 interrupt occurs
void FCM_tm1(); //this macro is called when INT (pin RB0) interrupt occurs


//Variable declarations


//Defines:

/**** Macro Substitutions ****
porta = LED Port Register
trisa = LED Data Direction Register
1 = LED Pin Mask
1 = LED Active Polarity
******************************/




//LED(0): //Macro function declarations

void FCD_LED0_LEDOn();
void FCD_LED0_LEDOff();



//LED(0): //Macro implementations


void FCD_LED0_LEDOn()
{
	
		#ifdef MX_10F_TRIS
			tvar = tvar & ~1;
			asm("movf(_tvar),w");
			asm("tris 6");
		#else
			trisa = trisa & ~1;			//Convert pin to output
		#endif

			if( 1 )					//Active high polarity
				porta = porta | 1;
			else					//Active low polarity
				porta = porta & ~1;

}

void FCD_LED0_LEDOff()
{
	
		#ifdef MX_10F_TRIS
			tvar = tvar & ~1;
			asm("movf(_tvar),w");
			asm("tris 6");
		#else
			trisa = trisa & ~1;			//Convert pin to output
		#endif

			if( 1 )					//Active high polarity
				porta = porta & ~1;
			else					//Active low polarity
				porta = porta | 1;

}

//Macro implementations

void FCM_tint()
{
	
	//C Code
	//C Code:
	
	tmr0 = 128;



	//Call Component Macro
	//Call Component Macro: LED(0)::LEDOn
	FCD_LED0_LEDOn();


	//Delay
	//Delay: 1 us
	delay_us(1);


	//Call Component Macro
	//Call Component Macro: LED(0)::LEDOff
	FCD_LED0_LEDOff();


}

void FCM_tm1()
{
	
	//Interrupt (timer0)
	//Interrupt: Enable TMR0
	cr_bit(option_reg,T0CS);
	st_bit(option_reg,T0SE);
	option_reg = (option_reg & 0xF0) | 0x07;
	st_bit(intcon,GIE);
	st_bit(intcon, T0IE);


}

void main()
{
	
	//Initialisation
	cmcon = 0x07;


	//Interrupt initialisation code
	option_reg = 0xC0;


	//Interrupt (INT)
	//Interrupt: Enable RB0INT
	st_bit(option_reg,INTEDG);
	st_bit(intcon,GIE);
	st_bit(intcon, INTE);


	//Loop
	//Loop: While 1
	while (1)
	{
	}


	mainendloop: goto mainendloop;
}

void MX_INTERRUPT_MACRO(void)
{
	//Handler code for [RB0INT]
	#ifndef MX_INTHANDLER_intcon_INTF
	#define MX_INTHANDLER_intcon_INTF
	if (ts_bit(intcon, INTF) && ts_bit(intcon, INTE))
	{
		FCM_tm1();
		cr_bit(intcon, INTF);
	}

	//Handler code for [TMR0]
	#ifndef MX_INTHANDLER_intcon_T0IF
	#define MX_INTHANDLER_intcon_T0IF
	if (ts_bit(intcon, T0IF) && ts_bit(intcon, T0IE))
	{
		FCM_tint();
		cr_bit(intcon, T0IF);
	}
	
}
 

If you are using a timer it you must enable also the PEIE bit:

INTCON.6 (PEIE), Peripheral Interrupt Enable bit ... 1 = Enables all unmasked peripheral interrupts
 

Thanks a lot Zuisti for your quick reply

I did not find any tutorial regarding PEIE bit of INTCON register. I read a tutorial from microchip which used only the following bits
GIE: Global Interrupt Enable bit
T0IE: Timer0 Overflow Interrupt Enable bit
T0IF: Timer0 Overflow Interrupt Flag bit

Could you please clarify this thing?
 

I did not find any tutorial regarding PEIE bit of INTCON register.
...
Could you please clarify this thing?

Hi;
You are right, it is not as easy to find a proper clarifying for the PEIE bit
(almost nothing in the datasheet).

Look at this site:
PIC Interrupts - MicroAutomate

"Peripheral interrupts contains internal EEPROM, Serial Comms (USART, SPI/I2C), Timers, Analog/Digital converting, etc."
These all require also the set of PEIE
...
Meanwhile, I became a little uncertain: it may be that the timer 0 is an exception? 8-O , sorry
But ... see also this:
Timer0 Programming | PIC 2 LEARN
 
Last edited:

How can I pass variable to ISR function? I'm using Hi Tech C.

I defined a variable in the main function whose value varies with pulse in input PORT pins. When External Interrupt will occur occurs the ISR function will be called and in that ISR function the value need to be used.

ISR (){
char a,b;
b=1;
a=b+z;

}

main (){
char z;
z=9
}

It does not compile and gives the error for z in ISR as "undefined identifier"

How can I do it?

---------- Post added at 19:54 ---------- Previous post was at 19:09 ----------

How can I pass variable to ISR function? I'm using Hi Tech C.

I defined a variable in the main function whose value varies with pulse in input PORT pins. When External Interrupt will occur occurs the ISR function will be called and in that ISR function the value need to be used.

ISR (){
char a,b;
b=1;
a=b+z;

}

main (){
char z;
z=9
}

It does not compile and gives the error for z in ISR as "undefined identifier"

How can I do it?
 

Simply define the variable "z" at begin, outside of all functions.
 

I wrote the following code (Hi Tech C) using INT and Timer0 and it is working in MPLAM as expected. But it is not working in Proteus. But I cannot find the reason why it is not working in Proteus. Could anybody please help me in this regard?

Function of this program:
When a pin change interrupt occurs it starts counting with Timer0 and set output at RA0 after 5.12mili second (inidelay 20). This delay can be increased/decreased by using RB1 & RB2.

[4MHz internal clock, 256 prescaler, Timer0]

Code:
#define _XTAL_FREQ 4000000
#include <htc.h>

char iniDelay,count;

void interrupt isr(void)
{
	if(INTCON & (1<<1))	//check Ext Int Flag (INTF)
	{
		PORTA &=~1; 		//clear RA0
		INTCON |=(1<<5);	//set TMR0 Overflow Int Enable bit (T0IE)
    	TMR0=256-iniDelay;
    	INTCON &=~ (1<<1);	//clear Ext Int Flag (INTF)
	}

	if(INTCON & (1<<2))	//check TMR0 overflow Int Flag (T0IF)
	{
		INTCON|=(1<<4);		//set Ext Int Enable bit (INTE)
		INTCON &=~ (1<<2);	//clear TMR0 Overflow Interrupt Flag bit (T0IF)
		INTCON &=~ (1<<5);	//clear TMR0 Overflow Int Enable bit (T0IE)
		PORTA |= 1;			//set RA0
	}	

}

 
void main()
{
char pB,p,pB2,pOut;	


	CMCON = 0b00000111; 	//comperators are off
	
	OPTION_REG=(1<<6);		//set Interrupt Edge Select bit, INTEDG (rising)							//clear bit#7: enable PORTB pull-up resistors
	OPTION_REG|=(1<<3);		
	OPTION_REG|=0b00000111;	//Prescaler Rate for TMR0, 1:256
	
	INTCON |=((1<<7) | (1<<4)); //set GIE and INTE
	
	TRISA = 0x00;	//PortA as Output
	TRISB = 0xFF;	//PortB as input
PORTA=0xff;
	iniDelay=20;
	//PORTA &=~(1<<1); //clear 

start:
	PORTA &=~1;

	pB=PORTB;
	while (pB>1 && pB<8)
	{ 
  
scan:
	pB2=PORTB;
	if (pB2<=1)
	{

		if (pB>=2 && pB<4)
		{
			if (iniDelay<36){
    		iniDelay=iniDelay+2;
			}
		}
	
		if (pB>=4 && pB<8)
		{
			if (iniDelay>4){
    		iniDelay=iniDelay-2;
			}
		}
	
		pB=0;
	}
else goto scan;

	}	//end while loop

goto start;
}
 
Last edited:

Could anybody please check the code and suggest something to do?
 

Check in htc.h file, looks like the defined memory location is not available in H/W
 

Check in htc.h file, looks like the defined memory location is not available in H/W

Thanks ravurig for your reply. But it is not clear to me what is needed to be checked in htc.h file.

I did not make any change in the htc.h file. This is the default header file of high tech c. Moreover this code is working in MPLAB SIM.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top