?? lk1000_2.c
字號:
#include <stdio.h>
#include <pic.h>
#include "delay.h"
#include "usart\usart.h"
#include "i2c\i2c.h"
#include "lcd\lcd.h"
#include "ps2\ps2.h"
#include "keyboard\keyboard.h"
#define ROM 0xA0 /* I2C EEPROM */
#define KB_TIMERS 5
#define DEBUG 0
void WriteByte(unsigned char , unsigned char);
int ReadByte(unsigned char);
static unsigned char kb_counter, kb_char, kb_error, kb_check;
static unsigned char kb_shift, kb_function_key, kb_make, kb_break;
unsigned char kb_bufs[KB_BFSIZE];
unsigned int kb_index_head, kb_index_rear ,kb_flag;
const unsigned char kb_scan2ascii[200]={
0,0,0,0,0,0,0,0,0,0, //0
0,0,0,0,'`',0,0,0,3,0, //1
0,'Q','1',0,0,' ','Z','S','A','W', //2
'2',0,0,'C','X','D','E','4','3',0, //3
0,0,'V','F','T','R','5',0,0,'N', //4
'B','H','G','Y','6',0,0,0,'M','J', //5
'U','7','8',0,'@',',','K','I','O','0', //6
'9',0,0,'.','/','L',';','P','-',0, //7
0,0,0,0,'[','=',0,0,0,3, //8
0,']',0,'\\',0,0,0,0,0,0, //9
0,0,0,0,0,0,0,0,0,0, //10
0,0,0,0,'~',0,0,0,0,0, //11
0,'q','!',0,0,' ','z','s','a','w', //12
'@',0,0,'c','x','d','e','$','#',0, //13
0,0,'v','f','t','r','%',0,0,'n', //14
'b','h','g','y','^',0,0,0,'m','j', //15
'u','&','*',0,0,'<','k','i','o',')', //16
'(',0,0,'>','?','l',':','p','_',0, //17
0,0,0,0,'{','+',0,0,0,3, //18
0,'}',0,'|',0,0,0,0,0,0, //19
// 0,0,0,0,0,0,0,0,0,0, //20
// 0,0,0,0,0,0,0,0,0,0, //21
// 0,0,0,0,2,0,0,0,0,0, //22
// 0,0,0,0,0,0,0,0,0,0, //23
// 1,0,0,0,0,0,0,0,0,0, //24
// 0,0,0,0,0, //25
};
static unsigned int kb_timer;
#if DEBUG
static unsigned char kb_parity;
#endif
void
main(void)
{
unsigned char count,val,datain, key;
// setup stuff
TRISA = 0x0;
TRISB = 0b11000011;
TRISC = 0b11000000;
//setup LCD stuff
lcd_init(); // setup UART stuff
INTCON=0; // purpose of disabling the interrupts.
init_comms(); // set up the USART - settings defined in usart.h
// setup ps/2 interrupt stuff
kb_index_head = 0;
kb_index_rear = 0;
kb_counter = 0;
kb_char = 0;
kb_error = 0;
kb_flag = 0;
kb_break = FALSE;
kb_function_key = FALSE;
kb_shift = FALSE;
kb_timer = KB_TIMERS;
KBDAT_DIR = KB_INPUT;
KBCLK_DIR = KB_INPUT;
T0CS = 0; // Timer increments on instruction clock
T0IE = 1; // Enable interrupt on TMR0 overflow
INTEDG = 0; // falling edge trigger the interrupt
INTE = 1; // enable the external interrupt
GIE = 1; // Global interrupt enable
lcd_clear();
lcd_goto(0);
while(1)
{
CLRWDT();
if( kb_flag )
{
#if DEBUG
printf("\r\n Scan Code=%x", kb_char);
printf("\r\nParity bit %d/ checksum bit %d", kb_parity, kb_check);
#else
if((key=kb_Getch())!=0)
switch(key)
{
case KB_E0 : {
if(kb_break) kb_break = kb_function_key = FALSE;
else kb_function_key = TRUE;
break;
}
case KB_LSHFT :
case KB_RSHFT :
{
if(kb_break) kb_break = kb_shift = FALSE;
else kb_shift = TRUE;
break;
}
case KB_F0 : {
kb_break = TRUE;
break;
}
default : {
if(kb_break) kb_break = FALSE;
else {
if(kb_shift)
#if DEBUG
printf("\r\nkbbufs=%c",kb_scan2ascii[key+100]);
#else
lcd_putch(kb_scan2ascii[key+100]);
#endif
else
#if DEBUG
printf("\r\nkbbufs=%c",kb_scan2ascii[key]);
#else
lcd_putch(kb_scan2ascii[key]);
#endif
}
break;
}
} //end of switch(key)
else printf("\r\nkey is undefine.");
#endif
kb_flag--;
}//end if(kb_flag)
else
if( kb_error )
{
switch(kb_error)
{
case 1 : printf("\r\nStart bit error"); break;
case 2 : printf("\r\nParty bit error"); break;
case 3 : printf("\r\nStop bit error"); break;
default : printf("\r\nNo error in list"); break;
}// end switch
kb_error = 0;
}
}// end while
}// end of main
/*
SCL_DIR = I2C_OUTPUT;
SDA_DIR = I2C_OUTPUT;
SDA = 1;
SCL = 1;
printf("\r\nEEPROM Check start");
count = 2;
datain = 0xff;
while(1){
CLRWDT(); // Idly kick the dog
//for(count=0; count < 255; ++count) {
WriteByte(count,datain); // write to I2C EEPROM
//DelayMs(10);
val = ReadByte(count); // read back value
if (val != datain) // verify
printf("\r\nDATA error addr:%d = %d / %d", count, val, datain);
else
printf("\r\nDATA correct! addr:%d = %d / %d ", count, val, datain);
// DelayMs(10);
//}
DelayMs(100);
}
}
*/
static void interrupt
isr(void) // Here be interrupt function - the name is
// unimportant.
{
if(INTF) { // did we see a button press?
//RELAY = 0; // turn the relay on
kb_timer = KB_TIMERS; // start the timer - 4000 ticks = 1 second
if(!kb_error && !kb_flag)
{
switch(kb_counter)
{
case 0 :
{
kb_char = 0;
kb_check = 0;
#if DEBUG
kb_parity = 0;
#endif
if( !KB_DAT ) kb_counter++;
else kb_error = 1;
break;
}// end of case 0
case 1 : case 2 : case 3 :
case 4 : case 5 : case 6 :
case 7 : case 8 :
{
kb_char = kb_char >> 1;
if(KB_DAT) kb_char |= 0x80;
kb_check ^= KB_DAT;
kb_counter++;
break;
}
case 9 :
{
#if DEBUG
kb_parity = KB_DAT;
kb_counter++;
#else
if(KB_DAT != kb_check) kb_counter++;
else kb_error = 2;
#endif
break;
}
case 10 :
{
kb_counter = 0;
if( !KB_DAT ) kb_error = 3;
else {
if(kb_char > 5)
{
//kb_Putch(kb_scan2ascii[kb_char]);
kb_Putch(kb_char);
kb_flag ++;
}
}
break;
}
default :
{
kb_error = 1;
break;
}
}// end of switch
}// end of if(!kb_error && !kb_flag)
INTF = 0; // clear the interrupt
}// end of if(INTF)
if(T0IF) { // timer interrupt
TMR0 -= 250; // reload the timer - 250uS per interrupt
T0IF = 0; // clear the interrupt flag
if(kb_timer != 0) // is the relay timer running?
kb_timer--; // decrement it
if(kb_timer == 0) // if it has time out
{
kb_counter = 0; // turn the relay off
kb_error = 0;
}// end of if(kb_timer == 0)
PORTC ^= 0x10; // toggle a bit to say we're alive
}// end of if(T0IF)
}// end of isr()
void WriteByte(unsigned char addr, unsigned char byte)
{
if (i2c_WriteTo(ROM))
printf("\r\nWrite I2C ROMaddr ERROR");
if (i2c_PutByte(addr)==I2C_ERROR)
printf("\r\nWrite I2C sub-addr ERROR");
if (i2c_PutByte(byte)==I2C_ERROR)
printf("\r\nWrite I2C data ERROR");
//i2c_Stop();
//i2c_SendAcknowledge(I2C_LAST);
}
int ReadByte(unsigned char addr)
{
if (i2c_WriteTo(ROM))
printf("\r\nRead I2C ROMaddr ERROR");
if (i2c_PutByte(addr)==I2C_ERROR)
printf("\r\nRead I2C sub-addr ERROR");
if (i2c_ReadFrom(ROM))
printf("\r\nRead I2C addr+1 ERROR");
return i2c_GetByte(I2C_LAST);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -