Matrix Keypad, Analog Keypad dan Alphabet LCD

Cheat Sheet: Antaramuka Pengguna atau Panel Kawalan

LCD dan Analog Keypad test – Versi Arduino c code

Masa mula mula saya belajar arduino, secara jujurnya, saya tidak menggunakan Library LCD atau Library Keypad yang sudah sedia ada di dalam software Arduino IDE. Saya code sendiri function LCD saya, saya code sendiri function keypad saya.

Ini mungkin disebabkan sejarah saya yang bermula dengan PIC. Masa bermain dengan PIC dahulu, penggunaan library dari luar adalah kurang, saya banyak menulis function sendiri untuk mengawal mana mana output atau membaca mana mana input. Jadi di bawah ini saya kongsikan code yang saya tulis sendiri berdasarkan artikel tentang LCD.

Di bawah adalah pautan tentang artikel LCD yang saya sebutkan di atas, artikelnya sangat lengkap. Ia menceritakan bagaimana LCD jenis ini berfungsi.

>> Baca Artikel LCD 1 <<

>> Baca Artikel LCD 2 <<

Di bawah adalah senarai funtion LCD :

void lcd1_initialize(void);
void lcd1_home(void);
void lcd1_clear(void);
void lcd1_goto(unsigned char address);
void lcd1_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms);  //Note : direction = LCD_RIGHT, LCD_LEFT
void lcd1_cgram(unsigned char cgram_address, unsigned char *graphic);  //Note :  ex; data[8] = {0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111}; , cgram_address : 0 to 7
void lcd1_senddata(unsigned char c, unsigned char b_rs);
void lcd1_putchar(unsigned char charachter);
void lcd1_printf(unsigned char *string);
void lcd1_numbertoascii(float number);  //Note : Output format in xxx.xxx where x = number
void lcd1_printnumber(unsigned long decimal_number);  //Note : Output on LCD shows 32 bit binary, hexadecimal and decimal
void lcd1_num(unsigned long int no, char base, char digit);

void lcd2_initialize(void);
void lcd2_home(void);
void lcd2_clear(void);
void lcd2_goto(unsigned char address);
void lcd2_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms);  //Note : direction = LCD_RIGHT, LCD_LEFT
void lcd2_cgram(unsigned char cgram_address, unsigned char *graphic);  //Note :  ex; data[8] = {0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111}; , cgram_address : 0 to 7
void lcd2_senddata(unsigned char c, unsigned char b_rs);
void lcd2_putchar(unsigned char charachter);
void lcd2_printf(unsigned char *string);
void lcd2_numbertoascii(float number);  //Note : Output format in xxx.xxx where x = number
void lcd2_printnumber(unsigned long decimal_number);  //Note : Output on LCD shows 32 bit binary, hexadecimal and decimal
void lcd2_num(unsigned long int no, char base, char digit);

Di bawah adalah Code LCD Penuh :

#define	LCD_RS         8
#define	LCD_E1         9
#define	LCD_E2         10
#define	LCD_DB4        4
#define	LCD_DB5        5
#define	LCD_DB6        6
#define	LCD_DB7        7
#define LCD_BACKLIGHT  11

#define LCD_RIGHT  0b00011100
#define LCD_LEFT   0b00011000

unsigned char userDefinedGraphic1[8] = {0b01110,
/*address = 64 to 127*/                 0b10001,
                                        0b01110,
                                        0b00100,
                                        0b11111,
                                        0b00100,
                                        0b01010,
                                        0b10001};
                          
unsigned char userDefinedGraphic2[8] = {0b00000,
/*address = 64 to 127*/                 0b01010,
                                        0b10101,
                                        0b10001,
                                        0b01010,
                                        0b00100,
                                        0b00000,
                                        0b00000};                        

void lcd1_initialize(void);
void lcd1_home(void);
void lcd1_clear(void);
void lcd1_goto(unsigned char address);
void lcd1_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms);  //Note : direction = LCD_RIGHT, LCD_LEFT
void lcd1_cgram(unsigned char cgram_address, unsigned char *graphic);  //Note :  ex; data[8] = {0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111}; , cgram_address : 0 to 7
void lcd1_senddata(unsigned char c, unsigned char b_rs);
void lcd1_putchar(unsigned char charachter);
void lcd1_printf(unsigned char *string);
void lcd1_numbertoascii(float number);  //Note : Output format in xxx.xxx where x = number
void lcd1_printnumber(unsigned long decimal_number);  //Note : Output on LCD shows 32 bit binary, hexadecimal and decimal
void lcd1_num(unsigned long int no, char base, char digit);

void lcd2_initialize(void);
void lcd2_home(void);
void lcd2_clear(void);
void lcd2_goto(unsigned char address);
void lcd2_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms);  //Note : direction = LCD_RIGHT, LCD_LEFT
void lcd2_cgram(unsigned char cgram_address, unsigned char *graphic);  //Note :  ex; data[8] = {0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111}; , cgram_address : 0 to 7
void lcd2_senddata(unsigned char c, unsigned char b_rs);
void lcd2_putchar(unsigned char charachter);
void lcd2_printf(unsigned char *string);
void lcd2_numbertoascii(float number);  //Note : Output format in xxx.xxx where x = number
void lcd2_printnumber(unsigned long decimal_number);  //Note : Output on LCD shows 32 bit binary, hexadecimal and decimal
void lcd2_num(unsigned long int no, char base, char digit);

