[PicForth] AtoD (again)

Kevin L. Pauba klpauba at cox.net
Thu May 25 05:48:24 CEST 2006


David Trethewey wrote:

> Hi all,
> Sorry about that previous message ( forgot the shift when I hit enter )
> I am currently working on a project fir the pic16f876 and I am using 
> picforth. My concern is with the analog to digital converter module. I 
> have read the documentation for the pic and its A-to-D registers and 
> have come across a few problems. First, the documentation says, for 
> the ADCON1 register, bits 0,1,2,3 and 7 are used, but in the 
> picforth.fs file these are not the bits used, and they do not have the 
> corresponding names.
> My second problem is from what looks to me to be a typo of some kind. 
> In the pdf datasheet it lists the bits of the ADCON0 register, and it 
> gives a brief description of each one. Under the go//done bit it 
> explains:
> GO/DONE: A/D Conversion Status bit
> If ADON = 1:
> 1 = A/D conversion in progress (setting this bit starts the A/D 
> conversion)
> 0 = A/D conversion not in progress (this bit is automatically cleared 
> by hardware when the A/D
> conversion is complete)
> This clearly is confusing, because it is in fact the next ( 
> implemented ) bit that is named ADON. Its description:
> ADON: A/D On bit
> 1 = A/D converter module is operating
> 0 = A/D converter module is shut-off and consumes no operating current
>
> Any helpful hints for implelentation would be gratefully appreciated.
> Thanks in advance.
>
>------------------------------------------------------------------------
>
>_______________________________________________
>PicForth mailing list
>PicForth at lists.rfc1149.net
>http://lists.rfc1149.net/mailman/listinfo/picforth
>  
>
Here's some code that I have used that works with an '877 (and I would 
think that it would work with the '876):

pic16f87x
include flib.fs

\ define the variables used
2 pin-c LED0
3 pin-c LED1

: adcReadLowRes ( -- n )
    adif bit-clr            \ Clear A/D done flag
    100us                   \ Stabilize after channel change
    go//done bit-set        \ Start conversion
    begin
        go//done bit-clr?   \ Wait for conversion to complete
    until

    adresh @                \ Use only the top 8 bits   
;

main : VariableFlashingLED ( -- )
    init                    \ Initialize the ports
    $81 adcon0 !            \ Fosc/32, channel 0, ADC on
    $0e adcon1 !            \ Left justified, RA0 analog,
                            \ RA1..RA7 digital

    LED0 >output LED0 high  \ Initialize outputs
    LED1 >output LED1 high

    begin
        adcReadLowRes dup   ( -- n n )
        *10ms LED0 low      ( -- n )
        *10ms LED0 high     ( -- )
    again
;

fosc-hs set-fosc            \ High-speed (20MHz) oscillator
false set-wdte              \ Do not use watchdog timer
false set-lvp               \ No low-voltage programming




More information about the PicForth mailing list