Capture PWM from external source

Status
Not open for further replies.

Ilia Gildin

Junior Member level 3
Joined
Sep 27, 2014
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
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; 
   }    
	}
}
}
 

Hi,

its not working
This could be a microsoft windows error message...

What did you expect?
And what is not like expected?


Klaus
 

hello


Why are you using polling mode for testing "PIR1bits.CCP1I"
instead of interrupt ?
if your PWM frequency input is High, very difficult to keep synchronisation with the signal...


and you have choosen iNTERNAL FOSC wich is by default at 1Mhz only..

Code:
	OSCCONbits.SCS=0x10;     // see DS page 37	
	OSCCONbits.IRCF	= 7;    // choix 16Mhz
	/*
	OSCCONbits.IRCF	= 6;    // choix 8Mhz
	OSCCONbits.IRCF	= 5;    // choix 4Mhz
	OSCCONbits.IRCF	= 4;    // choix 2Mhz
    OSCCONbits.IRCF	= 3;    // choix 1Mhz par defaut !!!
   */

don't forget also to count and add number of overflow (* 65536) of timer1, if necessary.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…