// 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
/pad &= (0<<r2);
DDRA, DDRB, DDRC, DDRD, PORTD, PIND, PD4, PD5, PD6
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'
[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;
#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 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
}
* 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 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 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 */
} ;
} ;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?