1) How do i display a 2 digit No. on keypress?
2) When I press 5 on the keypad, the LCD displays '5'. How do i then store the value ive inputted?
I want to compare the number ive pressed to a randomly generated number.
How do i save the keys to memory as they are pressed? keypad uses 74C922 encoder
- CODE: SELECT_ALL_CODE
#include <xc.h>
#include "config.h"
#define DA PORTAbits.RA5 // set dataAvailable PORT A bit 5
#define LCD_DATA PORTD
#define LCD_RS PORTBbits.RB1 // RS signal for LCD
#define LCD_E PORTBbits.RB2 // E signal for LCD
#define bulb PORTBbits.RB3 // assign PORT B bit 3 to light bulb
void Init_LCD(void);// Initialize the LCD
void LCD_sendCW(char);// 8-bit Control word for LCD
void LCD_sendData(char);// 8-bit Text Data for LCD
void readKey();
void main(void)
{
ADCON1=0x0F; // Setports A,B & E as digital I/O
TRISB = 0b11111001;// RB1 & 2 for LCD interface RS & E
TRISD = 0; // Port D as LCD data output
TRISE = 0b11101111;// RE4:OUTPUT to make PORTD digital I/O
Init_LCD();// Init LCD 8-bit interface,multiple line
char x;
int num,num2,i;
while(1)
{
readKey();
}
}
void Init_LCD()
{
LCD_sendCW(0b00111000);// Function Set -8-bit, 2 lines, 5X7
LCD_sendCW(0b00001100);// Display on, cursor on
LCD_sendCW(0b00000110);// Entry mode -inc addr, no shift
LCD_sendCW(0b00000001);// Clear display
LCD_sendCW(0b00000010);// Return cursor to home position
}
void LCD_sendCW(char x)
{
LCD_RS = 0;
LCD_E = 1;
LCD_DATA = x;
LCD_E= 0;
_delay(1000);// 2ms delay
}
void LCD_sendData(char x)
{
LCD_RS= 1;
LCD_E= 1;
LCD_DATA = x;
LCD_E= 0;
_delay(500);// 1ms delay
}
void readKey()
{
int key[]={1,2,3,'F',4,5,6,'E',7,8,9,'D','A',0,'B','C'};
char dataIn;
TRISA=0b11111111;
ADCON1=0X0F;
if(DA==1)
{
dataIn = PORTA&0b00001111;
if(dataIn==0b00000000)
{
LCD_sendData(key[0]+'0');
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00000001)
{
LCD_sendData(key[1]+'0');
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00000010)
{
LCD_sendData(key[2]+'0');
LCD_sendCW(0b1000000);
}
else if(dataIn==0b00000011)
{
LCD_sendData(key[3]);
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00000100)
{
LCD_sendData(key[4]+'0');
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00000101)
{
LCD_sendData(key[5]+'0');
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00000110)
{
LCD_sendData(key[6]+'0');
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00000111)
{
LCD_sendData(key[7]);
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00001000)
{
LCD_sendData(key[8]+'0');
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00001001)
{
LCD_sendData(key[9]+'0');
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00001010)
{
LCD_sendData(key[10]+'0');
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00001011)
{
LCD_sendData(key[11]);
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00001100)
{
LCD_sendData(key[12]);
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00001101)
{
LCD_sendData(key[13]+'0');
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00001110)
{
LCD_sendData(key[14]);
LCD_sendCW(0b10000000);
}
else if(dataIn==0b00001111)
{
LCD_sendData(key[15]);
LCD_sendCW(0b10000000);
}
}
}