void setup()
{
  pinMode(LCD_RS,OUTPUT);
  pinMode(LCD_E1,OUTPUT);
  pinMode(LCD_E2,OUTPUT);
  pinMode(LCD_DB4,OUTPUT);
  pinMode(LCD_DB5,OUTPUT);
  pinMode(LCD_DB6,OUTPUT);
  pinMode(LCD_DB7,OUTPUT);
  analogWrite(LCD_BACKLIGHT,10);
  
  lcd1_initialize();
  lcd2_initialize();
  lcd2_cgram(0, userDefinedGraphic1);
  lcd2_cgram(1, userDefinedGraphic2);
  delay(5000);
}

void loop()
{ 
  for(int i=0; i<999; i++)
  {
    lcd2_goto(0x00);
    lcd2_num(i, 10, 3); 
  }
  lcd1_clear();
  for(float i=999; i<1000; i=i+0.001)
  {
    lcd2_goto(0x00);
    lcd2_numbertoascii(i);
  }
  
  lcd2_clear();
  lcd2_goto(0x00);
  lcd2_printf(" CONTROL PANEL");
  lcd2_goto(0x46);
  lcd2_putchar(0);
  lcd2_putchar(1);
  lcd2_putchar(0);
  
  lcd2_shift(LCD_RIGHT, 5, 500);
  lcd2_shift(LCD_LEFT, 5, 500);
  lcd2_shift(LCD_LEFT, 5, 500);  
  lcd2_shift(LCD_RIGHT, 5, 500);
  
  analogWrite(LCD_BACKLIGHT,0);
  for(int i=0; i<=100; i++)
  {
    lcd1_goto(0x00);
    lcd1_printnumber(i); 
  }
  analogWrite(LCD_BACKLIGHT,10);
  lcd2_goto(0x40);
  lcd2_printf(" TEST COMPLETE  ");
  while(1);
}

void lcd1_initialize(void){
	delay(15);
	digitalWrite(LCD_E1, LOW);
	digitalWrite(LCD_RS, LOW);
	lcd1_senddata(0b0011,0);
	delay(5);
	lcd1_senddata(0b0011,0);
	delay(5);
	lcd1_senddata(0b0011,0);
	delay(5);
	lcd1_senddata(0b0010,0);
	delay(5);
	lcd1_senddata(0b00101000,0);    //2 line
	lcd1_senddata(0b00000110,0);	//entry mode
	lcd1_senddata(0b00001100,0);	//display on
	lcd1_senddata(0b00000001,0);	//clear
	lcd1_senddata(0b00000010,0);	/*home*/
}

void lcd1_home(void){
	lcd1_senddata( 0b00000010,0);}

void lcd1_clear(void){
	lcd1_senddata( 0b00000001,0);
	delay(1);}

void lcd1_goto(unsigned char address){
	lcd1_senddata( address | 0b10000000,0);}

void lcd1_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms){
	signed char i;
	for(i=1;i<=shifting_time;i++){
		delay(speed_ms/2);
		lcd1_senddata( direction,0);
		delay(speed_ms/2);}}

void lcd1_cgram(unsigned char cgram_address, unsigned char *graphic){
	signed char i;
	lcd1_senddata( (0x40 |  (8 * cgram_address ) ) , 0 );
	for(i=0; i<=7 ;graphic++,i++){
		lcd1_senddata(*graphic,1);}}

void lcd1_senddata(unsigned char c, unsigned char b_rs){
	unsigned char data = 0;
	digitalWrite(LCD_E1, LOW);
	if( b_rs == 1 )	digitalWrite(LCD_RS, HIGH);
	else	digitalWrite(LCD_RS, LOW);
	data=(data&0x0F)|(c&0xF0);
	if( 0b00010000 & data )	digitalWrite(LCD_DB4, HIGH);
	else	digitalWrite(LCD_DB4, LOW);
	if( 0b00100000 & data )	digitalWrite(LCD_DB5, HIGH);
	else	digitalWrite(LCD_DB5, LOW);
	if( 0b01000000 & data )	digitalWrite(LCD_DB6, HIGH);
	else	digitalWrite(LCD_DB6, LOW);
	if( 0b10000000 & data )	digitalWrite(LCD_DB7, HIGH);
	else	digitalWrite(LCD_DB7, LOW);
	digitalWrite(LCD_E1, HIGH);
	digitalWrite(LCD_E1, LOW);
	data=(data&0x0F)|((c<<4)&0xF0);
	if( 0b00010000 & data )	digitalWrite(LCD_DB4, HIGH);
	else	digitalWrite(LCD_DB4, LOW);
	if( 0b00100000 & data )	digitalWrite(LCD_DB5, HIGH);
	else	digitalWrite(LCD_DB5, LOW);
	if( 0b01000000 & data )	digitalWrite(LCD_DB6, HIGH);
	else	digitalWrite(LCD_DB6, LOW);
	if( 0b10000000 & data )	digitalWrite(LCD_DB7, HIGH);
	else	digitalWrite(LCD_DB7, LOW);
	digitalWrite(LCD_E1, HIGH);
	digitalWrite(LCD_E1, LOW);
	delay(1);}

