Witam wszystkich, jestem pocztykujacym w temacie programowania i napotkalem sie na problem odczytu klawiatury matrycowej 4x4 w mikrokontrolerze atmega32. program wydaje mi sie poprawny ale czyta tyklo dwa klawisze.
#include <util\delay_basic.h>
#include <stdio.h>
#include <avr/io.h>
#include "lcd.h"
#include <string.h>
#include <stdlib.h>
#define key_1 0b0000000000000001
#define key_2 0b0000000000000010
#define key_3 0b0000000000000100
#define key_A 0b0000000000001000
#define key_4 0b0000000000010000
#define key_5 0b0000000000100000
#define key_6 0b0000000001000000
#define key_B 0b0000000010000000
#define key_7 0b0000000100000000
#define key_8 0b0000001000000000
#define key_9 0b0000010000001000
#define key_C 0b0000100000000000
#define key_gwiazdka 0b0001000000000000
#define key_0 0b0010000000000000
#define key_krzyzyk 0b0100000000000000
#define key_D 0b1000000000000000
//wartosci zmiennej motor_state
#define JAZDA_LEWO 1
#define JAZDA_PRAWO 2
#define STOP 0
unsigned char motor_state = STOP;
typedef struct{
unsigned int b0:1,
b1:1,
b2:1,
b3:1,
b4:1,
b5:1,
b6:1,
b7:1,
b8:1,
b9:1,
b10:1,
b11:1,
b12:1,
b13:1,
b14:1,
b15:1;
}_bity;
typedef union{
_bity bity;
unsigned int bajt;
}bajt_bity;
//----------------------------------------------------------------
//czytanie klawiatury matrycowej 4x4
bajt_bity read_key(void)
{
bajt_bity stany;
PORTB=0b11111110; //pierwszy wiersz
DDRB=~PORTB;
stany.bajt=PINB>>4;
DDRB=0;
PORTB=0b11111101; //drugi wiersz
DDRB=~PORTB;
stany.bajt+=(PINB>>4)<<4;
DDRB=0;
PORTB=0b11111011; //trzeci wiersz
DDRB=~PORTB;
stany.bajt+=(PINB>>4)<<8;
DDRB=0;
PORTB=0b11110111; //czwarty wiersz
DDRB=~PORTB;
stany.bajt+=(PINB>>4)<<12;
DDRB=0;
return stany;
};