;****************************************************************************** ;Filename: atod.asm ;Author: Eric Torrence ;Date: 14/05/06 ;Version: 1.00 ;Description: ; Take A/D reading from AN0 (pin 7) when GP3 is LO (pin 4) and output ; results in 4 bits on GP5, GP4, GP2, GP1 (pins 2,3,5,6) ;****************************************************************************** ;Revision History: ; none ;****************************************************************************** list p=12f675 ; list directive to define processor #include ; processor specific variable definitions errorlevel -302 ; suppress message 302 from list file __CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT ; '__CONFIG' directive is used to embed configuration word within .asm file. ; The labels following the directive are located in the respective .inc file. ; See data sheet for additional information on configuration word settings. ;************************** VARIABLE DEFINITIONS ****************************** COUNT equ 0x20 ;****************************** Start of Program ****************************** org 0x000 ; processor reset vector banksel TRISIO clrf OPTION_REG bsf OPTION_REG, NOT_GPPU ; Weak pullups: disabled clrf TRISIO ; Set all I/O pins as outputs bsf TRISIO, GP3 ; Except GP3 (pin 4) as button bsf TRISIO, GP0 ; And GP0 (pin 7) for ADC banksel GPIO clrf GPIO ; clear all outputs ;; Setup A/D conversion banksel ANSEL clrf ANSEL bsf ANSEL, ANS0 ; Select AN0 as analog input bsf ANSEL, ADCS0 ; Convert at Fosc/8 (2 us) banksel ADCON0 clrf ADCON0 bsf ADCON0, ADON ; Turn on A/D convertor ;; Loop waiting for the button to be pressed. If it is, start ;; the ADC and update the output bits Main: btfsc GPIO, GP3 ; Pin 4 pulled lo (button press) goto Main ;; Button is pressed, wait for switch to settle (~ 1 ms) clrf COUNT BounceWait: nop decfsz COUNT, f goto BounceWait ;; Now, wait for the switch to be released (GP3 HI) ReleaseWait: btfss GPIO, GP3 goto ReleaseWait ;; Start the A/D conversion bsf ADCON0, GO ; Start the conversion ADCWait: btfsc ADCON0, GO ; Wait for bit to clear (ADC done) goto ADCWait ;; Set our four output bits by hand ;; Look at the top 4 bits in ADRESH and set GPIO accordingly ;; GP5, GP4, GP2, GP1 (pins 2, 3, 5, 6) clrf GPIO btfsc ADRESH, 7 bsf GPIO, GP5 btfsc ADRESH, 6 bsf GPIO, GP4 btfsc ADRESH, 5 bsf GPIO, GP2 btfsc ADRESH, 4 bsf GPIO, GP1 goto Main ; All Done end ; directive 'end of program'