【问题标题】:Can't initialize Qapass 1602A LCD with PIC18F4620无法使用 PIC18F4620 初始化 Qapass 1602A LCD
【发布时间】:2020-01-18 12:00:52
【问题描述】:

我无法使用 PIC18F4620 初始化 QAPASS 1602A LCD,我总是在两条线上得到方形块。一开始以为是液晶屏坏了,换了个新的,结果还是一样。我使用通过 PICkit3 连接到 PC 的 logifind PIC 40 Mini 开发板,其他一切在每个端口上都工作正常,但不是 LCD。

非常感谢您的帮助。

//main.c
#include "newxc8_header.h"
#include <xc.h>
#include "lcd_16x2.h"

void main(void) {
    ADCON1=0x0F; //configuring all analog ports to digital
    TRISB=0x00; //Set RBs as output
    TRISD=0x00; //Set RDs as output
    TRISE=0x00; //Set REs as output

    LCD_Init();  //Initialize 16x2 LCD

    LCD_String("Hello");
    while(1);
}


//lcd_16x2.c
#include "lcd_16x2.h"

void LCD_Init(void)
{
    __delay_ms(1000);
    EN = 0;
    RS = 0;
    ldata = 0x00;
    LCD_Command(0x38);  // Initialization of 16X2 LCD: 8-bit, 2 Lines, 5x7 Dots
    LCD_Command(0x0C);  // Display ON Cursor OFF
    LCD_Command(0x01);   //clear LCD
    LCD_Command(0x02);   //cursor beginning first line
}

void LCD_Command(unsigned char cmd)
{
    ldata = cmd;  /* Send data to PORT as a command for LCD */
    RS = 0;  /* Command Register is selected */
    EN = 1;  /* High-to-Low pulse on Enable pin to latch data */
    __delay_ms(5);
    EN = 0;
    __delay_ms(3);
}

void LCD_Char(unsigned char data)
{
    ldata = data;  /* Send data to LCD */  
    RS = 1;  // Data Register is selected */
    EN = 1;  // High-to-Low pulse on Enable pin to latch data */   
    __delay_ms(5);
    EN = 0;
    __delay_ms(3);
}

void LCD_String(const char *msg)
{
    while(*msg != 0)
    {
      LCD_Char(*msg);
      msg++;
    }
}

//lcd_16x2.h
#ifndef LCD_16X2_H
#define LCD_16X2_H

#ifdef  __cplusplus
extern "C" {
#endif

#include <xc.h>

#define _XTAL_FREQ 16000000 //16 MHz (4 MHz x 4 PLL)

#define ldata LATB                  /*PORTB(RB0-RB7) is used for transmitting data to LCD*/
#define RS LATEbits.LATE0           /*RE0 pin is used for Register Select*/
#define EN LATEbits.LATE1           /*RE1 pin is used for Enable*/

void LCD_Init(void);
void LCD_Command(unsigned char );
void LCD_Char(unsigned char x);
void LCD_String(const char *);
void LCD_Clear();

#ifdef  __cplusplus
}
#endif

#endif  /* NEWFILE_H */

