1

我在尝试使用时遇到了一些麻烦LCD。我正在使用LCDHD447802x16PIC18FMPLAB. 我认为我的代码很好,我可以编译项目,但是当我尝试使用它来显示一个简单的字符时,LCD 上仍然没有任何反应。以下代码有什么问题:

#include "LCD_HD44780.h"


void LCD_HD44780_reset(void)
{
    TRIS_D7 = 0;
    TRIS_D6 = 0;
    TRIS_D5 = 0;
    TRIS_D4 = 0;
    DATA_D7 = 0;
    DATA_D6 = 0;
    DATA_D5 = 0;
    DATA_D4 = 0;

    RW_PIN = 0; // R/W pin made low
    RS_PIN = 0; // Register select pin made low
    E_PIN = 0;  // Clock pin made low
    TRIS_RW = 0; // All control signals made outputs
    TRIS_RS = 0;
    TRIS_E = 0;

    __delay_ms(15);

    LCD_HD44780_sendCmd8bit(0x30);
    __delay_ms(10);
    LCD_HD44780_sendCmd8bit(0x30);
     __delay_ms(10);
    LCD_HD44780_sendCmd8bit(0x30);
    __delay_ms(10);
}

void LCD_HD44780_init(void)
{
    LCD_HD44780_reset();

    // Enable 4 bit mode
    LCD_HD44780_sendCmd8bit(0x20);
     __delay_ms(10);
    LCD_HD44780_sendCmd(0x28);          //Function set: 4 bit interface
    LCD_HD44780_sendCmd(0x0f);    //Display on/off: display on, cursor off,        blink off
    LCD_HD44780_ClearScreen();
    LCD_HD44780_sendCmd(0x06);    //Entry mode: increment, no shift
}

void LCD_HD44780_sendCmd8bit(BYTE c)
{
    RW_PIN = 0; // Set the control signals
    RS_PIN = 0; // for sending a command

    LCD_HD44780_writeNibble((c & 0xF0) >> 4); //send only 4 msb
}

void LCD_HD44780_sendCmd(BYTE c)
{
    RW_PIN = 0; // Set the control signals
    RS_PIN = 0; // for sending a command

    LCD_HD44780_writeNibble((c & 0xF0) >> 4);
    LCD_HD44780_writeNibble(c & 0x0F);
}

void LCD_HD44780_sendData(BYTE d)
{
    RW_PIN = 0; // Set the control signals
    RS_PIN = 1; // for sending data

    LCD_HD44780_writeNibble((d & 0xF0) >> 4);
    LCD_HD44780_writeNibble(d & 0x0F);
}

void LCD_HD44780_sendStr(BYTE *str)
{
    while(*str != 0)
    {
        LCD_HD44780_sendData(*str++);
    }
}

void LCD_HD44780_setCursor(BYTE row, BYTE col)
{
    BYTE command;
    BYTE add = row*0x40 + col;
    command = 0x80 | add;
    LCD_HD44780_sendCmd(command);
}


void LCD_HD44780_writeNibble(BYTE nb)
{
    BYTE a;

    if(nb & 0x08)
    {  
        DATA_D7 = 1;
    }
    else
    { 
        DATA_D7 = 0;
    }

    if(nb & 0x04)
    {  
        DATA_D6 = 1;

    }
    else
    { 
        DATA_D6 = 0;
    }

    if(nb & 0x02)
    {
        DATA_D5 = 1;
    }
    else
    {
        DATA_D5 = 0;
    }

    if(nb & 0x01)
    {
        DATA_D4 = 1;

    }
    else
    {
        DATA_D4 = 0;
    }

    E_PIN = 1;

    for(a = 0; a < 4; a++);

    E_PIN = 0;

    for(a = 0; a < 4; a++);
}

在我的 .h 文件中,我有:

#define _XTAL_FREQ 20000000

#define RW_PIN   LATCbits.LATC0        /* PORT for RW */
#define TRIS_RW  TRISCbits.TRISC0      /* TRIS for */
#define RS_PIN   LATCbits.LATC2        /* PORT for RS */
#define TRIS_RS  TRISCbits.TRISC2      /* TRIS for RS */
#define E_PIN    LATCbits.LATC1        /* PORT for E  */ 
#define TRIS_E   TRISCbits.TRISC1      /* TRIS for E  */
#define DATA_D7  LATEbits.LATE7        // LAT for DISP 7
#define DATA_D7_RD  PORTEbits.RE7      // PORT for DISP 7 
#define TRIS_D7  TRISEbits.TRISE7      // TRIS for DISP 7
#define DATA_D6  LATEbits.LATE6        // LAT for DISP 6
#define DATA_D6_RD  PORTEbits.RE6      // PORT for DISP 6 
#define  TRIS_D6  TRISEbits.TRISE6      // TRIS for DISP 6
#define DATA_D5  LATEbits.LATE5        // LAT for DISP 5
#define DATA_D5_RD  PORTEbits.RE5      // PORT for DISP 5
#define TRIS_D5  TRISEbits.TRISE5      // TRIS for DISP 5
#define DATA_D4  LATEbits.LATE4        // LAT for DISP 4
#define DATA_D4_RD  PORTEbits.RE4      // PORT for DISP 4
#define TRIS_D4  TRISEbits.TRISE4      // TRIS for DISP 4

#define LCD_HD44780_CursorHome()    LCD_HD44780_sendCmd(0x02);
#define LCD_HD44780_ScrollLeft()    LCD_HD44780_sendCmd(0x18); 
#define LCD_HD44780_ScrollRight()   LCD_HD44780_sendCmd(0x1E);
#define LCD_HD44780_ClearScreen()   LCD_HD44780_sendCmd(0x01); 
#define LCD_HD44780_CursorOff()     LCD_HD44780_sendCmd(0x0C);
#define LCD_HD44780_ClearOn()       LCD_HD44780_sendCmd(0x0E);
4

1 回答 1

0

Bruno:这是使用 PIC18 系列 MCU 的同一 LCD 的工作教程点击这里

我还建议将您的硬件与此参考设计进行比较。 在此处输入图像描述

于 2016-08-03T12:11:30.893 回答