Problems with delay for phase firing [SOLVED]
Posted: Fri Jul 24, 2020 5:34 am
Hi, I'm working on a program where I control the phase firing of a heater using a microcontroller. I am using the interrupt on change on pin RB2 of the PIC16f887 to check for the zero crossing moment of the AC line. At the Positive cycle, the input to the PIC is low, at the negative cycle, the input to the PIC is high. The problem is the {void Pause(double milliseconds)} function is not delaying the pulse I send for the phase firing ciruit. Am I passing the value wrongly? Please help
Thank you in advance
- 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