void lcd1_putchar(unsigned char charachter){
	lcd1_senddata(charachter,1);}

void lcd1_printf(char *string){
	for(;*string != '\0';string++){
		lcd1_senddata(*string,1);}}

void lcd1_numbertoascii(float number){
	signed int i;
	unsigned int temp;
	unsigned char result;
	for(i=1;i<=7;i++){
		if(i==1){
			result = (unsigned int)number / 100;
			temp = (unsigned int)number % 100;}
	    else if(i==2)   result = temp / 10;
	    else if(i==3)   result = temp % 10;
	    else if(i==4)   number = (number - (unsigned int)number) * 1000;
	    else if(i==5){
			result = (unsigned int)number / 100;
			temp = (unsigned int)number % 100;}
	    else if(i==6)   result = temp / 10;
		else if(i==7)   result = temp % 10;
	    if(i==4)	lcd1_putchar('.');
		else	lcd1_putchar(result | 0b00110000);}}

void lcd1_printnumber(unsigned long decimal_number){
	unsigned long temp;
	unsigned long mask = 0b10000000000000000000000000000000;
	signed char i;
	lcd1_home();
	lcd1_putchar('0');
	lcd1_putchar('b');
	lcd1_goto(0x03);
	for(i=1;i<=32;i++){
		if( i == 1  ) lcd1_goto(0x03);
		else if( i == 5  ) lcd1_goto(0x08);
		else if( i == 9  ) lcd1_goto(0x43);
		else if( i == 13 ) lcd1_goto(0x48);
		else if( i == 17 ) lcd1_goto(0x17);
		else if( i == 21 ) lcd1_goto(0x1C);
		else if( i == 25 ) lcd1_goto(0x57);
		else if( i == 29 ) lcd1_goto(0x5C);
	    if(mask & decimal_number) lcd1_putchar('1');
	    else lcd1_putchar('0');
	    mask = mask >> 1;}
	lcd1_goto(0x0D);
	lcd1_putchar('0');
	lcd1_putchar('x');
	mask = 0b11110000000000000000000000000000;
	for(i=0;i<=31;i=i+4){
		if( i == 16  ) lcd1_goto(0x4F);
		temp = decimal_number & ( mask >> i );
		temp = temp >> (28-i);
		if( temp < 10 ){
			temp = temp | 0b00110000;
			lcd1_putchar(temp);}
		else{
			if(temp == 10 ) lcd1_putchar('A');
			else if(temp == 11 ) lcd1_putchar('B');
			else if(temp == 12 ) lcd1_putchar('C');
			else if(temp == 13 ) lcd1_putchar('D');
			else if(temp == 14 ) lcd1_putchar('E');
			else if(temp == 15 ) lcd1_putchar('F');}}
	lcd1_goto(0x21);
	lcd1_putchar('0');
	lcd1_putchar('d');
	mask = 1000000000;
	for(i=1;i<=10;i++){
		if(i==6) lcd1_goto(0x63);
		if(i==1) temp = decimal_number / mask;
		else temp = temp / mask;
		lcd1_putchar(temp | 0b00110000);
		temp = decimal_number % mask;
		mask = mask/10;}}

void lcd1_num(unsigned long int no, char base, char digit){
	char i,j,di[10];
	for(i=0;i<=9;i++) di[i]=0;
	i=0;
	do{
		di[i]=no%base;
		no=no/base;
		i++;}
	while(no!=0);
	for(j=digit;j>0;j--){
		if(di[j-1]<=9) lcd1_senddata(di[j-1]+'0',1);
		else lcd1_senddata(di[j-1]-10+'A',1);}}

void lcd2_initialize(void){
	delay(15);
	digitalWrite(LCD_E2, LOW);
	digitalWrite(LCD_RS, LOW);
	lcd2_senddata(0b0011,0);
	delay(5);
	lcd2_senddata(0b0011,0);
	delay(5);
	lcd2_senddata(0b0011,0);
	delay(5);
	lcd2_senddata(0b0010,0);
	delay(5);
	lcd2_senddata(0b00101000,0);    //2 line
	lcd2_senddata(0b00000110,0);	//entry mode
	lcd2_senddata(0b00001100,0);	//display on
	lcd2_senddata(0b00000001,0);	//clear
	lcd2_senddata(0b00000010,0);	/*home*/
}

void lcd2_home(void){
	lcd2_senddata( 0b00000010,0);}

void lcd2_clear(void){
	lcd2_senddata( 0b00000001,0);
	delay(1);}

void lcd2_goto(unsigned char address){
	lcd2_senddata( address | 0b10000000,0);}

