⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p18demo.asm

📁 pic系列单片机得控制程序 主要进行温度采集和转换控制
💻 ASM
📖 第 1 页 / 共 3 页
字号:
;************************************************************************
;*  Microchip Technology Inc. 2002                                      *
;*  Assembler version: 2.0000                                           *
;*  Filename:                                                           *
;*      p18demo.asm (main routine)                                      *
;*  Dependent on:                                                       *
;*      p18lcd.asm                                                      *
;*      p18math.asm                                                     *
;*      16f877.lkr                                                      *
;*  March 14,2002                                                       *
;*  PICDEM 2 PLUS DEMO code. The following functions are included       *
;*  with this code:                                                     *
;*      1. Voltmeter                                                    *
;*          The center tap of R16 is connected to RA0, the              *
;*          A/D converter converts this analog voltage and              *
;*          the result is displayed on the LCD in a range               *
;*          from 0.00V - 5.00V.                                         *
;*      2. Buzzer                                                       *
;*          The Piezo buzzer is connected to RC2 and is                 *
;*          driven by the CCP1 module. The period and duty              *
;*          cycle are adjustable on the fly through the LCD             *
;*          and push-buttons.                                           *
;*      3. Temperature                                                  *
;*          A TC74 Serial Digital Thermal Sensor is used to             *
;*          measure ambient temperature. The PIC and TC74               *
;*          communicate using the MSSP module. The TC74 is              *
;*          connected to the SDA & SCL I/O pins of the PIC              *
;*          and functions as a slave. Every 2 seconds, the              *
;*          temperature is logged into the external EEPROM              *
;*          in a specific memory location.                              *
;*      4. Clock                                                        *
;*          This function is a real-time clock. When the                *
;*          mode is entered, time begins at 00:00:00. The               *
;*          user can set the time if desired.                           *
;*                                                                      *
;*      The data that is sent to the LCD is also sent to the            *
;*      USART through the RS-232 port to be displayed on a PC           *
;*      HyperTerminal.                                                  *
;*                                                                      *
;************************************************************************

    list p=18f452
    #include p18f452.inc


;Program Configuration Registers
;       __CONFIG    _CONFIG1H, _OSCS_OFF_1H & _EC_OSC_1H
        __CONFIG    _CONFIG2L, _BOR_OFF_2L & _PWRT_ON_2L
;       __CONFIG    _CONFIG2H, _WDT_OFF_2H
;       __CONFIG    _CONFIG3H, _CCP2MX_OFF_3H
        __CONFIG    _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
        __CONFIG    _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L 
;       __CONFIG    _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
        __CONFIG    _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L 
;       __CONFIG    _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
        __CONFIG    _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
;       __CONFIG    _CONFIG7H, _EBTRB_OFF_7H


    #define scroll_dir  TRISA,4
    #define scroll      PORTA,4     ;Push-button RA4 on PCB
    #define select_dir  TRISB,0     
    #define select      PORTB,0     ;Push-button RB0 on PCB


    EXTERN  LCDInit, temp_wr, d_write, i_write, LCDLine_1, LCDLine_2
    EXTERN  UMUL0808L, UDIV1608L, AARGB0, AARGB1, BARGB0



ssprw   macro               ; check for idle SSP module routine
    movlw   0x00
    andwf   SSPCON2,W
    sublw   0x00
    btfss   STATUS,Z
    bra     $-8

    btfsc   SSPSTAT,R_W
    bra     $-2
    endm



variables   UDATA
ptr_pos     RES 1
ptr_count   RES 1
temp_1      RES 1
temp_2      RES 1
temp_3      RES 1
cmd_byte    RES 1
temperature RES 1
LSD         RES 1
MsD         RES 1
MSD         RES 1
seconds     RES 1
minutes     RES 1
hours       RES 1

NumH        RES 1
NumL        RES 1
TenK        RES 1
Thou        RES 1
Hund        RES 1
Tens        RES 1
Ones        RES 1



