Author: eur.van.andel
Date: Mon Oct  6 17:11:45 2008
New Revision: 366

Modified:
    trunk/unvalidated/include/external/temperature/temp_tc77.jal
    trunk/unvalidated/include/jal/format.jal
    trunk/unvalidated/sample/by_device/16f876a/test_tc77.jal

Log:
now TC77 also below zero and sdword formatting


Modified: trunk/unvalidated/include/external/temperature/temp_tc77.jal
==============================================================================
--- trunk/unvalidated/include/external/temperature/temp_tc77.jal        
(original)
+++ trunk/unvalidated/include/external/temperature/temp_tc77.jal        Mon Oct 
 6  
17:11:45 2008
@@ -32,7 +32,7 @@

  -- get raw temperature of TC77
  -- output in word, right justified
-procedure read_raw_tc77(word out raw) is
+procedure tc77_read_raw(word out raw) is
     raw = 0
     var bit a at raw : 0 = low

@@ -51,12 +51,12 @@
  -- 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
+-- no rounding, celsius fraction is chopped
+procedure tc77_read_celsius_sbyte(sbyte out temperature) is
+   var sword 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
+   var bit raw_sign  at raw: 12     -- zero if above 0C, 1 if below 0C
+   var sword tmp

     for 13 loop             -- 13 shifts, 12 bits
        raw = raw << 1
@@ -69,13 +69,10 @@

     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
+      tmp = 0b111_0000_0000_0000 + raw -- both above zero for compiler
        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

@@ -83,13 +80,13 @@
  -- 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
+-- best procedure if you want to display C in 0.1 accurate
+-- take care of rounding during printing
+procedure tc77_read_celsius_sword(sword out temperature) is
+   var sword 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
+   var bit raw_sign  at raw: 12  -- zero if above 0C, 1 if below 0C
+   var sdword tmp

     for 13 loop             -- 13 shifts, 12 bits
        raw = raw << 1
@@ -102,16 +99,16 @@

     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 = 0xFF_FF_F0_00              -- all ones, execpt for last 12
+      tmp = tmp + raw                  -- 16ths degC below zero, can't  
overflow
        tmp = tmp * 625                  -- celsius * 10.000
-      temperature = sword(tmp / 100)    -- celsius*100
-      temp_sign = true
+      temperature = sword(tmp / 100)   -- celsius*100
+--      temp_sign = true
     else
-      tmp = raw                        -- 16th degrees C
+      tmp = raw                        -- 16ths degrees C
        tmp = tmp * 625                  -- celsius * 10.000
-      temperature = sword(tmp / 100)    -- celsius*100
-      temp_sign = false
+      temperature = sword(tmp / 100)   -- celsius*100
+--      temp_sign = false
     end if
  end procedure

@@ -119,11 +116,10 @@
  -- 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
+procedure tc77_read_celsius_sdword(sdword out temperature) is
+   var sword 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
+   var bit raw_sign  at raw: 12  -- zero if above 0C, 1 if below 0C

     for 13 loop             -- 13 shifts, 12 bits
        raw = raw << 1
@@ -134,15 +130,13 @@
        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
+   if raw_sign then                    -- below zero C
+      asm bcf raw_sign                 -- clear bit 13
+      temperature = 0xFF_FF_F0_00      -- all ones, execpt for last 12
+      temperature = temperature + raw  -- 16ths degC below zero, can't  
overflow
+      temperature = temperature * 625  -- celsius * 10.000
     else
-      temperature = sdword(raw)           -- 16th degrees C
-      temperature = temperature * 625     -- celsius * 10.000
-      temp_sign = false
+      temperature = raw                -- 16th degrees C
+      temperature = temperature * 625  -- celsius * 10.000
     end if
  end procedure

Modified: trunk/unvalidated/include/jal/format.jal
==============================================================================
--- trunk/unvalidated/include/jal/format.jal    (original)
+++ trunk/unvalidated/include/jal/format.jal    Mon Oct  6 17:11:45 2008
@@ -17,6 +17,7 @@
  -- .     var sword BHL = -684
  -- .
  -- .     -- send the signed word to the LCD display
