Code Example for Sending a CAN Message

The following code example initializes the CAN interface, creates a new object “cantx_msg” of the type can_t and prepares the sending of a CAN message. In the last step, the message data bytes are set and the function “can_send_message” is called.

//  include  header  files
 
#include  <avr/io.h>
#include  <avr/pgmspace.h>
#include  "can.h"
 
//  initialize  the  CAN  interface,  set  baud  rate  to  125  kbps
 
can_init  (BITRATE_125_KBPS);1.7   CAN-Functions 21
//  to  send  a  message,  you  have  to  first  create  an  instance  of  the  structure can_t
 
can_t    cantx_msg;
 
//  now  you  can  set  the  variables  for  message  ID,  remote-transmit-request-frame,  length  etc
 
cantx_msg.id  =  0x123456;
cantx_msg.flags.rtr  =  0;
cantx_msg.flags.extended  =  1;
 
//  note  that  a  message  length  of  2  equals  4  hexadecimal  signs.
//  message  length  can  be  8  bytes  at  the  maximum
 
cantx_msg.length  =  2;
 
//  set  the  2  data  bytes
 
cantx_msg.data[0]  =  0xaa;
cantx_msg.data[1]  =  0xbb;
 
//  the  function  can_send_message  requires  the  address  of  the  cantx_msg  - object  (that  was  created  above)    as  a  parameter
 
can_send_message  (&cantx_msg);
en/hardware/avr-can/code_example_for_sending_a_can_message.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0