Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ERROR:Program to get input from keypad and display it on LCD.

Status
Not open for further replies.

smitgovani

Member level 2
Member level 2
Joined
Dec 25, 2011
Messages
50
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
1,641
When i write code in MPLAB software and buid it i got error.............
C:\Users\DCS\Desktop\example\ex1.c:76:Error: syntax error



nd my code is


Code:
// Program to get input from keypad and display it on LCD.

#define pad PORTD
#define r1 PD0
#define r2 PD1
#define r3 PD2
#define r4 PD3
 
#define c1 PD4
#define c2 PD5
#define c3 PD6
 
void check1(void);
void check2(void);  
void check3(void);
void check4(void);
 
#define LCD_DATA PORTA		//LCD data port
 
#define ctrl PORTB
#define en PB2		//enable signal
#define rw PB1		//read/write signal
#define rs PB0		//resister select signal
 
void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
 
unsigned int press;
 
int main()
{
	unsigned char value; 
	DDRA=0xff;		//LCD_DATA port as output port
	DDRB=0x07;		//signal as out put
	DDRD=0x0F;
	pad=0xf0;
	init_LCD();		//initialization of LCD
	LCD_write_string("press a key");
	LCD_cmd(0xc0);
 
 
	while(1)
	{
		PORTD=0xF0;		//set all the input to one
		value=PIND;		//get the PORTD value in variable “value”
		if(value!=0xf0)		//if any key is pressed value changed
		{
			check1();
			check2();
			check3();
			check4();
		}
	}
	return 0;
}
 
void check1(void)
{
	//DDRD = 0xf0;
	pad =0b11111110;
	//pad &= (0<<r1);
	_delay_us(10);
	if(bit_is_clear(PIND,c1))
	LCD_write('1');
	else if(bit_is_clear(PIND,c2))
	LCD_write('2');
	else if(bit_is_clear(PIND,c3))
	LCD_write('3');
}
 
 
void check2(void)
{
	pad=0b11111101;
	/pad &= (0<<r2);
	_delay_us(10);                            //ERROR LINE SHOWN
	if(bit_is_clear(PIND,c1))
	LCD_write('4');
	else if(bit_is_clear(PIND,c2))
	LCD_write('5');
	else if(bit_is_clear(PIND,c3))
	LCD_write('6');
}
 
void check3(void)
{
	pad=0b11111011;
	//pad &= (0<<r3);
	_delay_us(10);
	if(bit_is_clear(PIND,c1))
	LCD_write('7');
	else if(bit_is_clear(PIND,c2))
	LCD_write('8');
	else if(bit_is_clear(PIND,c3))
	LCD_write('9');
}
 
void check4(void)
{
	pad =0b11110111;
	//pad &= (0<<r4);
	_delay_us(10);
	if(bit_is_clear(PIND,c1))
	LCD_write('#');
	else if(bit_is_clear(PIND,c2))
	LCD_write('0');
	else if(bit_is_clear(PIND,c3))
	LCD_write('*');
}
 
 
 
void init_LCD(void)
{
 
	LCD_cmd(0x38);		//initializtion of 16X2 LCD in 8bit mode
	_delay_ms(1);
 
	LCD_cmd(0x01);		//clear LCD
	_delay_ms(1);
 
	LCD_cmd(0x0E);		//cursor ON
	_delay_ms(1);
 
	LCD_cmd(0x80);		// ---8 go to first line and --0 is for 0th position
	_delay_ms(1);
	return;
}
 
 
void LCD_cmd(unsigned char cmd)
{
	LCD_DATA=cmd;
	ctrl =(0<<rs)|(0<<rw)|(1<<en);	// making RS and RW as LOW and EN as HIGH
	_delay_ms(1);
	ctrl =(0<<rs)|(0<<rw)|(0<<en);	// making RS, RW , LOW and EN as LOW
	_delay_ms(50);
	return;
}
 
 
void LCD_write(unsigned char data)
{
	LCD_DATA= data;
	ctrl = (1<<rs)|(0<<rw)|(1<<en);	// making RW as LOW and RS, EN as HIGH
	_delay_ms(1);
	ctrl = (1<<rs)|(0<<rw)|(0<<en);	// making EN and RW as LOW and RS HIGH
	_delay_ms(50);						// give a 10 milli second delay to get thigs executed
	return ;
}
 
