Hello, eveyone.
I've stumbled upon some errors on a program I'm developing for the scanning of a 8x4 Matrix Keyboard. The board I'm using is the Arduino Due.
I am using a Timer Interrupt in order to change the state of the columns defined as outputs each 10ms one of them go to 0 and as soon as one of the rows (input pull-ups) identify a low state one number is stored and showed on the serial monitor.
It's partially working but there's some minor bugs I would like to get rid of.
It keeps displaying on the serial monitor the last row of numbers even though I haven't connected anything to do so. (Numbers 25 to 32)
When I connect the columns to the row it displays the numbers correctly, but for some reason I can't understand why the last column does not display aything if connected to the rows.
Here is the first part of the program:
// =============================================================================================================
// --- Hardware ---
#define col_1 2 //coluna 1 do teclado
#define col_2 3 //coluna 2 do teclado
#define col_3 4 //coluna 3 do teclado
#define col_4 5 //coluna 4 do teclado
#define col_5 6 //coluna 5 do teclado
#define col_6 7 //coluna 6 do teclado
#define col_7 8 //coluna 7 do teclado
#define col_8 9 //coluna 8 do teclado
#define row_A 10 //linha A do teclado
#define row_B 11 //linha B do teclado
#define row_C 12 //linha C do teclado
#define row_D 13 //linha D do teclado
#include <hd44780.h>
// =============================================================================================================
// --- Functions ---
void readKeyboard(); //Função para leitura do teclado
void store(char value); //Função para armazenar o número digitado no teclado matricial
void numero(); //Função para imprimir o número digitado na tela do LCD
// =============================================================================================================
// --- Global Variables ---
//char control = 0x01;
// char counter = 0x00;
//int number = 0x00;
volatile uint8_t control = 0x01; //variável de controle de teclado
volatile uint8_t number = 0x00; //variável para armazenar o número pressionado no teclado
volatile uint8_t counter = 0x00; //variável auxiliar de contagem
volatile uint8_t flag = 0x00;
The interruption:
// =============================================================================================================
// --- Interruption ---
void TC7_Handler()
{
//get the status to clear it and allow the interrupt to fire again
TC_GetStatus(TC2, 1);
counter++;
if(counter == 0x05)
{
counter = 0x00;
flag = 0x01;
}
}
// =============================================================================================================
// --- Initial Setup ---
void setup()
{
Serial.begin(9600);
for(char i=2;i< 9;i++) pinMode(i, OUTPUT); //Saídas para varredura das colunas
for(char j=9;j<13;j++) pinMode(j, INPUT_PULLUP); //Entradas para as linhas
digitalWrite(col_1, HIGH); //Inicializa coluna 1 em HIGH
digitalWrite(col_2, HIGH); //Inicializa coluna 2 em HIGH
digitalWrite(col_3, HIGH); //Inicializa colune 3 em HIGH
digitalWrite(col_4, HIGH); //Inicializa coluna 4 em HIGH
digitalWrite(col_5, HIGH); //Inicializa coluna 5 em HIGH
digitalWrite(col_6, HIGH); //Inicializa colune 6 em HIGH
digitalWrite(col_7, HIGH); //Inicializa coluna 7 em HIGH
digitalWrite(col_8, HIGH); //Inicializa coluna 8 em HIGH
pmc_set_writeprotect(false); // disable write protection for pmc registers
//************** TIMER INTERRUPT *****************************
/* turn on the timer clock in the power management controller */
pmc_enable_periph_clk(ID_TC7); // enable peripheral clock TC7
/* we want wavesel 01 with RC */
TC_Configure(/* clock */TC2,/* channel */1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
TC_SetRC(TC2, 1, 6562); //10ms
TC_Start(TC2, 1);
// enable timer interrupts on the timer
TC2->TC_CHANNEL[1].TC_IER=TC_IER_CPCS; // IER = interrupt enable register
TC2->TC_CHANNEL[1].TC_IDR=~TC_IER_CPCS; // IDR = interrupt disable register
/* Enable the interrupt in the nested vector interrupt controller */
/* TC4_IRQn where 4 is the timer number * timer channels (3) + the channel number (=(1*3)+1) for timer1 channel1 */
NVIC_EnableIRQ(TC7_IRQn);
//************** TIMER INTERRUPT *****************************
} //end setup
Void Loop:
// =============================================================================================================
// --- Infinite Loop ---
void loop()
{
if (flag == 0x01){
readKeyboard();
}
flag = 0x00;
} //end loop
I'm going to post the functions on the reply below.
If any of you guys could hint me with tips of what I can be doing wrong would be preety nice. Since I'm planning to expand the project and add a 20X4 LCD as well.
Thank you all in advance.
teclado_varredura_8x4_Due.ino (14.3 KB)