Menu

PIC18 A-D Interface Log in to Edit

Pete Zawasky

\ **
\
\ Filename: degC.txt
\ FlashForth: 5.0
\ MCU PIC18F6527
\ Application:
\
\ Author: Pete Zawasky
\ Created: 3:00 PM 7/28/2014 ppz
\ Last Edit 3:00 PM 1/14/2022 ppz
\
\
**
\FlashForth is licensed acording to the GNU General Public License
\ **
\ Use the A/D to read an LM35 Precision Centigrade Temperature Sensor.
\ The A/D resolution is 10 bit.
\ 10 bit -- result of A/D is 4.9mV/bit => approximately .5degC/bit
\ The LM35 is connected to AN0.
\
\
*
\ FlashForth is licensed acording to the GNU General Public License
\ *
\ This code is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty
\ of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
\
*

-degC
marker -degC

decimal ram

$ff92 constant trisa

$ffc4 constant adresh \ A/D Result High Register
$ffc3 constant adresl \ A/D Result Low Register
$ffc2 constant adcon0 \ A/D Control Register 0
$ffc1 constant adcon1 \ A/D Control Register 1
$ffc0 constant adcon2 \ A/D Control Register 2

\ adcon0 bits
\ 0 constant adon \ A/D On bit
\ 1 constant go/done* \ A/D Conversion Status bit

\ Initialze the A/D for temperature readings on AN0.
\ Assume POR Default conditions for the A/D Module.
\
A/D_init ( -- )
$81 adcon2 c! \ result right justified
\ 0 TAD (manual acquisition)
\ 8 TOSC (8MHz)
$01 adcon0 c! \ A/D ON
; \ Channel 0 (AN0)
\ Read LM35
\ 150 degC =~ $132
\
@LM35 ( -- u )
$03 adcon0 c! \ start conversion
begin
adcon0 c@ $02 and 0= \ wait for conversion complete
until
adresl @ ; \ get result, 10 bits right justified
\ Read the temperature sensor and convert to degC.
\ degC = (A/D reading*4.9mV/bit) / (10mv/degC) rounded down
\
@degC ( -- u )
@LM35 49 * 100 / ;

ram hex


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.