十年专注单片机方案开发的方案公司英锐恩,分享单片机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