void lcd2_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms){
	signed char i;
	for(i=1;i<=shifting_time;i++){
		delay(speed_ms/2);
		lcd2_senddata( direction,0);
		delay(speed_ms/2);}}

void lcd2_cgram(unsigned char cgram_address, unsigned char *graphic){
	signed char i;
	lcd2_senddata( (0x40 |  (8 * cgram_address ) ) , 0 );
	for(i=0; i<=7 ;graphic++,i++){
		lcd2_senddata(*graphic,1);}}

void lcd2_senddata(unsigned char c, unsigned char b_rs){
	unsigned char data = 0;
	digitalWrite(LCD_E2, LOW);
	if( b_rs == 1 )	digitalWrite(LCD_RS, HIGH);
	else	digitalWrite(LCD_RS, LOW);
	data=(data&0x0F)|(c&0xF0);
	if( 0b00010000 & data )	digitalWrite(LCD_DB4, HIGH);
	else	digitalWrite(LCD_DB4, LOW);
	if( 0b00100000 & data )	digitalWrite(LCD_DB5, HIGH);
	else	digitalWrite(LCD_DB5, LOW);
	if( 0b01000000 & data )	digitalWrite(LCD_DB6, HIGH);
	else	digitalWrite(LCD_DB6, LOW);
	if( 0b10000000 & data )	digitalWrite(LCD_DB7, HIGH);
	else	digitalWrite(LCD_DB7, LOW);
	digitalWrite(LCD_E2, HIGH);
	digitalWrite(LCD_E2, LOW);
	data=(data&0x0F)|((c<<4)&0xF0);
	if( 0b00010000 & data )	digitalWrite(LCD_DB4, HIGH);
	else	digitalWrite(LCD_DB4, LOW);
	if( 0b00100000 & data )	digitalWrite(LCD_DB5, HIGH);
	else	digitalWrite(LCD_DB5, LOW);
	if( 0b01000000 & data )	digitalWrite(LCD_DB6, HIGH);
	else	digitalWrite(LCD_DB6, LOW);
	if( 0b10000000 & data )	digitalWrite(LCD_DB7, HIGH);
	else	digitalWrite(LCD_DB7, LOW);
	digitalWrite(LCD_E2, HIGH);
	digitalWrite(LCD_E2, LOW);
	delay(1);}

void lcd2_putchar(unsigned char charachter){
	lcd2_senddata(charachter,1);}

void lcd2_printf(char *string){
	for(;*string != '\0';string++){
		lcd2_senddata(*string,1);}}

void lcd2_numbertoascii(float number){
	signed int i;
	unsigned int temp;
	unsigned char result;
	for(i=1;i<=7;i++){
		if(i==1){
			result = (unsigned int)number / 100;
			temp = (unsigned int)number % 100;}
	    else if(i==2)   result = temp / 10;
	    else if(i==3)   result = temp % 10;
	    else if(i==4)   number = (number - (unsigned int)number) * 1000;
	    else if(i==5){
			result = (unsigned int)number / 100;
			temp = (unsigned int)number % 100;}
	    else if(i==6)   result = temp / 10;
		else if(i==7)   result = temp % 10;
	    if(i==4)	lcd2_putchar('.');
		else	lcd2_putchar(result | 0b00110000);}}

void lcd2_printnumber(unsigned long decimal_number){
	unsigned long temp;
	unsigned long mask = 0b10000000000000000000000000000000;
	signed char i;
	lcd2_home();
	lcd2_putchar('0');
	lcd2_putchar('b');
	lcd2_goto(0x03);
	for(i=1;i<=32;i++){
		if( i == 1  ) lcd2_goto(0x03);
		else if( i == 5  ) lcd2_goto(0x08);
		else if( i == 9  ) lcd2_goto(0x43);
		else if( i == 13 ) lcd2_goto(0x48);
		else if( i == 17 ) lcd2_goto(0x17);
		else if( i == 21 ) lcd2_goto(0x1C);
		else if( i == 25 ) lcd2_goto(0x57);
		else if( i == 29 ) lcd2_goto(0x5C);
	    if(mask & decimal_number) lcd2_putchar('1');
	    else lcd2_putchar('0');
	    mask = mask >> 1;}
	lcd2_goto(0x0D);
	lcd2_putchar('0');
	lcd2_putchar('x');
	mask = 0b11110000000000000000000000000000;
	for(i=0;i<=31;i=i+4){
		if( i == 16  ) lcd2_goto(0x4F);
		temp = decimal_number & ( mask >> i );
		temp = temp >> (28-i);
		if( temp < 10 ){
			temp = temp | 0b00110000;
			lcd2_putchar(temp);}
		else{
			if(temp == 10 ) lcd2_putchar('A');
			else if(temp == 11 ) lcd2_putchar('B');
			else if(temp == 12 ) lcd2_putchar('C');
			else if(temp == 13 ) lcd2_putchar('D');
			else if(temp == 14 ) lcd2_putchar('E');
			else if(temp == 15 ) lcd2_putchar('F');}}
	lcd2_goto(0x21);
	lcd2_putchar('0');
	lcd2_putchar('d');
	mask = 1000000000;
	for(i=1;i<=10;i++){
		if(i==6) lcd2_goto(0x63);
		if(i==1) temp = decimal_number / mask;
		else temp = temp / mask;
		lcd2_putchar(temp | 0b00110000);
		temp = decimal_number % mask;
		mask = mask/10;}}

