main.c
Go to the documentation of this file.
1 // coding: utf-8
2 
3 #include <avr/io.h>
4 #include <avr/pgmspace.h>
5 
6 #include "can.h"
7 
8 // -----------------------------------------------------------------------------
60 const uint8_t can_filter[] PROGMEM =
61 {
62  // Group 0
63  MCP2515_FILTER(0), // Filter 0
64  MCP2515_FILTER(0), // Filter 1
65 
66  // Group 1
67  MCP2515_FILTER_EXTENDED(0), // Filter 2
68  MCP2515_FILTER_EXTENDED(0), // Filter 3
69  MCP2515_FILTER_EXTENDED(0), // Filter 4
70  MCP2515_FILTER_EXTENDED(0), // Filter 5
71 
72  MCP2515_FILTER(0), // Mask 0 (for group 0)
73  MCP2515_FILTER_EXTENDED(0), // Mask 1 (for group 1)
74 };
75 // You can receive 11 bit identifiers with either group 0 or 1.
76 
77 
78 // -----------------------------------------------------------------------------
79 // Main loop for receiving and sending messages.
80 
81 int main(void)
82 {
83  // Initialize MCP2515
85 
86  // Load filters and masks
87  can_static_filter(can_filter);
88 
89  // Create a test messsage
90  can_t msg;
91 
92  msg.id = 0x123456;
93  msg.flags.rtr = 0;
94  msg.flags.extended = 1;
95 
96  msg.length = 4;
97  msg.data[0] = 0xde;
98  msg.data[1] = 0xad;
99  msg.data[2] = 0xbe;
100  msg.data[3] = 0xef;
101 
102  // Send the message
103  can_send_message(&msg);
104 
105  while (1)
106  {
107  // Check if a new messag was received
108  if (can_check_message())
109  {
110  can_t msg;
111 
112  // Try to read the message
113  if (can_get_message(&msg))
114  {
115  // If we received a message resend it with a different id
116  msg.id += 10;
117 
118  // Send the new message
119  can_send_message(&msg);
120  }
121  }
122  }
123 
124  return 0;
125 }
std::uint8_t uint8_t
Definition: std.hpp:24
uint8_t can_send_message(const can_t *msg)
Verschickt eine Nachricht über den CAN Bus.
#define MCP2515_FILTER(id)
Definition: can.h:165
int extended
extended ID?
Definition: can.h:183
uint8_t can_get_message(can_t *msg)
Liest eine Nachricht aus den Empfangspuffern des CAN Controllers.
bool can_check_message(void)
Ueberpruefen ob neue CAN Nachrichten vorhanden sind.
const uint8_t can_filter [] PROGMEM
Definition: main.c:60
uint32_t id
ID der Nachricht (11 oder 29 Bit)
Definition: can.h:180
struct can_t::@136 flags
uint8_t length
Anzahl der Datenbytes.
Definition: can.h:192
#define MCP2515_FILTER_EXTENDED(id)
Definition: can.h:157
int rtr
Remote-Transmit-Request-Frame?
Definition: can.h:182
uint8_t data[8]
Die Daten der CAN Nachricht.
Definition: can.h:193
Datenstruktur zum Aufnehmen von CAN Nachrichten.
Definition: can.h:177
bool can_init(can_bitrate_t bitrate)
Initialisierung des CAN Interfaces.
void can_static_filter(const uint8_t *filter_array)
Setzt die Werte für alle Filter.
int main(void)
Definition: main.c:81


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39