scan keyboard

This commit is contained in:
Jeff Wang 2019-03-20 03:21:11 -04:00
parent f4f70853c1
commit 84b6888d7e
7 changed files with 119 additions and 8 deletions

View File

@ -1,9 +1,9 @@
SDCC ?= sdcc
STCCODESIZE ?= 4089
SDCCOPTS ?= --code-size $(STCCODESIZE) --xram-size 0 --data-loc 0x30
SDCCOPTS ?= --std-c99 --code-size $(STCCODESIZE) --xram-size 0 --data-loc 0x30
FLASHFILE ?= main.hex
SRC = src/lcd.c src/utils.c
SRC = src/lcd.c src/key.c src/utils.c
OBJ=$(patsubst src%.c,build%.rel, $(SRC))

62
src/key.c Normal file
View File

@ -0,0 +1,62 @@
/*
* key.c
*
* Created on: Mar 20, 2019
*/
#include <stdint.h>
#include "stc15.h"
static uint8_t keys[5];
void KeyInit(void){
//set column drivers bits 7:4 to quasi-bidirectional w/ pullup M[1:0]=b00
//and row inputs bits 3:0 to quasi-bidirectional w/ pullup M[1:0]=b00
P1M1 = 0x00;
P1M0 = 0x00;
//all initially pulled up
P1 = 0xff;
//read top row input buttons using quasi-bidirectional w/ pullup M[1:0]=b00
//P3[1:0]
P3M1 &= ~(0x3);
P3M0 &= ~(0x3);
P3 |= 0x3; //pull up
//P5[5:4]
P5M1 &= ~(0x30);
P5M0 &= ~(0x30);
P5 |= 0x30; //pull up
}
void KeyScan(void){
//top row not part of matrix
static const uint8_t M_ROWS = 4;
static const uint8_t M_COLS = 4;
uint8_t i, j;
//scan top row
keys[0] = 0; //initially
keys[0] += (P3_0 ? 0 : (1<<0)); //on
keys[0] += (P3_1 ? 0 : (1<<1));
keys[0] += (P5_4 ? 0 : (1<<2));
keys[0] += (P5_5 ? 0 : (1<<3));
//scan matrix
for (i = 0; i < M_ROWS; i++){
keys[i+1] = 0; //initially
for (j = 0; j < M_COLS; j++){
uint8_t read;
P1 = (~(1 << (j+4))); //pulldown column (all others pull up)
read = (P1 & (1<<i)); //read button
read = (read == 0 ? 1 : 0); //pressed if grounded by column driver
keys[i+1] += (read << j);
P1 = 0xff; //reset all as pullups
}
}
}
uint8_t* GetKeys(void){
return keys;
}

15
src/key.h Normal file
View File

@ -0,0 +1,15 @@
/*
* key.h
*
* Created on: Mar 20, 2019
*/
#ifndef SRC_KEY_H_
#define SRC_KEY_H_
void KeyInit(void);
void KeyScan(void);
uint8_t* GetKeys(void);
#endif /* SRC_KEY_H_ */

View File

@ -150,13 +150,13 @@ static void LCD_OutChar(unsigned char c) {
void LCD_Open(void) {
static int open_error = 0;
if (!OpenFlag) {
//set ports to output
//P2
//set ports to push-pull output M[1:0] = b01
//P2 entire port
P2M1 = 0;
P2M0 = 0xff;
//P3
P3M1 &= ~(0xfe);
P3M0 |= (0xfe);
//P3 pins 7:4
P3M1 &= ~(0xf0);
P3M0 |= (0xf0);
_delay_ms(30); // to allow LCD powerup
outCsrBlindNibble(0x03); // (DL=1 8-bit mode)
@ -251,6 +251,15 @@ short TERMIO_PutChar(unsigned char letter) {
return 1;
}
void LCD_OutNibble(uint8_t x){
x &= 0xf; //ensure only bottom nibble
if (x <= 9){
TERMIO_PutChar(x + '0');
} else {
TERMIO_PutChar(x - 10 + 'a');
}
}
void LCD_Clear() {
if (OpenFlag) {
outCsr(0x01);

View File

@ -12,6 +12,7 @@ void LCD_SingleLineGoTo(unsigned int row_to);
void LCD_OutString(const char* string);
short TERMIO_PutChar(unsigned char letter);
void LCD_OutNibble(uint8_t x);
unsigned char LCD_Timeout_Error(void);

View File

@ -5,6 +5,7 @@
#include "stc15.h"
#include <stdint.h>
#include "lcd.h"
#include "key.h"
#include "utils.h"
#define FOSC 11059200
@ -13,7 +14,7 @@
#define WDT_CLEAR() (WDT_CONTR |= 1 << 4)
void timer0_isr() __interrupt 1 __using 1
{
P3_1 ^= 1;
// P3_1 ^= 1;
}
@ -40,8 +41,11 @@ char buf[17];
int main()
{
uint32_t i;
uint8_t* keys;
uint8_t key_i;
Timer0Init(); // display refresh & switch read
LCD_Open();
KeyInit();
P3_4 = 0; //turn on led backlight
LCD_OutString("Hello world !!!!");
LCD_GoTo(1,0);
@ -51,6 +55,15 @@ int main()
// LOOP
while (1)
{
LCD_GoTo(0,0);
//scan keyboard
KeyScan();
keys = GetKeys();
for (key_i = 0; key_i < 5; key_i++){
LCD_OutNibble(keys[key_i]);
}
//counter
LCD_GoTo(1,7);
LCD_OutString(u32str(i, buf, 10));
i++;

View File

@ -18,6 +18,17 @@ __sbit __at (0xC5) P4_5 ;
__sbit __at (0xC6) P4_6 ;
__sbit __at (0xC7) P4_7 ;
/* P5 */
__sfr __at (0xc8) P5 ;
__sbit __at (0xc8) P5_0 ;
__sbit __at (0xc9) P5_1 ;
__sbit __at (0xca) P5_2 ;
__sbit __at (0xcb) P5_3 ;
__sbit __at (0xcc) P5_4 ;
__sbit __at (0xcd) P5_5 ;
__sbit __at (0xce) P5_6 ;
__sbit __at (0xcf) P5_7 ;
__sfr __at 0x94 P0M0;
__sfr __at 0x93 P0M1;
__sfr __at 0x92 P1M0;