我无法使用PIC18F4620初始化QAPASS 1602A LCD,我总是在两条线上都得到正方形块。一开始,我以为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 */
您的问题没有足够的详细信息来确定答案。
最佳猜测是您的LogiFind PIC-40-MINI板之间存在接线错误:和QAPASS 1602A LCD模块:有关它们如何连接的照片会很有帮助。
如果您确信接线正确,则LCD_Init()函数可能存在问题,有关在8位并行模式或未发布的代码中初始化模块的流程图,请参见HD44780 data sheet第45页。
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 */