PIC Microcontroller for Beginners (Part 1)

Publisher:快乐阳光Latest update time:2017-12-07 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Reference address:PIC Microcontroller for Beginners (Part 1)

Previous article:读写PIC16F877单片机內部EEPROM的实例
Next article:PIC Microcontroller for Beginners (Part 3)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号