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 }
MCP2515_FILTER_EXTENDED
#define MCP2515_FILTER_EXTENDED(id)
Definition: can.h:157
can_send_message
uint8_t can_send_message(const can_t *msg)
Verschickt eine Nachricht über den CAN Bus.
can_t::length
uint8_t length
Anzahl der Datenbytes.
Definition: can.h:192
can_t::rtr
int rtr
Remote-Transmit-Request-Frame?
Definition: can.h:182
can_t
Datenstruktur zum Aufnehmen von CAN Nachrichten.
Definition: can.h:177
can_t::data
uint8_t data[8]
Die Daten der CAN Nachricht.
Definition: can.h:193
can_get_message
uint8_t can_get_message(can_t *msg)
Liest eine Nachricht aus den Empfangspuffern des CAN Controllers.
can.h
Header-Datei für das allgemeine CAN Interface.
uavcan::uint8_t
std::uint8_t uint8_t
Definition: std.hpp:24
BITRATE_125_KBPS
@ BITRATE_125_KBPS
Definition: can.h:88
can_t::extended
int extended
extended ID?
Definition: can.h:183
can_check_message
bool can_check_message(void)
Ueberpruefen ob neue CAN Nachrichten vorhanden sind.
main
int main(void)
Definition: main.c:81
can_t::flags
struct can_t::@136 flags
can_init
bool can_init(can_bitrate_t bitrate)
Initialisierung des CAN Interfaces.
can_t::id
uint32_t id
ID der Nachricht (11 oder 29 Bit)
Definition: can.h:180
MCP2515_FILTER
#define MCP2515_FILTER(id)
Definition: can.h:165
PROGMEM
const uint8_t can_filter[] PROGMEM
Definition: main.c:60
can_static_filter
void can_static_filter(const uint8_t *filter_array)
Setzt die Werte für alle Filter.


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02