PIC microcontroller A\D conversion experiment program code

Publisher:chinalisaLatest update time:2016-01-20 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Experimental purpose: Familiar with A/D conversion

;Software idea: Select RAO as the analog input channel;
        Convert 4 times continuously and then calculate the average value as the conversion result
        The final structure only takes the lower 8 bits
        The result is sent to the lower 3 bits of the digital tube for display
;Hardware requirements: DIP switch S14 is ON in the 2nd position and OFF in the 1st position
        DIP switch S6 is all ON, S5 is ON in the 4th-6th positions and OFF in the 1st-3rd positions
        To not affect the result, other DIP switches are set to OFF.
#INCLUDE      ;Include chip header file__CONFIG
_DEBUG_OFF&_CP_ALL&_WRT_HALF&_CPD_ON&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC
;Chip configuration word, watchdog off, power-on delay on, power-off detection off, low voltage programming off, encryption, 4M crystal HS oscillation
;************************Register definition************************
TEMP       EQU    20H      ;Temporary register
BAI        EQU    21H      ;Hundreds of conversion result
SHI        EQU    22H      ;Tens of conversion result
GE         EQU    23H      ;Units of conversion result
;****************************************************
 ORG       00H             ;Reset entry address
 NOP                       ;ICD required empty instruction
 GOTO      MAIN            ;Jump to main program entry
 ORG       04H             ;Interrupt entry address
 RETFIE                    ;Place an interrupt return instruction to prevent unexpected interrupts

;**************************Table lookup program************************
;Entry parameter: W
;Exit parameter: W
TABLE
 ADDWF       PCL,1         ;Instruction register plus offset address   
 RETLW       0C0H          ;0 encoding (public anode digital tube)                             
 RETLW       0F9H          ;1 encoding
 RETLW       0A4H          ;2 encoding
 RETLW       0B0H          ;3 encoding
 RETLW       99H           ;4 encoding
 RETLW       92H           ;5 encoding
 RETLW       082H          ;6
 RETLW       0F8H          ;7
 RETLW       080H          ;8
 RETLW       090H          ;9

;***********************Main program************************
MAIN
 MOVLW     30H
 MOVWF     FSR             ;Store the conversion result at the starting address          
LOOP
 BSF       STATUS,RP0      ;Select body 1
 MOVLW     7H              ;The high 3 bits of port A are output, and the low 3 bits are input
 MOVWF     TRISA
 CLRF      TRISD           ;Port D is set as output
 MOVLW     8EH
 MOVWF     ADCON1          ;Right-align the result, only select RA0 as the ADC port, and the rest as ordinary digital ports
 BCF       STATUS,RP0      ;Return to body 0
 MOVLW     41H
 MOVWF     ADCON0          ;Select the clock source as fosc/8, allow ADC to work
 CALL      DELAY           ;Call the delay program to ensure enough sampling time
 BSF       ADCON0,GO       ;Start ADC conversion
WAIT
 BTFSS     PIR1,ADIF       ;Is the conversion completed
 GOTO      WAIT            ;Wait for the completion of the conversion
 BSF       STATUS,RP0
 MOVFW     ADRESL          ;Read the conversion result
 BCF       STATUS,RP0
 MOVWF     INDF            ;Save to temporary register
 INCF      FSR,1
 BTFSS     FSR,2           ;Convert 4 times in a row and calculate the average value
 GOTO      LOOP
 CALL      CHANGE          ;Call result conversion program
 CALL      DISPLAY         ;Call display program
 GOTO      MAIN            ;Loop work

;************************Conversion program************************
;Entry parameters: 30H---33H
;Exit parameters: BAI, SHI, GE
CHANGE
 CLRF      2AH
 CLRF      BAI
 CLRF      SHI
 CLRF      GE              ;Clear the result register first
 MOVFW     31H             ;The following 8 instructions calculate the average value of 4 conversion results
 ADDWF     30H,1
 BTFSC     STATUS,C
 INCF      2AH
 MOVFW     32H
 ADDWF     30H,1
 BTFSC     STATUS,C
 INCF      2AH,1
 MOVFW     33H
 ADDWF     30H,1
 BTFSC     STATUS,C
 INCF      2AH,1

 RRF       2AH,1
 RRF       30H,1
 RRF       2AH,0
 RRF       30H,0           
 MOVWF     TEMP
 MOVLW     64H             ;Subtract 100, keep the result in W
 SUBWF     TEMP,0
 BTFSS     STATUS,C        ;Judge whether it is greater than 100
 GOTO      SHI_VAL         ;No, switch to the tens digit
 MOVWF     TEMP            ;Yes, send the difference back to TEMP
 INCF      BAI,1           ;Add 1 to the hundreds digit
 GOTO      $-6             ;Return and continue to find the value of the hundreds digit
SHI_VAL
 MOVLW     0AH             ;Subtract 10, keep the result in W
 SUBWF     TEMP,0          
 BTFSS     STATUS,C        ;Judge whether it is greater than 10
 GOTO      GE_VAL          ;No, switch to the ones digit
 MOVWF     TEMP            ;Yes, send the difference back to TEMP
 INCF      SHI,1           ;Add 1 to the tens digit
 GOTO      $-6             ; Move to find the value of the tens digit
GE_VAL
 MOVFW     TEMP
 MOVWF     GE              ; The value of the ones digit
 RETURN

;**************************Display program********************
;Entry parameters: BAI, SHI, GE
;Exit parameters: None
DISPLAY
 MOVFW    BAI              ;Display hundreds digit
 CALL     TABLE
 MOVWF    PORTD
 BCF      PORTA,3
 CALL     DELAY
 CALL     DELAY
 BSF      PORTA,3
 MOVFW    SHI              ;Display tens digit
 CALL     TABLE
 MOVWF    PORTD
 BCF      PORTA,4
 CALL     DELAY
 CALL     DELAY
 BSF      PORTA,4
 MOVFW    GE               ;Display units digit
 CALL     TABLE
 MOVWF    PORTD
 BCF      PORTA,5
 CALL     DELAY
 CALL     DELAY
 BSF      PORTA,5
 RETURN
 
;***************************Delay program***********************
;Entry parameters: None
;Exit parameters: None
DELAY
 MOVLW    5FH
 MOVWF    TEMP
 DECFSZ   TEMP,1
 GOTO     $-1

Reference address:PIC microcontroller A\D conversion experiment program code

Previous article:PIC microcontroller "marquee" experimental program code
Next article:PIC 8-bit microcontroller assembly language common instructions reading

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号