Microchip PIC18F4550 Push Button 按鍵程式很簡單,並沒有作 debounce 檢測。首先配置液晶顯示屏、PortA.RA1 作 LED 顯示及 PortB.RB5 作為按鍵之用。然後讀取 PortB RB5 的狀態,決定按鍵是否按下。如果 PortB RB5 的數值是 0 (低電位) ,LED 便會亮、LCD 顯示 Key=0。程式便會循環讀取 PortB RB5 的狀態。
▲ Software Clock 流程圖 |
void main(void) { // 1234567890123456 char LCD_name[]=" BWS Push Button"; int i; unsigned int timeout=0; char data; char key; ADCON0 = 0x00; // Disable the AD converter ADCON1 = 0x0F; // Set all ports to digital TRISA = 0b00000000; // Setup PORTA as Output TRISB = 0b11100000; // Initialise LCD OpenXLCD(FOUR_BIT&LINES_5X7); // Init the LCD Display while(BusyXLCD()); WriteCmdXLCD(BLINK_OFF & CURSOR_OFF ); while(BusyXLCD()); stdout = _H_USER; // Redirect I/O to LCD LcdSetLine1(); // Put cursor on start of line 1 putsXLCD(LCD_name); // Display text while(1) { key = PORTBbits.RB5; LcdSetLine2(); // Put cursor on start of line 2 printf("PortB.RB5 Key=%02i",key); if (key == 0) { LATAbits.LATA0 = 1; // RA0=1 LED=ON Scope=1 Delay10KTCYx(240); // 10,000 x 1 x (4 x 0.05us) = 2ms // Scope = 2ms } LATAbits.LATA0 = 0; // RA0=0 LED=OFF Scope=50ms } // End While } |
▲平常 Push Button ,LCD Key=01 |
▲按下 Push Button, LCD Key=00,LED 發亮 |
请问下 如果我要按按钮就能更改LCD里面的号码 我应该怎样弄?
回覆刪除