【问题讨论】:

    标签: pic microchip lcd


    【解决方案1】:

    您的问题没有包含足够的详细信息来确定答案。

    最好的猜测是您的 LogiFind PIC-40-MINI 板之间存在接线错误: 和您的 QAPASS 1602A LCD 模块: 一张他们如何联系的照片会很有帮助。

    如果您确信接线正确,则 LCD_Init() 函数可能存在问题,请参阅第 45 页的HD44780 data sheet 了解有关在 8 位并行模式下或在您未发布的代码中初始化模块的流程图.

    添加于 2020 年 1 月 19 日:

    我按照你的描述组装了一个 PIC18F4620 和 LCD 模块。

    发现您发布的代码没有显示您正在使用的配置字。

    在默认频率 (1MHz) 下使用内部振荡器时,您的代码运行但比预期慢 16 倍。

    调整一些设置后,这是代码的最终版本:

    //main.c
    
    // PIC18F4620 Configuration Bit Settings
    
    // 'C' source line config statements
    
    // CONFIG1H
    #pragma config OSC = INTIO67    // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
    #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
    #pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
    
    // CONFIG2L
    #pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
    #pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)
    
    // CONFIG2H
    #pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
    #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
    
    // CONFIG3H
    #pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
    #pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
    #pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
    #pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
    
    // CONFIG4L
    #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    #pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
    #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
    
    // CONFIG5L
    #pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-003FFFh) not code-protected)
    #pragma config CP1 = OFF        // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
    #pragma config CP2 = OFF        // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
    #pragma config CP3 = OFF        // Code Protection bit (Block 3 (00C000-00FFFFh) not code-protected)
    
    // CONFIG5H
    #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
    #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
    
    // CONFIG6L
    #pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
    #pragma config WRT1 = OFF       // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
    #pragma config WRT2 = OFF       // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
    #pragma config WRT3 = OFF       // Write Protection bit (Block 3 (00C000-00FFFFh) not write-protected)
    
    // CONFIG6H
    #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
    #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
    #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
    
    // CONFIG7L
    #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
    #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
    #pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
    #pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
    
    // CONFIG7H
    #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
    
    #include <xc.h>
    #include "lcd_16x2.h"
    
    void main(void) {
        OSCCON  = 0b01100000;   // sets clock = 4MHz
        OSCTUNE = 0b01000000;   // turn on 4xPLL
    
        ADCON1=0x0F; //configuring all analog ports to digital
        TRISB=0x00; //Set RBs as output
        TRISD=0x00; //Set RDs as output
        TRISE=0x00; //Set REs as output
    
        LCD_Init();  //Initialize 16x2 LCD
    
        LCD_Command(0x80);
        LCD_String("Hello");
        LCD_Command(0xC0);
        LCD_String(__TIME__ " v1 ");
        while(1);
    }
    
    //lcd_16x2.c
    #include <xc.h>
    #include "lcd_16x2.h"
    
    void LCD_Init(void)
    {
        __delay_ms(1000);
        EN = 0;
        RS = 0;
        LCD_Command(0x38);  // Initialization of 16X2 LCD: 8-bit, 2 Lines, 5x7 Dots
        LCD_Command(0x0C);  // Display ON Cursor OFF
        LCD_Command(0x01);   //clear LCD
        LCD_Command(0x02);   //cursor beginning first line
    }
    
    void LCD_Command(unsigned char cmd)
    {
        ldata = cmd;  /* Send data to PORT as a command for LCD */
        RS = 0;  /* Command Register is selected */
        EN = 1;  /* High-to-Low pulse on Enable pin to latch data */
        __delay_us(1);
        EN = 0;
        __delay_ms(4);
    }
    
    void LCD_Char(unsigned char data)
    {
        ldata = data;  /* Send data to LCD */  
        RS = 1;  // Data Register is selected */
        EN = 1;  // High-to-Low pulse on Enable pin to latch data */   
        __delay_us(1);
        EN = 0;
        __delay_us(50);
    }
    
    void LCD_String(const char *msg)
    {
        while(*msg != 0)
        {
          LCD_Char(*msg);
          msg++;
        }
    }
    
    //lcd_16x2.h
    #ifndef LCD_16X2_H
    #define LCD_16X2_H
    
    #ifdef  __cplusplus
    extern "C" {
    #endif
    
    #include <xc.h>
    
    #define _XTAL_FREQ 16000000 //16 MHz (4 MHz x 4 PLL)
    
    #define ldata LATB                  /*PORTB(RB0-RB7) is used for transmitting data to LCD*/
    #define RS LATEbits.LATE0           /*RE0 pin is used for Register Select*/
    #define EN LATEbits.LATE1           /*RE1 pin is used for Enable*/
    
    void LCD_Init(void);
    void LCD_Command(unsigned char );
    void LCD_Char(unsigned char x);
    void LCD_String(const char *);
    void LCD_Clear();
    
    #ifdef  __cplusplus
    }
    #endif
    
    #endif  /* NEWFILE_H */
    

    【讨论】:

    • 感谢您的回复,我已经用测试仪检查了所有连接,它们都很好,如下所示:RB0 ------->DB0,RB1-------- >DB1、RB2-------->DB2、RB3-------->DB3、RB4-------->DB4、RB5-------->DB5 , RB6-------->DB6, RB7-------->DB7, RE0-------->RS, RE1-------->E, 接地-->RW。 VDD 和 LED+ 连接到 5V,VSS 和 LED- 连接到地。关于数据表,在第 45 页我看到我必须向 LCD 发送 0x30 3 次,我之前已经尝试过,但如果我这样做了,我只会看到 1 行块字符,这意味着没有初始化。我的两个 QAPASS LCD 都是一样的。
    • @user3432143 你是否也做了流程图中显示的 0x30 写入之间的延迟?
    • 如上面的代码所示,我在“void LCD_Command(unsigned char cmd)”函数中设置了延迟,远远超过了要求。
    • @user3432143 我已经更新了我的答案以包含适用于我的硬件的代码。看看它是否适合你。
    • 非常感谢您的时间和代码,LCD 正在工作!我使用的是外部 4 mhz 晶体振荡器,所以我设置了 #pragma config OSC = HSPLL intead of INTIO67,并且我没有设置 OSCCON 和 OSCTUNE。关于速度,我在这里和那里设置了一些延迟,用于调试和定时的 LED 开/关似乎没问题,无论如何,真正有所作为的指令如下:#pragma config LVP = OFF。我的代码上它是 ON 的,这是一个错误,我不明白为什么。
    猜你喜欢
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 2023-03-18
    • 2016-07-17
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多