Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#define KBD_CLK portc.2
#define KBD_DAT portc.1
unsigned char GetKey() //
{ unsigned char kbdata = 0; //
while(KBD_CLK == 1); // wait for KBD_CLK to go 'lo'
for(i = 0; i < 8; i++) // collect 8 data bits
{ kbdata >>= 1; // shift previous bit to right
while(KBD_CLK == 0); // wait for KBD_CLK to go 'hi'
while(KBD_CLK == 1); // wait for KBD_CLK to go 'lo'
if(KBD_DAT) kbdata.7 = 1; // collect data bit
}
........
return(KeyMap[kbdata]); // return table character
}
rrncf kbdata, 1,1
btfss PORTC, 1,0
bcf kbdata,7,1
btfsc PORTC, 1,0
bsf kbdata,7,1
while(kb_cr == 1); // wait for KBD_CLK to go 'lo'
for(i = 0; i < 8; i++) // collect 8 data bits
{
while(kb_cr == 0); // wait for KBD_CLK to go 'hi'
while(kb_cr == 1); // wait for KBD_CLK to go 'lo'
if(kb_dr) kbdata |= 0x80; // collect data bit
kbdata >>= 1; // shift previous bit to right
}
#define kb_dr PORTCbits.RC1
#define kb_dw LATCbits.LATC1
#define kb_cr PORTCbits.RC2
#define kb_cw LATCbits.LATC2
if(kb_dr) kbdata |= 0x80;
kbdata >>= 1;
while(kb_cr == 1); // wait for KBD_CLK to go 'lo'
while(kb_cr == 0); // wait for KBD_CLK to go 'hi'
while(kb_cr == 1); // wait for KBD_CLK to go 'lo'
for(i = 0; i < 8; i++) // collect 8 data bits
{
if(kb_dr == 1) kbdata |= 0x80; // collect data bit
kbdata >>= 1; // shift previous bit to right
while(kb_cr == 0); // wait for KBD_CLK to go 'hi'
while(kb_cr == 1); // wait for KBD_CLK to go 'lo'
}
GetKB:
_asm
A:
btfsc PORTC, 2,0
bra A
B:
btfss PORTC, 2,0
bra B
C:
btfsc PORTC, 2,0
bra C
MyByte:
rrncf kbdata, 1,1
btfss PORTC, 1,0
bcf kbdata,7,1
btfsc PORTC, 1,0
bsf kbdata,7,1
D:
btfss PORTC, 2,0
bra D
E:
btfsc PORTC, 2,0
bra E
decfsz count , 1,1
bra MyByte
_endasm
unsigned char GetKey() //
{ unsigned char kbdata = 0; //
while(kb_cr == 1); // wait for CLK pin 'hi'
for(i = 0; i < 9; i++) // <start> and 8 <data> bits
{ while(kb_cr == 0); // wait for CLK pin 'hi'
while(kb_cr == 1); // wait for CLK pin 'lo'
kbdata >>= 1; // shift bits to right
if(kb_dr) kbdata |= 128; // add bit to kbdata
}
while(kb_cr == 0); // throw away <parity> bit
while(kb_cr == 1); //
while(kb_cr == 0); // throw away <stop> bit
while(kb_cr == 1); //
........
//I took out the first WHILE() loop since it doesnt do anything really lol
//kbdata is a global i use.
for(i = 0; i < 9; i++) // <start> and 8 <data> bits
{ while(kb_cr == 0); // wait for CLK pin 'hi'
while(kb_cr == 1); // wait for CLK pin 'lo'
kbdata >>= 1; // shift bits to right
if(kb_dr) kbdata |= 128; // add bit to kbdata
}
while(kb_cr == 0); // throw away <parity> bit
while(kb_cr == 1); //
while(kb_cr == 0); // throw away <stop> bit
while(kb_cr == 1); //
/* *****************************************************************************
; *
; Filename: *
; Date: *
; File Version: 001 *
; *
; Author: Jason Lopez *
; Company: AtomSoft *
; *
;***************************************************************************** */
#include <p18f2525.h>
#include <delays.h>
#include <string.h>
#pragma config WDT = OFF, LVP = OFF, OSC = INTIO67, XINST = OFF
/************************************
Prototypes
*************************************/
void main(void);
void LCDString(rom unsigned char *str);
void InitLCD(void);
void LCDLine(unsigned char line);
void SendLCD(unsigned char dat,unsigned char RSV);
void ToggleE(void);
unsigned char GetKey(void);
/************************************
Variables and Defines
*************************************/
unsigned char tempKey;
unsigned char a2z[104];
unsigned char shiftcap = 0;
unsigned char count;
unsigned char kbdata;
//Keyboard Defines
#define kbt_d TRISCbits.TRISC1
#define kbt_c TRISCbits.TRISC2
#define kb_dr PORTCbits.RC1
#define kb_dw LATCbits.LATC1
#define kb_cr PORTCbits.RC2
#define kb_cw LATCbits.LATC2
//LCD Defines
#define LCDt TRISB
#define LCD PORTB
#define RS LATBbits.LATB4
#define ET LATBbits.LATB5
/************************************
Main
*************************************/
void main(void){
OSCCON = 0x72; //8MHz clock
while(!OSCCONbits.IOFS); //wait for osc stable
kbt_d = 1; //Keyboard Data Tris is INPUT
kbt_c = 1; //Keyboard Clock Tris is INPUT
InitLCD();
LCDString((rom unsigned char*)"AtomSoftTech.");
LCDLine(2);
//LCDString((rom unsigned char*)"Keyboard Test");
//a2zi = 0;
while(1){
tempKey = GetKey();
if(tempKey >= 0x20)
if(tempKey <= 0x7E){
if(shiftcap >= 1){
if(tempKey >= 0x61)
if(tempKey <= 0x7A)
tempKey -= 0x20;
if(shiftcap == 1)
shiftcap = 0;
}
SendLCD(tempKey,1);
Delay10KTCYx(100); //500mS
}
if(tempKey == 0x08){
SendLCD(0x10,0);
Delay1KTCYx(16);
SendLCD(0x20,1);
Delay1KTCYx(16);
SendLCD(0x10,0);
Delay1KTCYx(16);
Delay10KTCYx(60); //500mS
}
if(tempKey == 0)
Delay10KTCYx(100); //500mS
//a2z[a2zi++] = tempKey;
}
}
unsigned char GetKey(void){
char i;
kbdata = 0;
for(i = 0; i < 9; i++) // <start> and 8 <data> bits
{
while(kb_cr == 0); // wait for CLK pin 'hi'
while(kb_cr == 1); // wait for CLK pin 'lo'
kbdata >>= 1; // shift bits to right
if(kb_dr) kbdata |= 128; // add bit to kbdata
}
while(kb_cr == 0); // throw away <parity> bit
while(kb_cr == 1); //
while(kb_cr == 0); // throw away <stop> bit
while(kb_cr == 1); //
Delay10TCYx(70); //Delay 320uS aka 2 clocks(bits)
//return tmp;
switch(kbdata){
case 0x00:
return 0x00;
case 0x01:
return 0x00;
case 0x02:
return 0x00;
case 0x03:
return 0x00;
case 0x04:
return 0x00;
case 0x05:
return 0x00;
case 0x06:
return 0x00;
case 0x07:
return 0x00;
case 0x08:
return 0x00;
case 0x09:
return 0x00;
case 0x0A:
return 0x00;
case 0x0B:
return 0x00;
case 0x0C:
return 0x00;
case 0x0D:
return 0x09;
case 0x0E:
if(shiftcap == 0)
return 0x60;
else
return 0x7E;
case 0x0F:
return 0x00;
case 0x10:
return 0x00;
case 0x11:
return 0x00;
case 0x12:
shiftcap = 1;
return 0x00;
case 0x13:
return 0x00;
case 0x14:
return 0x00;
case 0x15:
return 'q';
case 0x16:
if(shiftcap == 0)
return '1';
else
return 0x21;
case 0x17:
return 0x00;
case 0x18:
return 0x00;
case 0x19:
return 0x00;
case 0x1A:
return 'z';
case 0x1B:
return 's';
case 0x1C:
return 'a';
case 0x1D:
return 'w';
case 0x1E:
if(shiftcap == 0)
return '2';
else
return 0x40;
case 0x1F:
if(shiftcap == 0)
return '8';
else
return 0x2A;
case 0x20:
return 0x00;
case 0x21:
return 'c';
case 0x22:
return 'x';
case 0x23:
return 'd';
case 0x24:
return 'e';
case 0x25:
if(shiftcap == 0)
return '4';
else
return 0x24;
case 0x26:
if(shiftcap == 0)
return '3';
else
return 0x23;
case 0x27:
return '-';
case 0x28:
return 0x00;
case 0x29:
return ' ';
case 0x2A:
return 'v';
case 0x2B:
return 'f';
case 0x2C:
return 't';
case 0x2D:
return 'r';
case 0x2E:
if(shiftcap == 0)
return '5';
else
return 0x25;
case 0x2F:
return 0x00;
case 0x30:
return 0x00;
case 0x31:
return 'n';
case 0x32:
return 'b';
case 0x33:
return 'h';
case 0x34:
return 'g';
case 0x35:
return 'y';
case 0x36:
if(shiftcap == 0)
return '6';
else
return 0x5E;
case 0x37:
return 0x00;
case 0x38:
return 0x00;
case 0x39:
return 0x00;
case 0x3A:
return 'm';
case 0x3B:
return 'j';
case 0x3C:
return 'u';
case 0x3D:
return 0x00;
case 0x3E:
return '8';
case 0x3F:
return 0x00;
case 0x40:
return 0x00;
case 0x41:
if(shiftcap == 0)
return ',';
else
return '<';
case 0x42:
return 'k';
case 0x43:
return 'i';
case 0x44:
return 'o';
case 0x45:
if(shiftcap == 0)
return '0';
else
return ')';
case 0x46:
if(shiftcap == 0)
return '9';
else
return '(';
case 0x47:
return 0x00;
case 0x48:
return 0x00;
case 0x49:
if(shiftcap == 0)
return '.';
else
return '>';
case 0x4A:
if(shiftcap == 0)
return '/';
else
return '?';
case 0x4B:
return 'l';
case 0x4C:
if(shiftcap == 0)
return ';';
else
return ':';
case 0x4D:
return 'p';
case 0x4E:
if(shiftcap == 0)
return '-';
else
return '_';
case 0x4F:
return 0x00;
case 0x50:
return 0x00;
case 0x51:
return 0x00;
case 0x52:
if(shiftcap == 0)
return 0x2C;
else
return '"';
case 0x53:
return 0x00;
case 0x54:
if(shiftcap == 0)
return '[';
else
return '{';
case 0x55:
if(shiftcap == 0)
return '=';
else
return '+';
case 0x56:
return 0x00;
case 0x57:
return 0x00;
case 0x58:
if(shiftcap == 2)
shiftcap = 0;
else
shiftcap = 2;
return 0x00;
case 0x59:
shiftcap = 1;
return 0x00;
case 0x5A:
return 0x0A; //Line Feed :D
case 0x5B:
if(shiftcap == 0)
return ']';
else
return '}';
case 0x5C:
return 0x00;
case 0x5D:
if(shiftcap == 0)
return 0x5C;
else
return '|';
case 0x5E:
return 0x00;
case 0x5F:
return 0x00;
case 0x60:
return 0x00;
case 0x61:
return 0x00;
case 0x62:
return 0x00;
case 0x63:
return 0x00;
case 0x64:
return 0x00;
case 0x65:
return 0x00;
case 0x66:
return 0x08;
case 0x67:
return 0x00;
case 0x68:
return 0x00;
case 0x69:
return 0x00;
case 0x6A:
return 0x00;
case 0x6B:
return 0x00;
case 0x6C:
return 0x00;
case 0x6D:
return 0x00;
case 0x6E:
return 0x00;
case 0x6F:
return 0x00;
case 0x70:
return 0x00;
case 0x71:
return 0x00;
case 0x72:
return 0x00;
case 0x73:
return 0x00;
case 0x74:
return 0x00;
case 0x75:
return 0x00;
case 0x76:
return 0x00;
case 0x77:
return 0x00;
case 0x78:
return 0x00;
case 0x79:
return 0x00;
case 0x7A:
return 0x00;
case 0x7B:
return 0x00;
case 0x7C:
return 0x00;
case 0x7D:
return 0x00;
case 0x7E:
return 0x00;
case 0x7F:
return 0x00;
case 0x80:
return 0x00;
case 0x81:
return 0x00;
case 0x82:
return 0x00;
case 0x83:
return 0x00;
case 0x84:
return 0x00;
case 0x85:
return 0x00;
case 0x86:
return 0x00;
case 0x87:
return 0x00;
case 0x88:
return 0x00;
case 0x89:
return 0x00;
case 0x8A:
return 0x00;
case 0x8B:
return 0x00;
case 0x8C:
return 0x00;
case 0x8D:
return 0x00;
case 0x8E:
return 0x00;
case 0x8F:
return 0x00;
case 0x90:
return 0x00;
case 0x91:
return 0x00;
case 0x92:
return 0x00;
case 0x93:
return 0x00;
case 0x94:
return 0x00;
case 0x95:
return 0x00;
case 0x96:
return 0x00;
case 0x97:
return 0x00;
case 0x98:
return 0x00;
case 0x99:
return 0x00;
case 0x9A:
return 0x00;
case 0x9B:
return 0x00;
case 0x9C:
return 0x00;
case 0x9D:
return 0x00;
case 0x9E:
return 0x00;
case 0x9F:
return 0x00;
case 0xA0:
return 0x00;
case 0xA1:
return 0x00;
case 0xA2:
return 0x00;
case 0xA3:
return 0x00;
case 0xA4:
return 0x00;
case 0xA5:
return 0x00;
case 0xA6:
return 0x00;
case 0xA7:
return 0x00;
case 0xA8:
return 0x00;
case 0xA9:
return 0x00;
case 0xAA:
return 0x00;
case 0xAB:
return 0x00;
case 0xAC:
return 0x00;
case 0xAD:
return 0x00;
case 0xAE:
return 0x00;
case 0xAF:
return 0x00;
case 0xB0:
return 0x00;
case 0xB1:
return 0x00;
case 0xB2:
return 0x00;
case 0xB3:
return 0x00;
case 0xB4:
return 0x00;
case 0xB5:
return 0x00;
case 0xB6:
return 0x00;
case 0xB7:
return 0x00;
case 0xB8:
return 0x00;
case 0xB9:
return 0x00;
case 0xBA:
return 0x00;
case 0xBB:
return 0x00;
case 0xBC:
return 0x00;
case 0xBD:
return 0x00;
case 0xBE:
return 0x00;
case 0xBF:
return 0x00;
case 0xC0:
return 0x00;
case 0xC1:
return 0x00;
case 0xC2:
return 0x00;
case 0xC3:
return 0x00;
case 0xC4:
return 0x00;
case 0xC5:
return 0x00;
case 0xC6:
return 0x00;
case 0xC7:
return 0x00;
case 0xC8:
return 0x00;
case 0xC9:
return 0x00;
case 0xCA:
return 0x00;
case 0xCB:
return 0x00;
case 0xCC:
return 0x00;
case 0xCD:
return 0x00;
case 0xCE:
return 0x00;
case 0xCF:
return 0x00;
case 0xD0:
return 0x00;
case 0xD1:
return 0x00;
case 0xD2:
return 0x00;
case 0xD3:
return 0x00;
case 0xD4:
return 0x00;
case 0xD5:
return 0x00;
case 0xD6:
return 0x00;
case 0xD7:
return 0x00;
case 0xD8:
return 0x00;
case 0xD9:
return 0x00;
case 0xDA:
return 0x00;
case 0xDB:
return 0x00;
case 0xDC:
return 0x00;
case 0xDD:
return 0x00;
case 0xDE:
return 0x00;
case 0xDF:
return 0x00;
case 0xE0:
return 0x00;
case 0xE1:
return 0x00;
case 0xE2:
return 0x00;
case 0xE3:
return 0x00;
case 0xE4:
return 0x00;
case 0xE5:
return 0x00;
case 0xE6:
return 0x00;
case 0xE7:
return 0x00;
case 0xE8:
return 0x00;
case 0xE9:
return 0x00;
case 0xEA:
return 0x00;
case 0xEB:
return 0x00;
case 0xEC:
return 0x00;
case 0xED:
return 0x00;
case 0xEE:
return 0x00;
case 0xEF:
return 0x00;
case 0xF0:
if(shiftcap == 0)
return '7';
else
return '&';
case 0xF1:
return 0x00;
case 0xF2:
return 0x00;
case 0xF3:
return 0x00;
case 0xF4:
return 0x00;
case 0xF5:
return 0x00;
case 0xF6:
return 0x00;
case 0xF7:
return 0x00;
case 0xF8:
return 0x00;
case 0xF9:
return 0x00;
case 0xFA:
return 0x00;
case 0xFB:
return 0x00;
case 0xFC:
return 0x00;
case 0xFD:
return 0x00;
case 0xFE:
return 0x00;
case 0xFF:
return 0x00;
}
return ' ';
}
/* ***********************************
LCD Stuff
************************************ */
void LCDString(rom unsigned char *str){
while(*str != 0)
SendLCD(*str++,1);
}
void InitLCD(void){
LCDt = 0x00;
RS = 0;
Delay10KTCYx(4); //Delay 40000 TCY aka 20mS
LCD = 0x03;
ToggleE();
Delay10KTCYx(1); //Delay 10000 TCY aka 5mS
ToggleE();
Delay10KTCYx(1); //Delay 10000 TCY aka 5mS
ToggleE();
Delay10KTCYx(1); //Delay 10000 TCY aka 5mS
Delay1KTCYx(2); //Delay 2000 TCY aka 1mS // Total = 6mS
LCD = 0x02;
ToggleE();
Delay1KTCYx(8); //Delay 8000 TCY aka 4mS
SendLCD(0x28,0); //Data = 0x28, RS = 0 (command mode)
Delay1KTCYx(8); //Delay 8000 TCY aka 4mS
SendLCD(0x10,0); //Data = 0x10, RS = 0 (command mode)
Delay1KTCYx(8); //Delay 8000 TCY aka 4mS
SendLCD(0x06,0); //Data = 0x06, RS = 0 (command mode)
Delay1KTCYx(8); //Delay 8000 TCY aka 4mS
SendLCD(0x0F,0); //Data = 0x0F, RS = 0 (command mode)
Delay1KTCYx(8); //Delay 8000 TCY aka 4mS
SendLCD(0x01,0); //Data = 0x01, RS = 0 (command mode)
Delay1KTCYx(8); //Delay 8000 TCY aka 4mS
}
void LCDLine(unsigned char line){
if(line == 1)
SendLCD(0x80,0);
if(line == 2)
SendLCD(0xC0,0);
}
void SendLCD(unsigned char dat,unsigned char RSV){
char cmdHi;
char cmdLo;
cmdHi = dat >> 4;
cmdLo = dat & 0x0F;
LCD = cmdHi;
RS = RSV;
ToggleE();
Delay10TCYx(2); //10uS Delay
LCD = cmdLo;
RS = RSV;
ToggleE();
}
void ToggleE(void){
ET = 1;
Delay10TCYx(2); //10uS Delay
ET = 0;
}