/* * LCDxKEYx.c * * Created: 7/10/2015 10:26:52 pm * Author: Dimitrios Porlidas */ /* LCD 2x16 with keyboard 4x4 and 8 buttons PC160LRS-QWB-B (with GREEK characters) www.porlidas.gr */ #include "LCD_2x16.h" unsigned char out; //LCD function unsigned char i1; //Keybord function unsigned char i2; //Logo counter unsigned char i3; //Buttons function unsigned char key; //Keyboard function unsigned char logo[] = {' ',' ',' ',' ','L','C','D',' ','2','x','1','6',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ', 'w','w','w','.','p','o','r','l','i','d','a','s','.','g','r',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; void WrIn (unsigned char tmp) //Write Instruction rutine { out = tmp & 0b11110000; //Upper PORTB = out; _delay_us (100); PORTB = (out | En); _delay_us (100); PORTB = out; _delay_us (100); out = tmp << 4; //Lower PORTB = out; _delay_us (100); PORTB = (out | En); _delay_us (100); PORTB = out; _delay_us (50); } void WrDa (unsigned char tmp) //Write Data rutine { out = tmp & 0b11110000; //Upper PORTB = (out | Da); _delay_us (100); PORTB = (out | En | Da); _delay_us (100); PORTB = (out | Da); _delay_us (100); out = tmp << 4; //Lower PORTB = (out | Da); _delay_us (100); PORTB = (out | En | Da); _delay_us (100); PORTB = (out | Da); _delay_us (50); } void SeCu (unsigned char addr) //Send cursor to a specific address (0-80) { WrIn (0b10000000 | addr); } void TypeLogo (unsigned char logo2[]) //Logo rutine { for (i2 = 0; i2 < 80; i2++) //Send logo WrDa (logo2[i2]); } void InitLCD (void) //Start up, initialization rutine { PORTB = 0; out = 0b00110000; _delay_ms (20); //Power on _delay_ms 20ms PORTB = out; _delay_us (100); PORTB = (out | En); //Init _delay_us (100); PORTB = out; _delay_ms (5); PORTB = (out | En); //Init _delay_us (100); PORTB = out; _delay_us (200); PORTB = (out | En); //Init _delay_us (100); PORTB = out; _delay_ms (5); out = 0b00100000; //4 bit interface PORTB = out; _delay_us (100); PORTB = (out | En); _delay_us (100); PORTB = out; _delay_us (50); WrIn (0b00101000); //4 bit, 2 lines, 5x8 (LCD) WrIn (0b00001110); //Display on, cursor on, blinking off (LCD) WrIn (0b00000110); //Cursor increase, display not shift (LCD) WrIn (CDRS); //Clear display, reset cursor (LCD) _delay_ms (2); TypeLogo (logo); } void Keyb (void) //Keyboard rutine { key = 1; for (i1 = 0; i1 < 4; i1++) { PORTA = ~key; key = key << 1; while (PINA < 0b11101111) { _delay_ms (5); switch (PINA) { case 0b11101110: //Button 1 pressed WrDa ('1'); while (PINA == 0b11101110); //Wait if button is still pressed break; case 0b11101101: //Button 4 pressed WrDa ('4'); while (PINA == 0b11101101); //Wait if button is still pressed break; case 0b11101011: //Button 7 pressed WrDa ('7'); while (PINA == 0b11101011); //Wait if button is still pressed break; case 0b11100111: //Button * pressed WrDa ('*'); while (PINA == 0b11100111); //Wait if button is still pressed break; case 0b11011110: //Button 2 pressed WrDa ('2'); while (PINA == 0b11011110); //Wait if button is still pressed break; case 0b11011101: //Button 5 pressed WrDa ('5'); while (PINA == 0b11011101); //Wait if button is still pressed break; case 0b11011011: //Button 8 pressed WrDa ('8'); while (PINA == 0b11011011); //Wait if button is still pressed break; case 0b11010111: //Button 0 pressed WrDa ('0'); while (PINA == 0b11010111); //Wait if button is still pressed break; case 0b10111110: //Button 3 pressed WrDa ('3'); while (PINA == 0b10111110); //Wait if button is still pressed break; case 0b10111101: //Button 6 pressed WrDa ('6'); while (PINA == 0b10111101); //Wait if button is still pressed break; case 0b10111011: //Button 9 pressed WrDa ('9'); while (PINA == 0b10111011); //Wait if button is still pressed break; case 0b10110111: //Button # pressed WrDa ('#'); while (PINA == 0b10110111); //Wait if button is still pressed break; case 0b01111110: //Button A pressed WrDa ('A'); while (PINA == 0b01111110); //Wait if button is still pressed break; case 0b01111101: //Button B pressed WrDa ('B'); while (PINA == 0b01111101); //Wait if button is still pressed break; case 0b01111011: //Button C pressed WrDa ('C'); while (PINA == 0b01111011); //Wait if button is still pressed break; case 0b01110111: //Button D pressed WrDa ('D'); while (PINA == 0b01110111); //Wait if button is still pressed break; } } } } void Buttons (void) { while ( (PINC == 0b11111110) || //Check buttons 0 - 7 (PINC == 0b11111101) || (PINC == 0b11111011) || (PINC == 0b11110111) || (PINC == 0b11101111) || (PINC == 0b11011111) || (PINC == 0b10111111) || (PINC == 0b01111111) ) { _delay_ms (5); switch (PINC) { case 0b01111111: //Button 0 pressed i3++; WrDa (i3); _delay_ms (300); WrIn (CurL); //Shift cursor left break; case 0b10111111: //Button 1 pressed i3--; WrDa (i3); _delay_ms (300); WrIn (CurL); //Shift cursor left break; case 0b11011111: //Button 2 pressed WrIn (CDRS); //Clear display, reset cursor _delay_ms (2); TypeLogo (logo); //Send logo while (PINC == 0b11011111); //Wait if button is still pressed break; case 0b11101111: //Button 3 pressed WrIn (DisR); //Shift display right while (PINC == 0b11101111); //Wait if button is still pressed break; case 0b11110111: //Button 4 pressed WrIn (DisL); //Shift display left while (PINC == 0b11110111); //Wait if button is still pressed break; case 0b11111011: //Button 5 pressed WrIn (CurR); //Shift cursor right while (PINC == 0b11111011); //Wait if button is still pressed break; case 0b11111101: //Button 6 pressed WrIn (CurL); //Shift cursor left while (PINC == 0b11111101); //Wait if button is still pressed break; case 0b11111110: //Button 7 pressed WrIn (CDRS); //Clear display, reset cursor _delay_ms (2); while (PINC == 0b11111110); //Wait if button is still pressed break; } } }