void LCD_write_string(unsigned 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 
	{
		LCD_write(str[i]);				// sending data on CD byte by byte
		i++;
	}
	return;
}


Please Use Code Tags When Posting Code
 
Last edited by a moderator:

At line 76, you commented with a single bar, instead 2 bars :

Code:
/pad &= (0<<r2);

+++
 
then i got lots of errors.....

C:\Users\DCS\Desktop\example\ex1.c:31:Warning [2103] default startup code expects main function declared as 'void main (void)'
C:\Users\DCS\Desktop\example\ex1.c:34:Error [1105] symbol 'DDRA' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:34:Error [1101] lvalue required
C:\Users\DCS\Desktop\example\ex1.c:35:Error [1105] symbol 'DDRB' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:35:Error [1101] lvalue required
C:\Users\DCS\Desktop\example\ex1.c:36:Error [1105] symbol 'DDRD' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:36:Error [1101] lvalue required
C:\Users\DCS\Desktop\example\ex1.c:37:Error [1105] symbol 'PORTD' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:37:Error [1101] lvalue required
C:\Users\DCS\Desktop\example\ex1.c:39:Warning [2058] call of function without prototype
C:\Users\DCS\Desktop\example\ex1.c:45:Error [1105] symbol 'PORTD' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:45:Error [1101] lvalue required
C:\Users\DCS\Desktop\example\ex1.c:46:Error [1105] symbol 'PIND' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:61:Error [1105] symbol 'PORTD' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:61:Error [1101] lvalue required
C:\Users\DCS\Desktop\example\ex1.c:63:Warning [2058] call of function without prototype
C:\Users\DCS\Desktop\example\ex1.c:64:Error [1105] symbol 'PIND' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:64:Error [1105] symbol 'PD4' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:64:Warning [2058] call of function without prototype
C:\Users\DCS\Desktop\example\ex1.c:66:Error [1105] symbol 'PIND' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:66:Error [1105] symbol 'PD5' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:66:Warning [2058] call of function without prototype
C:\Users\DCS\Desktop\example\ex1.c:68:Error [1105] symbol 'PIND' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:68:Error [1105] symbol 'PD6' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:68:Warning [2058] call of function without prototype
C:\Users\DCS\Desktop\example\ex1.c:75:Error [1105] symbol 'PORTD' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:75:Error [1101] lvalue required
C:\Users\DCS\Desktop\example\ex1.c:77:Warning [2058] call of function without prototype
C:\Users\DCS\Desktop\example\ex1.c:78:Error [1105] symbol 'PIND' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:78:Error [1105] symbol 'PD4' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:78:Warning [2058] call of function without prototype
C:\Users\DCS\Desktop\example\ex1.c:80:Error [1105] symbol 'PIND' has not been defined
C:\Users\DCS\Desktop\example\ex1.c:80:Error [1105] symbol 'PD5' has not been defined
Halting build on first failure as requested.
 

Pehaps you forgot to include some header file.

+++
 

can u help me which header files???
i think
#include<avr/io.h>
#include<util/delay.h>

yhen i got errors
C:\Users\DCS\Desktop\example\ex1.c:3:Error [1027] unable to locate 'avr/io.h'
C:\Users\DCS\Desktop\example\ex1.c:4:Error [1027] unable to locate 'util/delay.h'
 

The one in where you find defined bellow constants :


Code:
DDRA, DDRB, DDRC, DDRD, PORTD, PIND, PD4, PD5, PD6

+++
 

it get solved but i need header files
'avr/io.h'
'util/delay.h'
can u help me from where i can get it??
 

can u help me which header files???
i think
#include<avr/io.h>
#include<util/delay.h>

yhen i got errors
C:\Users\DCS\Desktop\example\ex1.c:3:Error [1027] unable to locate 'avr/io.h'
C:\Users\DCS\Desktop\example\ex1.c:4:Error [1027] unable to locate 'util/delay.h'

It appears you have failed to specific a device specific header file, example <pic16f877a.h>, if you are using a Microchip compiler or the <htc.h> header is you are using a HiTech compiler.

What device are you using, PIC16F877A, PIC18F4550, etc?

The header files you reference above are for AVR not PIC.

Have you specified the device in the MPLAB IDE?

BigDog
 

i m using pic18f4550...my code is mentioned above
what should i do next??
 