void lcd2_num(unsigned long int no, char base, char digit){
	char i,j,di[10];
	for(i=0;i<=9;i++) di[i]=0;
	i=0;
	do{
		di[i]=no%base;
		no=no/base;
		i++;}
	while(no!=0);
	for(j=digit;j>0;j--){
		if(di[j-1]<=9) lcd2_senddata(di[j-1]+'0',1);
		else lcd2_senddata(di[j-1]-10+'A',1);}}

Video LCD Test

Di bawah adalah senarai function Analog Keypad :

void analog_keypad(void);	//Note : If no button push, KeypadInput = ' '

Di bawah adalah code LCD + Analog Keypad penuh :

#define	LCD_RS         8
#define	LCD_E1         9
#define	LCD_E2         10
#define	LCD_DB4        4
#define	LCD_DB5        5
#define	LCD_DB6        6
#define	LCD_DB7        7
#define LCD_BACKLIGHT  11

#define LCD_RIGHT  0b00011100
#define LCD_LEFT   0b00011000

unsigned char userDefinedGraphic1[8] = {0b01110,
/*address = 64 to 127*/                 0b10001,
                                        0b01110,
                                        0b00100,
                                        0b11111,
                                        0b00100,
                                        0b01010,
                                        0b10001};
                          
unsigned char userDefinedGraphic2[8] = {0b00000,
/*address = 64 to 127*/                 0b01010,
                                        0b10101,
                                        0b10001,
                                        0b01010,
                                        0b00100,
                                        0b00000,
                                        0b00000};                        

void lcd1_initialize(void);
void lcd1_home(void);
void lcd1_clear(void);
void lcd1_goto(unsigned char address);
void lcd1_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms);  //Note : direction = LCD_RIGHT, LCD_LEFT
void lcd1_cgram(unsigned char cgram_address, unsigned char *graphic);  //Note :  ex; data[8] = {0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111}; , cgram_address : 0 to 7
void lcd1_senddata(unsigned char c, unsigned char b_rs);
void lcd1_putchar(unsigned char charachter);
void lcd1_printf(unsigned char *string);
void lcd1_numbertoascii(float number);  //Note : Output format in xxx.xxx where x = number
void lcd1_printnumber(unsigned long decimal_number);  //Note : Output on LCD shows 32 bit binary, hexadecimal and decimal
void lcd1_num(unsigned long int no, char base, char digit);

void lcd2_initialize(void);
void lcd2_home(void);
void lcd2_clear(void);
void lcd2_goto(unsigned char address);
void lcd2_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms);  //Note : direction = LCD_RIGHT, LCD_LEFT
void lcd2_cgram(unsigned char cgram_address, unsigned char *graphic);  //Note :  ex; data[8] = {0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111,0b11111}; , cgram_address : 0 to 7
void lcd2_senddata(unsigned char c, unsigned char b_rs);
void lcd2_putchar(unsigned char charachter);
void lcd2_printf(unsigned char *string);
void lcd2_numbertoascii(float number);  //Note : Output format in xxx.xxx where x = number
void lcd2_printnumber(unsigned long decimal_number);  //Note : Output on LCD shows 32 bit binary, hexadecimal and decimal
void lcd2_num(unsigned long int no, char base, char digit);

#define Vref 4.97
#define Resolution	Vref/1024.0
#define analogkeypad	0
#define upper_boundary (Vref/12.0)+0.1
#define lower_boundary (Vref/12.0)-0.1
unsigned char KeypadInput;
void analog_keypad(void);	//Note : If no button push, KeypadInput = ' '

void setup()
{
  pinMode(LCD_RS,OUTPUT);
  pinMode(LCD_E1,OUTPUT);
  pinMode(LCD_E2,OUTPUT);
  pinMode(LCD_DB4,OUTPUT);
  pinMode(LCD_DB5,OUTPUT);
  pinMode(LCD_DB6,OUTPUT);
  pinMode(LCD_DB7,OUTPUT);
  analogWrite(LCD_BACKLIGHT,10);
  
  lcd1_initialize();
  lcd2_initialize();
  lcd2_printf("    LCD TEST    "); 
  lcd2_goto(0x40);
  lcd2_printf("PRESS ANY BUTTON"); 
}

void loop()
{ 
  analog_keypad();
  for(int i=0; i<0x68 ; i++){lcd1_putchar(KeypadInput);}
}

void lcd1_initialize(void){
	delay(15);
	digitalWrite(LCD_E1, LOW);
	digitalWrite(LCD_RS, LOW);
	lcd1_senddata(0b0011,0);
	delay(5);
	lcd1_senddata(0b0011,0);
	delay(5);
	lcd1_senddata(0b0011,0);
	delay(5);
	lcd1_senddata(0b0010,0);
	delay(5);
	lcd1_senddata(0b00101000,0);    //2 line
	lcd1_senddata(0b00000110,0);	//entry mode
	lcd1_senddata(0b00001100,0);	//display on
	lcd1_senddata(0b00000001,0);	//clear
	lcd1_senddata(0b00000010,0);	/*home*/
}

