12f675 ile analog giriş

MoSTaR

Üye
Katılım
17 Ara 2007
Mesajlar
82
Puanları
1
Herkese merhaba;

Pic12f675'in analog girişine 0 ila 5V arası değişen gerilim vererek bu gerilim değerini dijital sayıya çevirmek istiyorum. Yani temel olarak analog dijital converter'ünü kullanmak istiyorum ama yapamadım. Hiç kullanmadım bu pic'i. Bana Assembly ile temel bir ADC programı yazabilecek arkadaş varsa cok memnun olurum.
 
Hiç yokmu bu pic i bilen bir arkadaşım. Temel bir ADC programı istiyorum işin ustaları için basit olsa gerek. ben bir tane program buldum ama macro felan kullanmıs biraz anlamadım.

Program gelen voltaj seviyesini anlıyor ve gelen gerilim 2.93V ile 3.42V arasında ise çıkışında ki ledi yakıyor.



;gpi.asm
;Gear position indicator program for PIC12F675
;measures voltage on AN0 (pin 7) with ADC
;if voltage is between 2.93 V and 3.42 V, LED (pin 2) is on
;otherwise it is off
;ADC is 10 bits, and has a max. value of 0x3FF or 1023.
;With a 5 V reference, this gives a resolution of 5/1024
;or 4.88 mV.

list p=12f675 ;list directive to define processor
#include "p12f675.inc" ;processor specific variable definitions

;-----------------------------------------------------------------
;defines
;-----------------------------------------------------------------

#define LED 5
#define ADC 0
#define UPPER_LIMIT .700 ;3.42 V
#define LOWER_LIMIT .600 ;2.93 V

;------------------------------------------------------------------
;macro definitions
;------------------------------------------------------------------

;macro compares the 16 bit value stored in fr,
;fr-1 to a 16 bit literal number. It jumps to
;an address if the value in the registers is higher
;than the literal.

cjalwd macro fr1,L1,L2,addr
movf fr1,W
sublw L1
btfss STATUS,C
goto addr
movlw L1
subwf fr1,W
btfss STATUS,C
goto $+5
movf fr1-1,W
sublw L2
btfss STATUS,C
goto addr
endm

;-----------------------------------------------------------------
;variables
;-----------------------------------------------------------------
cblock 0x20
ADC_value_lo
ADC_value_hi
endc

;------------------------------------------------------------------
;initialisation
;------------------------------------------------------------------

org 0
banksel ANSEL
movlw 11h ;AN0 as analog input,conversion clock Fosc/8
movwf ANSEL
bankseL CMCON
movlw 07h ;comparators off
movwf CMCON
movlw 0x81 ;ADC on, right justified
movwf ADCON0
banksel TRISIO
movlw 0x0F ;LED output GPIO5 (pin 2), AN0 input GPIO0 (pin 7) 00001111
movwf TRISIO

;-----------------------------------------------------------------------
;main program loop
;-----------------------------------------------------------------------
loop:
call read_ADC ;read ADC
banksel GPIO
cjalwd ADC_value_hi,HIGH UPPER_LIMIT,LOW UPPER_LIMIT,LED_off ;compare ADC value with upper limit
cjalwd ADC_value_hi,HIGH LOWER_LIMIT,LOW LOWER_LIMIT,LED_on ;compare ADC value with lower limit
LED_off:
bsf GPIO,LED ;LED off
goto loop
LED_on:
bcf GPIO,LED ;LED on
goto loop


;---------------------------------------------------------------------
;read_ADC
;function to read ADC
;returns high and low bytes (10 bits) in ADC_value_hi
;and ADC_value_lo variables. Value is right-justified.
;---------------------------------------------------------------------

read_ADC:
banksel ADCON0
bsf ADCON0,1 ;start conversion
WaitADC:
btfss PIR1,ADIF
goto WaitADC ;wait for ADC to finish
GetADC1:
movf ADRESH, W ;get high byte
movwf ADC_value_hi
banksel ADRESL
movf ADRESL, W ;get low byte
movwf ADC_value_lo
banksel PIR1
bcf PIR1,ADIF ;clear conversion flag
return

;----------------------------------------------------------------------

end
 
Birtane daha buldum. Bu daha kolay ve anlaşılır bir program; Analog girişten gelen gerilim seviyesine göre çıkışına bağlı olan ledin parlaklığını ayarlıyor.

;fade

list P=12f675
#include <p12f675.inc>
__config _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CPD_OFF & _CP_OFF
ERRORLEVEL -302


cblock 20h
timer1,timer2,timer3,slow,pwm
endc

bsf STATUS,RP0 ;bank 1
movlw 0x38
movwf ANSEL
movlw 0x38
movwf TRISIO
bcf STATUS,RP0

movlw 0x07 ;turn off comparitors
movwf CMCON
movlw 0x0d
movwf ADCON0

start
bsf ADCON0,1
waiting
btfsc ADCON0,1
goto waiting
start1
movlw 0x0f
movwf timer3 ;timer3=0000001111
start2
movf ADRESH,0 ;move value in adresh to w
movwf timer1 ;timer1=adresh
movwf timer2 ;timer2=adresh
start3
movlw 0x01
movwf GPIO ;turn on
decfsz timer1,1
goto start3
pause1
movlw 0x00
movwf GPIO
incfsz timer2,1
goto pause1
decfsz timer3,1
goto start2
goto start
end
 

Forum istatistikleri

Konular
128,261
Mesajlar
916,282
Kullanıcılar
450,083
Son üye
bilgees

Yeni konular

Geri
Üst