After further examination of your posts, it appears you are attempting to compile an AVR program with a PIC IDE (MPLAB).

What device are you using in your design, PIC or AVR? And what is the device model?

BigDog
 

Alright you are using a PIC18F4550 in your design.

From where did you obtain the code? It does not appear at first glance to have been written for the PIC, but AVR instead?

BigDog
 

i got code for engineer garage website
yes u r right it's for AVR
 

The following "#defines" usually refer to AVR ports, not PIC ports:

Code:
	[COLOR="#FF0000"]DDRA[/COLOR]=0xff;		//LCD_DATA port as output port
	[COLOR="#FF0000"]DDRB[/COLOR]=0x07;		//signal as out put
	[COLOR="#FF0000"]DDRD[/COLOR]=0x0F;

Also do you have a schematic of the circuit showing how the LCD is connected to your PIC?

BigDog

---------- Post added at 20:14 ---------- Previous post was at 20:13 ----------

We seem to be out of sync with our posts.

Which compiler are you using Microchip or HiTech?

BigDog
 

so i need code for 3x4 keypad interfacing with pic 18f4550....
can u help me for that code???

---------- Post added at 00:48 ---------- Previous post was at 00:46 ----------

Hi tech compiler
 

The HiTech PICC18 Compiler has example code for an LCD interface:

lcd.h
Code:
#ifndef _LCD_H_
#define _LCD_H_

/*
 *	LCD interface header file
 */

/* 	Defining CHECKBUSY will check if the LCD is busy. The RW bit of the 
 * 	LCD must connected to a port of the processor for the check busy
 * 	process to work.
 * 
 * 	If CHECKBUSY is not defined it will instead use a delay loop.
 * 	The RW bit of the LCD does not need to connected in this case.
 */

// #define CHECKBUSY	1

#ifdef CHECKBUSY
	#define	LCD_WAIT lcd_check_busy()
#else
	#define LCD_WAIT DelayMs(5)

#endif

#define MESSAGE_LINE		0x0

#define LCD_RS	LA3
#define LCD_EN	LA1
#define LCD_RW	LA2 
#define LCD_DATA	LATD
#define LCD_DATA_PORT	PORTD 
#define LCD_RS_TRIS	TRISA3
#define LCD_EN_TRIS	TRISA1
#define LCD_RW_TRIS	TRISA2 
#define LCD_DATA_TRIS	TRISD

#define FOURBIT_MODE	0x0
#define EIGHTBIT_MODE	0x1
#define OUTPUT_PIN      0x0	
#define INPUT_PIN       0x1	
#define OUTPUT_DATA     0x0	
#define INPUT_DATA      0x0F	

#define LCD_STROBE()	LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0

#define LCD_STROBE_READ(value)	LCD_EN = 1; \
				asm("nop"); asm("nop"); \
				value=LCD_DATA_PORT; \
				LCD_EN = 0; 

#define lcd_cursor(x)			lcd_cmd(((x)&0x7F)|0x80)
#define lcd_clear()			lcd_cmd(0x1)
#define lcd_putch(x)			lcd_data(x)
#define lcd_goto(x)			lcd_cmd(0x80+(x));
#define lcd_cursor_right()		lcd_cmd(0x14)
#define lcd_cursor_left()		lcd_cmd(0x10)
#define lcd_display_shift()		lcd_cmd(0x1C)
#define lcd_home()			lcd_cmd(0x2)

extern void lcd_cmd(unsigned char);
extern void lcd_data(unsigned char);
extern void lcd_puts(const char * s);
extern void lcd_init(unsigned char);

#endif

lcd.c
Code:
/*
 *	LCD interface example
 *	Uses routines from delay.c
 *	This code will interface to a standard LCD controller
 *	like the Hitachi HD44780. It uses it in 4 or 8 bit mode
 *	
 */

#include	<pic18.h>
#include	"lcd.h"
#include	"delay.h"
 

static bit fourbit;		// four or eight bit mode?

#ifdef CHECKBUSY

