shoeb.eee
Member level 1
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?
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);
}
}