Revision: 1773
Author: sebastien.lelong
Date: Sat Mar 13 02:08:36 2010
Log: added dynamic ADC sample for 18f4550, updated TORELEASE and CHANGELOG
http://code.google.com/p/jallib/source/detail?r=1773

Added:
 /trunk/sample/18f4550_dynamic_adc.jal
Modified:
 /trunk/CHANGELOG
 /trunk/TORELEASE

=======================================
--- /dev/null
+++ /trunk/sample/18f4550_dynamic_adc.jal       Sat Mar 13 02:08:36 2010
@@ -0,0 +1,183 @@
+-- Title: Sample showing how to use dynamic ADC 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 library, but the dynamic way. +-- What's dynamic ? When triggering dynamic ADC, you can change the number of +-- ADC channels at runtime. Using 18F4550 and static ADC (default), number of ADC +-- channels are specified with constant ADC_NCHANNEL. Once specicied, you can't +-- change this. Using dynamic ADC, you can adjust ADC channels at runtime, while +-- your program is running. You can also change several other parameters like
+-- the resolution, Rsource, etc...
+-- --
+-- This sample basically shows how to select 1 or 2 channels, high or low resolution.
+-- It uses two switches:
+--   - sw1: connected to pin_D6, select 1 or 2 channels
+--   - sw2: connected to pin_D7, select high or low resolution
+-- (this setup is what you have when using Jaluino and Crumboard shield. You can have
+-- a look at the following schematic:
+-- http://justanotherlanguage.org/sites/default/files/crumboard_schematic.png)
+-- --
+-- You'll have to add two voltage dividers connected to pin_RA0 and pin_RA1. +-- Choose different resistor values in order to observe different ADC values...
+-- Reproduce the following for pin_RA1 too:
+--
+--     5V
+--     |
+--     Z
+--     Z R1        -----------
+--     Z           |
+--     |------ RA0-|   PIC
+--     Z           |
+--     Z R2        |
+--     Z
+--     |
+--    GND
+--
+--
+--   - R1=1K, R2=1K: you should get ~127/~512 as ADC value (low/high)
+--   - R1=1K, R2=2.2K: you should get ~175/~704 as ADC value (low/high)
+--
+
+include 18f4550
+pragma target clock 48_000_000
+-- magical statements: using a 20MHz Xtal, you can run @48MHz !
+pragma target PLLDIV       P5
+pragma target CPUDIV       P2
+pragma target USBPLL       F48MHZ
+pragma target OSC          HS_PLL
+
+pragma target WDT  disabled        -- no watchdog
+pragma target LVP  disabled        -- no Low Voltage Programming
+pragma target MCLR external        -- reset externally
+
+-- We'll start to set all pins as digital
+-- then, using ADC lib, we'll configure needed
+-- ones as analog.
+enable_digital_io()
+
+include print
+include delay
+
+-- ok, now setup serial, we'll use this
+-- to get ADC measures
+const serial_hw_baudrate = 115_200
+include serial_hardware
+serial_hw_init()
+
+-- define inputs
+alias sw1 is pin_D6
+pin_D6_direction = INPUT
+alias sw2 is pin_D7
+pin_D7_direction = INPUT
+
+-- led will inform user input as been processed
+alias led is pin_C2
+pin_C2_direction = OUTPUT
+
+-- vars to stores states
+var bit use_2_channels = FALSE
+var bit use_high_resolution = FALSE
+-- also store previous value to optimize input processing
+-- init with different value so it's processed the first time
+var bit prev_use_high_resolution = TRUE
+var bit prev_use_2_channels = TRUE
+
+-- Dynamic ADC trigger !
+const bit ADC_DYNAMIC = true
+-- now ADC parameters are variables
+var bit adc_high_resolution = low
+-- we'll start with one channel
+var byte adc_nchannel = 1
+-- and no voltage reference, it won't change during
+-- execution, so let's keep it as a constant
+const byte ADC_NVREF = 0
+-- now we can include the library
+include adc
+-- and run the initialization step
+adc_init()
+
+
+-- will periodically send those chars
+var word wmeasure
+var byte bmeasure
+const byte chanstr[] = "Channel "
+const byte prefix[] = " result in "
+const byte highstr[] = "high "
+const byte lowstr[] = "low "
+const byte suffix[] = "resolution: "
+
+procedure strobe() is
+   led = HIGH
+   delay_1ms(5)
+   led = LOW
+   delay_1ms(5)
+end procedure
+
+forever loop
+   -- first we process inputs (switches)
+   -- SW1: switch between 1 or 2 channels
+   if sw1 == high then
+      use_2_channels = ! use_2_channels
+   end if
+   -- SW2: switch between high or low resolution
+   if sw2 == high then
+      use_high_resolution = ! use_high_resolution
+   end if
+
+   -- now dynamically adjust parameters, if needed
+   if use_2_channels != prev_use_2_channels then
+      prev_use_2_channels = use_2_channels
+      if use_2_channels == TRUE then
+         adc_nchannel = 2
+      else
+         adc_nchannel = 1
+      end if
+      -- re-init/setup the whole
+      adc_init()
+      -- feedback
+      strobe()
+   end if
+
+   if use_high_resolution != prev_use_high_resolution then
+      prev_use_high_resolution = use_high_resolution
+      adc_high_resolution = use_high_resolution
+      -- re-init/setup the whole
+      adc_init()
+      -- feedback
+      strobe()
+   end if
+
+   -- debounce push buttons
+   delay_1ms(400)
+
+   var byte channel
+   for adc_nchannel using channel loop
+      -- report
+      print_string(serial_hw_data,chanstr)
+      print_byte_dec(serial_hw_data,channel)
+      print_string(serial_hw_data,prefix)
+
+      if use_high_resolution == true then
+         -- get value in high resolution
+         wmeasure = adc_read(channel)
+         print_string(serial_hw_data,highstr)
+          print_string(serial_hw_data,suffix)
+         print_word_dec(serial_hw_data,wmeasure)
+      else
+         -- get value
+         bmeasure = adc_read_low_res(channel)
+         print_string(serial_hw_data,lowstr)
+         print_string(serial_hw_data,suffix)
+         print_byte_dec(serial_hw_data,bmeasure)
+      end if
+
+      print_crlf(serial_hw_data)
+   end loop
+
+end loop
+
=======================================
--- /trunk/CHANGELOG    Thu Mar  4 13:12:28 2010
+++ /trunk/CHANGELOG    Sat Mar 13 02:08:36 2010
@@ -18,6 +18,11 @@

 peripherals:
  - serial_hw libraries support 16 bit baudrate register of eusart.
+ - ADC libraries now supports PIC with 28 analog pins. Some Vref setups have been fixed,
+   callstack usage optimized, Vref- only can now be selected
+ - ADC configuration can now be dynamic and eval'ed at runtime when using
+ "const bit ADC_DYNAMIC = true", allowing to change number of channels, resolution, ...
+ -

 jal extension:
  -
@@ -27,6 +32,8 @@
- Added blink-a-led samples for 16 extended midrange PICS (18/19[L]F18/19xx) - Added 2 samples of serial comms with 16F1937 (different libraries and oscillators)
  - Added blink-a-led samples for 18f87j50 group of PICs
+ - Added static and dynamic ADC setup samples for 18f4550
+ -

 compiler:
  -
=======================================
--- /trunk/TORELEASE    Fri Feb 19 12:26:10 2010
+++ /trunk/TORELEASE    Sat Mar 13 02:08:36 2010
@@ -692,8 +692,10 @@
 sample/18f4523_blink.jal
 sample/18f4525_blink.jal
 sample/18f4539_blink.jal
+sample/18f4550_adc.jal
 sample/18f4550_blink.jal
 sample/18f4550_data_eeprom.jal
+sample/18f4550_dynamic_adc.jal
 sample/18f4550_i2c_hw_l0.jal
 sample/18f4550_i2c_hw_l1.jal
 sample/18f4550_i2c_hw_slave_check_bus.jal

--
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