unsigned char 
lcd_read_cmd_nowait(void)
{
	unsigned char c, readc;

	LCD_DATA_TRIS	 = INPUT_DATA;

	LCD_RW = 1; // Read LCD
	asm("nop"); // short propagation delay
	asm("nop"); // short propagation delay

	if (fourbit)
	{
		LCD_STROBE_READ(readc); // Read high nibble
		// Move 4 bits to high nibble while zeroing low nibble
		c = ( ( readc << 4 ) & 0xF0 ); 
		LCD_STROBE_READ(readc); // Read low nibble
    		c |= ( readc & 0x0F ); // Or in 4 more bits to low nibble
	}
	else
	{
		LCD_STROBE_READ(readc); 
		c = readc;
	}
	LCD_RW = 0; // Return to default mode of writing LCD
	LCD_DATA_TRIS	 = OUTPUT_DATA; // Return to default mode of writing LCD

	return(c);
}

void
lcd_check_busy(void) // Return when the LCD is no longer busy, or we've waiting long enough!
{
	// To avoid hanging forever in event there's a bad or 
	// missing LCD on hardware.  Will just run SLOW, but still run.
	unsigned int retry; 
	unsigned char c;

	for (retry=1000; retry-- > 0; ) {
		c = lcd_read_cmd_nowait();
		if (0==(c&0x80)) break; // Check busy bit.  If zero, no longer busy
	}
}

#endif

/* send a command to the LCD */
void
lcd_cmd(unsigned char c)
{
	LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h

	if (fourbit)
	{
		LCD_DATA = ( ( c >> 4 ) & 0x0F );
		LCD_STROBE();
    		LCD_DATA = ( c & 0x0F );
		LCD_STROBE();
	}
	else
	{
		LCD_DATA = c;
		LCD_STROBE();
	}
}

/* send data to the LCD */
void
lcd_data(unsigned char c)
{
	LCD_WAIT; // may check LCD busy flag, or just delay a little, depending on lcd.h

	LCD_DATA = 0;
	LCD_RS = 1;
	if (fourbit)
	{
    		LCD_DATA |= ( ( c >> 4 ) & 0x0F );      
		LCD_STROBE();
		LCD_DATA &= 0xF0;
		LCD_DATA |= ( c & 0x0F ); 
		LCD_STROBE();
	}
	else
	{
		LCD_DATA = c;
		LCD_STROBE();
	}
	LCD_RS = 0;
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s)
{
	while(*s)
		lcd_data(*s++);
}

/* initialize the LCD */
void
lcd_init(unsigned char mode)
{
	char init_value;

	fourbit		= 0;
	if (mode == FOURBIT_MODE){
		fourbit = 1;
		init_value = 0x3;
	}else{
		init_value = 0x3F;
	}
	LCD_RS = 0;
	LCD_EN = 0;
	LCD_RW = 0;
	LCD_RS_TRIS	 = OUTPUT_PIN;
	LCD_EN_TRIS	 = OUTPUT_PIN;
	LCD_RW_TRIS	 = OUTPUT_PIN;
	LCD_DATA_TRIS	 = OUTPUT_DATA;
	DelayMs(15);
	LCD_DATA	 = init_value;
	LCD_STROBE();
	DelayMs(5);
	LCD_DATA	 = init_value;
	LCD_STROBE();
	DelayUs(200);
	LCD_DATA	 = init_value;
	LCD_STROBE();
	
	if (fourbit){
		LCD_WAIT; //may check LCD busy flag, or just delay a little, depending on lcd.h
		LCD_DATA = 0x2; // Set 4-bit mode
		LCD_STROBE();

		lcd_cmd(0x28); // Function Set
	}else{
		lcd_cmd(0x38);
	}
	lcd_cmd(0xF); //Display On, Cursor On, Cursor Blink
	lcd_cmd(0x1); //Display Clear
	lcd_cmd(0x6); //Entry Mode
	lcd_cmd(0x80); //Initialize DDRAM address to zero
}

Before compiling the above code in Microchip's C18 Compiler, you will need to make some changes.

Including but not limited to, substituting appropriate delay routines.

BigDog

---------- Post added at 20:24 ---------- Previous post was at 20:23 ----------

The code you posted above is for an LCD.

Do you need a keypad routine in addition to the LCD routines?

BigDog
 

ya thanx.....
nd i need 3x4 keypad interfacing code
bcoz i m displaying keypad input........

---------- Post added at 01:02 ---------- Previous post was at 00:58 ----------

Yes i need that
 

Use the I provided above along with the following demo for the PICDEM2 board which uses the LCD routines and the delay routines below, adapt it to you project.

Once you have gotten the LCD routines to function with your project, then move on to incorporating the keypad routines.

