Ilia Gildin
Junior Member level 3
- Joined
- Sep 27, 2014
- Messages
- 26
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 184
I wrote program to capture PWM from external source and its not working please help
Code:
#pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF // CONFIG1H
#pragma config WDTEN = OFF, WDTPS = 2048 // CONFIG2H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L
#pragma config CPB = OFF, CPD = OFF // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L
#pragma config EBTRB = OFF , PBADEN = OFF ,MCLRE = EXTMCLR , CCP2MX = PORTB3
#pragma config P2BMX = PORTD2
#include <p18f46k22.h>
#include <stdlib.h>
#include <stdio.h>
// *************** Initialize Capture mode for PWM input *************************
void main (void)
{
unsigned int uPWM_Low;
unsigned int uPWM_Hi;
unsigned int bRisingEdge;
unsigned int uPWM;
ANSELB=0b0;
ANSELC=0b0;
TRISC=0b00000011;
TRISB=0b0;
//trigger on rising edge
uPWM=0;
bRisingEdge=1;
PORTBbits.RB1=0b0;
PORTBbits.RB3=0b0;
PORTBbits.RB0=0b0;
PORTBbits.RB2=0b0;
T1CON= 0b00000111;
CCP1CON=0b0000101;
PIE1bits.CCP1IE=0b1;
CCPTMRS0bits.C1TSEL0 = 0;
CCPTMRS0bits.C1TSEL1 = 0;
while(1)
{
if (PIR1bits.CCP1IF==0b1)
{
// get PWM capture reading
uPWM_Low = CCPR1L;
uPWM_Hi = CCPR1H;
uPWM = CCPR1H*256UL|CCPR1L;
if (bRisingEdge=1)
{
// Set trigger on falling edge
CCP1CON = 0b00000100;
bRisingEdge = 0;
PORTBbits.RB3=0b1;
PORTBbits.RB2=0b1;
PORTBbits.RB1=0b1;
PORTBbits.RB0=0b1;
PIR1bits.CCP1IF=0b0;
}
else
{
// Set trigger on rising edge
CCP1CON = 0b00000101;
bRisingEdge = 1;
PORTBbits.RB3=0b1;
PORTBbits.RB2=0b1;
PORTBbits.RB1=0b1;
PORTBbits.RB0=0b1;
PIR1bits.CCP1IF=0b0;
}
}
}
}