Šoreiz autors ir izveidojis vienkāršu elektrisko principiālo shēmu, ar kuras palīdzību, izmantojot ATMEGA328P sērijas mikrokontrolleri, ir iespējams nolasīt nospiesto taustiņu, kurš pēc atšifrēšanas tiek attēlots uz 16x2 šķidro kristālu displeja.
1.1.att. Elektriskā principiālā shēma
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lcd.h"
#define KEY_PRT PORTD // Tastatūras ports
#define KEY_PIN PIND
unsigned char Key();
int main(void){
DDRB = 0x0F;
DDRC = 0x0F; // Porti tiek iestatīti gan uz izeju, gan uz ieeju
DDRD = 0xF0;
PORTD = 0x0F; // Aktivizē augsta signāla līmeņa piesaistes rezistorus
InitLCD(0);
LCDClear();
unsigned char tmp[2]=" ";
while (1){
_delay_ms(100);
tmp[0] = Key();
LCDClear();
_delay_ms(50);
LCDWriteStringXY(0,0,"Simbols = ");
LCDWriteStringXY(10,0,tmp);
}
return 0;
}
unsigned char Key(){
unsigned int temp_PIND = KEY_PIN;
unsigned int val=0;
unsigned char simbols;
KEY_PRT = 0xFF;
for(int row=0;row<4;row++){ // Rindas
val=0;
temp_PIND = KEY_PIN;
if(row == 0){
PORTD &=~(1<<PD4);
PORTD |= (1<<PD5);
PORTD |= (1<<PD6);
PORTD |= (1<<PD7);
val = 10;
}
else if(row == 1){
PORTD |= (1<<PD4);
PORTD &=~(1<<PD5);
PORTD |= (1<<PD6);
PORTD |= (1<<PD7);
val = 20;
}
else if(row == 2){
PORTD |= (1<<PD4);
PORTD |= (1<<PD5);
PORTD &=~(1<<PD6);
PORTD |= (1<<PD7);
val = 30;
}
else{
PORTD |= (1<<PD4);
PORTD |= (1<<PD5);
PORTD |= (1<<PD6);
PORTD &=~(1<<PD7);
val = 40;
}
_delay_ms(20); // Kolonnu pārbaude
if(!(KEY_PIN & (1<<PIND0)) && (temp_PIND & (1<<PD0))){
val = val + 1; // val = rinda+kolonna, piemēram, ja '2', tad val = 32
break; // rinda - desmiti, kolonna - vieni
}
else if(!(KEY_PIN & (1<<PIND1)) && (temp_PIND & (1<<PD1))){
val = val + 2;
break;
}
else if(!(KEY_PIN & (1<<PIND2)) && (temp_PIND & (1<<PD2))){
val = val + 3;
break;
}
else if(!(KEY_PIN & (1<<PIND3)) && (temp_PIND & (1<<PD3))){
val = val + 4;
break;
}
else{
val = 0;
}
}
switch(val){
case (11):
simbols = '7';
break;
case (12):
simbols = '8';
break;
case (13):
simbols = '9';
break;
case (14):
simbols = '/';
break;
case (21):
simbols = '4';
break;
case (22):
simbols = '5';
break;
case (23):
simbols = '6';
break;
case (24):
simbols = '*';
break;
case (31):
simbols = '1';
break;
case (32):
simbols = '2';
break;
case (33):
simbols = '3';
break;
case (34):
simbols = '-';
break;
case (41):
simbols = 'C';
break;
case (42):
simbols = '0';
break;
case (43):
simbols = '=';
break;
case (44):
simbols = '+';
break;
default:
simbols = ' ';
break;
}
val = 0;
_delay_ms(10);
return simbols;
}