STARTUP CODE
    NOP
    goto    start
    NOP
    NOP
    NOP



PROG1   CODE

stan_table                          ; table for standard code
;           "XXXXXXXXXXXXXXXX"
;                                    ptr:
    data    "   Voltmeter    "      ;   0
    data    "     Buzzer     "      ;  16
    data    "  Temperature   "      ;  32
    data    "     Clock      "      ;  48
    data    "RA4=Next RB0=Now"      ;  64
    data    "   Microchip    "      ;  80
    data    " PICDEM 2 PLUS  "      ;  96
    data    "RA4=Set RB0=Menu"      ; 112
    data    "RA4= --> RBO= ++"      ; 128
    data    "   RB0 = Exit   "      ; 144
    data    "Volts =     "          ; 160
    data    "Prd.=128 DC=128 "      ; 176



start   
    call    LCDInit
    
    movlw   B'10100100'     ; initialize USART
    movwf   TXSTA           ; 8-bit, Async, High Speed
    movlw   .25
    movwf   SPBRG           ; 9.6kbaud @ 4MHz
    movlw   B'10010000'
    movwf   RCSTA

    bcf     TRISC,2         ; configure CCP1 module for buzzer
;   bcf     TRISC,6
    movlw   0x80
    movwf   PR2             ; initialize PWM period 
    movlw   0x80            ; initialize PWM duty cycle
    movwf   CCPR1L
    bcf     CCP1CON,CCP1X
    bcf     CCP1CON,CCP1Y
    
    movlw   0x05            ; postscale 1:1, prescaler 4, Timer2 ON
    movwf   T2CON
        
    bsf     TRISA,4         ; make switch RA4 an Input
    bsf     TRISB,0         ; make switch RB0 an Input



;**************** STANDARD CODE MENU SELECTION *****************
                            ; Introduction
    movlw   .80             ; send "Microchip" to LCD
    movwf   ptr_pos
    call    stan_char_1

    movlw   .96             ; send "PICDEM 2 PLUS" to LCD
    movwf   ptr_pos
    call    stan_char_2
    call    delay_1s        ; delay for display
    call    delay_1s        ; delay for display


menu
;------------------ VOLT MEASUREMENT  --------------------------
    btfss   scroll          ; wait for RA4 release
    goto    $-2     
    btfss   select          ; wait for RB0 release
    goto    $-2

    movlw   0x00            ; voltmeter
    movwf   ptr_pos
    call    stan_char_1

    movlw   .64             ; RA4=Next  RB0=Now
    movwf   ptr_pos
    call    stan_char_2

v_wait
    btfss   select          ; voltmeter measurement ??
    bra     voltmeter
    btfsc   scroll          ; next mode ??
    bra     v_wait          ; NO
    btfss   scroll          ; YES
    bra     $-2             ; wait for RA4 release


;------------------ BUZZER -------------------------------------
menu_buz
    btfss   select          ; wait for RB0 release
    bra     $-2         

    movlw   .16             ; buzzer
    movwf   ptr_pos
    call    stan_char_1

    movlw   .64             ; RA4=Next  RB0=Now
    movwf   ptr_pos
    call    stan_char_2
b_wait
    btfss   select          ; Buzzer sound ??
    goto    buzzer          ; YES
    btfsc   scroll          ; NO, next mode ??
    goto    b_wait          ; NO
    btfss   scroll          ; YES
    bra     $-2             ; wait for RA4 release


;----------------- TEMPERATURE MEASUREMENT ---------------------
menu_temp
    btfss   scroll          ; wait for RA4 release
    bra     $-2     

    movlw   .32             ; temperature
    movwf   ptr_pos
    call    stan_char_1

    movlw   .64             ; RA4=Next  RB0=Now
    movwf   ptr_pos
    call    stan_char_2