main.c
Code:
* Features demonstrated include driving the LCD display and
 * the A2D converter. 
 
 Additional files required for this demo are included in the
 PIC18\SAMPLES directories:
 DELAY\delay.c
 DELAY\delay.h
 LCD\lcd.c
 LCD\lcd.h
 */

#include <pic18.h>
#include <stdio.h>
#include "lcd.h"

/* this is the maximum value of an A2D conversion. */
#define MAXVOLTAGE 5

void init(void){
	lcd_init(FOURBIT_MODE);
	ADON=1;	/* enable A2D converter */
	ADIE=0;	/* not interrupt driven */
	ADCON1=0;
	ADCON0=1;
}

void main(void){
	unsigned char last_value;
	unsigned char volts;
	unsigned char decivolts;
	unsigned char outString[20];

	init();
	lcd_puts("Starting");

	while(1){
		GODONE=1;
		while(GODONE)continue;
		ADIF=0;
		if(ADRESH!=last_value)
		{
			volts=0;
			for(decivolts=(ADRESH*10*MAXVOLTAGE/255);decivolts>=10;decivolts-=10)
				volts++;
			lcd_clear();
			sprintf(outString,"A2D = %d.%d volts",volts,decivolts);
			lcd_puts(outString);
		}
		last_value=ADRESH;
	}
}

delay.h
Code:
/*
 *	Delay functions for HI-TECH C on the PIC18
 *
 *	Functions available:
 *		DelayUs(x)	Delay specified number of microseconds
 *		DelayMs(x)	Delay specified number of milliseconds
 *
 *	Note that there are range limits: 
 *	- on small values of x (i.e. x<10), the delay becomes less
 *	accurate. DelayUs is accurate with xtal frequencies in the
 * 	range of 4-16MHZ, where x must not exceed 255. 
 *	For xtal frequencies > 16MHz the valid range for DelayUs
 *	is even smaller - hence affecting DelayMs.
 *	To use DelayUs it is only necessary to include this file.
 *	To use DelayMs you must include delay.c in your project.
 *
 *	Set the crystal frequency in the CPP predefined symbols list
 *	on the PICC-18 commmand line, e.g.
 *	picc18 -DXTAL_FREQ=4MHZ
 *
 *	or
 *	picc18 -DXTAL_FREQ=100KHZ
 *	
 *	Note that this is the crystal frequency, the CPU clock is
 *	divided by 4.
 *
 *	MAKE SURE this code is compiled with full optimization!!!
*/

#define	MHZ	*1

#ifndef	XTAL_FREQ
#define	XTAL_FREQ	4MHZ		/* Crystal frequency in MHz */
#endif

#if	XTAL_FREQ < 8MHZ
#define	uS_CNT 	238			/* 4x to make 1 mSec */
#endif

#if	XTAL_FREQ == 8MHZ
#define uS_CNT  244
#endif

#if	XTAL_FREQ > 8MHZ
#define uS_CNT  246
#endif

#define FREQ_MULT	(XTAL_FREQ)/(4MHZ)

#define	DelayUs(x)	{ unsigned char _dcnt; \
			  if(x>=4) _dcnt=(x*(FREQ_MULT)/2); \
			  else _dcnt=1; \
			  while(--_dcnt > 0) \
				{\
				asm("nop");\
				asm("nop");\
				continue; }\
		} 

extern void DelayMs(unsigned char);

delay.c
Code:
/*
 *	Delay functions
 *	See delay.h for details
 *
 *	Make sure this code is compiled with full optimization!!!
 */

#include	"delay.h"

void
DelayMs(unsigned char cnt)
{
	unsigned char i;
	while (cnt--) {
		i=4;
		while(i--) {
			DelayUs(uS_CNT);	/* Adjust for error */
		} ;
	} ;
}

Post your results.

BigDog
 

C:\Program Files (x86)\MCC18\example\getting_started\program3\main.c:9:Error [1027] unable to locate 'pic18.h'
C:\Program Files (x86)\MCC18\example\getting_started\program3\main.c:10:Error [1027] unable to locate 'lcd.h'
C:\Program Files (x86)\MCC18\example\getting_started\program3\main.c:11:Error [1027] unable to locate 'delay.h'

only for lcd
 
Substitute <htc.h> for <pic18.h> and make sure you have included all the other files I have given you in the same directory as main.c.

BigDog
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top