Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Measuring Low frequency 18f4580 CCP1

Status
Not open for further replies.

morbak

New Member
Hi,

I want to measure Low freq (1-1kHz) with the capture CCP1. But My code isn't working correctly: Th ecalculated freq always changing and isn't ok.
I've read the AN and the datasheet, but i can explain the pb.

I use a 18f4580 @ 10 Mhz with PLL. CCP1 on RC2 and timer1 16bits.

Here's my code:

INTERRPUT

#pragma code VecteurItHi=0x08 // ALONE Définir le vecteur interruption à l'adresse 8
//#pragma code VecteurItHi=0x48 // BOOT définir le vecteur interruption à l'adresse 48
void VecteurItHi(void)
{
_asm goto Interrupt_Hi // lien avec le sousprogramme It en C
_endasm
}
#pragma code // fermer le segment avec adresse absolue
#pragma interrupt Interrupt_Hi // définir le sous-programme it

void Interrupt_Hi(void)
{
unsigned static int ancien;

if (PIR1bits.CCP1IF==1)
{
duree=CCPR1-ancien;
ancien=CCPR1;
maj=1;
PIR1bits.CCP1IF=0;
}

}


MAIN

unsigned int duree=5555;
char maj; // indique qu'une nouvelle mesure est prête
float freq;
float frpm;
float mes;
float mes2;

void main( void)
{
init_port();
maj=0;
duree=0;

T1CONbits.RD16=0;
T1CONbits.TMR1CS=0; // int clk
T1CONbits.T1CKPS1=1; // pre 1:8
T1CONbits.T1CKPS0=1;
T1CONbits.T1SYNC=1;
T1CONbits.TMR1ON=1;
T3CONbits.T3CCP2=0;
CCP1CON=0x05;
PIE1bits.CCP1IE=1;
RCONbits.IPEN=1;
INTCONbits.GIE=1;

while (1)
{
if (maj==1)
{
freq=1.0/((float)(duree*8e-7));
mes=(float)duree*8e-7;
mes2=1/mes;
// frpm=60/((float)duree*8e-7);
Nop();
Nop();
maj=0;
}
}
}



Anybody can help me ?
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top