PIC series microcontroller programming basics, applications and principles (Part 2)

Publisher:YudieLatest update time:2017-12-03 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  The following example writes data to the 30H and 50H registers of BANK1 and BANK2.
  Example 1. (Assume that the current bank is BANK0)
      BSF 4, 5; set bit5 = 1, select BANK1
      MOVLW DATA
      MOVWF 10H; DATA → 30H
      BCF 4, 5
      BSF 4, 6; bit6 = 1, bit5 = 0 select BANK2
      MOVWF 10H; DATA → 50H
  From the above example, we can see that to read and write registers in a bank (BANK), we must first operate the bank addressing bit in F4. In actual applications, after power-on reset, we usually clear bit6 and bit5 of F4 to 0 first, so that it points to BANK0, and then point to the corresponding bank as needed.
  Note that in the example, when writing data to the 30H register (BANK1) and the 50H register (BANK2), the register address in the instruction "MOVWF 10H" is "10H", not "MOVWF 30H" and "MOVWF 50H" as expected by the reader. Why?
  Let's review the instruction table. In the instruction codes of all registers related to the PIC16C5X microcontroller, the register addressing bits only occupy 5 bits: fffff, and can only address 32 (00H-1FH) registers. Therefore, to address 80 registers, the two-bit body addressing bits PA1 and PA0 are used. When we set the body addressing bits PA1 and PA0 to point to a BANK, the instruction "MOVWF 10H" will place the content of W into the corresponding register in this BANK (10H, 30H, 50H, or 70H).
  Some designers are in contact with the concept of bank address selection for the first time, and inevitably have different understandings. The following is an example:
  Example 2: (assuming that the current bank address selection is BANK0)
      MOVLW 55H 
      MOVWF 30H; to change the register from 55H to 30H
      MOVLW 66H
      MOVWF 50H; to change the register from 66H to 50H
  It is wrong to think that "MOVWF 30H" can definitely put W into 30H, and "MOVWF 50H" can definitely put W into 50H. Because the actual effect of these two instructions is "MOVWF 10H", the reason has been explained above. So the final result of this program in Example 2 is F10H=66H, and the real F30H and F50H are not operated.
  Suggestion: In order to make the bank address selection program clear and concise, it is recommended to use more name definition symbols to write the program, so that it is not easy to confuse. Example 3: Assume that the several registers of BANK0, BANK1, and BANK2 are used in the program as follows:



       B EQU 10H ;BANK1
       C EQU 10H ;BANK2
          ┋
       FSR EQU 4
       Bit6 EQU 6
       Bit5 EQU 5
       DATA EQU 55H
          ┋
       MOVLW DATA
       MOVWF A  
       BSF FSR,Bit5
       MOVWF B ;DATA→F30H
       BCF FSR,Bit5
       BSF FSR,Bit6
       MOVWF C ;DATA →F50H
          ┋



  The following introduces PIC16C5X microcontroller (1) "GOTO" cross-page
  Example: Suppose the current program is on page 0 (PAGE0), and you want to use "GOTO" to jump to a certain
KEY (PAGE1) on page 1.
       STATUS EQU 3
       PA1 EQU 6
       PA0 EQU 5
           ┋
       BSF STATUS, PA0; PA0=1, select PAGE page
       GOTO KEY; jump to the KEY of page 1
           ┋
       KEY NOP; program on page 1
           ┋
  (2) PIC microcontroller "CALL" cross-page
  Example: Suppose the current program is on page 0 (PAGE0), and now you want to call the subroutine DELAY placed on page 1 (PAGE1).
           ┋
       BSF STATUS, PA0; PA0=1, select PAGE1 page
       CALL DELAY; cross-page call
       BCF STATUS, PA0; restore page 0 address
           ┋
       DELAY NOP; subroutine of page 1
           ┋
  Note: the program sets the page address for cross-page CALL. After returning from the subroutine, the original page address must be restored.
  (3) Writing cross-page jumps and calls in PIC microcontroller programs
  When the reader sees this, he must ask: When I write the source program (.ASM), I don’t pay attention to the storage address of each instruction. How do I know that this GOTO is going to cross pages, and that CALL is going to cross pages? Indeed, when you start writing the source program, you don’t know when a cross-page jump or call will occur, but when you assemble the source program, it will be automatically given. When the assembly result shows:
       XXX (address) "GOTO out of Range"
       XXX (address) "CALL out of Range",
  this indicates that your program has cross-page jumps and calls, and the corresponding page addresses have not been set before these cross-page GOTO and CALL in your program. At this time, you should check the .LST file generated by the assembly, find these GOTO and CALL, and check which page the address they are going to jump to is on, and then return to the source program (.ASM) to make necessary changes. Continue until your source program passes the assembly (0 Errors and Warnnings).
  (4) Connection of PIC microcontroller program pages
  Some processing should be done at the connection of the 4 program pages. It is generally recommended to use the following format: that is, after entering another page, immediately set the corresponding page address bit (PA1, PA0). Page processing is the most troublesome part of PIC16C5X microcontroller programming, but it is not difficult. As long as you do a practical programming exercise, you will be able to master it.


Reference address:PIC series microcontroller programming basics, applications and principles (Part 2)

Previous article:PIC series microcontroller programming basic analysis and application
Next article:Section 12: PIC series microcontroller system definition words (Configuration)

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号