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

📄 aaa.txt

📁 pic系列单片机得控制程序 主要进行温度采集和转换控制
💻 TXT
📖 第 1 页 / 共 3 页
字号:
 
; 控制加热系统
; 14.7.2002 
; 描述: 
; 计算机监测串行口,当它接受命令小包(79 字节 + 检查和)
;  , 它首先核对 检查和,如正确就发送,
; If checksum is ok and the command is recognized, it is executed 
; and the a response packet is sent terminating the transaction. 
; the commands with prefix Cp_ are processed in the CPU only. 
; Other commands access the desired peripheral connected to the CPU 
; through I2C-bus. 
; The second byte in the received command packet is an relative address 
; to a computed goto table with offsets. This table is place into the 
; program memory. 
; Watchdog feature is used to a) revive from a run-time code error and 
; b) perform a time-out function to the command packet receiving. 

        #include P16F876.inc 
        __CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _HS_OSC & _WRT_ENABLE_ON & _LVP_OFF & _DEBUG_OFF & _CPD 

#define FOSC             D'20000000'           ; 20MHz  
#define I2CClock         D'100000'             ; 100kHz 
#define ClockValue       (((FOSC/I2CClock)/4) -1); 

        errorlevel       -302 
;--------------------------------
w_temp           EQU      0x70              
status_temp      EQU      0x71             
looptemp         EQU      0x72             
temp             EQU      0x73 
                                      
checksum         EQU      0x74 
busaddress       EQU      0x75 
stateflags       EQU      0x76 
maxcount         EQU      0x77 

; command packet (80 bytes) is placed to 
; bank 2, 0x120 - 0x16F 
; response/error packet (80 bytes) is placed to 
; the same area, 0x120 - 0x16F 

command_packet    EQU        0x120 
response_packet   EQU        0x120    ; the same RAM area 
error_packet      EQU        0x120    ; the same RAM area 

; 标志定义
MEM_READ         EQU      0x00 
SHORT_ADDR       EQU      0x01 
CLK_READ         EQU      0x02 
TEMP_READ        EQU      0x03 
ADC_READ         EQU      0x04 
LCD_READ         EQU      0x05 
TO_OCCURED       EQU      0x07 

;=========================================
                ORG      0x000              
                clrf     PCLATH            
                goto     main            
                ORG      0x004             
                movwf    w_temp            
                movf     STATUS,w         
                movwf    status_temp       
                movf     status_temp,w      
                movwf    STATUS           
                swapf    w_temp,f 
                swapf    w_temp,w          
                retfie                     

;-------------- 补尝 ------------
Get_offset 
        banksel EEADR 
        movwf    EEADR 
        movlw    high Command_table 
        banksel EEADRH 
        movwf    EEADRH 
        banksel EECON1 
        bsf      EECON1, EEPGD    ; point to program memory 
        bsf      EECON1, RD       ; the required sequence 
        nop 
        nop 
        banksel EEDATA 
        movf     EEDATA, W 
        return 

;----------------------------------
Send_byte_RS 
        banksel PIR1 
        btfss    PIR1, TXIF 
        goto     $-1       
        movwf    TXREG 
        return 

;----------------------------------
Read_byte_RS  
        banksel PIR1 
        btfss    PIR1, RCIF 
        goto     $-1     
        movf     RCREG, W 
        return 

;-------------------------------------------------- 
I2C_Start 
        banksel SSPCON2 
        bsf      SSPCON2, SEN 
        btfsc    SSPCON2, SEN 
        goto     $-1              
        return 

;---------------------------------- 
I2C_Stop 
        banksel SSPCON2 
        bsf      SSPCON2, PEN 
        btfsc    SSPCON2, PEN 
        goto     $-1              
        return 

;----------------------------------
I2C_Write  
        banksel SSPBUF 
        movwf SSPBUF 
        banksel SSPSTAT 
        btfsc    SSPSTAT, R_W 
        goto     $-1            
        return 

;---------------------------------- 
I2C_Read 
        banksel SSPCON2 
        bsf      SSPCON2, RCEN 
        btfsc    SSPCON2, RCEN 
        goto     $-1             
        banksel SSPBUF 
        movf     SSPBUF, W 
        return 

;----------------------------------
I2C_Ack 
        banksel SSPCON2 
        bcf      SSPCON2, ACKDT 
        bsf      SSPCON2, ACKEN 
        btfsc    SSPCON2, ACKEN 
        goto     $-1              
        return 

;----------------------------------
I2C_Nack 
        banksel SSPCON2 
        bsf      SSPCON2, ACKDT 
        bsf      SSPCON2, ACKEN 
        btfsc    SSPCON2, ACKEN 
        goto     $-1              
        return 

;----------------------------------
Send_packet 
        clrf     checksum 
        movlw    D'79' 
        movwf    looptemp 
        movlw    low (response_packet) 
        movwf    FSR 
Packet_loop 
        movf     INDF, W 
        incf     FSR, F 
        addwf    checksum, F 
        call     Send_byte_RS 
        clrwdt 
        decfsz   looptemp, F 
        goto     Packet_loop 
        movf     checksum, W 
        movwf    INDF 
        call     Send_byte_RS 
        return 

;----------------------------------
Check_count_range  
        movlw    low (command_packet + D'4') 
        movwf    FSR 
        movf     INDF, W 
        btfsc    STATUS, Z 
        bsf      INDF, 0x00      
        subwf    maxcount, W 
        btfsc    STATUS, C 
        goto     Count_below_max 
        btfsc    STATUS, Z 
        goto     Count_below_max 
        movf     maxcount, W 
        movwf    INDF 
