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

📄 p18demo.asm

📁 pic系列单片机得控制程序 主要进行温度采集和转换控制
💻 ASM
📖 第 1 页 / 共 3 页
字号:
    addwf   TBLPTRL,F
    clrf    WREG
    addwfc  TBLPTRH,F
    addwfc  TBLPTRU,F

stan_next_char_1
    tblrd   *+
    movff   TABLAT,temp_wr          
    call    d_write         ; send character to LCD

    decfsz  ptr_count,F     ; move pointer to next char
    bra     stan_next_char_1

    movlw   "\n"            ; move data into TXREG
    movwf   TXREG           ; next line
    btfss   TXSTA,TRMT      ; wait for data TX
    goto    $-2
    movlw   "\r"            ; move data into TXREG
    movwf   TXREG           ; carriage return
    btfss   TXSTA,TRMT      ; wait for data TX
    goto    $-2

    return

; end stan_char_1 ----------------------------------------------



;----Standard code, Place characters on line-2------------------

stan_char_2 
    call    LCDLine_2       ; move cursor to line 2 
    movlw   .16             ; 1-full line of LCD
    movwf   ptr_count
    movlw   UPPER stan_table
    movwf   TBLPTRU
    movlw   HIGH stan_table
    movwf   TBLPTRH
    movlw   LOW stan_table
    movwf   TBLPTRL
    movf    ptr_pos,W
    addwf   TBLPTRL,F
    clrf    WREG
    addwfc  TBLPTRH,F
    addwfc  TBLPTRU,F

stan_next_char_2
    tblrd   *+
    movff   TABLAT,temp_wr
    call    d_write         ; send character to LCD

    decfsz  ptr_count,F     ; move pointer to next char
    bra     stan_next_char_2

    movlw   "\n"            ; move data into TXREG
    movwf   TXREG           ; next line
    btfss   TXSTA,TRMT      ; wait for data TX
    goto    $-2
    movlw   "\r"            ; move data into TXREG
    movwf   TXREG           ; carriage return
    btfss   TXSTA,TRMT      ; wait for data TX
    goto    $-2

    return

; end stan_char_2 ----------------------------------------------



;------------------ 100ms Delay --------------------------------

delay_100ms
    movlw   0xFF
    movwf   temp_1
    movlw   0x83
    movwf   temp_2

d100l1
    decfsz  temp_1,F
    bra     d100l1
    decfsz  temp_2,F
    bra     d100l1

    return

; end delay_100ms ----------------------------------------------



;---------------- 1s Delay -------------------------------------

delay_1s
    movlw   0xFF
    movwf   temp_1
    movwf   temp_2
    movlw   0x05
    movwf   temp_3

d1l1
    decfsz  temp_1,F
    bra     d1l1
    decfsz  temp_2,F
    bra     d1l1
    decfsz  temp_3,F
    bra     d1l1

    return  

; end delay_1s -------------------------------------------------



;---------------- Set Current Time ----------------------------

set_time
    movlw   .128            ; send "RA4= --> RBO= ++" to LCD
    movwf   ptr_pos
    call    stan_char_2

set_time_again
    btfss   scroll          ; wait for button release
    bra     $-2

    call    LCDLine_1       ; start at 0x00 on LCD

    btfss   select          ; wait for RB0 button release
    bra     $-2
    call    delay_100ms         
    btfss   select          ; increment hours (tens) ?
    bra     inc_hours
    bra     next_digit

inc_hours   
    incf    hours
    movf    hours,W         ; check if hours has passed 12 ?
    sublw   .13
    btfss   STATUS,Z
    bra     next_digit
    clrf    hours           ; YES, reset hours to 00

next_digit
    btfss   scroll          ; move to next digit
    bra     inc_mins
    movf    hours,W     

    call    bin_bcd         ; get hours ready for display
    
    movf    MsD,W           ; send tens digit
    movwf   temp_wr
    call    d_write
    movf    LSD,W           ; send ones digit
    movwf   temp_wr
    call    d_write 
    movlw   0x3A            ; send   :   colon
    movwf   temp_wr
    call    d_write

    bra set_time_again
    
inc_mins
    btfss   scroll          ; wait for RA4 button release
    bra     $-2
    call    LCDLine_1
    movlw   0x14            ; shift cursor to right 3 places
    movwf   temp_wr
    call    i_write
    movlw   0x14
    movwf   temp_wr
    call    i_write
    movlw   0x14
    movwf   temp_wr
    call    i_write
    
    btfss   select          ; wait for RB0 button release
    bra     $-2
    call    delay_100ms
    btfss   select          ; increment minutes (tens) ?
    bra     inc_minutes
    bra     next_digit?

inc_minutes 
    incf    minutes
    movf    minutes,W       ; check if hours has passed 12 ?
    sublw   .60
    btfss   STATUS,Z
    bra     next_digit?
    clrf    minutes

