ARM9教程DSP教程FPGA教程CPLD教程6A3软硬件设计0229

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

// Must be called regularly to scan the KBD // if a key has been pressed, set global bit KbHit and global LastKey //-----------------------------------------------------------------------------unsigned char Kbd_DoScan(void) { static __bit AllKeysOff = 1; register unsigned char row; // flag for key release
第 பைடு நூலகம்3 页
共 82 页
register unsigned char columndata; for(row = 0; row < 4; row ++) { KEY_PORT = ~(1 << row); columndata = KEY_PORT; columndata ^=0xFF; if(columndata & 0x80) { columndata = KEY_TEST; } else { columndata &= 0x0F; } if(columndata) { switch(columndata) { // translate bitnumber into column number case 1: case 2: columndata -= 1; break; case 4: columndata = 2; break; case 8: columndata = 3; break; case KEY_TEST: row = 0; // this will make the lower two bits of the scancode // any other keys are down? // mask top bits // top bit set --> Test Key Code // output low bit to scan row // read in pattern for current row // invert --> active keys have bit set // top Bit set --> Test/Reset is down
//-----------------------------------------------------------------------------// scans the Keyboard // returns: // 0 if no key was pressed // scankey of the cuurent keyboard scan: // // bits 0..1 indicate the column bits 2..3 indicate the row
void DelayMs(unsigned int time) { register unsigned char i; while (time --) for(i=0; i<5; i++) Delay10Us(20); }
static void LCD_WaitWhileBusy(unsigned int Del10Us) { #ifndef LCD_READ_BUSY_FLAG if(Del10Us > 200) DelayMs(Del10Us/100); else Delay10Us(Del10Us); #else while(LCD_CTRL_R & 0x80); #endif // wait for busy flag to be low
void LCD_SetBacklight(unsigned char On) //------------------------------------// turns LCD Backlight on or off { LCD_BACKLIGHT = On;
第 67 页
共 82 页
}
unsigned char LCD_GetBacklight(void) //-------------------------------------// returns state of LCD backlight { return LCD_BACKLIGHT; }
第 65 页
共 82 页
1.22
OMT_LCD.C
#include "hware.h" #include "OMT_Lcd.h"
void Delay10Us(unsigned char time) { unsigned long i; for(i=0;i<(FOSC/180000L);i++) { __asm( "NOP\n\t"); } } // this is very imprecise but only used for // non-critical delays
第 61 页
共 82 页
1.21
OMT_KBT.C
#include "hware.h" #include "OMT_Kbt.h"
__rom unsigned char KeyTranslationTable[] = {'1','2','3','C', '4','5','6','D', '7','8','9','E', 'A','0','B','F' };
// if 'Format'==KEY_FORMAT_ASCII, the ASCII representation of the key on the kbd // is returned, eg. '1' for top left, 'F' for bottom right
// Uses: // Globals LastKey and KbHit //---------------------------------------------------------------------------------unsigned char GetKey(unsigned char Format) { register unsigned char retval=LastKey; if(KEY_TEST != retval) { // function return value
第 64 页
共 82 页
break; default: return(0); } columndata |= (row << 2); if(AllKeysOff) { LastKey = columndata; KbHit = 1; AllKeysOff = 0; } return(columndata); } } AllKeysOff = 1; return (0); } // at this point we know that no key is pressed // set global key variable // set KbHit flag // clear release flag // assemble scan code // lower two nibbles are column, next two are row // was it a new key? TODO: debounce // abort if more than one key is pressed
__bit KbHit = 0; unsigned char LastKey;
//---------------------------------------------------------------------------------// returns last key that was pressed // KEY_TEST if the test key was pressed // if 'Format'==KEY_FORMAT_SCANCODE, a scan code is returned as follows: // // // bits 0..1 indicate the column bits 2..3 indicate the row bit 4 is always set
第 62 页
共 82 页
switch(Format) { case KEY_FORMAT_ASCII: retval = KeyTranslationTable[retval]; break; case KEY_FORMAT_SCANCODE: retval |= 0x10; break; default: break; } } // if KbHit = 0; return (retval); } // set bit 4 // look ASCII value up in table
第 66 页
共 82 页
}
//---------------------------------------------------------------// initialises the LCD controller void LCD_Init(void) { RAM_LCD = SELECT_LCD; DelayMs(20); LCD_CTRL_W = 0x38; DelayMs(8); LCD_CTRL_W = 0x38; DelayMs(2); LCD_CTRL_W = 0x38; Delay10Us(10); LCD_CTRL_W = 0x38; Delay10Us(10); LCD_CTRL_W = 0x06; Delay10Us(10); LCD_CTRL_W = 0x0E; LCD_WaitWhileBusy(1000); LCD_CTRL_W = 0x01; LCD_WaitWhileBusy(17*100); RAM_LCD = SELECT_RAM; } // Clear Display // This takes at least 1.64ms to execute // Display on, Cursor On // Display Off, Cursor off, Blink off // Function set 8 Bit, 2 lines, 5X7 dots // wait for more than 15ms after powerup // Function set 8 Bit // wait for another 8 ms // Function set 8 Bit // wait for another 2 ms // Function set 8 Bit
// returns byte at offset 'Addr' in device 'DevAddr' // if 'TwoByteAddress' !=0 two adress bytes are sent { #ifndef __TURBOC__ unsigned char retval; I2C_Start(); DevAddr&=(~0x01); retval=I2C_TxByte(DevAddr); if(TwoByteAddress) { retval=I2C_TxByte(Addr>>8); } retval=I2C_TxByte(Addr&0xFF); I2C_Start(); DevAddr|=(0x01); retval=I2C_TxByte(DevAddr); retval=I2C_RxByte(0); I2C_Stop(); return(retval); #endif } // create start condition // set read flag // send device address // 16 Bit internal address? // create start condition // clear read flag to write address // send device address
相关文档
最新文档