/*
* File: main.c
* Author: burt
*
* Created on February 4, 2018, 3:31 PM
*/
// BEGIN CONFIG
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
//END CONFIG
#define _XTAL_FREQ 20000000
#include <pic16f876a.h>
#include <xc.h>
#include <stdio.h> // Standard I/O - required for printf() function
#include <stdlib.h> // formatting text
#include <stdbool.h>
#include "uart.h"
#define row1port PORTBbits.RB0
#define row2port PORTBbits.RB1
#define row3port PORTBbits.RB2
#define row4port PORTBbits.RB3
#define col1port PORTBbits.RB4
#define col2port PORTBbits.RB5
#define col3port PORTBbits.RB6
#define col4port PORTBbits.RB7 //if a 4x4 keypad is used
#define row1tris TRISBbits.TRISB0
#define row2tris TRISBbits.TRISB1
#define row3tris TRISBbits.TRISB2
#define row4tris TRISBbits.TRISB3
#define col1tris TRISBbits.TRISB4
#define col2tris TRISBbits.TRISB5
#define col3tris TRISBbits.TRISB6
#define col4tris TRISBbits.TRISB7
void putch(char c)
{
UART_Write(c); // this don't work can't use prinf
}
char const keyPadMatrix[] =
{
'1','2','3','4',
'5','6','7','8',
'9','0','A','B',
'C','D','E','F',
0xFF
};
void ScanKeyMatrixInit()
{
// we scan the keypad by turning on the row outputs and
// reading the columns
row1tris = 0;
row2tris = 0;
row3tris = 0;
row4tris = 0;
col1tris = 1;
col2tris = 1;
col3tris = 1;
col4tris = 1;
}
char ScanKeyMatrix()
{
// This routine returns the first key found to be
// pressed during the scan.
char key = 0, row;
for( row = 0b00000001; row < 0b00010000; row <<= 1 )
{
{ // turn on row output
row1port = (row & 0x0001)>>0;
row2port = (row & 0x0002)>>1;
row3port = (row & 0x0004)>>2;
row4port = (row & 0x0008)>>3;
__delay_ms(1);
}
// read colums - break when key press detected
if( col1port )
break;
key++;
if( col2port )
break;
key++;
if( col3port )
break;
key++;
if( col4port )
break;
key++;
}
row1port = 0;
row2port = 0;
row3port = 0;
row4port = 0;
return keyPadMatrix[ key ];
}
void main()
{
ScanKeyMatrixInit();
UART_Init(9600);
char c;
if (c != 0xFF) {
c = ScanKeyMatrix();
UART_Write(c);
__delay_ms(100);
}
else if (c == 0xFF)
{
UART_Write_Text("waiting on key\n");
__delay_ms(1000);
}
while (1)
{
}
}