專注差異化嵌入式產(chǎn)品解決方案 給智能產(chǎn)品定制注入靈魂給予生命
提供開(kāi)發(fā)工具、應(yīng)用測(cè)試 完善的開(kāi)發(fā)代碼案例庫(kù)分享
從全面的產(chǎn)品導(dǎo)入到強(qiáng)大技術(shù)支援服務(wù) 全程貼心伴隨服務(wù),創(chuàng)造無(wú)限潛能!
提供新的芯片及解決方案,提升客戶產(chǎn)品競(jìng)爭(zhēng)力
提供最新的單片機(jī)資訊,行業(yè)消息以及公司新聞動(dòng)態(tài)
十年專注單片機(jī)方案開(kāi)發(fā)的方案公司英銳恩,分享采集11路AD\2路頻率由串口送PC。英銳恩現(xiàn)提供服務(wù)產(chǎn)品涉及主控芯片:8位單片機(jī)、16位單片機(jī)、32位單片機(jī)及各類運(yùn)算放大器等。
877A只有8路AD通過(guò)外加CD4053完成13路.以下為程序,多謝大家指教!
;************************************************************************************* ;波特率設(shè)為9600,數(shù)據(jù)位8位, ;停止位1位,無(wú)校驗(yàn)位,數(shù)組15組.第一個(gè)數(shù)組為0X55最后一個(gè)為0XAA,PC接收時(shí)校驗(yàn)用.
;DATE:2005-07-01 ;VERSION:1.0 ;************************************************************************************** LIST P=16F877 INCLUDE "P16F877.INC"
TXSTART EQU 20 ;累加校驗(yàn)高位 AD0 EQU 21 ;采樣發(fā)送模擬數(shù)組 AD1 EQU 22 AD2 EQU 23 AD3 EQU 24 AD4 EQU 25 AD5 EQU 26 AD6 EQU 27 AD7 EQU 28 AD8 EQU 29 AD9 EQU 2A AD10 EQU 2B CK0 EQU 2C ;采樣發(fā)送頻率數(shù)組 CK1 EQU 2D TXEND EQU 2E ;累加校驗(yàn)低位
COUNTER EQU 2F ;ADC計(jì)數(shù)器 COUNT1 EQU 30 ;定時(shí)計(jì)數(shù)器1 COUNT2 EQU 31 ;定時(shí)計(jì)數(shù)器2 ADBUFF1 EQU 32 ;AD采樣緩沖數(shù)組 ADBUFF2 EQU 33 ADBUFF3 EQU 34 ADBUFF4 EQU 35 ADBUFF5 EQU 36 ADBUFF6 EQU 37 TXPTR EQU 38 ;發(fā)送指針 TXCOUNT EQU 39 ;發(fā)送計(jì)數(shù)器 T2COUNT EQU 3A ;T2計(jì)數(shù)器 W_TEMP EQU 70H ;備份寄存器 S_TEMP EQU 71H F_TEMP EQU 72H
;********************************************************* ORG 0X00 GOTO MAIN
;********************************************************* ORG 0X04 GOTO INT ORG 0X05
;*********************************************************************************** INT ;*********************************************************************************** MOVWF W_TEMP SWAPF STATUS,W CLRF STATUS MOVWF S_TEMP MOVFW FSR MOVWF F_TEMP
CHKTXINT BTFSC PIR1,TMR2IF GOTO T2 CHTX BANKSEL PIE1 BTFSS PIE1,TXIE GOTO ENDINT CLRF STATUS BTFSS PIR1,TXIF GOTO ENDINT
MOVLW TXSTART MOVWF FSR MOVF TXPTR,W ADDWF FSR,F MOVF INDF,W MOVWF TXREG INCF TXPTR,F DECFSZ TXCOUNT GOTO ENDINT BANKSEL PIE1 BCF PIE1,TXIE GOTO ENDINT
T2 CLRF STATUS BCF PIR1,TMR2IF INCF T2COUNT MOVLW .10 SUBWF T2COUNT,W BTFSS STATUS,Z GOTO ENDINT MOVF TMR0,W MOVWF CK0 MOVF TMR1L,W MOVWF CK1 CLRF TMR0 CLRF TMR1L CLRF T2COUNT ENDINT CLRF STATUS MOVFW F_TEMP MOVWF FSR SWAPF S_TEMP,W MOVWF STATUS SWAPF W_TEMP,F SWAPF W_TEMP,W
RETFIE ;*********************************************************************************** INITIAL ;*********************************************************************************** BSF STATUS,RP0 ;設(shè)置寄存器體1 CLRF ADCON1 ;轉(zhuǎn)換結(jié)果左對(duì)齊,A/D通道全選 MOVLW 0FFH ;RA0為模擬輸入 MOVWF TRISA ;RA4=CK1,RA0~5=AD MOVWF TRISE ;RE0~2=AD MOVLW 00H ;不用設(shè)為輸出 MOVWF TRISB MOVWF TRISD MOVLW B'10000001' ;RC7=RX,RC6=TX,RC2=選擇輸入,RC0=CK2, MOVWF TRISC MOVLW B'11111111' ;上拉+中斷NO ;外部時(shí)鐘//上升沿計(jì)數(shù) MOVWF OPTION_REG ;預(yù)分頻分給WDT;分頻比為1:128 MOVLW B'00100100' ;8位數(shù)據(jù)/發(fā)送使能/異步方式 MOVWF TXSTA ;高速波特率/ MOVLW .25 ;9600// 51 (4800) MOVWF SPBRG ; MOVLW 0C3H ;設(shè)定每50MS中斷一次 MOVWF PR2 CLRF PIE1 BSF PIE1,TMR2IE
BCF STATUS,RP0 ;恢復(fù)寄存器體0 MOVLW B'10010000' MOVWF RCSTA MOVLW B'01000001' ;選擇:時(shí)鐘時(shí)鐘源為FOSC/8,允許ADC工作 MOVWF ADCON0 ;通道AN0,暫時(shí)不啟動(dòng)轉(zhuǎn)換過(guò)程 MOVLW B'00000010' ;外部計(jì)數(shù)/分頻1:1/暫不啟動(dòng) MOVWF T1CON MOVLW B'01111111' ;預(yù)后分頻1:16,計(jì)數(shù)暫停 MOVWF T2CON CLRF TMR0
CLRF PORTC MOVLW 0x20 MOVWF FSR RE CLRF INDF INCF FSR,F MOVLW 0x3B XORWF FSR,W BTFSS STATUS,Z GOTO RE CLRF INTCON BSF INTCON,PEIE BSF INTCON,GIE RETURN ;*********************************************************************************** MAIN ;*********************************************************************************** CALL INITIAL LOOP CLRWDT BCF PORTC,2 ;RE通道1 CALL DELAY NOP CALL ADC MOVLW B'01001001' ;選擇通道1 MOVWF ADCON0 CALL LEACH MOVWF AD0
CALL ADC MOVLW B'01010001' ;選擇通道2 MOVWF ADCON0 CALL LEACH MOVWF AD1
CALL ADC MOVLW B'01011001' ;選擇通道3 MOVWF ADCON0 CALL LEACH MOVWF AD2
CALL ADC MOVLW B'01100001' ;選擇通道4 MOVWF ADCON0 CALL LEACH MOVWF AD3
CALL ADC MOVLW B'01101001' ;選擇通道5 MOVWF ADCON0 CALL LEACH MOVWF AD4
CALL ADC MOVLW B'01110001' ;選擇通道6 MOVWF ADCON0 CALL LEACH MOVWF AD5
CALL ADC MOVLW B'01111001' ;選擇通道7 MOVWF ADCON0 CALL LEACH MOVWF AD6
CALL ADC MOVLW B'01101001' ;選擇通道5 MOVWF ADCON0 CALL LEACH MOVWF AD7
BSF PORTC,2 ;RE通道2 CALL DELAY
CALL ADC MOVLW B'01110001' ;選擇通道6 MOVWF ADCON0 CALL LEACH MOVWF AD8
CALL ADC MOVLW B'01111001' ;選擇通道7 MOVWF ADCON0 CALL LEACH MOVWF AD9
CALL ADC MOVLW B'01000001' ;選擇通道0 MOVWF ADCON0 CALL LEACH MOVWF AD10
MOVLW 55H MOVWF TXSTART MOVLW 0AAH MOVWF TXEND CLRF TXPTR MOVLW .15 MOVWF TXCOUNT BANKSEL PIE1 BSF PIE1,TXIE CLRF STATUS GOTO
LOOP ;************************************************************************ ;*********************************************************************************** ADC ;*********************************************************************************** MOVLW ADBUFF1 MOVWF FSR MOVLW 6H MOVWF COUNTER ADCLOOP BSF ADCON0,GO ;開(kāi)啟A/D轉(zhuǎn)換過(guò)程 WAIT BTFSC ADCON0,GO ;等待A/D轉(zhuǎn)換過(guò)程結(jié)束,檢測(cè)ADC中斷標(biāo)志位 GOTO WAIT ;如果沒(méi)有轉(zhuǎn)換結(jié)束,則返回循環(huán)檢測(cè) MOVF ADRESH,W ;僅取高8位,把A/D結(jié)果讀到W MOVWF INDF INCF FSR DECFSZ COUNTER,F GOTO ADCLOOP RETURN ;***********************************************************************************
LEACH ;過(guò)濾 (采用6駔數(shù)據(jù)去掉最高最低數(shù),中間四位取平均) ;***********************************************************************************
MOVF ADBUFF1,W SUBWF ADBUFF6,W BTFSS STATUS,C GOTO CHANGE1 BUFF2 MOVF ADBUFF2,W SUBWF ADBUFF6,W BTFSS STATUS,C GOTO CHANGE2 MOVF ADBUFF2,W SUBWF ADBUFF1,W BTFSC STATUS,C GOTO CHANGE3 BUFF3 MOVF ADBUFF3,W SUBWF ADBUFF6,W BTFSS STATUS,C GOTO CHANGE4 MOVF ADBUFF3,W SUBWF ADBUFF1,W BTFSC STATUS,C GOTO CHANGE5 BUFF4 MOVF ADBUFF4,W SUBWF ADBUFF6,W BTFSS STATUS,C GOTO CHANGE6 MOVF ADBUFF4,W SUBWF ADBUFF1,W BTFSC STATUS,C GOTO CHANGE7 BUFF5 MOVF ADBUFF5,W SUBWF ADBUFF6,W BTFSS STATUS,C GOTO CHANGE8 MOVF ADBUFF5,W SUBWF ADBUFF1,W BTFSC STATUS,C GOTO CHANGE9 ;****************************************************************************************** AVERAGE ;平均 ;********************************************************************
CLRF ADBUFF1 MOVF ADBUFF2,W ADDWF ADBUFF3,F BTFSC STATUS,C INCF ADBUFF1 MOVF ADBUFF4,W ADDWF ADBUFF3,F BTFSC STATUS,C INCF ADBUFF1 MOVF ADBUFF5,W ADDWF ADBUFF3,F BTFSC STATUS,C INCF ADBUFF1 RRF ADBUFF1 RRF ADBUFF3 RRF ADBUFF1 RRF ADBUFF3 MOVF ADBUFF3,W RETURN ;*********************************************************************************** CHANGE1 ;互換 ;************************************************************************
ADDWF
ADBUFF1,F SUBWF ADBUFF6,F GOTO BUFF2 CHANGE2 ADDWF ADBUFF2,F SUBWF
ADBUFF6,F GOTO BUFF3 CHANGE3 ADDWF ADBUFF2,F SUBWF ADBUFF1,F GOTO BUFF3
CHANGE4 ADDWF ADBUFF3,F SUBWF ADBUFF6,F GOTO BUFF4 CHANGE5 ADDWF
ADBUFF3,F SUBWF ADBUFF1,F GOTO BUFF4 CHANGE6 ADDWF ADBUFF4,F SUBWF
ADBUFF6,F GOTO BUFF5 CHANGE7 ADDWF ADBUFF4,F SUBWF ADBUFF1,F GOTO BUFF5
CHANGE8 ADDWF ADBUFF5,F SUBWF ADBUFF6,F GOTO AVERAGE CHANGE9 ADDWF
ADBUFF5,F SUBWF ADBUFF1,F GOTO AVERAGE
;******************************************************************************************
DELAY ;顯示廷時(shí)常數(shù),廷時(shí)時(shí)間為3*255*65+6us+4us=50ms
MOVLW 41H MOVWF COUNT1 L2 CLRWDT MOVLW 0FFH MOVWF COUNT2 L3 DECFSZ
COUNT2,1 GOTO L3 DECFSZ COUNT1,1 GOTO L2 RETLW 0
;*************************************************************************************
END