it could be around 0.9ms so its a long duration.
so how can I use the "counter" method, after all its 16bit, so the total counting time is (65535 x 200ns = 0.013ms) (if working on 20MHz)
Dear all,
I'm using a PICDEM USB mounted with PIC18F4550 and I'm using the C18 compiler. I
want to measure a certain period of time at one of the PIC's inputs, lets say between a falling edge and a rising edge. so what i need to do is to store the value of a "timer" at the falling edge moment and store it again at the rising edge moment and do the comparison, but how to write it in C18 with all timer configuration and stuff !??
Thanks in advance
Best Regards
Hello!!I´m from Spain and my english is not very good but...I will try to explain my problem in the best possible way...
I want to measure a period of time of my signal...because I need the signal's frequency.
I want to use CCP on capture mode, with timer3.
I use the PIC18f4620.
My problem is that...when I read the timer..it is different with the same signal..and I don´t know why??
This is my code:
#include <p18f4620.h>
#include <delays.h>
#include <capture.h>
#include <stdio.h>
#include <timers.h>
#include <stdlib.h>
#pragma config DEBUG=OFF
#pragma config OSC=XT
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config PWRT=OFF
#pragma config MCLRE=OFF
#pragma config CCP2MX = PORTBE
#define LED LATEbits.LATE0
void main(){
unsigned int cambio;
unsigned int tiempo;
float uSxTick = 1.0;
float f=0.00;
float st=0.0;
ADCON1=0XFF;
TRISBbits.TRISB3=1;
TRISEbits.TRISE0=0;
// Configure Timer1
OpenTimer3( TIMER_INT_OFF &
T3_SOURCE_INT &
T3_SOURCE_CCP );
// Configure Capture2
OpenCapture2( C2_EVERY_RISE_EDGE &
CAPTURE_INT_OFF );
PIR2bits.CCP2IF=0;
while(PIR2bits.CCP2IF==1)// this should be while(PIR2bits.CCP2IF===0);
//so that it waits here until CCP generates a flag condition marking the start of the wave
{// delete this
WriteTimer3(0);
//LED=1;
PIR2bits.CCP2IF=0;
cambio=1;// delete this
}// delete this
while(cambio==1)// delete this
{// delete this
while(PIR2bits.CCP2IF==1)// this should be while(PIR2bits.CCP2IF===0);
//So that it stays here until CCP generates a flag condition and marks the end of a full period
{// delete this
tiempo=ReadTimer3();
st = uSxTick * tiempo;
f = 1 / (st/1000000);
//LED=0;
PIR2bits.CCP2IF=0;
cambio=0;// delete this
}
}// delete this
}// delete this
Do you have any suggestions??
Thank you very much!!
while(RB3==1); //Wait for end of previous pulse
while(RB3==0); //Wait for start of new pulse
WriteTimer3(0); //Clear Timer
while(RB3==1); //Wait for end of new pulse
While(RB3==0); //Wait for start of next pulse
Time=ReadTimer3(); //read timer
WriteTimer3(0);
OldTime=0;
while(1){
if(PIR2bite.CCP2IF==1){ //do we have new time?
NewTime=CCPR1L+256*CCPR1H; //get captured time
PulseTime=NewTime-OldTime; //calculate cycle time
OldTime=NewTime; //keep copy for next time
PIR2bits.CCP2IF=0; //clear flag
}
//do whatever here
}
If you are going to tie up the processor by polling then you may as well take the simple approach,
Code:while(RB3==1); //Wait for end of previous pulse while(RB3==0); //Wait for start of new pulse WriteTimer3(0); //Clear Timer while(RB3==1); //Wait for end of new pulse While(RB3==0); //Wait for start of next pulse Time=ReadTimer3(); //read timer
I also use this code...and it works fine...but it measures a frequency a little different...and it only works until 20KHZ...
#include <p18f4620.h>
#include <delays.h>
#include <capture.h>
#include <stdio.h>
#include <timers.h>
#include <stdlib.h>
#pragma config DEBUG=OFF
#pragma config OSC=XT
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config PWRT=OFF
#pragma config MCLRE=ON
#pragma config CCP2MX = PORTBE
#define LED LATEbits.LATE0
void main(){
unsigned int time;
unsigned float uSxTick = 1.0;
unsigned float f;
unsigned float st=0.0;
ADCON1=0XFF;
TRISBbits.TRISB3=1;
TRISEbits.TRISE0=0;
// Configure Timer1
OpenTimer3( TIMER_INT_OFF &
T3_SOURCE_INT &
T3_PS_1_8);
while(PORTBbits.RB3==1); //Wait for end of previous pulse
while(PORTBbits.RB3==0); //Wait for start of new pulse
WriteTimer3(0); //Clear Timer
while(PORTBbits.RB3==1); //Wait for end of new pulse
while(PORTBbits.RB3==0); //Wait for start of next pulse
time=ReadTimer3(); //read timer
st = uSxTick * time;
f = 1 / (st/1000000);
}
I don´t know why..
Thanks!!
I don´t know why..
Thanks!!
If you want to use CCP with interrupts and use the libraries, than there is such an example in my blog, which measures the pulse time of a wave. Its written for a 18F1320 but would do your task with some editing.
//This is a program that displays the use of the CCP feature with
//Microchips Libraries documented on page 21.It measures the High and the Total Time
//and sends it to the serial port.
//The maximum Pulse Time is 16.38ms
//The Timer Section is documented on P#57 of C18 Libraries
//Use 181320capture.DSN
//In this simulation a switch has been used, for hardware use a function generator since the debounce time for the //switch will exceed the highest possible time measurement.
#include<p18f1320.h>// Include files and definitions for the Processor
#include<capture.h>//Include function definitions for the Capture library
#include<timers.h>// Include function definitions for Timer Library
#include <usart.h>// Include function definitions for USART Library
#include<stdlib.h>// Include function definitions for string manupulation Library
#pragma config LVP=OFF,OSC=HSPLL,WDT=OFF// Configuration settings for 18f1320
unsigned int result,hightime,totaltime,dutycycle;//Variables used
unsigned char start=0;
unsigned char highlow,end;
char str[5];
void Send(void);// Function for sending data to serial port
void chk_isr(void);// ISR Handler
void hi_prioriint(void)// ISR Routine
{
_asm
GOTO chk_isr
_endasm
}
#pragma interrupt chk_isr
void chk_isr(void)
{
if(PIR1bits.CCP1IF)
{
T1CONbits.TMR1ON=1;
start++;
if(start==2)
{
totaltime=(CCPR1H*256)+CCPR1L;// Read contents of Capture registers
CloseTimer1();// Close timer 1
Send();// Send the measured time values to the serial port
INTCONbits.GIE=0;
}
PIR1bits.CCP1IF=0;// Enable interrupt again
}
}
void main(void)
{
OSCCON=0x60;//Internal 4MHz oscillator
ADCON1=0x7f;//Make all pins Digital
TRISA=0x00;//Only for test
LATA=0;
TRISBbits.TRISB3=1;//Make CCP1 pin an input for the pulse
TRISBbits.TRISB1=0;//Make TX pin an output
//Initiazlize the capture moudule(ccp) for LtoH
OpenCapture1( C1_EVERY_RISE_EDGE
& CAPTURE_INT_ON );
OpenTimer1( TIMER_INT_OFF &//configure Timer1 for ccp
T1_16BIT_RW &// Can also use Timer3
T1_SOURCE_INT & // Timer1 clock source is internal
T1_SOURCE_CCP &// Make Timer1 source for CCP module
T1_PS_1_1);// No prescaler for timer1
T1CONbits.TMR1ON=0;
TMR1H=0x00;//Start timer 1 for maximum count
TMR1L=0x00;
//Initialize USART for tx high time,total time
OpenUSART(USART_TX_INT_OFF &// Trasnmit interrupt off
USART_ASYNCH_MODE & // Use USART in asynchronous mode
USART_EIGHT_BIT &// Eight bits for data
USART_CONT_RX &// Enable continous receiving
USART_BRGH_LOW,// Dont use baud rate multiplier
12);// SPBRG value for a BR of 4800
INTCONbits.PEIE=1;// Enable peripheral interrupts
INTCONbits.GIEH=1;// Enable all interrupts
while(1);// wait here
}
void Send(void)// For sending the times
{
putrsUSART ("\nThe High Time in us is");
ultoa(hightime,str);// convert integer in to string
putsUSART(str);// Send string to serial port
putrsUSART ("\nThe total time in us is");
ultoa(totaltime,str);// convert integer in to string
putsUSART(str);// Send string to serial port
CloseCapture1();// Close CCP Module
INTCONbits.GIE=0;//Disbale interrupts
while(1);// wait here
}
#include "p18f452.h"// Include files and definitions for the Processor
#include<xlcd.h>// Include function definitions for the External LCD Library library
#include<delays.h>// Include function definitions for built in Delay routines
#include <stdio.h>
#include <timers.h>
//#include<stdlib.h>// Include function definitions for string manupulation Library
#include<capture.h>//Include function definitions for the Capture library
//#include <stdlib.h> /**********tin aferesa epeidi ekane problima sto itoa. Kanonika auti einai bibliothiki poy exei to idoa mesa tin ebala otan ebla ton ADC kodika Den ipirxe prin. Douleue sosta xvris ayti*/
#pragma config LVP=OFF
#pragma config OSC=HS
#pragma config WDT=OFF// Use internal Oscillator, Watchdog off, LVP off
#pragma config DEBUG = ON
#define DATALCD PORTD
#define dirDATA TRISD
unsigned int result,hightime,totaltime,dutycycle;//Variables used
unsigned char start=1;
unsigned char highlow,end;
unsigned char data[]="this is Thigh";
char str1[5];
char str2[5];
unsigned char newlineadd=0x40;
void DelaySTRING(void);
void LCDdata (unsigned char);
void LCDDelay( void );
void lcdcmd(unsigned char);
void lcdcmd(unsigned char);
void stringtoLCD(unsigned char *m);
unsigned char * itoa(unsigned int value , unsigned char * string);//
unsigned char dataString[10];
unsigned char *biginin;
void Send(void);// Function for sending data to serial port
void chk_isr(void);// ISR Handler
void hi_prioriint(void)// ISR Routine
{
_asm
GOTO chk_isr
_endasm
}
#pragma interrupt chk_isr
void chk_isr(void)
{
if(PIR1bits.CCP1IF)
{
if(start==1)
{
WriteTimer1(00);//Start timer 1 for maximum count
OpenCapture1( CAPTURE_INT_ON & //Initialize CCP for HtoL
C1_EVERY_FALL_EDGE );
start=0;
highlow=1;
PIR1bits.CCP1IF=0;// Enable interrupt again
}
else
{
if(highlow==1)
{
hightime = ReadCapture1();// Read contents of capture registers
OpenCapture1( CAPTURE_INT_ON & //Initialize CCP for LtoH
C1_EVERY_RISE_EDGE );
highlow=0;
end=1;
PIR1bits.CCP1IF=0;
}
else
if(end==1)
{
totaltime=ReadCapture1();// Read contents of Capture registers
CloseTimer1();// Close timer 1
Send();// Send the measured time values to the serial port
}
}
}
}
void main(void)
{
// OSCCON=0x60;//Internal 4MHz oscillator
ADCON1=0x7f;//Make all pins Digital
//Initiazlize the capture moudule(ccp) for LtoH
OpenCapture1( C1_EVERY_RISE_EDGE
& CAPTURE_INT_ON );
OpenTimer1( TIMER_INT_OFF &//configure Timer1 for ccp
T1_16BIT_RW &// Can also use Timer3
T1_SOURCE_INT & // Timer1 clock source is internal
T1_SOURCE_CCP &// Make Timer1 source for CCP module
T1_PS_1_1);// No prescaler for timer1
OpenXLCD( EIGHT_BIT & LINES_5X7 );// Use 8 bit Data, 5x7 pixel Matrix per character
while( BusyXLCD());// Wait till LCD finishes executing command
WriteCmdXLCD( CURSOR_ON);// Turn cursor ON
while( BusyXLCD() );// Wait till LCD finishes executing command
INTCONbits.PEIE=1;// Enable peripheral interrupts
INTCONbits.GIEH=1;// Enable all interrupts
while(1);// wait here
}
void Send(void)// For sending the times
{
int integer,i,k,t,z;//????
biginin=&dataString[0];
WriteCmdXLCD( 0x80);// Force curson to the begining of 2nd line
while( BusyXLCD() );
WriteCmdXLCD( SHIFT_DISP_LEFT );//Shift Cursor Display Left
while( BusyXLCD());// Wait till LCD finishes executing command
putsXLCD(data);// Write the String data to the LCD
while( BusyXLCD());// Wait till LCD finishes executing command
WriteCmdXLCD( 0xC0);// Force curson to the begining of 2nd line
while( BusyXLCD() );
DelaySTRING();
integer=hightime;
DelaySTRING();
LCDDelay();
itoa (integer, biginin);
WriteCmdXLCD( 0xC0); //Emfanizontai sti 2 grammi
stringtoLCD(dataString);
while( BusyXLCD());
DelaySTRING();
WriteCmdXLCD(0b00000001);// Clear Display
while( BusyXLCD() );
WriteCmdXLCD( SHIFT_DISP_LEFT );//Shift Cursor Display Left
while( BusyXLCD());// Wait till LCD finishes executing command
DelaySTRING();
integer=totaltime;
DelaySTRING();
LCDDelay();
ultoa (integer, biginin);
WriteCmdXLCD( 0xCA); //Emfanizontai sti 2 grammi
stringtoLCD(dataString);
while( BusyXLCD() );
while( BusyXLCD() );
CloseCapture1();// Close CCP Module
INTCONbits.GIE=0;//Disbale interrupts
while(1);// wait here
}
void LCDdata (unsigned char value)
{
BusyXLCD();//busylcd();
TRISD = 0;
DATALCD = value;
RS_PIN=1;
RW_PIN=0;
E_PIN=1;
LCDDelay();
E_PIN=0;
}
void LCDDelay(void)
{
int i=0;
for (i=0;i<250;i++);
}
void stringtoLCD(unsigned char *m)
{
unsigned char i;
i = 0;
while(m[i] != 0)
{
LCDdata(m[i]);
i++;
}
}
void DelayFor18TCY(void)
{
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
_asm NOP _endasm
}
void DelayPORXLCD(void)
{
Delay1KTCYx(15);
}
void DelayXLCD(void)
{
Delay1KTCYx(5);
}
void DelaySTRING(void)
{
//Delay1KTCYx(450); // Stin pragmatikotita MONO
Delay1KTCYx(100);//Gia prosomoiosi MONO
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?