使用PIC16F1829在C中初始化LCD显示屏?

时间:2015-07-01 11:31:54

标签: c microcontroller pic lcd

我正在尝试使用PIC16F1829初始化我的LCD屏幕。我已经按照液晶显示器给出的流程图,但无济于事。请参阅下面的代码,如果您知道我哪里错了,请告诉我们?我将在以后使用send_command()函数等对其进行优化。 。 。目前我只是想确保它会初始化。

//config bits that are part-specific for the PIC16F1829
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & LVP_OFF);

/*Function Prototypes*/
void init_disp(void);

/*Define outputs*/
#define _XTAL_FREQ 500000
#define RS LATCbits.LATC0 
#define EN LATCbits.LATC1 
#define RW LATCbits.LATC2
#define DB1 LATCbits.LATC4
#define DB2 LATCbits.LATC3
#define DB3 LATCbits.LATC6
#define DB4 LATCbits.LATC7
#define TRIG ((EN=1),(EN=0))

void main(void) {

  TRISC=0; //set PORTC to output 
  EN = 0;
 __delay_ms(50); 

 init_disp();

}

void init_disp(void){

    //function set 1
    RW = 0;
    RS = 0;
    DB1 = 1; //Hello LCD
    DB2 = 1;
    DB3 = 0;
    DB4 = 0;
    TRIG;

    //function set 2
    __delay_ms(5);
    TRIG;

    //function set 3
    __delay_ms(2);
    TRIG;

    // 4bit mode
    __delay_ms(2);
    DB1 = 0; 
    DB2 = 1;
    DB3 = 0;
    DB4 = 0;
    TRIG;

    //Display lines and font MS Nibble
    __delay_ms(2);
    DB1 = 0; 
    DB2 = 1;
    DB3 = 0;
    DB4 = 0;
    TRIG;
    //Display lines and font LS Nibble
    DB1 = 0; 
    DB2 = 0;
    DB3 = 1;
    DB4 = 1;
    TRIG;

    //Display off MS Nibble
    __delay_ms(1);
    DB1 = 0; 
    DB2 = 0;
    DB3 = 0;
    DB4 = 0;
    TRIG;
   //Display off LS Nibble 
    DB1 = 0; 
    DB2 = 0;
    DB3 = 0;
    DB4 = 1;
    TRIG;

    //Display Clear MS Nibble
    __delay_ms(5);
    DB1 = 0;
    DB2 = 0;
    DB3 = 0;
    DB4 = 0;
    TRIG;
    //display clear LS Nibble
    DB1 = 1; 
    DB2 = 0;
    DB3 = 0;
    DB4 = 0;
    TRIG;

    //Entry Mode MS Nibble
    __delay_ms(1);
    DB1 = 0;
    DB2 = 0;
    DB3 = 0;
    DB4 = 0;
    TRIG;
    //entry mode LS Nibble
    DB1 = 0;
    DB2 = 1;
    DB3 = 1;
    DB4 = 0;
    TRIG;

}

1 个答案:

答案 0 :(得分:1)

默认情况下,PIC微控制器的所有引脚都是模拟输入。在这里,您使用PORTC作为数字输出。因此,您需要初始化PORTC,如下所示

PORTC = 0x00;   // Init PORTC
LATC = 0x00;    // Data Latch
ANSELC = 0x00;  // This will enable PORTC as digital
TRISC = 0x00;   // Output Pins

我希望这适合你。您需要参考Datasheet of PIC16F1829