I'm using a PIC18F2455 microcontroller, I want the MCU detect a square wave and begins to send a signal for 0.5 seconds, and when the square wave stops, it can send another signal from another port for 0.5 seconds, The code complies but just doesn't function properly. (Works sometimes but fail in others )
could somebody help me with this , thanks
[MODNOTE]Please use Code Tags The "#" symbol[/MODNOTE]
could somebody help me with this , thanks
Code:
#include <stdio.h>
#include <stdlib.h>
#include <delays.h>
#include <p18f2455.h>
#pragma config FOSC = HS // Oscillator Selection:External oscillator
#pragma config WDT = OFF // Watchdog Timer
#pragma config LVP = OFF // Low Voltage ICSP(PIN 6):Disabled
unsigned int intr;
unsigned int state;
void InterruptHandlerHigh (void);
void main(void)
{
state = 0;
intr = 0;
TRISC = 0;
PIE2bits.CMIE = 1;
TRISAbits.TRISA0 = 1;
TRISAbits.TRISA1 = 0;
TRISAbits.TRISA2 = 0;
TRISAbits.TRISA3 = 1;
TRISAbits.TRISA4 = 0;
TRISAbits.TRISA5 = 0;
TRISC = 0;
INTCONbits.PEIE = 1 ;
INTCONbits.GIE = 1;
CMCON = 0b00000001;
while (1)
switch (state)
{ case (0):
PORTAbits.RA5 = 0;
PORTCbits.RC0 = 0;
Delay10KTCYx(20);
if ( intr)
{ state = 1;
intr = 0;
}
else {state = 0;}
break;
case (1):
PORTAbits.RA5 = 0;
PORTCbits.RC0 =1;
Delay10KTCYx(100);
state = 2;
intr = 0;
break;
case (2):
PORTAbits.RA5 = 0;
PORTCbits.RC0 =0;
Delay10KTCYx(20);
if (intr)
{
intr = 0;
state = 2 ;}
else {
intr=0;
state = 3;
}
break;
case (3):
PORTAbits.RA5 = 0;
PORTCbits.RC0 =0;
Delay10KTCYx(20);
if (intr)
{
intr = 0;
state = 2;}
else {state = 4;
intr = 0;
}
break;
case (4):
PORTAbits.RA5 = 1;
PORTCbits.RC0 =0;
Delay10KTCYx(100);
state = 0;
intr = 0;
break;
default:
PORTAbits.RA5 = 0;
PORTCbits.RC0 = 0;
state = 0;
break;}
}
//**BEGIN INTERRUPT CONTROL**
#pragma code InterruptVectorHigh = 0x08 //interrupt pointer address (0x08 high priority)
void InterruptVectorHigh (void)
{
_asm //assembly code starts
goto InterruptHandlerHigh //interrupt control
_endasm //assembly code ends
}
#pragma code
#pragma interrupt InterruptHandlerHigh //end interrupt control
void InterruptHandlerHigh() // Declaration of InterruptHandler
{
if(PIR2bits.CMIF){
intr = 1;
PIR2bits.CMIF = 0; //Clear Flag
}
INTCONbits.GIE = 1;
PIE2bits.CMIE = 1; //Re-enable all interrupts
return;
}
[MODNOTE]Please use Code Tags The "#" symbol[/MODNOTE]
Last edited by a moderator: