Revision: 1720
Author: bvwelch
Date: Sat Feb 27 13:05:31 2010
Log: LCD CAN monitor for 4 different CAN-IDs, 18f2585 version

http://code.google.com/p/jallib/source/detail?r=1720

Added:
 /trunk/sample/18f2585_canid4_monitor.jal

=======================================
--- /dev/null
+++ /trunk/sample/18f2585_canid4_monitor.jal    Sat Feb 27 13:05:31 2010
@@ -0,0 +1,196 @@
+-- Title: CAN bus monitor for STERM or other LCD display
+-- Author: William Welch Copyright (c) 2009, all rights reserved.
+-- Sponsored by: Fiwihex www.fiwihex.com
+-- Compiler: 2.4
+--
+-- This file is part of jallib (http://jallib.googlecode.com)
+-- Released under the ZLIB license (http://www.opensource.org/licenses/zlib-license.html)
+--
+-- Description: -- Monitor CAN bus, using can_legacy library.
+--
+-- Our monitor can be configured to either participate with ACKs
+-- (normal mode), or operate in 'listen-only' mode.
+--
+-- In order for CAN bus hardware to work properly, there must
+-- be at least two active nodes on the bus.
+--
+-- So, in listen-only mode, you will need at least two other nodes
+-- or the CAN bus will not operate.
+--
+
+include 18f2585
+pragma target clock 20_000_000
+pragma target OSC  HS
+pragma target XINST disabled
+pragma target WDT  disabled
+pragma target LVP  disabled
+pragma target MCLR external
+include delay
+include format
+include print
+
+const byte banner[] = "  MON: 4 CAN-IDs"
+
+enable_digital_io()
+
+const down  = 0
+const green = 1
+const red   = 2
+
+var bit led      is pin_c2
+var bit led_dir  is pin_c2_direction
+
+led_dir = input
+led = low
+
+procedure LEDs(byte in x) is
+if x == down then
+   led_dir  = input
+elsif x == red then
+   led_dir  = output
+   led      = low
+elsif x == green then
+   led_dir  = output
+    led     = high
+end if
+end procedure
+
+const byte LCD_ROWS = 4
+const byte LCD_CHARS = 20
+var bit lcd_sterm_pin is pin_C0
+var bit lcd_sterm_pin_dir is pin_C0_direction
+include lcd_sterm_master
+
+-- ECAN controller is built-in to 18F2585
+-- but we will operate in 'legacy' mode 0.
+pin_B2 = high -- set CANTX high for recessive level
+pin_B2_direction = output
+pin_B3_direction = input
+include can_legacy
+include can_ascii
+
+can_reset()
+
+-- FIXME: use EEprom instead.
+-- 125K bps
+can_set_config(4, 0x9E, 3)
+CIOCON = 0x20; -- TXCAN, high when recessive
+
+-- in this example we combine the hardware buffers.
+RXB0CON = 0x04 -- rollover/ double-buffer
+RXB1CON = 0x00 -- all valid frames go to buffer #1.
+
+-- FIXME: use EEprom instead.
+-- RXB0 will match all msgs by default
+can_set_rxb0_mask(0,0,0,0)
+can_set_rxb0_filter0(0xff, 0xff, 0xff, 0xff)
+can_set_rxb0_filter1(0xff, 0xff, 0xff, 0xff)
+
+-- disable RXB1
+can_set_rxb1_mask(0xff,0xff,0xff,0xff)
+can_set_rxb1_filter2(0xff, 0xff, 0xff, 0xff)
+can_set_rxb1_filter3(0xff, 0xff, 0xff, 0xff)
+can_set_rxb1_filter4(0xff, 0xff, 0xff, 0xff)
+can_set_rxb1_filter5(0xff, 0xff, 0xff, 0xff)
+
+TXB0CON = 0;
+TXB1CON = 0;
+TXB2CON = 0;
+
+-- FIXME: use EEprom instead.
+CANCON = 0; -- normal mode
+
+-- wait for STERM Slave to power-up and initialize.
+for 10 loop
+   LEDs(green)
+   delay_100ms(2)
+   LEDs(red)
+   delay_100ms(2)
+end loop
+LEDs(down)
+
+lcd_home()
+lcd_clear_screen()
+lcd_home()
+print_string(lcd, banner)
+
+-- leave banner up for a while
+for 25 loop
+   LEDs(green)
+   delay_100ms(2)
+   LEDs(down)
+   delay_100ms(2)
+end loop
+
+-- monitor loop
+forever loop
+   var bit got_msg
+   var bit toggle
+   var byte a,b,c,d, i, len
+   var byte rxdata[8]
+   var byte hb[4]
+   var dword can_id
+
+   a = CANSTAT
+   b = COMSTAT
+   c = RXB0CON
+   d = RXB1CON
+   -- wiggle the top-left corner
+   lcd_home()
+   if toggle then
+      lcd = "/"
+   else
+      lcd = "-"
+   end if
+
+   -- now the status line
+   -- lcd_cursor_position(1,0)
+   -- print_byte_hex(lcd, a)
+   -- lcd = " "
+   -- print_byte_hex(lcd, b)
+   -- lcd = " "
+   -- print_byte_hex(lcd, c)
+   -- lcd = " "
+   -- print_byte_hex(lcd, d)
+   -- lcd = " "
+
+   -- check for a message
+   if toggle then
+      got_msg = can_receive(0, can_id, rxdata, len)
+   else
+      got_msg = can_receive(1, can_id, rxdata, len)
+   end if
+
+   if got_msg then
+      var byte row
+      row = byte(can_id) & 3
+
+      lcd_cursor_position(row,0)
+      -- print only the low 8-bits of CAN-ID
+      print_byte_hex(lcd, byte(can_id) )
+      if (hb[row] & 1) != 0 then
+         lcd = "/"
+      else
+         lcd = "-"
+      end if
+      hb[row] = hb[row] ^ 1
+
+      for 8 using i loop
+         var byte v
+         if i < len then
+            print_byte_hex(lcd, rxdata[i])
+         else
+            lcd = " "
+            lcd = " "
+         end if
+      end loop
+   end if
+
+   toggle = ! toggle
+   if toggle then
+      LEDs(green)
+    else
+       LEDs(down)
+   end if
+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