2011-03-26 46 views
1

我不能設法讓我的LCD初始化,我做錯了什麼?任何幫助感謝!基於hd44780的LCD與基於P89LPC9351的微控制器之間的接口問題

#include <stdio.h> 
    #include <stdlib.h> 
    #include <p89lpc9351.h> 

    #define XTAL 7373000L 
    #define BAUD 115600L 

    #define LCD_data P2 
    #define LCD_rs P1_4 
    #define LCD_rw P1_6 
    #define LCD_en P1_7 

    void putchar (char c) 
    { 
     while (!TI); 
     TI=0; 

     SBUF=c; 
    } 

    char getchar (void) 
    { 
     while (!RI); 
     RI=0; 
     return SBUF; 
    } 

    void InitSerialPort(void) 
    { 
     BRGCON=0x00;     //Make sure the baud rate generator is off 
     BRGR1=((XTAL/BAUD)-16)/0x100; 
     BRGR0=((XTAL/BAUD)-16)%0x100; 
     BRGCON=0x03;     //Turn-on the baud rate generator 
     SCON=0x52;      //Serial port in mode 1, ren, txrdy, rxempty 
     P1M1=0x00;      //Enable pins RxD and Txd 
     P1M2=0x00;      //Enable pins RxD and Txd 
    } 

    void delay_ms(long ms) 
    { 
     long i; 

     while (ms--) 
      for (i=0; i < 330; i++) 
       ; 
    } 

    void command(char i) 
    { 
     LCD_data = i;     //put data on output Port 
     LCD_rs =0;      //D/I=LOW : send instruction 
     LCD_rw =0;      //R/W=LOW : Write 
     LCD_en = 1; 
     delay_ms(1);     //enable pulse width >= 300ns 
     LCD_en = 0; 
    } 

    void write(char i) 
    { 
     LCD_data = i;     //put data on output Port 
     LCD_rs =1;      //D/I=LOW : send data 
     LCD_rw =0;      //R/W=LOW : Write 
     LCD_en = 1; 
     delay_ms(1);     //enable pulse width >= 300ns 
     LCD_en = 0;      //Clock enable: falling edge 
    } 

    void LCD_init() 
    { 
     LCD_en = 0; 
     delay_ms(100);     //Wait >15 msec after power is applied 
     command(0x30);     //command 0x30 = Wake up 
     delay_ms(30);     //must wait 5ms, busy flag not available 
     command(0x30);     //command 0x30 = Wake up #2 
     delay_ms(10);     //must wait 160us, busy flag not available 
     command(0x30);     //command 0x30 = Wake up #3 
     delay_ms(10);     //must wait 160us, busy flag not available 
     command(0x38);     //Function set: 8-bit/2-line 
     command(0x10);     //Set cursor 
     command(0x0c);     //Display ON; Cursor ON 
     command(0x06);     //Entry mode set 
    } 

    void main (void) 
    { 
     InitSerialPort(); 
     LCD_init(); 
    } 
+0

你也可以嘗試在http://electronics.stackexchange.com/上詢問這個問題,因爲更多硬件導向的人在那裏閒逛。不過,我認爲這不是一個無關緊要的問題。 – RBerteig 2011-03-26 01:15:27

回答

2

init序列看起來沒問題。你忘了明確將P2和P1引腳設置爲輸出嗎?

+0

我做了,非常感謝你。 – Oniros 2011-03-27 04:16:50