+-- .     -- total field width 6 chars, including sign and decimal
  -- .     -- with 2 digits behind the decimal point
  -- .     format_sword_dec(LCD_char,BHL,6,2)
  -- .
@@ -116,9 +117,9 @@
  -- writes decimal formatted word to output device
  -- the output can be specified as a fraction
  --   n_tot = the total length (including sign and decimal point)
---   N2   = number of digits behind the point
+--   n2   = number of digits behind the point
  --  
-----------------------------------------------------------------------------
-procedure format_word_dec(volatile byte out device, word in data, byte in  
n_tot, byte in N2) is
+procedure format_word_dec(volatile byte out device, word in data, byte in  
n_tot, byte in n2) is

     const word C10000 = 10000
     const word C1000  = 1000
@@ -133,6 +134,89 @@
     if n_tot >=5 then
        digit = byte(data / C10000)
        data  = data % C10000
+      _write_digit(device, digit, n2==4)
+   end if
+
+   if n_tot >= 4 then
+      digit = byte(data / C1000)
+      data  = data % C1000
+      _write_digit(device, digit, n2==3)
+   end if
+
+   if n_tot >= 3 then
+      digit = byte(data / C100)
+      data  = data % C100
+      _write_digit(device, digit, n2==2)
+   end if
+
+   if n_tot >= 2 then
+      digit = byte(data / C10)
+      data  = data % C10
+      _write_digit(device, digit, n2==1)
+   end if
+
+   -- last digit must always be written
+   _format_digit = byte(data)
+   _write_real_digit(device)
+end procedure
+--  
-----------------------------------------------------------------------------
+
+--  
-----------------------------------------------------------------------------
+-- writes decimal formatted dword to output device
+-- the output can be specified as a fraction
+--   n_tot = the total length (including sign and decimal point)
+--   n2   = number of digits behind the point
+--  
-----------------------------------------------------------------------------
+procedure format_dword_dec(volatile byte out device, dword in data, byte  
in n_tot, byte in n2) is
+
+   const dword C1_000_000_000    = 1_000_000_000
+   const dword C100_000_000      = 100_000_000
+   const dword C10_000_000       = 10_000_000
+   const dword C1_000_000        = 1_000_000
+   const dword C100_000          = 100_000
+   const word  C10_000           = 10_000
+   const word  C1000             = 1000
+   const word  C100              = 100
+   const word  C10               = 10
+   var byte digit
+
+   -- start with spaces as a leader
+   _format_leader = " "
+
+   -- determine the next digit and display it
+   if n_tot >=10 then
+      digit = byte(data / C1_000_000_000)
+      data  = data % C1_000_000_000
+      _write_digit(device, digit, N2==9)
+   end if
+
+   if n_tot >=9 then
+      digit = byte(data / C100_000_000)
+      data  = data % C10_000_000
+      _write_digit(device, digit, N2==8)
+   end if
+
+   if n_tot >=8 then
+      digit = byte(data / C10_000_000)
+      data  = data % C10_000_000
+      _write_digit(device, digit, N2==7)
+   end if
+
+   if n_tot >=7 then
+      digit = byte(data / C1_000_000)
+      data  = data % C1_000_000
+      _write_digit(device, digit, N2==6)
+   end if
+
+   if n_tot >=6 then
+      digit = byte(data / C100_000)
+      data  = data % C100_000
+      _write_digit(device, digit, N2==5)
+   end if
+
+   if n_tot >=5 then
+      digit = byte(data / C10_000)
+      data  = data % C10_000
        _write_digit(device, digit, N2==4)
     end if

@@ -160,7 +244,6 @@
  end procedure
  --  
-----------------------------------------------------------------------------

-
  --  
-----------------------------------------------------------------------------
  -- writes decimal formatted signed byte to output device
  -- the output can be specified as a fraction
