Skip to content

Commit

Permalink
Implement keyboard scanning and finalize hw-test program
Browse files Browse the repository at this point in the history
  • Loading branch information
laneboysrc committed Mar 5, 2014
1 parent 92b2a77 commit 173b937
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 7 deletions.
27 changes: 23 additions & 4 deletions firmware/hk310-filter/hw-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdint.h>

#include "uart.h"
#include "keyboard-matrix.h"

static __code uint16_t __at (_CONFIG1) configword1 = _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF;
static __code uint16_t __at (_CONFIG2) configword2 = _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _LVP_OFF;
Expand All @@ -19,9 +20,8 @@ static void Init_hardware(void) {

//-----------------------------
// IO Port initialization
PORTA = 0;
LATA = 0;
ANSELA = 0;
ANSELC = 0;
APFCON0 = 0b00000000; // Use RC4/RC5 for UART TX/RX; RA4 for T1G
APFCON1 = 0;
TRISA = 0b11111111; // Make all ports A input
Expand All @@ -37,14 +37,33 @@ static void Init_hardware(void) {
No introduction needed ...
****************************************************************************/
void main(void) {
static uint16_t oldKeys = 0xffff;
uint16_t keys;

Init_hardware();
Init_UART();
Init_keyboard();

while (1) {
uint8_t b;

b = UART_read_byte();
UART_send(b);
keys = Scan_keyboard();
if (keys != oldKeys) {
UART_send_binary_uint(keys);
oldKeys = keys;
}

if (UART_receive_byte_pending()) {
b = UART_read_byte();
UART_send('E');
UART_send('c');
UART_send('h');
UART_send('o');
UART_send(':');
UART_send(' ');
UART_send(b);
UART_send('\n');
}
}
}

Expand Down
65 changes: 65 additions & 0 deletions firmware/hk310-filter/keyboard-matrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <pic16f1825.h>
#include <stdint.h>

#include "keyboard-matrix.h"

/*
Routines for handling a 12 key matrix
Rows: RA2, RA4, RA5
Columns: RC0, RC1, RC2, RC3
*/


void Init_keyboard(void)
{
WPUA = 0b00110100; // Weak pull-ups on RA2, RA4 and RA5
WPUC = 0b00001111; // Weak pull-ups on RC0, RC1, RC2 and RC3
NOT_WPUEN = 0;

TRISA &= ~0b00110100; // Ports RA2, RA4 and RA5 outputs
TRISC |= 0b00001111; // Ports RC0, RC1, RC2 and RC3 inputs

LATA2 = 0;
LATA4 = 0;
LATA5 = 0;
}


uint16_t Scan_keyboard(void)
{
uint16_t val = 0;
static uint8_t keyPressed = 0;

if (!keyPressed) {
LATA2 = 0;
LATA4 = 0;
LATA5 = 0;

if ((PORTC & 0x0f) != 0x0f) {
keyPressed = 1;
}
}

if (keyPressed) {
LATA4 = 1;
LATA5 = 1;
LATA2 = 0;
val = (~PORTC & 0x0f) << 8;

LATA2 = 1;
LATA4 = 0;
val |= (~PORTC & 0x0f) << 4;

LATA4 = 1;
LATA5 = 0;
val |= (~PORTC & 0x0f);

if (val == 0) {
keyPressed = 0;
}
}

return val;
}

7 changes: 7 additions & 0 deletions firmware/hk310-filter/keyboard-matrix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef __KEYBOARD_MATRIX_H
#define __KEYBOARD_MATRIX_H

extern void Init_keyboard(void);
extern uint16_t Scan_keyboard(void);

#endif /* __KEYBOARD_MATRIX_H */
9 changes: 8 additions & 1 deletion firmware/hk310-filter/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ hk310-filter.hex: hk310-filter.o uart.o
.PHONY: hw-test
hw-test: hw-test.hex

hw-test.hex: hw-test.o uart.o
hw-test.hex: hw-test.o keyboard-matrix.o uart.o
sdcc --opt-code-size --use-non-free -mpic14 -p16f1825 -o $@ $^



#############################################################################
hk310-filter.c: uart.h
hw-test.c: uart.h keyboard-matrix.h
keyboard-matrix.c: keyboard-matrix.h
uart.c: uart.h

#############################################################################
.c.o:
sdcc --opt-code-size --use-non-free -mpic14 -p16f1825 -c $<
Expand Down
32 changes: 30 additions & 2 deletions firmware/hk310-filter/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ void UART_send_uint(uint16_t tx_uint) {
}


/*****************************************************************************
Send tx_uint as binary number with leading zeros out via the UART
****************************************************************************/
void UART_send_binary_uint(uint16_t tx_uint) {
char i;
uint8_t c;

for (i = 15; i >= 0; i--) {
c = '0';
if (tx_uint & (1 << i)) {
c = '1';
}
UART_send(c);
}
UART_send('\n');
}


/*****************************************************************************
Send (unsigned char)tx_uint as decimal number with leading zeros out via
the UART
Expand Down Expand Up @@ -134,7 +152,6 @@ void UART_send_uchar(uint8_t tx_uint) {
uint8_t UART_read_byte(void)
{
do {
#if 0
if (OERR) {
CREN = 0;
WREG = RCREG;
Expand All @@ -146,8 +163,19 @@ uint8_t UART_read_byte(void)
if (FERR) {
WREG = RCREG;
}
#endif
} while (!RCIF);

return RCREG;
}


/******************************************************************************
; UART_receive_byte_pending
;
; Test whether there is a byte in the UART receive buffer.
;*****************************************************************************/
uint8_t UART_receive_byte_pending(void)
{
return RCIF;
}

2 changes: 2 additions & 0 deletions firmware/hk310-filter/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ extern uint8_t UART_read_byte(void);
extern void UART_send(uint8_t);
extern void UART_send_uint(uint16_t);
extern void UART_send_uchar(uint8_t);
extern void UART_send_binary_uint(uint16_t);
extern uint8_t UART_receive_byte_pending(void);

#endif /* __UART_H */

0 comments on commit 173b937

Please sign in to comment.