TS-8150-47XX KPAD
From embeddedTS Manuals
This header is designed to connect to the KPAD accessory which uses the odd DIO on this header to scan a 4x4 keypad. This example scans the KPAD and prints out the pressed character.
/* KPAD 4x4 keypad example code
*
* To compile, copy to the board and run:
* gcc kpad.c -o kpad */
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
volatile uint16_t *syscon = 0;
// Map DIO 14,13,11,10,8,7,6,4 to "out" as bits 0:7
const int pins[8] = {14,13,11,10,8,7,6,4};
uint16_t peek16(uint16_t addr)
{
uint16_t value;
if(syscon == 0) {
int mem = open("/dev/mem", O_RDWR|O_SYNC);
syscon = mmap(0,
getpagesize(),
PROT_READ|PROT_WRITE,
MAP_SHARED,
mem,
0x80004000);
}
return syscon[addr/2];
}
void poke16(uint16_t addr, uint16_t value)
{
if(syscon == 0) {
int mem = open("/dev/mem", O_RDWR|O_SYNC);
syscon = mmap(0,
getpagesize(),
PROT_READ|PROT_WRITE,
MAP_SHARED,
mem,
0x80004000);
}
syscon[addr/2] = value;
}
void set_output(uint8_t out)
{
uint16_t val = peek16(0x10);
int i;
for (i = 0; i < 8; i++)
{
if(out & (1 << i)) val |= (1 << pins[i]);
else val &= ~(1 << pins[i]);
}
poke16(0x10, val);
}
void set_ddr(uint8_t ddr)
{
uint16_t val = peek16(0x18);
int i;
for (i = 0; i < 8; i++)
{
if(ddr & (1 << i)) val |= (1 << pins[i]);
else val &= ~(1 << pins[i]);
}
poke16(0x18, val);
}
uint8_t get_input()
{
uint16_t val = peek16(0x20);
uint8_t in = 0;
int i;
for (i = 0; i < 8; i++)
{
if(val & (1 << pins[i])) in |= (1 << i);
else in &= ~(1 << i);
}
return in;
}
int main()
{
uint8_t ddr = 0x0f;
uint8_t out = 0xff;
int row, col;
char *keys[4][4] = {
{ "1", "2", "3", "UP" },
{ "4", "5", "6", "DOWN" },
{ "7", "8", "9", "2ND" },
{ "CLEAR", "0", "HELP", "ENTER" }
};
//set first 4 as outputs, last 4 as inputs
set_ddr(ddr);
while(1) {
for(row = 0; row < 4; row++) {
set_output(~(1 << row));
usleep(50000);
uint16_t in = get_input();
for(col = 4; col < 8; col++) {
if(~in & (1 << col)) {
// If we read it, sleep and read again to debounce
usleep(1000);
in = get_input();
if(~in & (1 << col)) {
printf("%s\n", keys[row][col - 4]);
fflush(stdout);
}
}
}
}
}
return 0;
}