void lcd1_home(void){
	lcd1_senddata( 0b00000010,0);}

void lcd1_clear(void){
	lcd1_senddata( 0b00000001,0);
	delay(1);}

void lcd1_goto(unsigned char address){
	lcd1_senddata( address | 0b10000000,0);}

void lcd1_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms){
	signed char i;
	for(i=1;i<=shifting_time;i++){
		delay(speed_ms/2);
		lcd1_senddata( direction,0);
		delay(speed_ms/2);}}

void lcd1_cgram(unsigned char cgram_address, unsigned char *graphic){
	signed char i;
	lcd1_senddata( (0x40 |  (8 * cgram_address ) ) , 0 );
	for(i=0; i<=7 ;graphic++,i++){
		lcd1_senddata(*graphic,1);}}

void lcd1_senddata(unsigned char c, unsigned char b_rs){
	unsigned char data = 0;
	digitalWrite(LCD_E1, LOW);
	if( b_rs == 1 )	digitalWrite(LCD_RS, HIGH);
	else	digitalWrite(LCD_RS, LOW);
	data=(data&0x0F)|(c&0xF0);
	if( 0b00010000 & data )	digitalWrite(LCD_DB4, HIGH);
	else	digitalWrite(LCD_DB4, LOW);
	if( 0b00100000 & data )	digitalWrite(LCD_DB5, HIGH);
	else	digitalWrite(LCD_DB5, LOW);
	if( 0b01000000 & data )	digitalWrite(LCD_DB6, HIGH);
	else	digitalWrite(LCD_DB6, LOW);
	if( 0b10000000 & data )	digitalWrite(LCD_DB7, HIGH);
	else	digitalWrite(LCD_DB7, LOW);
	digitalWrite(LCD_E1, HIGH);
	digitalWrite(LCD_E1, LOW);
	data=(data&0x0F)|((c<<4)&0xF0);
	if( 0b00010000 & data )	digitalWrite(LCD_DB4, HIGH);
	else	digitalWrite(LCD_DB4, LOW);
	if( 0b00100000 & data )	digitalWrite(LCD_DB5, HIGH);
	else	digitalWrite(LCD_DB5, LOW);
	if( 0b01000000 & data )	digitalWrite(LCD_DB6, HIGH);
	else	digitalWrite(LCD_DB6, LOW);
	if( 0b10000000 & data )	digitalWrite(LCD_DB7, HIGH);
	else	digitalWrite(LCD_DB7, LOW);
	digitalWrite(LCD_E1, HIGH);
	digitalWrite(LCD_E1, LOW);
	delay(1);}

void lcd1_putchar(unsigned char charachter){
	lcd1_senddata(charachter,1);}

void lcd1_printf(char *string){
	for(;*string != '\0';string++){
		lcd1_senddata(*string,1);}}

void lcd1_numbertoascii(float number){
	signed int i;
	unsigned int temp;
	unsigned char result;
	for(i=1;i<=7;i++){
		if(i==1){
			result = (unsigned int)number / 100;
			temp = (unsigned int)number % 100;}
	    else if(i==2)   result = temp / 10;
	    else if(i==3)   result = temp % 10;
	    else if(i==4)   number = (number - (unsigned int)number) * 1000;
	    else if(i==5){
			result = (unsigned int)number / 100;
			temp = (unsigned int)number % 100;}
	    else if(i==6)   result = temp / 10;
		else if(i==7)   result = temp % 10;
	    if(i==4)	lcd1_putchar('.');
		else	lcd1_putchar(result | 0b00110000);}}

void lcd1_printnumber(unsigned long decimal_number){
	unsigned long temp;
	unsigned long mask = 0b10000000000000000000000000000000;
	signed char i;
	lcd1_home();
	lcd1_putchar('0');
	lcd1_putchar('b');
	lcd1_goto(0x03);
	for(i=1;i<=32;i++){
		if( i == 1  ) lcd1_goto(0x03);
		else if( i == 5  ) lcd1_goto(0x08);
		else if( i == 9  ) lcd1_goto(0x43);
		else if( i == 13 ) lcd1_goto(0x48);
		else if( i == 17 ) lcd1_goto(0x17);
		else if( i == 21 ) lcd1_goto(0x1C);
		else if( i == 25 ) lcd1_goto(0x57);
		else if( i == 29 ) lcd1_goto(0x5C);
	    if(mask & decimal_number) lcd1_putchar('1');
	    else lcd1_putchar('0');
	    mask = mask >> 1;}
	lcd1_goto(0x0D);
	lcd1_putchar('0');
	lcd1_putchar('x');
	mask = 0b11110000000000000000000000000000;
	for(i=0;i<=31;i=i+4){
		if( i == 16  ) lcd1_goto(0x4F);
		temp = decimal_number & ( mask >> i );
		temp = temp >> (28-i);
		if( temp < 10 ){
			temp = temp | 0b00110000;
			lcd1_putchar(temp);}
		else{
			if(temp == 10 ) lcd1_putchar('A');
			else if(temp == 11 ) lcd1_putchar('B');
			else if(temp == 12 ) lcd1_putchar('C');
			else if(temp == 13 ) lcd1_putchar('D');
			else if(temp == 14 ) lcd1_putchar('E');
			else if(temp == 15 ) lcd1_putchar('F');}}
	lcd1_goto(0x21);
	lcd1_putchar('0');
	lcd1_putchar('d');
	mask = 1000000000;
	for(i=1;i<=10;i++){
		if(i==6) lcd1_goto(0x63);
		if(i==1) temp = decimal_number / mask;
		else temp = temp / mask;
		lcd1_putchar(temp | 0b00110000);
		temp = decimal_number % mask;
		mask = mask/10;}}

