Hi guys. After long time I managed to count the period and the high time of a rectangular waveform .
My problem now is that although the period and the high Time are display ok in the screen when I try to calculate the duty cycle (dutycycle = (Thigh *100)/period (the 100 is to get a 2 digit integer) ) I have the following .
If high time is until 330 -350μs I get the first two digits as I suppose to .
If the high time is greater than 350 μs I get a screen full of numbers and unknown symbols.
this is my code
i haven't add most of the lcd functions so the code is more readable .
Can anyone help me fix that problem
thank you.
My problem now is that although the period and the high Time are display ok in the screen when I try to calculate the duty cycle (dutycycle = (Thigh *100)/period (the 100 is to get a 2 digit integer) ) I have the following .
If high time is until 330 -350μs I get the first two digits as I suppose to .
If the high time is greater than 350 μs I get a screen full of numbers and unknown symbols.
this is my 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,x;
unsigned int period,per1; // unsigned
unsigned int total;
unsigned int thigh;
unsigned int th;
float dutycycle;
int dekadikos;
int c2=22;
float y,c1;
char str[5];
unsigned char newlineadd=0x40; // Line 2 addresses are 40h to 4Fh
ADCON1=0X7F;//Make all ports Digital
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; // o,2 convert to us from clock cyckle
th=thigh*0.2;
z=th*100;
t=total;
dutycycle=z/t;
dekadikos=dutycycle ;
ultoa(total,str);// convert integer in to string
putsXLCD(str);
ultoa(th,str);// convert integer in to string
putsXLCD(str);
DelaySTRING();
ultoa( dutycycle,str); // convert integer in to string
putsXLCD(str);
DelaySTRING();
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
}
i haven't add most of the lcd functions so the code is more readable .
Can anyone help me fix that problem
thank you.