12F1840 sample ADC and usart

17 views
Skip to first unread message

hans

unread,
Jan 17, 2023, 11:28:48 AM1/17/23
to jallib
Hello ,
Does anyone have an example for this pic, I want an2 in adc and A0 as Tx and A1 as Rx. I've tried to derive some from other programs but I keep stumbling on the datasheet.
Hans

-- chip setup
include 12f1840

-- Internal oscillator at 8 MHz with PLL enabled -> 32 MHz
pragma target clock    32_000_000         -- oscillator frequency

pragma target OSC      INTOSC_NOCLKOUT    -- internal oscillator
pragma target WDT      DISABLED           -- watchdog off
pragma target PWRTE    ENABLED            -- power up delay
pragma target MCLR     EXTERNAL           -- reset externally
pragma target BROWNOUT DISABLED           -- no brownout
pragma target CLKOUTEN DISABLED           -- no clock out
pragma target IESO     DISABLED           -- no osc switchover
pragma target FCMEN    DISABLED           -- no clock monitoring
pragma target PLLEN    ENABLED            -- PLL enabled
pragma target LVP      DISABLED            -- allow LVP

OSCCON_IRCF   = 0b1110                    -- 8 MHz (+ PLL -> 32 MHz)
OSCCON_SCS    = 0b00                      -- Clock determined by fuses
-- OSCCON_PLLEN  = TRUE                   -- PLL fixed enabled by configuration bits

enable_digital_io()                       -- all pins digital

const byte ADC_CHANNEL = 2                      -- potmeter connected to pin_AN3
ADCON0_CHS2 = TRUE                               -- port 3 is analog input (default)
pin_AN2_direction = input
-- Step 2: Set VDD and VSS as Vref
FVRCON_FVREN = true
-- Step 3: Use Frc as ADC clock
ANSEL_ADCS = 0b011
const ADC_RSOURCE = 4_700                       -- Input resistance: 4.7K potmeter
-- Now we can include the library
include adc
-- And initialize the whole with our parameters
adc_init()
var byte DIR
forever loop
DIR = adc_read_low_res(ADC_CHANNEL)
end loop

Rob CJ

unread,
Jan 17, 2023, 1:17:16 PM1/17/23
to jal...@googlegroups.com
Hi Hans,

Step 2 and step 3 should change:

-- Step 2: Set VDD as Vref
ADCON1_ADPREF = 0b00
-- Step 3: Use Frc as ADC clock
ADCON1_ADCS = 0b011

I hope this works.

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens hans <hanz...@zeelandnet.nl>
Verzonden: dinsdag 17 januari 2023 17:28
Aan: jallib <jal...@googlegroups.com>
Onderwerp: [jallib] 12F1840 sample ADC and usart
 
--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/17f8ecc8-484d-4763-aa30-fa7b15395f64n%40googlegroups.com.

hans

unread,
Jan 17, 2023, 3:11:20 PM1/17/23
to jallib
Hi Rob,
This gives no errors , thanks:
kind egards
:Hans

Op dinsdag 17 januari 2023 om 19:17:16 UTC+1 schreef rob...@hotmail.com:
Reply all
Reply to author
Forward
0 new messages