Author: eur.van.andel
Date: Wed Oct 29 15:31:33 2008
New Revision: 499

Added:
    trunk/unvalidated/include/jal/rtc_isr_tmr0.jal
    trunk/unvalidated/sample/by_device/16f876a/sample_rtc_lcd.jal
Modified:
    trunk/unvalidated/include/jal/format.jal
    trunk/unvalidated/include/peripheral/usart/serial_hardware.jal

Log:
added real time clock with timer0 ISR + sample
added format_time_hms() to format.jal
dunno what the hell is wrong with serial_hardware.jal


Modified: trunk/unvalidated/include/jal/format.jal
==============================================================================
--- trunk/unvalidated/include/jal/format.jal    (original)
+++ trunk/unvalidated/include/jal/format.jal    Wed Oct 29 15:31:33 2008
@@ -380,7 +380,7 @@
  -- Displays a time in HH:MM notation at specified position
  -- Hours and minutes are specified separate
  --  
-----------------------------------------------------------------------------
-procedure format_time_bytes(volatile byte out device,byte in HH, byte in  
MM) is
+procedure format_time_hm(volatile byte out device,byte in HH, byte in MM)  
is
     var byte D10

     D10 = HH / 10
@@ -399,12 +399,42 @@
  end procedure
  --  
-----------------------------------------------------------------------------

+--  
-----------------------------------------------------------------------------
+-- Displays a time in HH:MM:SS notation at specified position
+-- Hours and minutes are specified separate
+--  
-----------------------------------------------------------------------------
+procedure format_time_hms(volatile byte out device,byte in HH, byte in MM,  
byte in SS) is
+   var byte D10
+
+   D10 = HH / 10
+   HH  = HH % 10
+
+   -- always dsiplay Zeroes
+   device = "0" + D10
+   device = "0" + HH
+   device = ":"
+
+   D10 = MM / 10
+   MM  = MM % 10
+
+   device = "0" + D10
+   device = "0" + MM
+   device = ":"
+
+   D10 = SS / 10
+   SS  = SS % 10
+
+   device = "0" + D10
+   device = "0" + SS
+end procedure
+--  
-----------------------------------------------------------------------------
+

  --  
-----------------------------------------------------------------------------
  -- Displays a time in HH:MM notation at specified position
  -- the input is specified in minutes
  --  
-----------------------------------------------------------------------------
-procedure format_time_word(volatile byte out device,word in minutes) is
+procedure format_time_hm_word(volatile byte out device,word in minutes) is
     var byte D10
     var byte HH
     var byte MM
@@ -413,6 +443,6 @@
     MM = byte(minutes % 60)
     HH = HH % 24

-   format_time_bytes(device, HH, MM)
+   format_time_hm(device, HH, MM)
  end procedure
  --  
-----------------------------------------------------------------------------

