CAN24

CAN BUS library module for Firewing R2 / PIC 24HJ128GP502 device based on Microchip application notes AN1249 and code examples CE227, CE228 and CE229 to be referenced. You must use a transciever with 3V levels as the MCP2562 that still requires the main voltage of 5V.

Firewing PIC24 CAN Adapter

Library can be found here : PIC24 CAN Library

Download the compressed folder and with Firewing IDE open dragged it on editor window, will create a folder named CAN24 in your \MCU16 UserLibrary

Library test program:

clock = 80
imports "can24-lib"
imports "can24-utils"

sub OnStart() handles OnStartup
   console.setbaudrate(Baudrate.Is38400)
end sub 

sub OnMessage(ID as uinteger, 
              byref Data() as byte, 
              Len as byte, 
              Frame as byte, 
              BuffN as byte) handles OnCANMessage
   console.write("** INCOMING NORMAL MESSAGE **",13,10)           
   console.write("Id     :",hex(id,8), 13,10)   
   console.write("Data   :")
   for i as byte = 0 to (len - 1)
      console.write(hex(data(i),2))
      if i<>(Len-1) then          
         console.write(",")   
      end if
   next                                  
   console.write(13,10)
   console.write("Frame  :",hex(Frame,2), 13,10)   
   console.write("BuffN  :",hex(BuffN,2), 13,10)   
end sub

sub OnRtrMessage(ID as uinteger, 
                 Frame as byte, 
                 BuffN as byte) handles OnCANRtrMessage
   console.write("** INCOMING RTR MESSAGE **",13,10)           
   console.write("Id     :",hex(id,8), 13,10)   
   console.write("Frame  :",hex(Frame,2), 13,10)   
   console.write("BuffN  :",hex(BuffN,2), 13,10)   
end sub



Sub Main()
   CANSetRxAcptMask    (2,strtomask("S1?"),1,1)    
   CANSetRxAcptFilter  (1,StrToCanID("S1"),1,1,2)
   CANStart()

   dim TestMsg(8) as byte
   TestMsg(0) = 7
   TestMsg(1) = 6
   TestMsg(2) = 5
   TestMsg(3) = 4
   TestMsg(4) = 3
   TestMsg(5) = 2
   TestMsg(6) = 1
   TestMsg(7) = 0


   while true           
      CANSendMsg(StrToCanID("X02"),TestMsg,8)   
      delayms(10000)
      console.write("## CAN RX DISABLED ##",13,10) 
      CANStop()
      delayms(10000)
      console.write("## CAN RX ENABLED ##",13,10) 
      CANStart()      
   end while

End Sub