Author: eur.van.andel
Date: Thu Oct 2 12:18:06 2008
New Revision: 341
Added:
trunk/unvalidated/include/external/temperature/tc77.jal
trunk/unvalidated/sample/by_device/16f876a/test_tc77.jal
Modified:
trunk/unvalidated/include/jal/print.jal
Log:
addded TC77 temperature sensor lib and test program
Added: trunk/unvalidated/include/external/temperature/tc77.jal
==============================================================================
--- (empty file)
+++ trunk/unvalidated/include/external/temperature/tc77.jal Thu Oct 2
12:18:06 2008
@@ -0,0 +1,151 @@
+-- Title: TC77
+-- Author: Eur van Andel, [EMAIL PROTECTED] Copyright (c) 2008
+-- Compiler: =2.4h
+--
+-- 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 library reads the TC77 temperature sensor from
Microchip.
+-- The TC77 has a 12-bit plus sign temperature resolution of 0.0625�C
+-- per Least Significant Bit (LSb). It works via the SPI protocol
+-- Specs:
+-- 1C (max.) accuracy from +25�C to +65C,
+-- 2C (max.) accuracy from -40�C to +85C
+-- 3C (max.) accuracy from -55�C to +125C
+-- 2.7V to 5.5V Operating Range, Low Power: 250uA (typ.) Continuous
Conversion
+-- Mode, 0.1uA (typ.) Shutdown Mode
+-- ----------
+-- I have several years of experience with hundreds of these sensors.
+-- They work very well: fast, good accuracy, good repeatability.
+-- If they have an offset, it can easily be calibrated in software.
+-- The hard part of using this sensor is gettting it watertigtht.
+-- It is SMD, so it needs a PCB, and a cable. Dipping in resin or
shrinking in
+-- glue-covered shrinktube works.
+-- The SPI protocol is bit-banged here and can be used with any pins.
+-- Clock data in on rising edge of clock, program must pull CS low. This
allows
+-- for multiple TC77s on the same 2-wire databus. Put a 100nF decoupling
+-- capacitor close the the TC77. If you don't, you WILL get weird data.
+-- This library assumes two pins: global bit SCK, wired to SCK of device
and
+-- global bit SIO,wired to SIO of device. SCK should be output, SIO input.
+-- Writing the status_word is not supported here.
+--
+
+-- get raw temperature of TC77
+-- output in word, right justified
+procedure read_raw_tc77(word out raw) is
+raw = 0
+var bit a at raw : 0 = low
+
+for 13 loop
+ raw = raw << 1
+ SCK = low -- clock low
+ delay_1us
+ a = SIO
+ SCK = high -- clock high
+ delay_1us
+end loop
+
+end procedure
+
+
+-- read celsius temperature of TC77
+-- output in one sbyte with zero digits, so T = +/- xxx C
+-- last bit is 1C, range is -40C ... +125C
+procedure read_celsius_sbyte_tc77(sbyte out temperature) is
+var word raw = 0
+var bit a at raw : 0 = low
+var bit raw_sign at raw: 12
+var bit temp_sign at temperature: 7 -- zero if positive, one if
negative
+var word tmp
+
+for 13 loop -- 13 shifts, 12 bits
+ raw = raw << 1
+ SCK = low -- clock low
+ delay_1us
+ a = SIO
+ SCK = high -- clock high
+ delay_1us
+end loop
+
+if raw_sign then -- below zero C
+ asm bcf raw_sign -- clear bit 13
+ tmp = 0b1_0000_0000_0000 -- 4096
+ tmp = tmp - raw -- 16th degrees below zero
+ temperature = sbyte(tmp >> 4) -- now celsius, divided by 16
+ temp_sign = true
+else
+ temperature = sbyte(raw >> 4) -- TC77 is limited to +125C
+ temp_sign = false
+end if
+
+end procedure
+
+
+-- read celsius temperature of TC77
+-- output in one sword with 2 digits, so T = xxx.xx C
+-- last bit is 0.01C , range is -40C ... +125C
+procedure read_celsius_sword_tc77(sword out temperature) is
+var word raw = 0
+var bit a at raw : 0 = low
+var bit raw_sign at raw: 12
+var bit temp_sign at temperature: 15 -- zero if positive, one if
negative
+var bit sign
+var dword tmp
+
+for 13 loop -- 13 shifts, 12 bits
+ raw = raw << 1
+ SCK = low -- clock low
+ delay_1us
+ a = SIO
+ SCK = high -- clock high
+ delay_1us
+end loop
+
+if raw_sign then -- below zero C
+ asm bcf raw_sign -- clear bit 13
+ tmp = 0b1_0000_0000_0000 -- 4096
+ tmp = tmp - raw -- 16th degrees C below zero
+ tmp = tmp * 625 -- celsius * 10.000
+ temperature = sword(tmp / 100) -- celsius*100
+ temp_sign = true
+else
+ tmp = raw -- 16th degrees C
+ tmp = tmp * 625 -- celsius * 10.000
+ temperature = sword(tmp / 100) -- celsius*100
+ temp_sign = false
+end if
+
+end procedure
+
+
+-- read celsius temperature of TC77
+-- output in one sdword with 4 digits, so T = xxx.xxxx C
+-- last bit is 0.0001C , range is -40C ... +125C
+procedure read_celsius_sdword_tc77(sdword out temperature) is
+var word raw = 0
+var bit a at raw : 0 = low
+var bit raw_sign at raw: 12
+var bit temp_sign at temperature: 31 -- zero if positive, one if
negative
+
+for 13 loop -- 13 shifts, 12 bits
+ raw = raw << 1
+ SCK = low -- clock low
+ delay_1us
+ a = SIO
+ SCK = high -- clock high
+ delay_1us
+end loop
+
+if raw_sign then -- below zero C
+ asm bcf raw_sign -- clear bit 13
+ temperature = 0b1_0000_0000_0000 -- 4096
+ temperature = temperature - raw -- 16th degrees below zero
+ temperature = temperature * 625 -- celsius * 10.000
+ temp_sign = true
+else
+ temperature = sdword(raw) -- 16th degrees C
+ temperature = temperature * 625 -- celsius * 10.000
+ temp_sign = false
+end if
+
+end procedure
Modified: trunk/unvalidated/include/jal/print.jal
==============================================================================
--- trunk/unvalidated/include/jal/print.jal (original)
+++ trunk/unvalidated/include/jal/print.jal Thu Oct 2 12:18:06 2008
@@ -180,19 +180,6 @@
end procedure
-procedure _print_byte_binary(volatile byte out device, byte in data) is
-
-
- for 8 loop
- if ((data & 0x80) != 0) then
- device = "1"
- else
- device = "0"
- end if
- data = data * 2
- end loop
-
-end procedure
procedure _print_suniversal_dec(volatile byte out device, sdword in data,
sdword in digit_divisor, byte in digit_number) is
Added: trunk/unvalidated/sample/by_device/16f876a/test_tc77.jal
==============================================================================
--- (empty file)
+++ trunk/unvalidated/sample/by_device/16f876a/test_tc77.jal Thu Oct 2
12:18:06 2008
@@ -0,0 +1,81 @@
+-- Title: read TC77 temperatures, show on LCD
+-- Author: Eur van Andel, [EMAIL PROTECTED] Copyright (c) 2008
+-- Adapted-by:
+-- Compiler: =2.4h
+--
+-- 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 sample shows how to read the temperature in Celsius
+-- with different precisions from the TC77 sensor.
+-- It demonstrates the different routines in the TC77 library.
+--
+
+-- select PIC
+include 16f876a
+
+pragma target clock 20_000_000 -- 20 MHz xtal
+pragma target OSC HS
+pragma target WDT disabled -- no watchdog, please
+pragma target LVP disabled -- no low voltage programming
+
+enable_digital_io() -- no analog pins here
+
+
+-- ------------------- INCLUDE LIBRARIES --------------------------
+
+var bit LCD_RS is pin_b5 -- LCD command/data select.
+var bit LCD_EN is pin_b4 -- LCD data trigger
+var byte lcd_data_port is portb_low -- LCD data nibble
+portb_direction = all_output -- LCD data is portb_low
+include lcd_hd44780_4 -- LCD library with 4 data lines
+include print -- nice formatted output
+
+var bit CS is pin_a0 -- chip select of TC77, active
low
+CS = high
+pin_a0_direction = output
+var bit SIO is pin_a2 -- data in/out of TC77
+pin_a2_direction = input -- only used as input here
+var bit SCK is pin_a1 -- clock of TC77
+pin_a1_direction = output
+include tc77 -- TC77 library
+
+-- ------------------- VARIABLES --------------------------------------
+
+var word temperature_raw -- raw 13-bit value, with sign
+var sbyte temperature_8 -- signed Celsius, precision 1C
+var sword temperature_16 -- signed Celsius, precision 0.01C
+var sdword temperature_32 -- signed Celsius, precision
0.0001C
+
+-- --------------- START PROGRAM ---------------------------
+
+lcd_clearscreen()
+
+forever loop
+ lcd_set_cursor(0,0)
+ CS = low
+ read_raw_tc77(temperature_raw)
+ CS = high
+ print_word_binary(lcd, temperature_raw)
+
+ lcd_set_cursor(1,0)
+ CS = low
+ read_celsius_sbyte_tc77(temperature_8)
+ CS = high
+ print_sbyte_dec(lcd, temperature_8)
+
+ lcd_set_cursor(0, 20) -- row 3, position = 1
+ CS = low
+ read_celsius_sword_tc77(temperature_16)
+ CS = high
+ _print_suniversal_dec(lcd, temperature_16, 10000, 2)
+
+ lcd_set_cursor(1, 20) -- row 4, position = 1
+ CS = low
+ read_celsius_sdword_tc77(temperature_32)
+ CS = high
+ _print_suniversal_dec(lcd, temperature_32, 100000000, 5)
+
+ delay_100ms(2) -- for proper display viewing
+
+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
-~----------~----~----~----~------~----~------~--~---