piclist 2000\04\17\142644a >
Thread: What is a Sigma-Delta AtoD converter? [EE]
www.piclist.com/techref/index.htm?key=
flavicon
face BY : Alice Campbell email (remove spam text)



OK, since you asked.  BUT this is still interim code, i have
not yet tested the 2-wire routine.  Instead of testing it all
afternoon yesterday i dug up the garden and planted tomatoes.
And took the night off.

schematic & code is waaaaay down at bottom.

{Quote hidden}

;       mostly by Scott, the fiddly stuff by alice 4/00
     list    p=16F84,t=ON,c=132,n=80,st=off
      __CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
       include "P16F84.INC"
;;      list    p=16F877
;;      __CONFIG   _CP_OFF & _WDT_OFF & _RC_OSC &_LVP_OFF
;;      include "P16F877.INC"
 cblock   0x0C
   count
   t1,t2,freq
   error_code
   total_lo,total_hi
   count_lo,count_hi
 endc
out_port        =       PORTB;PORTC
A2D_PORT        EQU     PORTA
A2D_IN_BIT      EQU     0
A2D_OUT_BIT     EQU     1
ERROR_STUCK_HI  EQU     .255
ERROR_STUCK_LO  EQU     2
beep_pin        equ     3
;          pot
;+5V __/\/\/\/\__ gnd
;          ^
;          |                 +---------------- pic input
;          |        10K      |       10k
;          +------/\/\/\/\---+----/\/\/\/\---- pic output
;          |                 |
;         === 100u          === .1 to 1u
;          |                 |
;         gnd               gnd
;
;The big 100u (or bigger if you want) cap will provide a low impedance
;voltage source.  This runs at 4 MHz.
;-----------------------------------------------------------
       nop
       ORG     0               ;Reset Vector
       ORG     4               ;Interrupt Vector
restart
;;      bcf     STATUS,RP1
;;      BCF     STATUS,RP0
       CLRF    A2D_PORT
       bsf     STATUS,RP0
       CLRF    out_port
       movlw   b'00000001'
       movwf   A2D_PORT
;;   movlw   b'00000110'
;;   movwf   ADCON1
       bcf     STATUS,RP0
       CLRF    A2D_PORT
       clrf    error_code
start:
loop:
       clrf    count_lo
       clrf    count_hi
       call    a2d_convert2
;       movfw   count_lo        ;varying pitch
;       call    beep            ;using squeeker
      movfw   count_lo         ;in lights
      movwf   out_port         ;on board
       call    big_delay
       call    big_delay
       CLRF    A2D_PORT
       clrf    error_code
       goto    loop
;--------------------------------------------------------
;a2dconvert
;
; 2I/O pins
a2d_convert:
       call    precharge
a2d_loop
       RLF     A2D_PORT,W        ;roll it in
      andlw   b'00000010'       ;isolate it  so it wont screw up the other buttons
      xorwf   A2D_PORT,F        ;flip it
       clrc
       movlw   1
       btfss   A2D_PORT,A2D_OUT_BIT
        addwf  count_lo,f
       skpnc
        addwf  count_hi,f
       addwf   total_lo,f
       skpnc
        addwf  total_hi,f
     clrf    t1        ;SLOW DOWN SPIKES
     decfsz  t1,f
     goto   $-1
       btfss   total_hi,0;4
        goto   a2d_loop
       return
;--------------------------------------------------------
; precharge
precharge:
       movlw   1
       clrf    total_lo
       clrf    total_hi
       bcf     A2D_PORT,A2D_OUT_BIT
   ; Make the capacitor voltage go below the trip threshold
pc_low_loop
       btfss   A2D_PORT,A2D_IN_BIT
        goto   pc_high
       addwf   total_lo,f
       skpnc
        addwf  total_hi,f
;;      btfss   total_hi,0
       skpc
        goto   pc_low_loop
       movlw   ERROR_STUCK_HI
       movwf   error_code
       goto    display_error
   ; Now make the capacitor voltage go above the trip threshold
pc_high:
       BSF     A2D_PORT,A2D_OUT_BIT
       clrf    total_lo
       clrf    total_hi
pc_high_loop:
       btfsc   A2D_PORT,A2D_IN_BIT
        goto   precharged
       addwf   total_lo,f
       skpnc
        addwf  total_hi,f
;;      btfss   total_hi,0
       skpc
        goto   pc_high_loop
       movlw   ERROR_STUCK_LO
       movwf   error_code
       goto    display_error
precharged:
       clrf    total_lo
       clrf    total_hi
       bcf     A2D_PORT,A2D_OUT_BIT
       return
