Hi
Yes CAN code is still very much same as sample code - just a lot extra other stuff extra.
**** do you define the proper msglen after the config canmob ? ****
I did specify the nr of bytes to send as 2 and also in the config Canmod 2 the tx MOB I specified msglen = 2 .
Problem is that the message length might vary so I prefer the compiler to send the correct qty of bytes per data type .
the sending itself is done in a do - loop so still can not see why first 2 times through the loop it will send a 2 byte message and then afterwards only a single byte message .
Just change your CANBUS sample code provided to send a word or long ( and nt just a BYTE ) and see if it works . That is basically my can code at this stage .
here is the CAN setup part in the TX program .
dim fets as word
Reset Getencoderv
On Can_int Can_int ' define the CAN interrupt
Enable Interrupts ' enable interrupts
Canreset ' reset can controller
Canclearallmobs ' clear alle message objects
Canbaud = 125000 ' use 125 KB
Config Canbusmode = Enabled ' enabled,standby,listening
Config Canmob = 0 , Bitlen = 11 , Idtag = &H0120 , Idmask = &H0120 , Msgobject = Receive , Msglen = 1 , Autoreply = Disabled 'first mob is used for receiving data
Config Canmob = 2 , Bitlen = 11 , Idtag = &H0121 , Msgobject = Disabled , Msglen = 2 ' this mob is used for sending data
Cangie = &B10111000 ' CAN GENERAL INTERRUPT and TX and RX and ERR
The int routine :
'multiple objects can generate an interrupt
Can_int:
_can_page = Canpage ' save can page because the main program can access the page too
Cangetints ' read all the interrupts into variable _can_mobints
For _can_int_idx = 0 To 14 ' for all message objects
If _can_mobints._can_int_idx = 1 Then ' if this message caused an interrupt
Canselpage _can_int_idx ' select message object
If Canstmob.5 = 1 Then ' we received a frame
_canid = Canid() ' read the identifier
If _canid = &H0120 Then Set Getencoderv
Breceived = Canreceive(stoor) ' read the data and store in stoor
Config Canmob = -1 , Bitlen = 11 , Msgobject = Receive , Msglen = 1 , Autoreply = Disabled , Clearmob = No
' reconfig with value -1 for the current MOB and do not set ID and MASK
Elseif Canstmob.6 = 1 Then 'transmission ready
Config Canmob = -1 , Bitlen = 11 , Msgobject = Disabled , Msglen = 1 , Clearmob = No
' reconfig with value -1 for the current MOB and do not set ID and MASK
Elseif Canstmob.0 = 1 Then 'ack error when sending data 'transmission ready
' Print #2 , "ERROR:" ; Hex(canstmob)
Config Canmob = -1 , Bitlen = 11 , Msgobject = Disabled , Msglen = 1 , Clearmob = No ' *** I tried this with msglen = 2 also ***
End If
End If
Next
Cansit1 = 0 : Cansit2 = 0 : Cangit = Cangit ' clear interrupt flags
Canpage = _can_page ' restore page
Return
The actual sending instruction :
Bok = Cansend(2 , fets ,2 ) ' send 2 bytes using MOB 2
Cheers
↧