Revision: 1772
Author: sebastien.lelong
Date: Fri Mar 12 12:39:36 2010
Log: new tested 18f4550 ADC sample + fix wrong pring statements
http://code.google.com/p/jallib/source/detail?r=1772

Added:
 /trunk/sample/18f4550_adc.jal
Modified:
 /trunk/sample/16f876a_adc.jal
 /trunk/sample/16f877_adc.jal

=======================================
--- /dev/null
+++ /trunk/sample/18f4550_adc.jal       Fri Mar 12 12:39:36 2010
@@ -0,0 +1,105 @@
+-- Title: Sample showing how to use 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. It regurlarly performs an +-- Analog-to-Digital Conversion, on pin RA0/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 channels AN0, 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:
+--
+--     5V
+--     |
+--     Z
+--     Z R1        -----------
+--     Z           |
+--     |------ AN0-|   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()
+
+
+-- ok, now let's configure ADC
+-- we want to measure using high resolution
+-- (that's our choice, we could use low resolution as well)
+const bit ADC_HIGH_RESOLUTION = low
+-- we said we want 1 analog channel...
+const byte ADC_NCHANNEL = 1
+-- and no voltage reference
+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 wprefix[] = "Result in high resolution: "
+const byte bprefix[] = "Result in low  resolution: "
+forever loop
+   -- get ADC result, on channel 0
+   -- this means we're currently reading on pin AN0 !
+
+   -- access results in high resolution
+   wmeasure = adc_read(0)
+   -- wmeasure contains the result, as a word (byte*2)
+   print_string(serial_hw_data,wprefix)
+   print_word_dec(serial_hw_data,wmeasure)
+   print_crlf(serial_hw_data)
+
+   -- though we are in high resolution mode,
+   -- we can still get a result as a byte, as though
+   -- it were in low resolution.
+   bmeasure = adc_read_low_res(0)
+   print_string(serial_hw_data,bprefix)
+   print_byte_dec(serial_hw_data,bmeasure)
+   print_crlf(serial_hw_data)
+
+   -- and sleep a litte to prevent flooding serial...
+   delay_1ms(200)
+end loop
+
=======================================
--- /trunk/sample/16f876a_adc.jal       Sun Jul 19 11:54:58 2009
+++ /trunk/sample/16f876a_adc.jal       Fri Mar 12 12:39:36 2010
@@ -90,7 +90,7 @@
    -- we can still get a result as a byte, as though
    -- it were in low resolution.
    bmeasure = adc_read_low_res(0)
-   print_string(serial_hw_data,wprefix)
+   print_string(serial_hw_data,bprefix)
    print_byte_dec(serial_hw_data,bmeasure)
    print_crlf(serial_hw_data)

=======================================
--- /trunk/sample/16f877_adc.jal        Sun Jul 19 11:54:58 2009
+++ /trunk/sample/16f877_adc.jal        Fri Mar 12 12:39:36 2010
@@ -90,7 +90,7 @@
    -- we can still get a result as a byte, as though
    -- it were in low resolution.
    bmeasure = adc_read_low_res(0)
-   print_string(serial_hw_data,wprefix)
+   print_string(serial_hw_data,bprefix)
    print_byte_dec(serial_hw_data,bmeasure)
    print_crlf(serial_hw_data)

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