3612 views|0 replies

1379

Posts

0

Resources
The OP
 

PIC series microcontroller programming basics II [Copy link]

4) Program that loops n times
  If you want to loop a program n times, you can use a register as a counter. In the following example, F10 is used as a counter to loop the program 8 times.
       COUNT EQU 10; define F10 as COUNT (counter)
          ┋
       MOVLW 8
       MOVWF COUNT LOOP; loop body
   LOOP
           ┋
       DECFSZ COUNT, 1; COUNT is reduced by 1, if the result is zero, jump to
       GOTO LOOP; if the result is not zero, continue looping
           ┋; if the result is zero, jump out of the loop
   5) Program in the format of "IF...THEN..."
  The following takes the format of "IF X=Y THEN GOTO NEXT" as an example.
       MOVF X, 0; X→W
       SUBWF Y, 0; Y—W (X)→W
       BTFSC STATUS, Z; X=Y No
       GOTO NEXT; X=Y, jump to NEXT to execute.
          ┋ ;X≠Y
   6) “FOR……NEXT” format program
  “FOR……NEXT” program makes the loop execute within a certain range. The following example is a program in the format of “FOR X=0 TO 5”. F10 puts the initial value of X, and F11 puts the final value of X.
      START EQU 10
      DAEND EQU 11
           ┋
      MOVLW 0
      MOVWF START ; 0→START(F10)
      MOVLW 5
      MOVWF DAEND ; 5→DAEND(F11)
   LOOP
           ┋
      INCF START,1 ; START value plus 1
      MOVF START,0
      SUBWF DAEND,0 ; START=DAEND ? (X=5 No)
      BTFSS STATUS, Z
      GOTO LOOP ;X<5, continue loop
           ┋ ;X=5, end loop
   7) "DO WHILE...END" format program
  "DO WHILE...END" program is to execute the loop when the condition is met. The following example is a "DO WHILE X=1" format program. F10 puts the value of X.
      X EQU 10
        ┋
      MOVLW 1
      MOVWF X ;1→X (F10), as the initial value
   LOOP
        ┋
      MOVLW 1
      SUBWF X, 0
      BTFSS STATUS, Z ;X=1 No?
      GOTO LOOP ;X=1 continue loop
        ┋ ;X≠1 jump out of loop
   8) PIC microcontroller table lookup program
  Table lookup is an operation often used in programs. The following example converts decimal 0~9 into 7-segment LED digital display values.

     Assuming the LEDs are common anodes, the line segment values corresponding to the numbers 0 to 9 are as follows:
Decimal
Segment Value
Decimal
Segment Value
0
C0H
5
92H
1
C9H
6
82H
2
A4H
7
F8H
3
B0H
8
80H
4
99H
9
90H

  
  The table lookup program of PIC microcontroller can be realized by using the feature of subroutine with value return. Specifically, in the main program, first get the table data address and put it into W, then call the subroutine. The first instruction of the subroutine puts W into PC, then the program jumps to the data address, and then the "RETLW" instruction puts the data into W and returns to the main program. The following program uses F10 to put the table header address.
      MOVLW TABLE ;Table header address→F10  
      MOVWF 10
          ┋
      MOVLW 1 ;1→W, prepare to get the line segment value of "1"
      ADDWF 10,1 ;F10+W = "1" data address
      CALL CONVERT
      MOVWF 6 ;Set the line segment value to port B, light up the LED
          ┋
  CONVERT MOVWF 2 ;W→PC TABLE
      RETLW 0C0H ;"0" line segment value
      RETLW 0F9H ;"1" line segment value
          ┋
      RETLW 90H ;"9" line segment value
   9) "READ...DATA, RESTORE" format program
  "READ...DATA" program reads one data in the data table each time, then adds 1 to the data pointer, and prepares to get the next data. In the following program, F10 is used as the starting address of the data table, and F11 is used as the data pointer.
      POINTER EQU 11 ;Define F11 as POINTER
          ┋
      MOVLW DATA
      MOVWF 10 ;Data table header address→F10
      CLRF POINTER ;Data pointer cleared
          ┋
      MOVF POINTER,0  
      ADDWF 10,0 ;W =F10+POINTER
          ┋
      INCF POINTER,1 ;Pointer plus 1
      CALL CONVERT ;Call subroutine, get table data
          ┋
  CONVERT MOVWF 2 ;Data address→PC
  DATA RETLW 20H ;Data
          ┋
      RETLW 15H ;Data
  If you want to execute "RESTORE", just execute one "CLRF POINTER".
  10)
PIC microcontroller delay program
  If the delay time is short, you can let the program simply execute several no-operation instructions "NOP" in succession. If the delay time is long, you can use a loop to implement it. The following example uses F10 to calculate and repeat the loop 100 times.
      MOVLW D'100'
      MOVWF 10
  LOOP DECFSZ 10, 1; F10—1→F10, if the result is zero, jump to
      GOTO LOOP
       ┋
  The delay time is the sum of the execution time of the instructions in the delay program. If a 4MHz oscillation is used, each instruction cycle is 1μS. Therefore, the single-cycle instruction time is 1μS, and the double-cycle instruction time is 2μS. In the above example, the LOOP loop delay time is: (1+2)*100+2=302 (μS). Inserting a no-operation instruction in the loop can extend the delay time:
      MOVLW D'100'
      MOVWF 10
  LOOP NOP
       NOP
       NOP
      DECFSZ 10, 1
      GOTO LOOP
        ┋
  Delay time = (1+1+1+1+2)*100+2=602 (μS).
  Using several loop nesting methods can greatly extend the delay time. The following example uses 2 loops to delay:
      MOVLW D'100'
      MOVWF 10
  LOOP MOVLW D'16'
      MOVWF 11
  LOOP1 DECFSZ 11,1
      GOTO LOOP1
      DECFSZ 10,1
      GOTO LOOP
       ┋Delay
  time = 1+1+[1+1+(1+2)*16-1+1+2]*100-1=5201(μS)
  11)
Use of PIC microcontroller RTCC counter   RTCC is a pulse counter. Its counting pulse has two sources, one is the external signal input from the RTCC pin, and the other is the internal instruction clock signal. The program can be used to select one of the signal sources as input. RTCC can be used by the program for timing; the program reads the RTCC register value to calculate the time. When RTCC is used as an internal timer, the RTCC pin must be connected to VDD or VSS to reduce interference and current consumption. The following program uses RTCC for delay:       RTCC EQU 1        ┋       CLRF RTCC; RTCC clears to 0       MOVLW 07H       OPTION; selects preset multiple 1: 256 → RTCC    LOOP MOVLW 255; RTCC count final value       SUBWF RTCC, 0








      BTFSS STATUS, Z; RTCC=255?
      GOTO LOOP
       ┋In
  this delay program, the RTCC register increases by 1 every 256 instruction cycles (division ratio = 1:256). Assuming the chip uses 4MHz oscillation, then:
  delay time = 256*256 = 65536 (μS)
  RTCC is self-oscillating. When it is counting, the program can do other things. It only needs to read it after a period of time to detect its count value.
  

This post is from Microchip MCU
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list