;-----------------------------------------------------------------
; a2d_convert
;
;1 I/O pin
a2d_convert2:
   ;Put the A2D_PORT's tris into the FSR so that we can
   ;access it with out having to do those damn bank selects
       movlw   TRISA
       movwf   FSR
       movlw   1
       clrf    count_lo
       clrf    count_hi
       clrf    total_lo
       clrf    total_hi
       clrf    t1
   ; Step 1 is the pre-charging cycle where the capacitor
   ;is made to pass through the low-to-high threshold and
   ;then again through the high-to-low threshold.
   ; This ensures that the voltage on the capacitor is the
   ;same for each time we pass through the algorithm.
   ;Make the capacitor go through the low to high threshold
       bsf     A2D_PORT,A2D_OUT_BIT
       bcf     INDF,A2D_OUT_BIT
a2d2_l1
       call    delay16
       call    delay16
       call    delay16
       call    delay16
       bsf     INDF,A2D_OUT_BIT
       call    delay16
       btfsc   A2D_PORT,A2D_OUT_BIT
        goto   a2d2_l2
       bsf     A2D_PORT,A2D_OUT_BIT
       bcf     INDF,A2D_OUT_BIT
       decfsz  t1,f
        goto   a2d2_l1
;       movlw   ERROR_STUCK_LO
;       movwf   error_code
;       goto    display_error
  ;now pass through the high to low threshold
a2d2_l2
       clrf    t1
       bcf     A2D_PORT,A2D_OUT_BIT
       bcf     INDF,A2D_OUT_BIT
a2d2_l3
       call    delay16
       call    delay16
       call    delay16
       call    delay16
       bsf     INDF,A2D_OUT_BIT
       call    delay16
       btfss   A2D_PORT,A2D_OUT_BIT
        goto   a2d2_l4
       bcf     A2D_PORT,A2D_OUT_BIT
       bcf     INDF,A2D_OUT_BIT
       decfsz  t1,f
        goto   a2d2_l3
;       movlw   ERROR_STUCK_HI
;       movwf   error_code
;       goto    display_error
a2d2_l4    ;If we get then we've successfully precharged the capacitor
   ;Also, the capacitor voltage is just below the high-to-low
   ;threshold (Vil) so let's begin by ramping it positive
       bsf     A2D_PORT,A2D_OUT_BIT
       bcf     INDF,A2D_OUT_BIT
a2d2_l5
       addwf   total_lo,f
       skpnc
        addwf  total_hi,f
       btfsc   total_hi,0
        return
       call    delay16
       call    delay16
       call    delay16
       call    delay16
       bsf     INDF,A2D_OUT_BIT
       call    delay4
       btfss   A2D_PORT,A2D_OUT_BIT
        goto   a2d2_low
       addwf   count_lo,F
       ;The cap voltage is too high
       bcf     A2D_PORT,A2D_OUT_BIT
       bcf     INDF,A2D_OUT_BIT
       skpnc
        addwf  count_hi,F
       goto    a2d2_l5
a2d2_low
       ;The cap voltage is too high
       bsf     A2D_PORT,A2D_OUT_BIT
       bcf     INDF,A2D_OUT_BIT
       goto    $+1
       goto    a2d2_l5
       return
;--------------------------------------------------------
;; error - write an error code to output device
display_error:
       bsf     STATUS,RP0
       clrf    out_port          ;Make portb i/o pins outputs
       bcf     STATUS,RP0
error_loop:
       movf    error_code,W
;       CALL    beep    ;if device is beeper
       movwf   out_port
;       skpz
;        goto   error_loop
       ;If the error code is zero, then flash the LEDs
      call    big_delay
;       comf    out_port,f      ;flasher
      call    big_delay
       GOTO    restart
       goto    error_loop
;----------------------------------------------------
big_delay:
       clrf    t1
       clrf    t2
       decfsz  t1,f
        goto   $-1
       decfsz  t2,f
        goto   $-3
       return
;------------------------------------------------------
delay16 NOP
delay15 NOP
delay14 NOP
delay13 NOP
delay12 NOP
delay11 NOP
delay10 NOP
delay9  NOP
delay8  NOP
delay7  NOP
delay6  NOP
delay5  NOP
delay4
       RETURN
;------------------------------------------------------
beep                            ;normally assumes freq is in work
;       movfw   count_lo        ;freq proportional to answer
       movwf   freq            ; get frequency
       movlw   .25
       movwf   t1              ;shorten up loop for simulator
;       clrf    t1
       clrwdt
beeploop
       movfw   freq
       movwf   t2
       bsf     A2D_PORT,beep_pin
        clrwdt
        movlw   b'00001000'     ;beeper is on porta,3.
        xorwf   A2D_PORT,f
        decfsz t2,f
        goto   $-4
       decfsz     t1,f
       goto    beeploop
       return
;------------------------------------------------------------------
;-------------------------------------------------------
       END
;==========================================================

<200004171828.OAA02098@mailgate.scseng.com> 7BIT

In reply to: <001b01bfa895$0875e200$0100a8c0@a.efplus.com>
See also: www.piclist.com/techref/index.htm?key=
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) What is a Sigma-Delta AtoD converter? [EE]

month overview.

new search...