Reputation: 11
I am trying to measure two PWM input signals and, based on them, turn a LED on or off. I am using a PIC16F18313 microcontroller.
I have a Futaba remote control, and through its receiver, I receive two PWM signals when toggling a switch. One signal has a pulse width of 1 ms, and the other has a pulse width of 1.5 ms.
Both signals are fed into the RA2 pin. I want to turn off the LED when the input PWM is 1 ms and turn it on when the input PWM is 1.5 ms.
#include <xc.h>
#include <pic16f18313.h>
#pragma config FEXTOSC = OFF
#pragma config RSTOSC = HFINT1
#pragma config WDTE = OFF
#pragma config MCLRE = OFF
#define _XTAL_FREQ 4000000
volatile unsigned int time_diff = 0;
volatile unsigned int prev_capture = 0;
volatile unsigned int capture_value = 0;
volatile unsigned char edge_flag = 0;
void __interrupt() ISR(void) {
if (PIR4bits.CCP1IF) {
PIR4bits.CCP1IF = 0;
capture_value = (CCPR1H << 8) | CCPR1L;
if (RA2 == 1 && edge_flag == 0) {
prev_capture = capture_value;
edge_flag = 1;
CCP1CON = 0b00000101;
} else if (RA2 == 0 && edge_flag == 1) {
time_diff = capture_value - prev_capture;
edge_flag = 0;
CCP1CON = 0b00000111;
if (time_diff >= 850 && time_diff <= 1150) {
LATA4 = 0;
} else if (time_diff >= 1350 && time_diff <= 1650) {
LATA4 = 1;
}
}
}
}
void main(void) {
OSCFRQ = 0x03;
TRISA2 = 1;
TRISA4 = 0;
ANSELA = 0;
T1CON = 0b00110001;
CCP1CON = 0b00000101;
CCP1EN = 1;
PIR4bits.CCP1IF = 0;
PIE4bits.CCP1IE = 1;
INTCONbits.GIE = 1;
while (1);
}
Upvotes: 1
Views: 17