CAN a enviar_TLMoto

Dependencies:   CANnucleo mbed

Fork of CANnucleo_Hello by Zoltan Hudak

Committer:
Crazyaboutmachines
Date:
Sun Jun 26 15:11:08 2016 +0000
Revision:
14:ff9c77b97bad
Parent:
13:77261ea62081
Child:
15:b1d0bf2626ed
before timer interrupt

Who changed what in which revision?

UserRevisionLine numberNew 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
Crazyaboutmachines 14:ff9c77b97bad 15 Serial pc(SERIAL_TX, SERIAL_RX);
Crazyaboutmachines 14:ff9c77b97bad 16
hudakz 0:c5e5d0df6f2a 17 int main() {
Crazyaboutmachines 13:77261ea62081 18 can1.frequency(100000); // set bit rate to 1Mbps
Crazyaboutmachines 13:77261ea62081 19 can2.frequency(100000); // set bit rate to 1Mbps
Crazyaboutmachines 12:4f5cec652e77 20 led = 1; // turn LED on
Crazyaboutmachines 14:ff9c77b97bad 21 pc.printf("pcprintf check");
hudakz 0:c5e5d0df6f2a 22
hudakz 0:c5e5d0df6f2a 23 while(1) {
Crazyaboutmachines 12:4f5cec652e77 24 wait(10);
Crazyaboutmachines 12:4f5cec652e77 25
hudakz 0:c5e5d0df6f2a 26 counter++; // increment counter
Crazyaboutmachines 12:4f5cec652e77 27 // ledState = led.read(); // get led state
Crazyaboutmachines 12:4f5cec652e77 28 ledState = 1;
Crazyaboutmachines 12:4f5cec652e77 29 txMsg.clear(); // clear Tx message storage
Crazyaboutmachines 12:4f5cec652e77 30 txMsg.id = TX_ID; // set ID (9 bits)
Crazyaboutmachines 12:4f5cec652e77 31 txMsg << counter; // append first data item (32? bits?=4bytes)
Crazyaboutmachines 12:4f5cec652e77 32 txMsg << ledState; // append second data item (total data lenght must be <= 8 bytes!) (=4bytes)
Crazyaboutmachines 12:4f5cec652e77 33
Crazyaboutmachines 13:77261ea62081 34
Crazyaboutmachines 13:77261ea62081 35 can1.write(txMsg);
Crazyaboutmachines 13:77261ea62081 36 // if(can1.write(txMsg)) // transmit message
Crazyaboutmachines 13:77261ea62081 37 // led = 1; // turn LED on
Crazyaboutmachines 13:77261ea62081 38 // else
Crazyaboutmachines 13:77261ea62081 39 // led = 0; // turn LED off
Crazyaboutmachines 13:77261ea62081 40
Crazyaboutmachines 13:77261ea62081 41 if(can2.read(rxMsg)) {
Crazyaboutmachines 14:ff9c77b97bad 42 // pc.printf("Message received: %d\n", rxMsg.data[0]);
Crazyaboutmachines 14:ff9c77b97bad 43 pc.printf("Message received: %d\n");
Crazyaboutmachines 13:77261ea62081 44 led = !led;
Crazyaboutmachines 13:77261ea62081 45 }
Crazyaboutmachines 13:77261ea62081 46
Crazyaboutmachines 12:4f5cec652e77 47
hudakz 0:c5e5d0df6f2a 48 }
Crazyaboutmachines 12:4f5cec652e77 49 }