![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
CAN a enviar_TLMoto
Fork of CANnucleo_Hello by
main.cpp@13:77261ea62081, 2016-06-26 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Sun Jun 26 14:14:01 2016 +0000
- Revision:
- 13:77261ea62081
- Parent:
- 12:4f5cec652e77
- Child:
- 14:ff9c77b97bad
first ack?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:c5e5d0df6f2a | 1 | #include "mbed.h" |
hudakz | 0:c5e5d0df6f2a | 2 | #include "CAN.h" |
hudakz | 0:c5e5d0df6f2a | 3 | |
Crazyaboutmachines | 12:4f5cec652e77 | 4 | // const unsigned int TX_ID = 0x101; |
Crazyaboutmachines | 12:4f5cec652e77 | 5 | const unsigned int TX_ID = 0x155; //em binario: 101010101 |
hudakz | 0:c5e5d0df6f2a | 6 | |
Crazyaboutmachines | 12:4f5cec652e77 | 7 | DigitalOut led(PA_5); |
hudakz | 11:07d927da1a94 | 8 | int ledState; |
Crazyaboutmachines | 13:77261ea62081 | 9 | CAN can1(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
Crazyaboutmachines | 13:77261ea62081 | 10 | CAN can2(PB_12, PB_13); // CAN Rx pin name, CAN Tx pin name |
hudakz | 11:07d927da1a94 | 11 | CANMessage txMsg; |
Crazyaboutmachines | 13:77261ea62081 | 12 | CANMessage rxMsg; |
hudakz | 11:07d927da1a94 | 13 | int counter = 0; |
hudakz | 0:c5e5d0df6f2a | 14 | |
hudakz | 0:c5e5d0df6f2a | 15 | int main() { |
Crazyaboutmachines | 13:77261ea62081 | 16 | can1.frequency(100000); // set bit rate to 1Mbps |
Crazyaboutmachines | 13:77261ea62081 | 17 | can2.frequency(100000); // set bit rate to 1Mbps |
Crazyaboutmachines | 12:4f5cec652e77 | 18 | led = 1; // turn LED on |
hudakz | 0:c5e5d0df6f2a | 19 | |
hudakz | 0:c5e5d0df6f2a | 20 | while(1) { |
Crazyaboutmachines | 12:4f5cec652e77 | 21 | wait(10); |
Crazyaboutmachines | 12:4f5cec652e77 | 22 | |
hudakz | 0:c5e5d0df6f2a | 23 | counter++; // increment counter |
Crazyaboutmachines | 12:4f5cec652e77 | 24 | // ledState = led.read(); // get led state |
Crazyaboutmachines | 12:4f5cec652e77 | 25 | ledState = 1; |
Crazyaboutmachines | 12:4f5cec652e77 | 26 | txMsg.clear(); // clear Tx message storage |
Crazyaboutmachines | 12:4f5cec652e77 | 27 | txMsg.id = TX_ID; // set ID (9 bits) |
Crazyaboutmachines | 12:4f5cec652e77 | 28 | txMsg << counter; // append first data item (32? bits?=4bytes) |
Crazyaboutmachines | 12:4f5cec652e77 | 29 | txMsg << ledState; // append second data item (total data lenght must be <= 8 bytes!) (=4bytes) |
Crazyaboutmachines | 12:4f5cec652e77 | 30 | |
Crazyaboutmachines | 13:77261ea62081 | 31 | |
Crazyaboutmachines | 13:77261ea62081 | 32 | can1.write(txMsg); |
Crazyaboutmachines | 13:77261ea62081 | 33 | // if(can1.write(txMsg)) // transmit message |
Crazyaboutmachines | 13:77261ea62081 | 34 | // led = 1; // turn LED on |
Crazyaboutmachines | 13:77261ea62081 | 35 | // else |
Crazyaboutmachines | 13:77261ea62081 | 36 | // led = 0; // turn LED off |
Crazyaboutmachines | 13:77261ea62081 | 37 | |
Crazyaboutmachines | 13:77261ea62081 | 38 | if(can2.read(rxMsg)) { |
Crazyaboutmachines | 13:77261ea62081 | 39 | //printf("Message received: %d\n", msg.data[0]); |
Crazyaboutmachines | 13:77261ea62081 | 40 | led = !led; |
Crazyaboutmachines | 13:77261ea62081 | 41 | } |
Crazyaboutmachines | 13:77261ea62081 | 42 | |
Crazyaboutmachines | 12:4f5cec652e77 | 43 | |
hudakz | 0:c5e5d0df6f2a | 44 | } |
Crazyaboutmachines | 12:4f5cec652e77 | 45 | } |