t_wait
    btfss   select          ; temperature measurement ??
    bra     temp            ; YES
    btfsc   scroll          ; NO, next mode ??
    bra     t_wait          ; NO
    btfss   scroll          ; YES
    bra     $-2             ; wait for release


;------------------ CLOCK TIME ---------------------------------
menu_clock
    btfss   select          ; wait for RB0 release
    bra     $-2     

    movlw   .48             ; clock
    movwf   ptr_pos
    call    stan_char_1

    movlw   .64             ; RA4=Next  RB0=Now
    movwf   ptr_pos
    call    stan_char_2

c_wait
    btfss   select          ; goto time ??
    bra     clock           ; YES
    btfsc   scroll          ; NO, next mode ??
    bra     c_wait          ; NO
    btfss   scroll          ; YES
    bra     $-2             ; wait for release


;---------------------------------------------------------------
    bra menu                ; begining of menu
    
    return

; end start ----------------------------------------------------

;*******************************************************************




;************* STANDARD USER CODE **********************************



;------------- Voltmeter--------------------------------------------
voltmeter
    btfss   select          ; wait for RB0 release
    bra     $-2

    movlw   B'01000001'     ; configure A/D converter   
    movwf   ADCON0          ; turn A/D on
    movlw   b'00001110'     ; RA0 = analog input
    movwf   ADCON1

    movlw   .160            ; send "Volts = " to the LCD
    movwf   ptr_pos
    call    stan_char_1

volts_again
    bsf     ADCON0,GO       ; start conversion
    btfsc   ADCON0,GO
    bra     $-2
    movf    ADRESH,W

    movwf   AARGB0          ; move adresh into AARGB1
    movlw   0xC3            ; 19.5mV/step   0xC3 = 195
    movwf   BARGB0
    call    UMUL0808L
    
    movlw   0x64            ; divide result by 100 (0x64)
    movwf   BARGB0
    call    UDIV1608L
    
    movf    AARGB0,W        ; prepare for 16-bit binary to BCD
    movwf   NumH
    movf    AARGB1,W
    movwf   NumL
    call    bin16_bcd       ; get volts ready for LCD
    
    call    LCDLine_2       ; display A/D result on 2nd line
    movf    Hund,W          ; get hunds
    call    bin_bcd
    movf    LSD,W           ; send high digit from the LSD #.xx 
    movwf   temp_wr
    call    d_write
    movlw   A'.'            ; send decimal point "."
    movwf   temp_wr
    call    d_write
    
    movf    Tens,W          ; get tens
    call    bin_bcd
    movf    LSD,W           ; send low digit   x.#x
    movwf   temp_wr
    call    d_write

    movf    Ones,W          ; get ones
    call    bin_bcd
    movf    LSD,W           ; send low digit   x.x#
    movwf   temp_wr
    call    d_write
    movlw   A'V'            ; send "V" unit
    movwf   temp_wr
    call    d_write

    movlw   0x20            ; 3 spaces  
    movwf   temp_wr
    call    d_write
    movlw   0x20            
    movwf   temp_wr
    call    d_write
    movlw   0x20            
    movwf   temp_wr
    call    d_write
    movlw   A'R'            ; send "RB0=Exit" to LCD
    movwf   temp_wr
    call    d_write
    movlw   A'B'            
    movwf   temp_wr
    call    d_write
    movlw   A'0'            
    movwf   temp_wr
    call    d_write
    movlw   A'='            
    movwf   temp_wr
    call    d_write
    movlw   A'E'            
    movwf   temp_wr
    call    d_write
    movlw   A'x'        
    movwf   temp_wr
    call    d_write
    movlw   A'i'            
    movwf   temp_wr
    call    d_write
    movlw   A't'            
    movwf   temp_wr
    call    d_write
    movlw   0x20            ; 2 spaces  
    movwf   temp_wr
    call    d_write
    movlw   0x20            
    movwf   temp_wr
    call    d_write

    movlw   "\r"            ; move data into TXREG

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -