Learning PIC microcontroller for beginners (Part 1): The pain and joy of writing driver for TS1620 character LCD module
I have been studying PIC16F87X series microcontrollers for half a month, and I have encountered many difficulties. Fortunately, there are some related books in the library of my unit, and I have purchased a third-party ICD, so my study is quite fulfilling.
However, I can't find like-minded people in the unit to study it together, but there are many enthusiastic friends here.
In the future, I plan to write down some of my learning experiences and questions, and make progress together with beginners like me in the forum, and I earnestly hope to get guidance from predecessors. The
first program I wrote was to control the display of three-digit eight-segment digital tubes , and I haven't had time to summarize it yet. This afternoon, I debugged and summarized the LCD driver. I encountered many difficulties, but I also gained some small gains. I'll put it out to air. Everyone can throw bricks at me. I'll drive a truck to buy them...
The writing of the driver program for TS1620 character LCD module is bitter and happy.
The controller of this LCM is HD47780, and its pin functions are as follows:
PIN 1 2 3 4 5 6 7~17 15 16
Function GND VDD VO RS RW E DB0~7 BL+ BL-
Description:
RS: Data/command selection, used to select whether DB0~7 input is display data or control word information, (H/L)
RW: Read/write selection, (H/L), when the delay time is sufficient, the LCM busy signal is generally not read and is often grounded.
E: Enable terminal, controls the writing or reading of data/commands of DB0~7, rising or falling edge is effective.
VO: LCD bias signal, used to adjust the contrast , generally connected to 10K potentiometer/ resistor to ground.
DB0~7: Data I/O, 8-bit data or 4-bit data (transmitted twice).
BL+: Backlight power positive input +5V DV.
BL-: Backlight power ground.
VDD: Power positive +5V DC.
GND: Power ground.
Write driver:
Process:
LCM automatically resets when powered on - clear screen - set display mode - open display and cursor setting - set the starting position of display - display character ASCII code input display.
Notes:
1. LCM is a slow display device, and its reset time after power-on is relatively long. Therefore, LCM can be initialized last during system initialization; or a 20~50ms delay subroutine can be called before initialization. Furthermore, if the system does not need to wait for LCM to complete each operation through delay, then the LCM busy flag should be detected (read). If it is not busy (the flag is low level ), then the instruction can be executed, otherwise the instruction will fail.
2. Since there is controversy over whether the enable of the E input is valid on the rising edge or the falling edge, this problem will be encountered during debugging. I have not had time to analyze it yet, and I am willing to analyze it with you.
3. Sometimes the results displayed by LCM do not follow our original intention. At this time, we need to first check whether the initialization function in the program is completed, especially in the initialization program that does not detect the busy flag. We can appropriately extend the delay time or repeat the initialization instruction. In addition, we should also check whether the hardware connection is wrong.
4. If the port is connected to the pin of LCM, we need to pay attention to whether the port is a normal digital port. For example, in this example, the RA port should be set to a normal digital port first. At first, I didn't set it this way. As a result, I always found that no changes could be found in the SFR observation window when operating the RA port in the software simulation mode...
5. Since the LCM module in the demo board circuit diagram I used is controlled by RA and RC, in order to observe the operation results, RA and RC are still used in the program to control LCM.
Here is a routine, which is passed on the simulator and demo board used by me, and the debugging phenomenon is also given:
1) Regarding the problem of enabling E, if the delay in the middle of the reset and set operation of the E input pin of LCM is too short or too long, some problems will occur:
Too short: 5ms, normal operation, but after reset in debug, some characters will remain
Very short: 3 nop instructions, no output after running
Longer: 200ms, normal operation, but after debug reset, all characters remain
Average: 50ms, normal display, no characters remain after reset
Can seniors analyze it?
2) Since the display mode used is 16 characters x 2 lines, it is found that if the number of characters defined by the pseudo instruction dt in the character table _table1 or _table2 is less than 16, then some other characters will appear at the end of the line of characters. Here, "||" appears, and then spaces are used to fill in 16 characters. The specific reason is willing to analyze with everyone.
3) The program uses ICD mode for debugging and burning. After running normally, I plan to run it offline, that is, without burning the debugging code, but the burning process always stops at the programming ID. If the ID burning is cancelled, the burning process stops at the programming program, and the ICD prompts a connection error. It is necessary to change the ICD mode to SIM mode again. At this time, it is found that the connection indicator of the ICD is no longer flashing, and the connection is normal.
(Maybe the problem description is not accurate enough. I will reproduce the problem and explain it in detail next time...)
4) Another interesting problem is that there should be no movfw instruction in the instruction system of the PIC16 series. However, in mplab, it is found that instructions such as movfw value; (value is a custom register variable) can be compiled, and the function seems to be equivalent to movf value. I am puzzled (I am using mplab 5.7full version, and the third-party ICD can be used directly in this version...
Please give me some advice. It would be even better if you can talk about the path you have taken in learning PIC microcontrollers over the years:)
;********************************************************
list p=16f877
#include
;Define the register
w_temp for protecting the scene EQU 0x71
status_temp EQU 0x72
pclath_temp EQU 0x73
;Define the register
count for the main program EQU 0x74; Define the count register address
tmp1 EQU 0x75 ; define temporary register address
x EQU 0x76 ; delay subroutine outer loop counter
y EQU 0x77 ; delay subroutine inner loop counter
; define LCM control bit constant
RS EQU 1 ; LCD register select signal pin is defined at RA.1 pin
RW EQU 2 ; LCD read/write signal pin is defined at RA.2 pin
E EQU 3 ; LCD chip select signal pin is defined at RA.3 pin
;***The program commented in this paragraph is the template contentorg
0x000 ; processor reset vector
nop ; nop required for icd
goto main ; go to beginning of program
org 0x004 ; interrupt vector LOC ation
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
movf PCLATH,w ; move pclath register into w register
movwf pclath_temp ; save off contents of PCLATH register
; isr code CAN go here or be located as a call subroutine elsewhere
movf pclath_temp,w ; retrieve copy of PCLATH register
movwf PCLATH ; restore pre-isr PCLATH register contents
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w _temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
;****************************
main
bsf STATUS,RP0
movlw 07H
movwf ADCON1 ;Set all RA ports as ordinary digital IO ports
clrf TR ISA
clrf TRISC ; Define RA ports, all RC ports as output
bcf STATUS,RP0
call _delay ;When calling the delay, the LCD reset may not be as fast as PIC when it is powered on
movlw 01H
movwf PORTC ; Clear the screen
call _enable
movlw 38H
movwf PORTC ; 8-bit data, 16 words x 2 lines, 5x7 dot matrix
call _enable
movlw 0CH ; Display is on, cursor does not flash
movwf PORTC
call _enable
movlw 06H ; Text does not move, cursor automatically moves right
movwf PORTC
call _enable
movlw 80H
movwf PORTC ; First line display position
call _enable
call _write1 ; Call the first line number subroutine " www.21ic.com "
movlw 0C0H
movwf PORTC ; The second line position
call _enable
call _write2 ; Call the second line number subroutine "best wish to you"
goto $
;***********************
_write1
clrf count ;send the first line of digital program
again1
movf count,W
call _table1
movwf tmp1
call _write
incf count
movf tmp1,W
xorlw 00H
btfss STATUS,Z
goto again1
retlw 0
;****************************
_write2 ;send the second line of digital subroutine
clrf count
again2
movf count,W
call _table2
movwf tmp1
call _write
incf count
movf tmp1,W
xorlw 00H
btfss STATUS,Z
goto again2
retlw 0
;******************************
_write ;send data to LCD subroutine
movwf PORTC
bsf PORTA,RS
bcf PORTA,RW
bcf PORTA,E
call _delay
bsf PORTA,E
retlw 0
;Write control command subroutine_enable
bcf
PORTA,RS
bcf PORTA,RW
bcf PORTA,E
call _delay
bsf PORTA,E
retlw 0
;******************************************************
_table1 ;Get the display code of the first line www.21ic.com
addwf PCL ;Address offset plus current PC value
dt " www.21ic.com "
retlw 00H
;-------------------- ----------------------------------
_table2 ;Get the display code of the first line best wish to you
addwf PCL ;Address offset plus current PC value
dt "best wish to you"
retlw 00H
;************************************************************
;Delay subroutine_delay
movlw
0x3c ; Crystal oscillator is 4 MHz , delay 50ms
movwf x
loop_x
movlw 0xff
movwf y
loop_y
decfsz y
goto loop_y
decfsz x
goto loop_x
return
end ; End of source program
Previous article:读写PIC16F877单片机內部EEPROM的实例
Next article:PIC Microcontroller for Beginners (Part 3)
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Implementation and understanding of Ethernet communication principles through FPGA
- The meaning and measurement method of noise
- The experience and IQ of the elderly are not proportional
- Summary of filter reliability test items, a must for designers!
- [GD32E231 DIY Contest] Part 3: Submit videos and documents
- Please make ST's hal library more rigorous!!!
- FeatherSnow can run CircuitPython or Arduino
- Passive high-pass filter circuit has no output
- Will there be any impact if the amplifier circuit is not zeroed? @【Analog Electronics】
- What is the difference between automotive electronics and other electronics?