![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
CAN a enviar_TLMoto
Fork of CANnucleo_Hello by
main.cpp@12:4f5cec652e77, 2016-06-26 (annotated)
- Committer:
- Crazyaboutmachines
- Date:
- Sun Jun 26 13:59:00 2016 +0000
- Revision:
- 12:4f5cec652e77
- Parent:
- 11:07d927da1a94
- Child:
- 13:77261ea62081
CAN a enviar mensagens;
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; |
hudakz | 11:07d927da1a94 | 9 | CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name |
hudakz | 11:07d927da1a94 | 10 | CANMessage txMsg; |
hudakz | 11:07d927da1a94 | 11 | int counter = 0; |
hudakz | 0:c5e5d0df6f2a | 12 | |
hudakz | 0:c5e5d0df6f2a | 13 | int main() { |
Crazyaboutmachines | 12:4f5cec652e77 | 14 | can.frequency(100000); // set bit rate to 1Mbps |
Crazyaboutmachines | 12:4f5cec652e77 | 15 | led = 1; // turn LED on |
hudakz | 0:c5e5d0df6f2a | 16 | |
hudakz | 0:c5e5d0df6f2a | 17 | while(1) { |
Crazyaboutmachines | 12:4f5cec652e77 | 18 | wait(10); |
Crazyaboutmachines | 12:4f5cec652e77 | 19 | |
hudakz | 0:c5e5d0df6f2a | 20 | counter++; // increment counter |
Crazyaboutmachines | 12:4f5cec652e77 | 21 | // ledState = led.read(); // get led state |
Crazyaboutmachines | 12:4f5cec652e77 | 22 | ledState = 1; |
Crazyaboutmachines | 12:4f5cec652e77 | 23 | txMsg.clear(); // clear Tx message storage |
Crazyaboutmachines | 12:4f5cec652e77 | 24 | txMsg.id = TX_ID; // set ID (9 bits) |
Crazyaboutmachines | 12:4f5cec652e77 | 25 | txMsg << counter; // append first data item (32? bits?=4bytes) |
Crazyaboutmachines | 12:4f5cec652e77 | 26 | txMsg << ledState; // append second data item (total data lenght must be <= 8 bytes!) (=4bytes) |
Crazyaboutmachines | 12:4f5cec652e77 | 27 | |
hudakz | 10:66da8731bdb6 | 28 | if(can.write(txMsg)) // transmit message |
Crazyaboutmachines | 12:4f5cec652e77 | 29 | led = 1; // turn LED on |
hudakz | 10:66da8731bdb6 | 30 | else |
Crazyaboutmachines | 12:4f5cec652e77 | 31 | led = 0; // turn LED off |
Crazyaboutmachines | 12:4f5cec652e77 | 32 | |
hudakz | 0:c5e5d0df6f2a | 33 | } |
Crazyaboutmachines | 12:4f5cec652e77 | 34 | } |