Author: sebastien.lelong
Date: Sun Jul  5 01:06:46 2009
New Revision: 1088

Added:
    trunk/sample/16f88_adc_highres.jal

Log:
high & low resolution mode sample for 16f88

Added: trunk/sample/16f88_adc_highres.jal
==============================================================================
--- (empty file)
+++ trunk/sample/16f88_adc_highres.jal  Sun Jul  5 01:06:46 2009
@@ -0,0 +1,123 @@
+-- Title: Sample showing how to use adc_hardware library
+-- Author: Sebastien Lelong, Copyright (c) 2008-2009, all rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4k
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the BSD license  
(http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: this program shows how to use adc_hardware library. It  
regurlarly performs an
+-- Analog-to-Digital Conversion, on channels AN0 to AN6 (see pin diagrams  
to know which pins are involved),
+-- and sends the result through serial.
+--
+-- Notes: as output, this sample produces characters on a serial link, and  
uses delay library.
+-- First be sure you're able to run serial and delay tests.
+--
+--
+-- Few words about diagram: this program performs ADC on channels AN0 to  
AN6, so you can measure
+-- any voltage source connected to these channels (no more than 5V  
please). To test the whole, you can
+-- use a voltage divider. For AN0:
+--             
+--     5V
+--     |
+--     Z
+--     Z R1        -----------
+--     Z           |
+--     |------ AN0-|   PIC
+--     Z           |
+--     Z R2        |
+--     Z
+--     |
+--    GND
+--
+--
+--             - R1=1K, R2=1K: you should get ~127 as ADC value
+--             - R1=1K, R2=2.2K: you should get ~175 as ADC value
+--
+
+-- select chip
+include 16f88
+-- set all IO as digital, will turn ADC on later (by adc lib)
+enable_digital_io()
+
+-- We'll use internal oscillator. It work @ 8MHz
+pragma target CLOCK            8_000_000
+pragma target OSC              INTOSC_NOCLKOUT
+-- Specify no postscaler, ie. really runs @8MHz
+OSCCON_IRCF = 0b_111
+-- no watchdog, please
+pragma target WDT              disabled
+
+include print
+include delay
+-- ok, now setup serial
+const usart_hw_serial = true   -- true = RS232, false = SPI
+const serial_hw_baudrate = 19_200
+include serial_hardware
+serial_hw_init()
+-- inform user PIC is ready !
+serial_hw_write("!")
+
+-- Configure ADC
+-- We won't use any external VRef, so measures are done
+-- according to PIC powering voltage
+const byte ADC_NVREF = 0
+-- The maximum resistance while measuring ADC is... (unit: ohms)
+-- Being accurate helps speeding up ADC acquisition
+const word ADC_RSOURCE = 2_000
+-- In this example, we'll perform low resolution ADC: results are
+-- coded on 8bits
+const bit ADC_HIGH_RESOLUTION = true
+-- Now include the famous library
+include adc
+-- And initialize the whole with our parameters
+adc_init()
+-- Declare analog channels
+set_analog_pin(0)
+set_analog_pin(1)
+set_analog_pin(2)
+set_analog_pin(3)
+set_analog_pin(4)
+set_analog_pin(5)
+set_analog_pin(6)
+
+
+-- will periodically send those chars
+var word measure
+var byte lowmeasure
+const byte prefix[] = "Channel "
+const byte highstr[] = " (high) "
+const byte lowstr[] = " (low) "
+const byte suffix[] = ": "
+forever loop
+   var byte channel = 0
+   -- loop over all channels and read
+   for 7 using channel loop
+
+         -- get ADC result, high resolution
+         measure = adc_read(channel)
+         -- send it back through serial
+         print_string(serial_hw_data,prefix)
+         print_string(serial_hw_data,highstr)
+         print_byte_dec(serial_hw_data,channel)
+         print_string(serial_hw_data,suffix)
+         -- print_word_binary(serial_hw_data,measure) ; this one works !
+         print_word_dec(serial_hw_data,measure) ; I should get ~512
+         print_crlf(serial_hw_data)
+         -- and sleep a litte...
+         delay_1ms(100)
+
+         -- Even if we set high resolution, we can still access results
+         -- in low resolution (the 2 LSb will be removed)
+         lowmeasure = adc_read_low_res(channel)
+         print_string(serial_hw_data,prefix)
+         print_string(serial_hw_data,lowstr)
+         print_byte_dec(serial_hw_data,channel)
+         print_string(serial_hw_data,suffix)
+         print_byte_dec(serial_hw_data,lowmeasure) ; I should get ~127
+         print_crlf(serial_hw_data)
+         -- and sleep a litte...
+         delay_1ms(100)
+   end loop
+end loop
+

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to