Continue to Site

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.

capture module with lcd

Status
Not open for further replies.

lakis

New Member
Hi guys , I am trying to measures 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 .
But although the program is build succeeded the screen doesn’t display anything .

I am working with pic18f452 and pic18f4520 I use mplap ide v8.63 and c18 lite v3,36.
The period of the waveform can be adjusted between 1,5ms and 3ms and it is between 3 and 5 volts .
This is the code I’m working on .


Code:
 #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 






unsigned int result,hightime,totaltime,dutycycle;//Variables used
unsigned char start=1;
unsigned char highlow,end;


 unsigned char data[]="Thigh";
unsigned char data1[]="totaltime";
char str1[5];
char str2[5];

void DelaySTRING(void);

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
                
                
                                                  INTCONbits.PEIE=1;// Enable peripheral interrupts
                                                   INTCONbits.GIEH=1;// Enable all interrupts
                while(1);// wait here
}
 
void Send(void)// For sending the times
{



              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
              WriteCmdXLCD( 0x80);// Force curson to the begining of 1st 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() );

   ultoa(hightime,str1);// convert integer in to string
                  putsXLCD(str1);
                  while( BusyXLCD());
                  WriteCmdXLCD(0b00000001);// Clear Display
                  while( BusyXLCD() );
                   WriteCmdXLCD( SHIFT_DISP_LEFT );//Shift Cursor Display Left
                	while( BusyXLCD());// Wait till LCD finishes executing command
  ultoa(totaltime,str2);// convert integer in to string
                   putsXLCD(str2);
                   
                   while( BusyXLCD() );
                   while( BusyXLCD() );

                CloseCapture1();// Close CCP Module

                INTCONbits.GIE=0;//Disbale interrupts

                while(1);// wait here

     }



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(75);

}

void DelayXLCD(void)

{

Delay1KTCYx(25);

}
void DelaySTRING(void)
{
Delay10KTCYx(456);//450//1562
}
  //the  cature part of the code was add by wonderboy in an other thread

I know this subject is been discussed again but I am stuck here for more than two weeks and I have try anything I can think. i have add the diagram of my circuit
 

Attachments

  • pic18f452.jpg
    pic18f452.jpg
    230.8 KB · Views: 194
I have some sample code, I'm afraid its for avr studio, but you can change it to suit your need.
Code:
/*******************************
4 bit lcd
interface 
*******************************/

void lcd_init()	// fuction for intialize 
{
	dis_cmd(0x02);		// to initialize LCD in 4-bit mode.
	dis_cmd(0x28);		//to initialize LCD in 2 lines, 5X7 dots and 4bit mode.
	dis_cmd(0x0C);
	dis_cmd(0x06);
	dis_cmd(0x80);
	_delay_ms(100);
}

void dis_cmd(char cmd_value)
{
	char cmd_value1;
	cmd_value1 = cmd_value & 0xF0;		//mask lower nibble because PA4-PA7 pins are used. 
	lcdcmd(cmd_value1);			// send to LCD
 	cmd_value1 = ((cmd_value<<4) & 0xF0);	//shift 4-bit and mask
	lcdcmd(cmd_value1);			// send to LCD
}						
 
void to_line(char line,char pos)
{
if(line ==1)
{
 	dis_cmd(0x80 + pos);
}
else
{
 	dis_cmd(0xC0+pos);
}
}
void clear()
{
    to_line(1,0); 
    lcd_write_string("                ");
}

void dis_data(char data_value)
{

	char data_value1;
	
	data_value1=data_value&0xF0;
	lcddata(data_value1);
 
	data_value1=((data_value<<4)&0xF0);
	lcddata(data_value1);
}
 
void lcdcmd(char cmdout)
{
	ctrl=cmdout;
	ctrl&=~(1<<rs);
	ctrl|=(1<<en);
	_delay_ms(1);
	ctrl&=~(1<<en);
}

void lcddata(char dataout)
{
	ctrl=dataout;
	ctrl|=(1<<rs);
	ctrl|=(1<<en);
	_delay_ms(1);
	ctrl&=~(1<<en);
}
 
void lcd_write_string(char *str)	//take address vaue of the string in pionter *str
{
	int i=0;
	while(str[i]!='\0')				// loop will go on till the NULL charaters is soon in string 
	{
		dis_data(str[i]);				// sending data on CD byte by byte
		i++;
	}

}
//convert the values .. eg: convert(1000,3);
void convert(unsigned int value,unsigned int numb)
{
unsigned char text[numb];
unsigned char t,temp_char;
 for(t=1;t<=numb;t++)
 {
   temp_char = value%10;
   value=value/10;
   text[t]=temp_char+48;
 }
 
 for(t=numb;t>=1;t--)
 {
  dis_data(text[t]);//=text[t];
 }

}
 
Firstly... You can't use OpenXLCD(); twice.... This should be once in the main routine... before while(1);

Secondly... The XLCD header is set up for 4 bit interface on PORTB... You need to change this...
 
Last edited:
Ian Rogers thanks for the answer i will move openXlcd the main .I think i have set ok the xlcd.h as for the program <magvitron> add i will try to understand and changed it but I'm not sure if i can
 

Attachments

  • xlcd.h
    4 KB · Views: 149
i try to modify my program but steel the screen doesn't display anything not even the words
<<this is thigh>>
If i program pic18f452 just to display some small phrases it works fine but now i can just see cursor in the begging of first line.
this is my code now :
Code:
#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++);

} 



i think  i have added   all the source file  that  the c18  functions need  to work.(cp1close,cp1open, cp1read,t1open,t1write)  and for lcd ( wcmdxlcd,putsxlcd,openxlcd,busyxlcd)
 
please help me .    

Thank you.
  



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				
}
 
i think i have added all the source file that the c18 functions need to work.(cp1close,cp1open, cp1read,t1open,t1write) and for lcd ( wcmdxlcd,putsxlcd,openxlcd,busyxlcd)

please help me .

Thank you.
 
I don't have time to check your code but I suspect your interrupt as you don't specify an address. See example 6 & 7 in this document for more details. Also, your comment states internal osc but your config is for a crystal.

Mike.
 
my circuit have 20MHz crystal oscillator connected on osc1 ,osc2 should i set the timer 1 as external source?
 
Hi guys. I finally managed measure and display on the 2x16 LCD the period and Thigh time of rectangular waveform.
My problem now is that duty cycle doesn’t display on screen .
The duty cycle (DC=thigh/Total ) is a decimal number . This is code

Code:
 float dutycycles;
char buffer[8];
WriteCmdXLCD(0x80); // Force curson to the begining of 1st line
	while( BusyXLCD() );
ftoa(dutycycles,4,buffer);   
while( BusyXLCD() );
    	putsXLCD(buffer);
while( BusyXLCD() );
but i get
Error - could not find definition of symbol 'ftoa' in file './titl.o'.
i have also try itoa () but still nothing

please help
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top