-- Project: ADC read and send on USB CDC
-- Author: Vasile Guta-Ciucur (funlw65)
-- License: Code released under New BSD license
-- For jallib libraries, see de licence inside jal.zip
include freejalduino4 -- FreeJALduino4 pinout layer for the last board model
-- include libraries
include usb_serial
include print
include format
include delay
var dword Value
var word AD_RESULT
var word Volts
-- var byte Millivolts
const byte str1[] = " Volts\n\r"
-- ==================================================
-- PROGRAM START ------------------------------------
-- ==================================================
-- configure pins
enable_digital_io() -- first, all pins set to digital
-- now configure ADC constants
const bit ADC_HIGH_RESOLUTION = true -- 10bit resolution
const byte ADC_NVREF = 0 -- no voltage reference
const byte ADC_NCHANNEL = 1 -- how many ADC channels we are using,
-- always starting the count from A0 (RA0/AN0)
-- This will automatically set A0 as analog input
const word ADC_RSOURCE = 909 -- impendance (R1*R2/(R1+R2))
-- then, load the ADC library
include adc
-- Initialize ADC
adc_init()
-- initialize the USB serial library
usb_serial_init()
-- start main loop
forever loop
usb_serial_flush()
AD_RESULT = adc_read(0)
print_word_dec(usb_serial_data, AD_RESULT)
usb_serial_data = " "
-- Value = 537 * AD_RESULT
Value = dword(537) * AD_RESULT -- one of the operands must be casted
-- as dword to have a 32bit integer math
-- because AD_RESULT is word and the other
-- operand is a generic one.
Volts = Value / 100
format_word_dec(usb_serial_data, Volts, 4, 2)
print_string(usb_serial_data, str1)
for 100 loop -- delay 1 second (a little more than)
usb_serial_flush()
delay_1ms(10)
end loop
end loop
--