无法使LCD1602显示器与ATmega2560配合使用

时间:2018-12-02 21:20:12

标签: assembly arduino avr atmega lcd

我一直试图将LCD1602显示器连接到我的ATmega2560,但是我无法使其工作。下面的代码应该在LCD上显示“ Hi”,但仅打开它就足够了。

主要功能:

.INCLUDE "m2560def.inc"

.EQU    LCD_DPRT = PORTA;
.EQU    LCD_DDDR = DDRA;
.EQU    LCD_DPIN = PINA;
.EQU    LCD_CPRT = PORTB;
.EQU    LCD_CDDR = DDRB;
.EQU    LCD_CPIN = PINB;
.EQU    LCD_RS = 0;
.EQU    LCD_RW = 1;
.EQU    LCD_EN = 2;  

    LDI R21, HIGH(RAMEND)
    OUT SPH,R21
    LDI R21, LOW(RAMEND)
    OUT SPL,R21; set up stack

    LDI R21, 0xFF;
    OUT LCD_DDDR, R21; make LCD data port output
    OUT LCD_CDDR, R21; make LCD command port output;

    CBI LCD_CPRT, LCD_EN; LCD_EN = 0;

    CALL DELAY_2ms; wait for power on

    LDI R16, 0x38; start initializing the matrix
    CALL CMNDWRT; write to the command registers
    CALL DELAY_2ms; wait 2ms

    LDI R16, 0x0E;
    CALL CMNDWRT; 

    LDI R16, 0x01;
    CALL CMNDWRT;

    CALL DELAY_2ms;

    LDI R16, 0x06; shift cursor right
    CALL CMNDWRT;

    LDI R16, 'H';
    CALL DATAWRT;

    LDI R16, 'i';
    CALL DATAWRT;

HERE:
    JMP HERE; stay here

;---------------------------------------------------------------------------------------------------------------------

这是用于编写命令的功能

CMNDWRT:
    OUT LCD_DPRT, R16; send R16 to Data Port
    CBI LCD_CPRT, LCD_RS; make RS pin 0, to enable command sending
    CBI LCD_CPRT, LCD_RW; make pin RW 0,  to enable writing
    SBI LCD_CPRT, LCD_EN; make pin E 1, start of high to low signal
    CALL SDELAY; make a wide pulse
    CBI LCD_CPRT, LCD_EN; clear E bit
    CALL DELAY_100us; wait for the LCD to run the command;
    RET

;---------------------------------------------------------------------------------------------------------------------

这是用于写入数据的功能

DATAWRT:
    OUT LCD_DPRT, R16; send R16 to data port
    SBI LCD_CPRT, LCD_RS; make RS pin 1, to enable data sending
    CBI LCD_CPRT, LCD_RW; make pin RW 0,  to enable writing
    SBI LCD_CPRT, LCD_EN; make pin E 1, start of high to low signal
    CALL SDELAY; make a wide pulse
    CBI LCD_CPRT, LCD_EN; EN = 0, for H-t-L Pulse
    CALL DELAY_100us
    RET

;---------------------------------------------------------------------------------------------------------------------

这些是我正在使用的延迟功能

SDELAY:
    NOP
    NOP
    RET

;---------------------------------------------------------------------------------------------------------------------

DELAY_100us:
    PUSH R17;
    LDI R17, 228; 
AGAIN:
    NOP
    NOP
    NOP
    NOP
    DEC R17
    BRNE AGAIN
    RET

;----------------------------------------------------------------------------------------------------------------------

DELAY_2ms:
    PUSH R17
    LDI R17,20; load the counter
LDR0:
    CALL DELAY_100us
    DEC R17
    BRNE LDR0
    POP R17
    RET

1 个答案:

答案 0 :(得分:0)

开机后的时间可能太快。在HD44780 datasheet之后,至少需要经过15毫秒,才能向显示控制器发出第一条命令。