next_digit?
    btfss   scroll          ; move to next digit
    bra     set_time_done
    movf    minutes,W
        
    call    bin_bcd         ; get minutes ready for display
    
    movf    MsD,W           ; send tens digit
    movwf   temp_wr
    call    d_write
    movf    LSD,W           ; send ones digit
    movwf   temp_wr
    call    d_write 
    movlw   0x3A            ; send  :   colon
    movwf   temp_wr
    call    d_write 
    bra     inc_mins

set_time_done
    btfss   scroll          ; wait for RA4 button release
    bra     $-2
    bra     overflow


    
;---------------- Binary (8-bit) to BCD -----------------------
;       255 = highest possible result

bin_bcd
    clrf    MSD
    clrf    MsD
    movwf   LSD         ; move value to LSD

ghundreth   
    movlw   .100        ; subtract 100 from LSD
    subwf   LSD,W
    btfss   STATUS,C    ; is value greater then 100
    bra     gtenth      ; NO goto tenths
    movwf   LSD         ; YES, move subtraction result into LSD
    incf    MSD,F       ; increment hundreths
    bra     ghundreth   

gtenth
    movlw   .10         ; take care of tenths
    subwf   LSD,W
    btfss   STATUS,C
    bra     over        ; finished conversion
    movwf   LSD
    incf    MsD,F       ; increment tenths position
    bra     gtenth

over                    ; 0 - 9, high nibble = 3 for LCD
    movf    MSD,W       ; get BCD values ready for LCD display
    xorlw   0x30        ; convert to LCD digit
    movwf   MSD
    movf    MsD,W
    xorlw   0x30        ; convert to LCD digit
    movwf   MsD
    movf    LSD,W
    xorlw   0x30        ; convert to LCD digit
    movwf   LSD

    retlw   0

; end bin_bcd -------------------------------------------------- 



;---------------- Binary (16-bit) to BCD -----------------------
;       xxx = highest possible result
; Takes number in NumH:NumL 
; Returns decimal in 
; TenK:Thou:Hund:Tens:Ones 

bin16_bcd
        swapf   NumH,W 
        andlw   0x0F
        addlw   0xF0
        movwf   Thou 
        addwf   Thou,F 
        addlw   0xE2 
        movwf   Hund 
        addlw   0x32 
        movwf   Ones 

        movf    NumH,W 
        andlw   0x0F 
        addwf   Hund,F 
        addwf   Hund,F 
        addwf   Ones,F 
        addlw   0xE9 
        movwf   Tens 
        addwf   Tens,F 
        addwf   Tens,F 

        swapf   NumL,W 
        andlw   0x0F 
        addwf   Tens,F 
        addwf   Ones,F 

        rlcf    Tens,F 
        rlcf    Ones,F 
        comf    Ones,F 
        rlcf    Ones,F 

        movf    NumL,W 
        andlw   0x0F 
        addwf   Ones,F 
        rlcf    Thou,F 

        movlw   0x07 
        movwf   TenK 

        movlw   0x0A                             ; Ten 

Lb1: 
        decf    Tens,F 
        addwf   Ones,F 
        btfss   STATUS,C 
        bra     Lb1 

Lb2: 
        decf    Hund,F 
        addwf   Tens,F 
        btfss   STATUS,C 
        bra     Lb2 

Lb3: 
        decf    Thou,F 
        addwf   Hund,F 
        btfss   STATUS,C
        bra     Lb3 

Lb4: 
        decf    TenK,F 
        addwf   Thou,F 
        btfss   STATUS,C 
        bra     Lb4 

        retlw   0

; end bin16_bcd ------------------------------------------------




;---------------------------- WRITE EEPROM ---------------------
write_eeprom
    
    bsf     SSPCON2,SEN     ; start bit
    btfsc   SSPCON2,SEN
    goto    $-2 
    movlw   B'10100000'     ; send control byte (write)
    movwf   SSPBUF
    ssprw
    btfsc   SSPCON2,ACKSTAT ; ack?
    goto    $-2

    movlw   0x00            ; send slave address HIGH byte
    movwf   SSPBUF
    ssprw
    btfsc   SSPCON2,ACKSTAT ; ack?
    goto    $-2

    movlw   0x05            ; send slave address LOW byte(0x0005)
    movwf   SSPBUF
    ssprw
    btfsc   SSPCON2,ACKSTAT ; ack?
    goto    $-2

    movf    temperature,w   ; send slave DATA = temperature
    movwf   SSPBUF
    ssprw
    btfsc   SSPCON2,ACKSTAT ; ack?
    goto    $-2

    bsf     SSPCON2,PEN     ; stop bit
    btfsc   SSPCON2,PEN
    goto    $-2
        
    bcf     PIR1,TMR1IF     ; clear TIMER1 overflow flag
    clrf    TMR1L           ; clear registers for next overflow
    clrf    TMR1H

    return

; end write_eeprom ---------------------------------------------



;*********************************************************************
    end 

⌨️ 快捷键说明

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