十年專注單片機方案開發的方案公司英銳恩,分享單片機AD轉換。英銳恩現提供服務產品涉及主控芯片:8位單片機、16位單片機、32位單片機及各類運算放大器等。
w EQU 00h f EQU 01h STATUS EQU 03h PORTB EQU 06h INTCON EQU 0Bh PIR1 EQU 0Ch PIR2 EQU 0Dh TMR1L EQU 0Eh TMR1H EQU 0Fh T1CON EQU 10h T2CON EQU 11h ADRESH EQU 1Eh ADCON0 EQU 1Fh TRISB EQU 86h PIE1 EQU 8Ch ADCON1 EQU 9FH TRISA EQU 85H
LAST EQU 20h W_TEMP EQU 21h STATUS_TEMP EQU 22h Scaler EQU 23h LAST1 EQU 24h TEMP EQU 25h
org 000h nop goto main org 004h goto intservice
intservice movwf W_TEMP ;存儲w寄存器內容 swapf STATUS,w movwf STATUS_TEMP btfsc PIR1,0 ;判斷TIMER1是否溢出中斷 call Timer1 ;調用Timer1中斷服務程序 swapf STATUS_TEMP,w movwf STATUS swapf W_TEMP,f swapf W_TEMP,w ;恢復w和status的內容 retfie
Timer1 bcf STATUS,5 bcf PIR1,0 ;將TIMER1的中斷使能標志清0 decf Scaler,f clrf TMR1L ;把TMR1清0,防止進位產生 movlw 88h;;;;;;;;;; movwf TMR1H movlw 2fh movwf TMR1L return
main call Initial mainloop call Delay call ADConvert subwf LAST,w btfsc STATUS,2 goto mainloop movwf LAST1 movf TEMP,w movwf LAST btfsc STATUS,0 goto Display1 goto Display2 ;goto mainloop
Initial movlw 00h movwf LAST bcf STATUS,5 clrf PORTB movlw 04h movwf Scaler bsf STATUS,5 clrf TRISB ;設定B口為輸出口 bcf STATUS,5 clrf T1CON ;設定TIMER1的時鐘為FOSC/4,不使用分頻器 clrf TMR1H clrf TMR1L clrf INTCON bsf INTCON,6 ;將外圍功能的中斷使能 bsf STATUS,5 clrf PIE1 bsf PIE1,0 ;將TIMER1的溢出中斷使能 bcf STATUS,5 clrf PIR1 clrf TMR1L movlw 88h;;;;;;;;;; movwf TMR1H movlw 2fh movwf TMR1L ;bsf T1CON,0 ;TIMER1開始計數 bsf STATUS,5 movlw B'00001110' ;選擇A/D轉換輸入端RA0,參考電壓為VDD及VSS movwf ADCON1 bsf TRISA,0 ;設定A口<0>為輸入端 bcf STATUS,5 movlw B'10000001' ;工作時鐘為FOSC/32,并啟動A/D轉換模塊 movwf ADCON0 bsf INTCON,7 ;將中斷總開關打開,使中斷可以產生 return
ADConvert bcf STATUS,5 bsf ADCON0,2 ;執行A/D轉換 wait btfsc ADCON0,2 goto wait movf ADRESH,w ;movlw B'00010000' andlw B'11111000' movwf TEMP bcf STATUS,0 rrf TEMP,f rrf TEMP,f rrf TEMP,f movf TEMP,w return
Display1 bcf STATUS,5 loop1 movlw B'11000000' movwf PORTB call Delay movlw B'01100000' movwf PORTB call Delay movlw B'00110000' movwf PORTB call Delay movlw B'10010000' movwf PORTB call Delay ;movlw B'11000000' ;movwf PORTB ;call Delay ;movlw B'01100000' ;movwf PORTB ;call Delay ;movlw B'00110000' ;movwf PORTB ;call Delay ;movlw B'10010000' ;movwf PORTB ;call
Delay decfsz LAST1,f goto loop1 movlw B'00000000' movwf PORTB bcf STATUS,0 goto mainloop Display2 bcf STATUS,5
loop2 movlw B'10010000' movwf PORTB call Delay movlw B'00110000' movwf PORTB call Delay movlw B'01100000' movwf PORTB call Delay movlw B'11000000' movwf PORTB call Delay ;movlw B'10010000' ;movwf PORTB ;call Delay ;movlw B'00110000' ;movwf PORTB ;call Delay ;movlw B'01100000' ;movwf PORTB ;call Delay ;movlw B'11000000' ;movwf PORTB ;call Delay incfsz LAST1,f goto loop2 movlw B'00000000' movwf PORTB bcf STATUS,0 goto mainloop
Delay bsf T1CON,0 ;TIMER1開始計數 Delay1 btfss Scaler,7 ;檢查Scaler的第7位是否為0 goto Delay1 bcf T1CON,0 ;TIMER1停止計數 clrf TMR1L ;把TMR1清0,防止進位產生 movlw 88h;;;;;;;;;; movwf TMR1H movlw 2fh movwf TMR1L movlw 05h addwf Scaler,f return
end