@@ -209,6 +292,27 @@
  end procedure
  --  
-----------------------------------------------------------------------------

+--  
-----------------------------------------------------------------------------
+-- writes decimal formatted signed dword to output device
+-- the output can be specified as a fraction
+--   n_tot = the total length (including sign and decimal point)
+--   n2   = number of digits behind the point
+--  
-----------------------------------------------------------------------------
+procedure format_sdword_dec(volatile byte out device, sdword in data, byte  
in n_tot, byte in n2) is
+
+   var bit sign at data:31
+
+   if sign then
+      _format_sign = "-"
+      data = -data
+   else
+      _format_sign = " "
+   end if
+
+   _format_signed = true
+   format_dword_dec(device, data, n_tot-1, N2)
+end procedure
+--  
-----------------------------------------------------------------------------

  --  
-----------------------------------------------------------------------------
  -- writes hex formatted byte to output device

Modified: trunk/unvalidated/sample/by_device/16f876a/test_tc77.jal
==============================================================================
--- trunk/unvalidated/sample/by_device/16f876a/test_tc77.jal    (original)
+++ trunk/unvalidated/sample/by_device/16f876a/test_tc77.jal    Mon Oct  6  
17:11:45 2008
@@ -23,13 +23,16 @@


  -- ------------------- INCLUDE LIBRARIES --------------------------
+var bit  LCD_RS           is pin_b5      -- LCD command/data select.
+var bit  lcd_rs_direction is pin_b5_direction
+var bit  LCD_EN           is pin_b4      -- LCD data trigger
+var bit  lcd_en_direction is pin_b4_direction

-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
+var byte  lcd_dataport    is portb_low   -- LCD data nibble
+var byte  lcd_dataport_direction is portb_low_direction
  include lcd_hd44780_4                     -- LCD library with 4 data lines
  include print                             -- nice formatted output
+include format                            -- more nice formatted output

  var bit CS                 is pin_a0      -- chip select of TC77, active  
low
  CS                         = high
@@ -38,7 +41,7 @@
  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
+include temp_tc77                         -- TC77 library

  -- ------------------- VARIABLES --------------------------------------

@@ -52,29 +55,31 @@
  lcd_clearscreen()

  forever loop
-   lcd_set_cursor(0,0)
+   lcd_setcursor(0,0)
     CS = low
-   read_raw_tc77(temperature_raw)
+   tc77_read_raw(temperature_raw)
     CS = high
     print_word_binary(lcd, temperature_raw)

-   lcd_set_cursor(1,0)
+   lcd_setcursor(1,0)
     CS = low
-   read_celsius_sbyte_tc77(temperature_8)
+   tc77_read_celsius_sbyte(temperature_8)
     CS = high
-   print_sbyte_dec(lcd, temperature_8)
+   print_sbyte_dec(lcd, temperature_8)    -- prints minus if below 0

-   lcd_set_cursor(0, 20)                   -- row 3, position = 1
+   lcd_setcursor(0, 20)                   -- row 3, position = 1
     CS = low
-   read_celsius_sword_tc77(temperature_16)
+   tc77_read_celsius_sword(temperature_16)
     CS = high
-   _print_suniversal_dec(lcd, temperature_16, 10000, 2)
+   format_sword_dec(lcd, temperature_16, 5, 2)  -- width 6: -xx.xx or  
xxx.xx
+   lcd =" "

-   lcd_set_cursor(1, 20)                   -- row 4, position = 1
+   lcd_setcursor(1, 20)                   -- row 4, position = 1
     CS = low
-   read_celsius_sdword_tc77(temperature_32)
+   tc77_read_celsius_sdword(temperature_32)
     CS = high
-   _print_suniversal_dec(lcd, temperature_32, 100000000, 5)
+   format_sdword_dec(lcd, temperature_32, 7, 4)  -- 8 wide: -xx.xxxx or  
xxx.xxxx
+   lcd = " "

     delay_100ms(2)                         -- for proper display viewing


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