Author: sebastien.lelong
Date: Wed Jan  7 11:43:23 2009
New Revision: 732

Added:
    trunk/sample/by_device/16f77/16f77_adc_lowres.jal
Modified:
    trunk/include/peripheral/adc/adc_hardware.jal

Log:
issue 31: support for 8-bits only ADC PIC + sample for 16f77

Modified: trunk/include/peripheral/adc/adc_hardware.jal
==============================================================================
--- trunk/include/peripheral/adc/adc_hardware.jal       (original)
+++ trunk/include/peripheral/adc/adc_hardware.jal       Wed Jan  7 11:43:23 2009
@@ -57,6 +57,11 @@

  include delay

+-- sanity check
+if adc_hardware_high_resolution & ! defined(ADRESH) then
+   pragma error          -- This pic does not seem to handle 10-bits ADC
+end if
+
  -- local variables

  var volatile byte _adcon0_shadow = 0
@@ -74,8 +79,17 @@
     delay_10us(_adc_acquisition_time)  -- wait acquisition time
     ADCON0_GO = true                          -- start conversion
     while ADCON0_GO loop end loop             -- wait till conversion  
finished
-   adc_byte = ADRESH                  -- read high byte
-                                     --   (low resolution is left  
justified !!
+   -- 2 cases:
+   --   - PIC has 10-bits ADC: since low resoltion if left
+   --     justified, need to read ADRESH
+   --   - PIC has only a 8-bits ADC: don't bother consider
+   --     justification, there's only one ADC register
+   if defined(ADRESH) == true then
+         adc_byte = ADRESH                  -- read high byte
+                                                --   (low resolution is left  
justified !!
+   else
+         adc_byte = ADRES
+   end if
     if target_clock <= (5_000_000 / 16)  then
        delay_10us(1)                    -- to satisfy 2*Tad for next  
conversion
     end if
@@ -124,7 +138,7 @@
     if ! adc_hardware_high_resolution then
        _adc_read_low_res(adc_chan,adc_value)
     else
-      _adc_read_low_res (adc_chan,adc_hbyte)
+      _adc_read_low_res(adc_chan,adc_hbyte)
        adc_value = ADRESL
        -- shift the 2 highest order bits form high byte to low byte
        asm  rrf   adc_hbyte,f

Added: trunk/sample/by_device/16f77/16f77_adc_lowres.jal
==============================================================================
--- (empty file)
+++ trunk/sample/by_device/16f77/16f77_adc_lowres.jal   Wed Jan  7 11:43:23  
2009
@@ -0,0 +1,86 @@
+-- Title: Sample showing how to use adc_hardware library
+-- Author: Sebastien Lelong, Copyright (c) 2008-2009, all rights reserved.
+-- Adapted-by:
+-- Compiler: >=2.4i
+--
+-- 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 channel AN0 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 channel AN0, so  
you can
+-- any voltage source connected to AN0 (no more 5V please). To test the  
whole, you can
+-- use a voltage divider:
+--             
+--     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 16f77
+-- 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            20_000_000
+pragma target OSC              HS
+-- Specify no postscaler, ie. really runs @8MHz
+-- no watchdog, please
+pragma target WDT              disabled
+
+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 just need only one ADC channel
+const adc_hardware_nchan = 1
+-- We won't use any external VRef, so measures are done
+-- according to PIC powering voltage
+const adc_hardware_nvref = 0
+-- The maximum resistance while measuring ADC is... (unit: ohms)
+-- Being accurate helps speeding up ADC acquisition
+const adc_hardware_rsource = 2_000
+-- In this example, we'll perform low resolution ADC: results are
+-- coded on 8bits
+const adc_hardware_high_resolution = false
+-- Now include the famous library
+include adc_hardware
+-- And initialize the whole with our parameters
+adc_init()
+
+
+-- will periodically send those chars
+var byte measure
+forever loop
+       -- get ADC result, on channel 0
+       measure = adc_read_low_res(0)
+       -- send it back through serial
+       serial_hw_write(measure)
+       -- and sleep a litte...
+       delay_1ms(200)
+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