void lcd1_num(unsigned long int no, char base, char digit){
	char i,j,di[10];
	for(i=0;i<=9;i++) di[i]=0;
	i=0;
	do{
		di[i]=no%base;
		no=no/base;
		i++;}
	while(no!=0);
	for(j=digit;j>0;j--){
		if(di[j-1]<=9) lcd1_senddata(di[j-1]+'0',1);
		else lcd1_senddata(di[j-1]-10+'A',1);}}

void lcd2_initialize(void){
	delay(15);
	digitalWrite(LCD_E2, LOW);
	digitalWrite(LCD_RS, LOW);
	lcd2_senddata(0b0011,0);
	delay(5);
	lcd2_senddata(0b0011,0);
	delay(5);
	lcd2_senddata(0b0011,0);
	delay(5);
	lcd2_senddata(0b0010,0);
	delay(5);
	lcd2_senddata(0b00101000,0);    //2 line
	lcd2_senddata(0b00000110,0);	//entry mode
	lcd2_senddata(0b00001100,0);	//display on
	lcd2_senddata(0b00000001,0);	//clear
	lcd2_senddata(0b00000010,0);	/*home*/
}

void lcd2_home(void){
	lcd2_senddata( 0b00000010,0);}

void lcd2_clear(void){
	lcd2_senddata( 0b00000001,0);
	delay(1);}

void lcd2_goto(unsigned char address){
	lcd2_senddata( address | 0b10000000,0);}

void lcd2_shift(unsigned char direction, unsigned char shifting_time, unsigned int speed_ms){
	signed char i;
	for(i=1;i<=shifting_time;i++){
		delay(speed_ms/2);
		lcd2_senddata( direction,0);
		delay(speed_ms/2);}}

void lcd2_cgram(unsigned char cgram_address, unsigned char *graphic){
	signed char i;
	lcd2_senddata( (0x40 |  (8 * cgram_address ) ) , 0 );
	for(i=0; i<=7 ;graphic++,i++){
		lcd2_senddata(*graphic,1);}}

void lcd2_senddata(unsigned char c, unsigned char b_rs){
	unsigned char data = 0;
	digitalWrite(LCD_E2, LOW);
	if( b_rs == 1 )	digitalWrite(LCD_RS, HIGH);
	else	digitalWrite(LCD_RS, LOW);
	data=(data&0x0F)|(c&0xF0);
	if( 0b00010000 & data )	digitalWrite(LCD_DB4, HIGH);
	else	digitalWrite(LCD_DB4, LOW);
	if( 0b00100000 & data )	digitalWrite(LCD_DB5, HIGH);
	else	digitalWrite(LCD_DB5, LOW);
	if( 0b01000000 & data )	digitalWrite(LCD_DB6, HIGH);
	else	digitalWrite(LCD_DB6, LOW);
	if( 0b10000000 & data )	digitalWrite(LCD_DB7, HIGH);
	else	digitalWrite(LCD_DB7, LOW);
	digitalWrite(LCD_E2, HIGH);
	digitalWrite(LCD_E2, LOW);
	data=(data&0x0F)|((c<<4)&0xF0);
	if( 0b00010000 & data )	digitalWrite(LCD_DB4, HIGH);
	else	digitalWrite(LCD_DB4, LOW);
	if( 0b00100000 & data )	digitalWrite(LCD_DB5, HIGH);
	else	digitalWrite(LCD_DB5, LOW);
	if( 0b01000000 & data )	digitalWrite(LCD_DB6, HIGH);
	else	digitalWrite(LCD_DB6, LOW);
	if( 0b10000000 & data )	digitalWrite(LCD_DB7, HIGH);
	else	digitalWrite(LCD_DB7, LOW);
	digitalWrite(LCD_E2, HIGH);
	digitalWrite(LCD_E2, LOW);
	delay(1);}

void lcd2_putchar(unsigned char charachter){
	lcd2_senddata(charachter,1);}

void lcd2_printf(char *string){
	for(;*string != '\0';string++){
		lcd2_senddata(*string,1);}}

