Dear All ...
第一次用有A/D 的12F510 練習寫程式...
居然可以工作啦!! 黑皮, 黑皮, 真是黑皮...
哈.哈...有成就感...來些抱抱(限女性),, 鼓勵一下囉.哈.哈...
當然啦 ! 程式有點笨笨,, 就像笨鴨一樣啦 ...hahaha.
給大家參考一下,,, 也歡迎來修正哦!!!!
嗯 ... 下個程式要練習寫" 查表程式 "...
;PIC12F510 12F510 ADC 05/23/2006
List p=12f510
#include <p12F510.inc> ; processor specific variables
; Configure: Controls the reset pin, code protect, oscillator selection, WDT.
__CONFIG _CP_OFF & _MCLRE_OFF & _IOSCFS_OFF & _WDT_OFF & _IntRC_OSC
;-----[Defines & Variable Assignments]---------------------------------------
cblock 0x0A ;
count ; count buffer
count1 ; count buffer
count2 ; count buffer
count3 ; For A/D RESULT.
endc
;-----[Reset Vector]---------------------------------------------------------
ORG 0x000 ; PIC goes here (Origin of code) after reset.
movwf OSCCAL ; Load the oscillator calibration factor.
movlw b'10001111' ; Disable wake on pin change, Enable pull-up.
OPTION
movlw b'00001100' ; Set GP2=AN2 ,, GP3 to an input GP0,GP1,GP4,GP5=O/P.
TRIS GPIO ; Register 06h
;-----[Main routine]------------------
init
bcf CM1CON0,3 ;Disable COMPARATOR Register bit3 C1ON to low.
bcf GPIO,0 ;
bcf GPIO,1 ;
bcf GPIO,4 ;
bcf GPIO,5 ;
call dly_1s ;
movlw b'01111001' ; GP2 to an A/D input , ADC ON
movwf ADCON0 ;
call dly_25us ; Wait for AN2 to set up .
bsf ADCON0,GO ; start A/D conversion for GP2/AN2.
call dly_25us ; Wait for coversion .
wait
btfsc ADCON0,GO ; Conversion complete yet? GO=0= Done.
goto wait ; No,
movf ADRES,W ; Read A/D result into W.
movwf count3 ; Save W into count3 register.
movlw b'11001100' ; 4V=204=11001100.
subwf count3,w ; RESULT - 4V.
btfsc STATUS,C ; If C=0 then RESULT< 4V
goto LED1 ; If C=1 then RESULT > 4V,.
movlw b'10011001' ; 3V=153=10011001.
subwf count3,w ; RESULT - 3V.
btfsc STATUS,C ; If C=0 then RESULT< 3V.
goto LED2 ; If C=1 then RESULT > 3V,
movlw b'01100110' ; 2V=102=01100110.
subwf count3,w ; RESULT -2V.
btfsc STATUS,C ; If C=0 then RESULT< 2V.
goto LED3 ; If C=1 then RESULT > 2V,
movlw b'00110011' ; 1V=51=00110011.
subwf count3,w ; RESULT - 1V.
btfsc STATUS,C ; If C=0 then RESULT< 1V.
goto LED4 ; If C=1 then RESULT > 1V,
goto LED5 ;
LED1
bsf GPIO,0 ; Turn LED on.
call dly_1s ;
call dly_1s ;
goto init ;
LED2
bsf GPIO,1 ; Turn LED on.
call dly_1s ;
call dly_1s ;
goto init ;
LED3
bsf GPIO,4 ; Turn LED on.
call dly_1s ;
call dly_1s ;
goto init ;
LED4
bsf GPIO,5 ; Turn LED on.
call dly_1s ;
call dly_1s ;
goto init ;
LED5
bsf GPIO,5 ; Turn LED on.
call dly_1s ;
bcf GPIO,5 ; Turn LED on.
call dly_1s ;
bsf GPIO,5 ; Turn LED on.
call dly_1s ;
goto init ;
; ---------------------------[Sub routine]-----------------------------------
dly_25us
movlw d'8' ;
movwf count ;
keep
decfsz count,f ;
goto keep ;
retlw 0 ;
dly_1s
movlw d'255' ;
movwf count1 ;
loop
movlw d'255' ;
movwf count2 ;
loop_1
goto $+1 ;
goto $+1 ;
goto $+1 ;
goto $+1 ;
goto $+1 ;
goto $+1 ;
decfsz count2,f ;
goto loop_1 ;
decfsz count1,f ;
goto loop ;
retlw 0
end