weird problem on capture mode

Status
Not open for further replies.

lakis

New Member
Hi guys . I have made a program to measure the period and the high time of rectangular waveform using capture mode and display them on a 2x16 lcd . I am using ccp1 as input. (C18 lite compiler )
The program is working but I have this weird problem. As long as the ICD3 is connected with the circuit and the PC the result are always right (even if the Mplab IDE is close or ). But if I disconnect the ICD3 the results are sometimes right and some times not.
Does anyone know why is that happening and how to fix it.
Thank you.

This is the code
Code:
include<p18f452.h>
#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 <capture.h>
#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*/

#include <math.h>

#pragma config LVP=OFF
#pragma config OSC=HS
#pragma config WDT=OFF// Use internal Oscillator, Watchdog off, LVP off
#pragma config DEBUG=ON





void DelaySTRING(void);
void main(void)
{




	int i,z,t;
    unsigned int period,per1;
 unsigned int total;
 unsigned int thigh;
unsigned int th;
float dutycycle;
int dekadik;

   overlay   char data[]="total time ";
	static const char data1[]="in us";
     overlay  char start[]="thigh in us ";
char str[5];

	unsigned char newlineadd=0x40; // Line 2 addresses are 40h to 4Fh
    
	ADCON1=0X7F;//Make all ports Digital	

	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
    WriteCmdXLCD(0x83); // Force curson to the begining of 1st line
	while( BusyXLCD() );// Wait till LCD finishes executing command
   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
DelaySTRING();
WriteCmdXLCD(0b00000001);
	while( BusyXLCD() );
   WriteCmdXLCD( 0xC0);// Force curson to the begining of 2nd line
  
	while( BusyXLCD() );

    putsXLCD(data1);// Write the String data to the LCD
   while( BusyXLCD());
  DelaySTRING();
WriteCmdXLCD(0b00000001);
	while( BusyXLCD());// Wait till LCD finishes executing command
TRISCbits.TRISC2 = 1; /* configure CCP1 pin for input */
T3CON = 0x81; /* use Timer1 as the time base for CCP1 capture */
PIE1bits.CCP1IE = 0; /* disable CCP1 capture interrupt */
PIR1bits.CCP1IF = 0; /* clear the CCP1IF flag */
T1CON = 0x81; /* enable 16-bit Timer1, prescaler set to 1 */
CCP1CON = 0x05; /* capture on every rising edge */
while (!(PIR1bits.CCP1IF)); /* wait for 1st rising edge */
PIR1bits.CCP1IF = 0;
per1 = CCPR1; /* save the first edge (CCPR1 is accessed as a 16-bit value) */
PIE1bits.CCP1IE = 0;
CCP1CON = 0x04;
while (!(PIR1bits.CCP1IF)); /* wait for the 2nd rising edge */
PIR1bits.CCP1IF = 0;
thigh=CCPR1;
PIE1bits.CCP1IE = 0;
CCP1CON = 0x05;
while (!(PIR1bits.CCP1IF));
CCP1CON = 0x00; //disable CCP1 capture */
period = CCPR1 -per1 ;
thigh=thigh-per1;
total=period*0.2;
th=thigh*0.2;
dutycycle=th/total;

while( BusyXLCD() );


WriteCmdXLCD( 0xC5);//
while( BusyXLCD() );

ultoa(total,str);// convert integer in to string
SetDDRamAddr( newlineadd );
	while( BusyXLCD() );	
putsXLCD(str);
DelaySTRING();
DelaySTRING();
	while( BusyXLCD() );
DelaySTRING();
WriteCmdXLCD(0b00000001);
	while( BusyXLCD() );
 WriteCmdXLCD(0x80); // Force curson to the begining of 1st line
	while( BusyXLCD() );// Wait till LCD finishes executing command
     putsXLCD(start);	
	while( BusyXLCD() );
 WriteCmdXLCD(0xC4); // Force curson to the begining of 1st line
	while( BusyXLCD() );// Wait till LCD finishes executing command
ultoa(th,str);// convert integer in to string
SetDDRamAddr( newlineadd );
	while( BusyXLCD() );
    	putsXLCD(str);
	while( BusyXLCD() );


    while(1);// Wait here
}





void DelayFor18TCY(void)
{
Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();

}


/****************************************************************/
//  DelayPORXLCD  =15 ms.
/****************************************************************/

void DelayPORXLCD(void)
{
Delay1KTCYx(75);
}

/****************************************************************/
//  DelayXLCD  =5 ms.
/****************************************************************/

void DelayXLCD(void)
{
Delay1KTCYx(25);
}

/****************************************************************/
//  DelayString  =250 ms.
/****************************************************************/

void DelaySTRING(void)
{
Delay10KTCYx(456);//450//1562
}
 
That sounds like the reset line is floating. It should have a 10k pull up resistor to Vdd. A 10 nF capacitor to ground can help as well.
 
Diver300 Thank you for your answer .
The 10k resistor and the 10nf capacitor should be after the reset before ground or it doesn't matter . Because there is a 10k resistor before reset .
I added my circuit diagram
 

Attachments

  • PIC-P40.pdf
    345.6 KB · Views: 320
The resistor is on your circuit, so that is not the problem.

Where is the waveform you are measuring coming from? The ICD3 could be providing an earth connection to your circuit. It is also possible that the ICD3 is supplying power to the circuit, if there is something wrong with your power supply.

You should also make sure that the you have set the ICD3 to program, not to debug, and that the code is in "release" if you want it to run without the ICD3.

I haven't looked at the code you wrote. It works all the time with the ICD3 connected, so there is no problem with the code.
 
Ian Rogers
i will try that .Should i set the MCLRE OFF if i am using pic18f4520 or it doesn't matter ?

Diver300
The waveform is coming for a simple op-amps circuit.
i check the voltage and it is 4,95 V which is supposed to be ok. My program is on release.
 
If you have a pullup resistor (10k ish ) on RE3 ( pin 1) it wont matter how you set MCLRE... If you haven't the set it to off.
 
i set OFF the debug on my program but it doesn't seem το help.
i can set off mclre off because then the reset doesn't work at all.
 
Add two 0.1µF decoupling caps one per side on the PIC as close as possible across VDD & VSS.

Also what's your clock source, the 18F452 doesn't have an internal OSC. If it's a crystal it'll need 18pF caps to GND on each leg of the crystal.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…