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
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
}