- Code: Select all
#pragma config CONFIG1 = 0x2CD2
#pragma config CONFIG2 = 0x0700
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#define _XTAL_FREQ 20000000
void initPhase(void);
void Pause(double);
static double C = 0;
double X = 0;
double milliseconds;
void initPhase(void)
{
PORTB = 0;
ANSELH = 0x00;
TRISBbits.TRISB0 = 1;
TRISAbits.TRISA4 = 0;
WPUB = 0b00000001;
IOCB = 0x04;
OPTION_REGbits.nRBPU = 1; // clear RBPU bit (OPTION_REG.7)
OPTION_REGbits.INTEDG = 1;
INTCONbits.GIE = 1; //global interrupt enable
INTCONbits.PEIE = 0; //disable peripheral interrupts (PEIE)
INTCONbits.RBIE = 1;
INTCONbits.INTE = 1;//For external interrupt (RB0/INT)PORTB = 0;
}
void light(X)
{
Pause(X);
PORTAbits.RA4 = 1;
__delay_ms(0.5);
PORTAbits.RA4 = 0;
}
void Pause(double milliseconds)
{
while(milliseconds > 0)
{
__delay_ms(0.1);
milliseconds = milliseconds - 0.1;
}
}
void __interrupt () isr()
{
INTCONbits.GIE = 0;
if (INTCONbits.RBIF == 1)
{
if (PORTBbits.RB2 == 1)
{
__delay_ms(1);
light(C);
}
else
light(C);
INTCONbits.RBIF = 0;
INTCONbits.GIE = 1;
}
if (INTCONbits.INTF == 1)
{
if(PORTBbits.RB1 == 1)
{
PORTBbits.RB1 = 0;
}
else if (PORTBbits.RB1 == 0)
{
PORTBbits.RB1 = 1;
}
INTCONbits.INTF = 0;
INTCONbits.GIE = 1;
}
}
void main(void)
{
TRISBbits.TRISB1 = 0;
PORTBbits.RB1 = 0;
__delay_ms(1000);
initPhase();
while(1)
{
C=0.8;
}
}
Thank you in advance