Count_below_max 
        return 

;----------------------------------
main 
        bcf      stateflags, TO_OCCURED 
        btfss    STATUS, NOT_TO 
        bsf      stateflags, TO_OCCURED 
        banksel OPTION_REG 
        bsf      OPTION_REG, PSA 
        bsf      OPTION_REG, PS2 ; WDT rate 1:16, timeout = 290ms 
        bcf      OPTION_REG, PS1 
        bcf      OPTION_REG, PS0 
        clrwdt 
        movlw    B'00001111' 
        banksel   TRISB 
        movwf    TRISB 
        banksel   PORTB 
        clrf     PORTB 
        movlw    B'11011111' 
        banksel   TRISC 
        movwf    TRISC 
        banksel   PORTC 
        bcf      PORTC, 0x05            
        banksel   TXSTA 
        bsf      TXSTA, BRGH      ; high speed 
        movlw    D'64' 
        banksel   SPBRG 
        movwf    SPBRG 
        banksel   TXSTA 
        bcf      TXSTA, SYNC 
        banksel   RCSTA 
        bsf      RCSTA, SPEN 
        banksel   TXSTA 
        bsf      TXSTA, TXEN 
        banksel   RCSTA 
        bsf      RCSTA, CREN       ; Enable I2C 

        movlw    B'00101000' 
        banksel   SSPCON 
        movwf    SSPCON 
        movlw    ClockValue 
        banksel   SSPADD 
        movwf    SSPADD 
        movlw    B'10000000' 
        banksel   SSPSTAT 
        movwf    SSPSTAT 
        banksel   PORTC 
        bsf      PORTC, 0x05 
        btfsc    stateflags, TO_OCCURED 
        goto     Send_timeout 
 
Serial_loop 
        banksel PIR1 
        clrwdt 
        btfss    PIR1, RCIF 
        goto     $-2      
        movf     RCREG, W 
        movwf    temp 
        bsf      STATUS, IRP      

        movlw    low command_packet 
        movwf    FSR 
        movf     temp, W 
        movwf    checksum 
        movwf    INDF 
        incf     FSR, F 
        movlw    D'78'    ; 79 chars left to read, read checksum separately 
        movwf    looptemp 

Command_packet_loop 
        call     Read_byte_RS 
        movwf    INDF 
        addwf    checksum, F 
        incf     FSR, F 
        decfsz   looptemp, F 
        goto     Command_packet_loop 
        call     Read_byte_RS            ; read checksum without adding it 
        movwf    INDF 
        clrwdt                           ; CLEAR WDT 

        subwf    checksum, W             ; check  checksum 
        btfss    STATUS, Z               ; skip if zero i.e. checksum ok 
        goto     Send_bad_checksum 
; check that it is a command packet (i.e. starting with 0x43) 
        movlw    low (command_packet) 
        movwf    FSR 
        movf     INDF, W 
        sublw    0x43 
        btfss    STATUS, Z 
        goto     Unused_command 
; the second byte contains the command 
        movlw    low (command_packet+D'1')        ; command position 
        movwf    FSR 
        movf     INDF, W 
        call     Get_offset 
; register W contains now the offset 
        goto     Jump_points 
        ORG      0x0100 
Jump_points 
        movwf    temp 
        movlw    high Offset_org 
        movwf    PCLATH 
        movf     temp, W 
        addwf    PCL, F   ; computed goto 
        ; the code for the commands is listed here 
        ; if the execution in long, the program 
        ; is redirected to the Service_ 
Offset_org 
;------------------------------------- 
Unused_command 
; Adjust the command packet: 
; first byte to 0x45, 79. to 0xF1 
        movlw    low (command_packet) 
        movwf    FSR 
        movlw    0x45 
        movwf    INDF 
        movlw    low (command_packet+D'78') 
        movwf    FSR 
        movlw    0xF1 
        movwf    INDF 
        call     Send_packet      ; it generates the checksum 
        goto     Serial_loop 
;------------------------------------- 
Memory_read 
        bsf      stateflags, MEM_READ     ; mark as read operation 
        goto     Service_Memory_access 
;------------------------------------- 
Memory_write 
        bcf      stateflags, MEM_READ     ; mark as write operation 
        goto     Service_Memory_access 
;------------------------------------- 
Clock_read 
        bsf      stateflags, CLK_READ 
        goto     Service_Clock_access 
;------------------------------------- 
Clock_write 
        bcf      stateflags, CLK_READ 
        goto     Service_Clock_access 
;------------------------------------- 
Cp_read_status 
        movlw    low (response_packet) 
        movwf    FSR 
        movlw    0x52 
        movwf    INDF 
        movlw    low (response_packet+D'5') 
        movwf    FSR 
        banksel PORTB 
        movf     PORTB, W 
        movwf    INDF 
        call     Send_packet 
        goto     Serial_loop 
;------------------------------------- 
Cp_write_status 
        movlw    low (command_packet+D'5') 
        movwf    FSR 

        movf     INDF, W 
        banksel PORTB 
        movwf    PORTB 
        movlw    low (response_packet) 
        movwf    FSR 
        movlw    0x52 
        movwf    INDF 
        call     Send_packet 
        goto     Serial_loop 
;------------------------------------- 
Cp_data_read 

⌨️ 快捷键说明

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