Added: trunk/unvalidated/include/jal/rtc_isr_tmr0.jal
==============================================================================
--- (empty file)
+++ trunk/unvalidated/include/jal/rtc_isr_tmr0.jal      Wed Oct 29 15:31:33 2008
@@ -0,0 +1,63 @@
+-- Title: Real Time Clock running from ISR on TMR0
+-- Author: Roman Black, Copyright (c) 2003..2008, all rights reserved.
+-- Adapted-by: Eur van Andel
+-- 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)
+--
+-- Sources:  http://www.romanblack.com/one_sec.htm,  
http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
+--
+-- Description: This Interrupt Service Routine updates the variable  
seconds about once per second.
+-- The precision is 0.2 ppm, the accuracy depends on the Xtal used.
+--
+-- Notes: This is the Bresenham Line Algorithm, invented at IBM in 1962,  
which gets an accurate end result
+-- by summing the small errors resulting of taking discrete steps and  
correcting when the error gets too large.
+-- This means that individual second lengths may show some jitter, but  
that long-term timekeeping is accurate
+--
+
+-- 20 MHz, so 200ns/clock, so 5 000 000 clocks/second
+-- the RTC has three bytes: bres_hi, bres_mid, bres_lo
+-- timer 0 runs on internal clock speed and interrupts on overflow
+-- every timer 0 interrupt decreases bres_mid by one.
+-- the  2 000 000 value is added when bres_hi en bres_mid are zero
+-- note that remainder is added to bres_lo, which cab overflow in bres_mid
+-- this keeps clock count accurate, although interrupts happen every 256  
clock counts
+-- the ISR subtracts 1 from the mid byte. It first checks
+-- the mid byte for zero and borrows a bit from bres_hi if needed
+
+procedure RTC() is
+pragma interrupt
+assembler         -- ISR should be as small as possible, so assembler
+local int_exit       -- labels for jumping
+
+   bcf   INTCON_TMR0IF     -- clear the timer 0 interrupt flag
+   movf  bres_mid,f        -- mid == 0 ?
+   btfsc STATUS_Z          -- not zero?
+   decf  bres_hi, f        -- zero, borrow one from high byte
+   decfsz bres_mid, f       -- this is the count of the interrupts
+   goto  int_exit          -- mid byte is not zero, so exit
+   movf  bres_hi, f        -- mid == 0, might hi == 0 too ?
+   btfss STATUS_Z
+   goto  int_exit          -- high byte not zero, so exit
+                           -- high byte zero, mid also zero
+                           -- we need to load the one-second timer
+
+   movlw hi                -- add  2 000 000 to hi-mid-lo
+   movwf bres_hi           -- hi & mid are both zero
+   movlw mid
+   movwf bres_mid
+   movlw lo                -- lo is not zero, and must be added
+   addwf bres_lo, f        -- bres_lo can overflow
+   btfsc STATUS_C          -- does it?
+   incf  bres_mid, f       -- yes, but mid was 0x4B, so will not overflow
+   incf  seconds, f        -- main routine should check seconds
+                           -- and call calendar when seconds > 59
+                           -- and take care of minutes, etc.
+int_exit:
+end assembler
+end procedure                   -- end of ISR
+
+
+
+

Modified: trunk/unvalidated/include/peripheral/usart/serial_hardware.jal
==============================================================================
--- trunk/unvalidated/include/peripheral/usart/serial_hardware.jal       
(original)
+++ trunk/unvalidated/include/peripheral/usart/serial_hardware.jal      Wed Oct 
 
29 15:31:33 2008
@@ -174,6 +174,10 @@
     TXREG = data
  end procedure

+procedure serial_send'put(byte in data) is      -- serial_hw_data gives  
trouble?
+   serial_hw_write(data)
+end procedure
+

  -- like Serial_H_write, but then with a word as input
  -- The MSB is outputed first

Added: trunk/unvalidated/sample/by_device/16f876a/sample_rtc_lcd.jal
==============================================================================
--- (empty file)
+++ trunk/unvalidated/sample/by_device/16f876a/sample_rtc_lcd.jal       Wed Oct 
 
