keyled.c
上传用户:shyuanyi
上传日期:2008-05-24
资源大小:69k
文件大小:4k
- #include "my_head.h"
- //#include "key.h"
- /*********************************************/
- #define SPI_HEAD_CHAR 0X00
- #define SPI_TAIL_CHAR 0xFF
- #define KEYBORD_ORDOR 0x01
- #define DIS_DAT_ORDOR 0X02
- #define DIS_LED_ORDOR 0X03
- #define FLASH_LED_ORDOR 0X04
- /*********************************************/
- #define LEDKEY_CTL P3_3 //int1
- bit ledkey_dat_flag = FALSE;
- void Key_Int1(void)interrupt 2 using 1 //for LED_KEY_SPI
- {
- //IE1 = 0;
- ledkey_dat_flag = TRUE;
- //P2_7 ^= 1; //BUZZER for test
- }
- #define BUF_SIZE 22
- //================================
- extern unsigned char pdata Rf_Buffer[];
- //BYTE idata spi_rbuf[BUF_SIZE-2];
- BYTE spi_rbuf_count = 0;
- bit spi_save_flag = FALSE;
- bit spi_fram_flag = FALSE;
- void SpiKeyReceive(BYTE ch)
- {
- if(ch==SPI_TAIL_CHAR)
- {
- spi_save_flag=FALSE;
- if(spi_rbuf_count>0)spi_fram_flag=TRUE;
- return;
- }
- if(ch==SPI_HEAD_CHAR)
- {
- spi_fram_flag=FALSE;
- spi_save_flag=TRUE;
- spi_rbuf_count=0;
- return;
- }
- if(spi_save_flag)
- {
- if(spi_rbuf_count<BUF_SIZE-2)Rf_Buffer[spi_rbuf_count++]=ch;
- }
- }
- void clear_spi_rbuf(void)
- {
- spi_fram_flag=FALSE;
- spi_save_flag=FALSE;
- spi_rbuf_count=0;
- }
- void Init_KeyLed(void)
- {
- EA = 0;
- IP &= ~0X04; //优先级0
- //IPH &= ~0X04;
- IT1 = 1;
- EX1 = 1;
- EA = 1;
- LEDKEY_CTL = 1;
- clear_spi_rbuf();
- }
- void kbint_open(void)
- {
- LEDKEY_CTL = 1;
- IE1 = 0;
- EX1 = 1;
- }
- void kbint_close(void)
- {
- EX1 = 0;
- }
- BYTE Get_SpiReceiveDat(BYTE *buf)
- {
- BYTE ii;
- if(spi_fram_flag==FALSE)return(0);
- *buf++ = Rf_Buffer[0];
- for(ii=1;ii<spi_rbuf_count;ii++,buf++)
- {
- *buf =(Rf_Buffer[ii]<<4);
- ii++;
- *buf |=(Rf_Buffer[ii]&0xf);
- }
- clear_spi_rbuf();
- return(ii/2+1);
- }
- BYTE Set_SpiSendDat(BYTE *buf,BYTE size,BYTE ordor,BYTE *spi_sbuf)
- {
- BYTE ii=0;
- if((size<1)||(size>9))return(ii);
- spi_sbuf[ii++]=SPI_HEAD_CHAR;
- spi_sbuf[ii++]=ordor;
- for(;ii<size*2+2;buf++)
- {
- spi_sbuf[ii++]=0x30|(*buf>>4);
- spi_sbuf[ii++]=0x30|(*buf&0xf);
- }
- spi_sbuf[ii++]=SPI_TAIL_CHAR;
- return(ii);
- }
- void SpiKey_SendStr(BYTE *ptr,BYTE count)
- {
- BYTE ii;
- for(ii=0;ii<count;ii++)
- {
- SpiKeyReceive(KeySpiByte_SendReceive(ptr[ii]));
- }
- }
- void Display_str(BYTE str[])
- {
- BYTE i;
- BYTE idata spi_sbuf[BUF_SIZE+1];
- i=Set_SpiSendDat(str,8,DIS_DAT_ORDOR,spi_sbuf);
- kbint_close();
- LEDKEY_CTL =0;
- SpiKey_SendStr(spi_sbuf,i);
- LEDKEY_CTL =1;
- kbint_open();
- }
- void Do_Led(BYTE led_dat)
- {
- BYTE i;
- BYTE idata spi_sbuf[10];
- i=Set_SpiSendDat(&led_dat,1,DIS_LED_ORDOR,spi_sbuf);
- kbint_close();
- LEDKEY_CTL =0;
- SpiKey_SendStr(spi_sbuf,i);
- LEDKEY_CTL =1;
- kbint_open();
- }
- void Do_DisLed_Flash(BYTE disflash,BYTE ledflash,BYTE flashtime)
- {
- BYTE i,buf[3];
- BYTE idata spi_sbuf[10];
- buf[0]=disflash;
- buf[1]=ledflash;
- buf[2]=flashtime;
- i=Set_SpiSendDat(buf,3,FLASH_LED_ORDOR,spi_sbuf);
- kbint_close();
- LEDKEY_CTL =0;
- SpiKey_SendStr(spi_sbuf,i);
- LEDKEY_CTL =1;
- kbint_open();
- }
- BYTE Check_Key_Event(void)
- {
- BYTE i,buf[8];
- if(!ledkey_dat_flag)return(1);
- do_buzzer(BUZZER_WAIT);
- kbint_close();
- LEDKEY_CTL =0;
- for(i=9;i--;)buf[i]=0xff;
- clear_spi_rbuf();
- Set_Timer20ms(2);
- while(!spi_fram_flag)
- {
- SpiKey_SendStr(buf,1);
- if(!Get_Timer20ms())break;
- }
- LEDKEY_CTL=1;
- kbint_open();
- ledkey_dat_flag = FALSE;
- //----------------------
- //do_buzzer(2);SendStr(spi_rbuf,BUF_SIZE-2); //for test.
- if( !Get_SpiReceiveDat(buf) || (buf[0]!=KEYBORD_ORDOR) )return(2);
- PostMessage(KEY_PRESS_EVENT,buf[1]);
- return(0);
- }