📜 ⬆️ ⬇️

Controlling the HD44780 LCD with an assembler

At the university, a booth based on the MK-51 microcontroller, HD44780 display, and keyboard began to be studied on a single subject. The whole thing is programmed through the COM port using an assembler. At that time I would study the AVR microcontrollers of the AVR family (namely, Atmega8), so there was a desire to learn how to initialize and display some information using the assembler without using libraries. After a long search, I found only how to program the display in C using libraries, in which it is not quite clear what is happening. Therefore, it was decided to write the code myself, using assembly commands. Display 0802, two lines.

First of all, it was necessary to assemble a device on which it was possible to practice, but since at that time I had a scarf from another “toy”, it was a sin not to use this board, since the MK harness is almost identical in both schemes. The original board layout is below:



Next, it was necessary to connect the display, we even have 2 unused outputs (PD0, PD1). Everything is programmed via JTAG (X1). Connect the display according to this scheme:

I decided to use 4-bit transfer mode, as I then thought that it would be easier to implement. The scheme had to be changed due to the absence of the LED backlight (there are no 15 and 16 displays on the display), it also removed the LED indicator that I did not need (D1, R2). I fed the whole circuit and the display from the battery from the cellular one.
')
I ended up with this:






The board was made using LUT (laser irons method)

It is time for the software part. I wrote the program on AVRstudio
Code

.include <m8def.inc> #define RS 2 //RS=PD2 #define E 3 //E=PD3 .def temp = r16 rjmp reset .org 40 reset: ldi r16, HIGH(RAMEND) ;  out SPH,r16 ldi r16,LOW(RAMEND) out SPL,r16 ldi r16,0xfc ;  PD2-PD7   out ddrd,r16 ldi r16,0x00 out portd,r16 rcall LCD_init sbi portb,6 ;  rcall LCD_dat rjmp loop LCD_init: ldi temp,0x30 ;  rcall LCD_com1 ;   ldi temp,0x30 rcall LCD_com1 ldi temp,0x30 rcall LCD_com1 ldi temp,0x20 ;4   rcall LCD_com1 ldi temp,0x20 rcall LCD_com ldi temp,0x0c rcall LCD_com ldi temp,0x06 rcall LCD_com ldi temp,0x01 rcall LCD_com ret LCD_dat: ldi ZL, LOW(DB << 1) ldi ZH, HIGH(DB << 1) ldi r21,8 ;    m1: lpm temp, Z+ rcall LCD_com dec r21 cpi r21,0 brne m1 ret LCD_com: ldi r24,(0<<RS)|(1<<E) sbic portb,6 ldi r24,(1<<RS)|(1<<E) out portD,r24 mov r23,temp andi temp,0b11110000 ;   or temp,r24 ;  E  RS out portD,temp ldi r24,(0<<RS)|(0<<E) out portD,r24 ; rcall func_delay ldi r24,(0<<RS)|(1<<E) sbic portb,6 ldi r24,(1<<RS)|(1<<E) out portD,r24 andi r23,0b00001111 ;   swap r23 mov temp,r23 or temp,r24 ;  E  RS out portD,temp ; ldi r24,(0<<RS)|(0<<E) out portD,r24 rcall func_delay ret LCD_com1: ldi r24,(0<<RS)|(1<<E) out portD,r24 or temp,r24 out portD,temp ldi r24,(0<<RS)|(0<<E) out portD,r24 rcall func_delay ret loop: rjmp loop func_delay: ; ldi r17,0x20 ldi r18,0x00 func_delay_subb: subi r18,1 sbci r17,0 brcc func_delay_subb ret DB: .db 0xA8,0x70,0x65,0xB3,0x65,0xE0,0x3A,0x29 ;  


I tried to write the program without departing from the algorithm given in the datasheet.
It was necessary to do the division into subroutines LCD_com1 and LCD_com, since it was not possible to send configuration commands and character codes through one subroutine.
If you need to output to the second line, you also need to add these lines:
  cbi portb,6 ;    ,   ldi temp,0xc2 rcall LCD_com sbi portb,6 ;  rcall LCD_dat 

However, for reasons I do not understand, when displaying information in a 2 line mode, the contrast is greatly reduced.

Character codes need to be taken from the table and put into an array, not forgetting to limit the screen size


In the future, it is planned to apply the skills in practice, a thermometer on the processor, reobas or somewhere else.

Project Source Code:
dl.dropbox.com/u/77527472/LCDasm.zip
It is connected to the button on the reset, when pressed, the contents of the display is updated
Datasheet on display
dl.dropbox.com/u/77527472/wh1602-datasheet.pdf
Datasheet on the microcontroller, there is also a system of commands on it
dl.dropbox.com/u/77527472/ATmega8 (L) .pdf

ps: post first, so I take all the criticism in my direction, I will try to take everything into account

Source: https://habr.com/ru/post/166735/


All Articles