void lcd2_numbertoascii(float number){
	signed int i;
	unsigned int temp;
	unsigned char result;
	for(i=1;i<=7;i++){
		if(i==1){
			result = (unsigned int)number / 100;
			temp = (unsigned int)number % 100;}
	    else if(i==2)   result = temp / 10;
	    else if(i==3)   result = temp % 10;
	    else if(i==4)   number = (number - (unsigned int)number) * 1000;
	    else if(i==5){
			result = (unsigned int)number / 100;
			temp = (unsigned int)number % 100;}
	    else if(i==6)   result = temp / 10;
		else if(i==7)   result = temp % 10;
	    if(i==4)	lcd2_putchar('.');
		else	lcd2_putchar(result | 0b00110000);}}

void lcd2_printnumber(unsigned long decimal_number){
	unsigned long temp;
	unsigned long mask = 0b10000000000000000000000000000000;
	signed char i;
	lcd2_home();
	lcd2_putchar('0');
	lcd2_putchar('b');
	lcd2_goto(0x03);
	for(i=1;i<=32;i++){
		if( i == 1  ) lcd2_goto(0x03);
		else if( i == 5  ) lcd2_goto(0x08);
		else if( i == 9  ) lcd2_goto(0x43);
		else if( i == 13 ) lcd2_goto(0x48);
		else if( i == 17 ) lcd2_goto(0x17);
		else if( i == 21 ) lcd2_goto(0x1C);
		else if( i == 25 ) lcd2_goto(0x57);
		else if( i == 29 ) lcd2_goto(0x5C);
	    if(mask & decimal_number) lcd2_putchar('1');
	    else lcd2_putchar('0');
	    mask = mask >> 1;}
	lcd2_goto(0x0D);
	lcd2_putchar('0');
	lcd2_putchar('x');
	mask = 0b11110000000000000000000000000000;
	for(i=0;i<=31;i=i+4){
		if( i == 16  ) lcd2_goto(0x4F);
		temp = decimal_number & ( mask >> i );
		temp = temp >> (28-i);
		if( temp < 10 ){
			temp = temp | 0b00110000;
			lcd2_putchar(temp);}
		else{
			if(temp == 10 ) lcd2_putchar('A');
			else if(temp == 11 ) lcd2_putchar('B');
			else if(temp == 12 ) lcd2_putchar('C');
			else if(temp == 13 ) lcd2_putchar('D');
			else if(temp == 14 ) lcd2_putchar('E');
			else if(temp == 15 ) lcd2_putchar('F');}}
	lcd2_goto(0x21);
	lcd2_putchar('0');
	lcd2_putchar('d');
	mask = 1000000000;
	for(i=1;i<=10;i++){
		if(i==6) lcd2_goto(0x63);
		if(i==1) temp = decimal_number / mask;
		else temp = temp / mask;
		lcd2_putchar(temp | 0b00110000);
		temp = decimal_number % mask;
		mask = mask/10;}}

void lcd2_num(unsigned long int no, char base, char digit){
	char i,j,di[10];
	for(i=0;i<=9;i++) di[i]=0;
	i=0;
	do{
		di[i]=no%base;
		no=no/base;
		i++;}
	while(no!=0);
	for(j=digit;j>0;j--){
		if(di[j-1]<=9) lcd2_senddata(di[j-1]+'0',1);
		else lcd2_senddata(di[j-1]-10+'A',1);}}

void analog_keypad(void){
  float temp;
  if(analogkeypad==0)  temp = (float)analogRead(A0) * Resolution;
  else if(analogkeypad==1)  temp = (float)analogRead(A1) * Resolution;
  else if(analogkeypad==2)  temp = (float)analogRead(A2) * Resolution;
  if( Vref - 0.1 >temp ){
    if(temp < 0*upper_boundary && temp > 0*lower_boundary)	KeypadInput = '#';
    else if(temp < 1*upper_boundary && temp > 1*lower_boundary)	KeypadInput = '0';
    else if(temp < 2*upper_boundary && temp > 2*lower_boundary)	KeypadInput = '*';
    else if(temp < 3*upper_boundary && temp > 3*lower_boundary)	KeypadInput = '7';
    else if(temp < 4*upper_boundary && temp > 4*lower_boundary)	KeypadInput = '8';
    else if(temp < 5*upper_boundary && temp > 5*lower_boundary)	KeypadInput = '9';
    else if(temp < 6*upper_boundary && temp > 6*lower_boundary)	KeypadInput = '6';
    else if(temp < 7*upper_boundary && temp > 7*lower_boundary)	KeypadInput = '5';
    else if(temp < 8*upper_boundary && temp > 8*lower_boundary)	KeypadInput = '4';
    else if(temp < 9*upper_boundary && temp > 9*lower_boundary)	KeypadInput = '1';
    else if(temp < 10*upper_boundary && temp > 10*lower_boundary)  KeypadInput = '2';
    else if(temp < 11*upper_boundary && temp > 11*lower_boundary)  KeypadInput = '3';}
  else KeypadInput = ' ';
}

Video Analog Keypad Test

LEAVE A REPLY

Please enter your comment!
Please enter your name here