29 15:31:33 2008
@@ -0,0 +1,197 @@
+-- Title: Sample program to show usage of rtc_isr
+-- Author: Eur van Andel, Copyright (c) 2008, all rights reserved.
+-- 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)
+--
+-- Sources:  http://www.romanblack.com/one_sec.htm,  
http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
+--
+-- Description: This program shows how to handle Real Time in a PIC,  
complete with setting the time
+-- and storing it in EEPROM so the next startup won't produce a blinking  
12:00.
+-- Without battery backup nothing can never keep time, however.
+--
+
+
+-- ------------------- SELECT PIC ----------------------------
+include 16f876a
+
+pragma target clock     20_000_000           -- 20 MHz xtal
+pragma target OSC       HS                   -- high speed
+pragma target WDT       disabled             -- no watchdog, please
+pragma target LVP       disabled             -- no low voltage programming
+
+enable_digital_io()                          -- no analog pins used in  
this sample
+
+OPTION_REG_T0CS = 0                          -- TMR0 on internal clock
+OPTION_REG_PSA  = 1                          -- so no prescaler for TMR0  
(= default)
+
+-- ------------------------ LIBRARIES ------------------------
+
+var volatile byte seconds                 -- variables used in ISR
+
+-- RTC ISR constants:
+--                      0x4C 0x4B 0x40  5 000 000  20 MZz xtal
+--                      0x0F 0x42 0x40  1 000 000   4 MHz int osc
+--                      0x12 0x13 0xD0  1 250 000   20 MHz, prescaler = 4
+
+const hi  = 0x4C  -- 2 000 000 for 20 MHz xtal and prescaler = 4
+const mid = 0x4B
+const lo  = 0x41  -- ideally modified in timeset too
+
+var volatile byte bres_hi  = hi
+var volatile byte bres_mid = mid
+var volatile byte bres_lo  = lo
+
+
+include rtc_isr_tmr0
+
+var byte minutes
+var byte hours
+
+var byte date
+var byte month
+var word year
+
+include now                      -- sets the time to system time + two  
minutes = compile and burn time
+                                 -- should be python-generated just before  
compiling
+pragma eedata 250                -- to check if first boot after  
programming
+
+
+
+include delay                    -- used by LCD lib
+
+const byte  LCD_ROWS = 4
+const byte  LCD_CHARS = 20
+var   bit   LCD_RS           is pin_b5       -- LCD command/data select.
+var   bit   LCD_EN           is pin_b4       -- LCD data trigger
+var   byte  LCD_dataport     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
+lcd_init()
+
+include print                                -- nice formatted output
+include format                               -- more nice formatting
+include pic_data_eeprom
+
+
+-- ------------------------ PROCEDURES ------------------------
+
+
+procedure calendar() is      -- should be called at least once a minute
+   var byte max_date = 31
+                              -- and only if seconds > 59
+   while seconds > 59 loop    -- if seconds < 60, nor harm is done
+      seconds = seconds - 60
+      minutes = minutes + 1
+   end loop
+
+   if minutes > 59 then
+      minutes = minutes - 60  -- we assume calendar is called every 3 min
+      hours = hours + 1
+      if TMR0 < 50 then    -- so no messing with ISR
+         data_eeprom_write(0, minutes)     -- reset first boot marker
+         data_eeprom_write(1, hours)
+      end if
+   end if
+
+   if hours > 23 then
+      hours = 0
+      date = date + 1
+      if TMR0 < 50 then
+         data_eeprom_write(2, date)
+      end if
+
+      if date > 26 then
+         if month < 8 then
+            if month % 2 == 1 then
+               max_date = 31     -- jan, mar, may, july
+            else
+               if month == 2 then
+                  if year % 4 == 0 then
+                     max_date = 29  -- feb, leap year, good thru 2099 AD
+                  else
+                     max_date = 28  -- feb, no leap year
+                  end if
+               else
+                   max_date = 30    -- apr, june
+               end if
+            end if
+         else
+            if month % 2 == 0 then
+               max_date = 31     -- aug, oct, dec
+            else
+               max_date = 30     -- sept, nov
+            end if
+         end if
+      end if
+
+      if date > max_date then
+         date = 1
+         month = month + 1
+         if month == 13 then
+            month = 1
+            year = year + 1
+            if TMR0 < 50 then
+               data_eeprom_write_word(4, year)
+            end if
+         end if
+         if TMR0 < 50 then
+            data_eeprom_write(3, month)
+         end if
+      end if
+   end if
+end procedure        -- %%%%%%%%%%%%%% CALENDAR %%%%%%%%%%%%%%%%%%%
+
+
+
+-- -------------------------- START PROGRAM -----------------
+
+if data_eeprom(0) != 250 then       -- not first boot after programming
+   seconds = 0                      -- time isn't accurate anyway
+   data_eeprom_read(0, minutes)     -- get old time from eeprom
+   data_eeprom_read(1, hours)
+   data_eeprom_read(2, date)
+   data_eeprom_read(3, month)
+   data_eeprom_read_word(4, year)
+end if                              -- if 250, now.jal is used for setting  
clock
+
+INTCON_TMR0IE = on                  -- if your PIC freezes, move these  
lines
+INTCON_GIE = on                     -- to see if the ISR is messed up
+
+
+lcd_clearscreen()
+lcd_setcursor(0,0)
+const byte str1[] = "Time RTC sample"
+print_string(lcd, str1)
+for 10 loop
+   lcd_setcursor(1,0)
+   print_byte_dec(lcd, date)
+   lcd = "-"
+   print_byte_dec(lcd, month)
+   lcd = "-"
+   print_word_dec(lcd, year)
+   lcd = " "
+   if seconds > 59 then
+      calendar()
+   end if
+   format_time_hms(lcd, hours, minutes, seconds)
+   delay_100ms(5)                              -- 5s splash screen
+end loop
+
+lcd_clearscreen()
+forever loop
+   lcd_setcursor(0,0)
+   const byte str2[] = "The time is:"
+   print_string(lcd, str2)
+   if seconds > 59 then
+      calendar()
+   end if
+   format_time_hms(lcd, hours, minutes, seconds)
+   delay_100ms(2)                            -- for proper LCD 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to