我正在使用带有 mplab xc16 的 dspic33fj128mc802。我想在 4 位模式下使用 LCD,但似乎无法初始化它,也不知道我在做什么错。任何帮助将不胜感激调试。我查看了其他示例,我的代码看起来与其他示例相似,但仍然无法正常工作
//LCD Control pins
#define LCD_RS _LATB11
#define LCD_EN _LATA4
#define TRIS_EN TRISAbits.TRISA4 /* TRIS for E */
#define TRIS_RS TRISBbits.TRISB11 /* TRIS for RS */
#define D7 LATBbits.LATB15
#define D6 LATBbits.LATB14
#define D5 LATBbits.LATB13
#define D4 LATBbits.LATB12
#define D7_TRIS TRISBbits.TRISB15
#define D6_TRIS TRISBbits.TRISB14
#define D5_TRIS TRISBbits.TRISB13
#define D4_TRIS TRISBbits.TRISB12
//LCD Data pins
#define lcdport _LATB11,_LATA4,_LATB15,_LATB14,_LATB13,_LATB12
void delay_ms();
void delay_us();
void LCD_Init();
void LCD_Command(char cmd);
void LCD_ClearDisp();
void LCD_RetHome();
void LCD_Display();
void LCD_ShiftR();
void LCD_ShiftL();
void LCD_4BitMode();
void LCD_Init();
void delay_ms()
{
__delay32(40000);
}
void delay_us()
{
__delay32(40);
}
void LCD_Command(char cmd) //LCD Command routine
{
lcdport = ((cmd >> 4) & 0x0F)|LCD_EN;
delay_ms(20);
lcdport = ((cmd >> 4) & 0x0F);
delay_ms(20);
lcdport = (cmd & 0x0F)|LCD_EN;
delay_ms(20);
lcdport = (cmd & 0x0F);
delay_ms(20);
}
void LCD_ClearDisp()
{
delay_ms(20);
LCD_Command(0b0000000001);
delay_ms(20);
}
void LCD_RetHome() //set cursor to first digit
{
delay_ms(20);
LCD_Command(0b0000000010);
delay_ms(20);
}
void LCD_Display()//disp on, cursor on, blinking on
{
delay_ms(20);
LCD_Command(0b0000001111);
delay_ms(20);
}
void LCD_ShiftR()//shift right
{
delay_ms(20);
LCD_Command(0b000001100);
delay_ms(20);
}
void LCD_ShiftL()//shift right
{
delay_ms(20);
LCD_Command(0b000001000);
delay_ms(20);
}
void LCD_4BitMode()//4 bit mode, 2 line, 5x7 dots
{
delay_ms(20);
LCD_Command(0b0000101000);
delay_ms(20);
}
void LCD_EntrySet()
{
delay_ms(20);
LCD_Command(0b0000000110);
delay_ms(20);
}
void DDRAM_address()
{
delay_ms(20);
LCD_Command(0b0010000000);
delay_ms(20);
}
void LCD_Strobe(void) //This function pulls the enable line high and then low
{
LCD_EN = 1;
delay_ms(20);
LCD_EN = 0;
delay_ms(20);
}
void write_lcd(char dat)
{
LCD_RS = 1; // RS = 1
LCD_EN = 1; // E = 1
lcdport = dat;
delay_ms(20);
LCD_EN = 0; // E = 0
delay_ms(20);
}
void LCD_Write_String(char *ptr)
{
while(*ptr)
{
write_lcd(*ptr);
delay_ms(1);
ptr++;
}
}
void LCD_Init()
{
delay_ms(20);
LCD_RS = 0;
LCD_4BitMode();
delay_ms(1);
LCD_Strobe();
delay_ms(1);
LCD_Strobe();
delay_ms(1);
LCD_Strobe();
LCD_ShiftR();
delay_ms(1);
LCD_EntrySet();
delay_ms(1);
DDRAM_address();
delay_ms(1);
LCD_ClearDisp();
}
int main ()
{
while(1)
{
LCD_Init(); //Intilize LCD in 4-Bit Mode
delay_ms(20);
LCD_Command(0X80); // Start Cursor From First Line
delay_ms(20);
LCD_Write_String("Hello"); //Print HELLO on LCD
delay_ms(20);
LCD_Command(0XC0); // Start Cursor From Second Line
delay_ms(20);
LCD_Write_String("World"); //Print HELLO on LCD
delay_ms(20);
LCD_Strobe();
delay_ms(20);
}
}