TLMoto

Dependencies:   CANnucleo CANnucleo_Hello mbed

Fork of CANnucleo_Hello by Zoltan Hudak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * An example showing how to use the CANnucleo library:
00003  *
00004  * Two affordable (less than $3 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash),
00005  * (see [https://developer.mbed.org/users/hudakz/code/STM32F103C8T6_Hello/] for more details)
00006  * are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.).
00007  * CAN transceivers are not part of NUCLEO boards, therefore must be added by you.
00008  * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends.
00009  *
00010  * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/>
00011  *
00012  * NOTE: If you'd like to use the official NUCLEO boards comment out line 22
00013  *
00014  * The same code is used for both NUCLEO boards, but:
00015  *      For board #1 compile the example without any change.
00016  *      For board #2 comment out line 23 before compiling
00017  *
00018  * Once the binaries have been downloaded to the boards reset board #1.
00019  *
00020  */
00021 
00022 //#define TARGET_STM32F103C8T6  1     // uncomment this line when using STM32F103C8T6 boards!
00023 //#define BOARD1                1     // comment out this line when compiling for board #2
00024 
00025 
00026 
00027 const unsigned int RX_ID = 0x100;
00028 const unsigned int TX_ID = 0x101;
00029 
00030 #include "CANnucleo.h"
00031 #include "mbed.h"
00032 
00033 /*
00034  * To avaoid name collision with the CAN and CANMessage classes built into the mbed library
00035  * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace.
00036  * Remember to qualify them with the CANnucleo namespace.
00037  */
00038 CANnucleo::CAN          can(PA_11, PA_12);  // CAN Rx pin name, CAN Tx pin name
00039 CANnucleo::CANMessage   rxMsg;
00040 CANnucleo::CANMessage   txMsg;
00041 CANnucleo::CANMessage   throttle_txMsg;
00042 
00043 
00044 DigitalOut              led(PA_5);
00045 
00046 Timer                   timer;
00047 int                     counter = 0;
00048 volatile bool           msgAvailable = false;
00049 volatile bool           to_send = false;
00050 
00051 /**
00052  * @brief   'CAN receive-complete' interrup handler.
00053  * @note    Called on arrival of new CAN message.
00054  *          Keep it as short as possible.
00055  * @param
00056  * @retval
00057  */
00058 void onMsgReceived()
00059 {
00060     msgAvailable = true;
00061 }
00062 
00063 /**
00064  * @brief   Main
00065  * @note
00066  * @param
00067  * @retval
00068  */
00069 
00070 bool key_switch = 0;
00071 
00072 void flip()
00073 {
00074     key_switch = !key_switch;
00075     led = key_switch;
00076     txMsg.clear();
00077     txMsg.id = 1000;
00078     txMsg << key_switch;
00079     to_send=1;
00080     
00081     
00082 
00083     //to_send = 1;
00084 }
00085 
00086 Ticker flipper;
00087 
00088 typedef union can_union {
00089     int i[2];
00090     char bytes[8];
00091     float f[2];
00092 } data;
00093 float received[12];
00094 char missed_can=0;
00095 int main()
00096 
00097 {
00098   
00099     flipper.attach(&flip, 5);                 // turn on or off
00100     led=key_switch;
00101     timer.start();  // start timer
00102     char counter = 0;
00103     printf("started\r\n");  can.frequency(1000000);
00104     data data;                 // set bit rate to 1Mbps
00105     can.attach(&onMsgReceived);                 // attach 'CAN receive-complete' interrupt handler
00106     while(true) {
00107 
00108         if(msgAvailable) {
00109             msgAvailable = false;               // reset flag for next use
00110             rxMsg.clear();
00111             can.read(rxMsg);
00112             //printf("ID: \t%d\n\r", rxMsg.id);
00113             /*for(int i=0;i<rxMsg.len;i++){
00114                 printf("\t%x",rxMsg.data[i]);
00115                 }
00116               */
00117             data.bytes[0] = rxMsg.data[0];
00118             data.bytes[1] = rxMsg.data[1];
00119             data.bytes[2] = rxMsg.data[2];
00120             data.bytes[3] = rxMsg.data[3];
00121             data.bytes[4] = rxMsg.data[4];
00122 
00123             //printf("\r\n");
00124             //printf("%f\t%d\r\n", data.f[0], data.bytes[4]);
00125             // Filtering performed by software:
00126             received[counter]=data.f[0];
00127 
00128             counter++;
00129         }
00130         if(counter == 12) {
00131             printf("=======================\r\n");
00132             printf("%d: %f, %d: %f, %d: %f, %d: %f,%d: %f, %d: %f, %d: %f, %d: %f,%d: %f, %d: %f, %d: %f, %d: %f\r\n",1,received[0],2,received[1],
00133                     3,received[2],4,received[3],5,received[4],
00134                     6,received[5],7,received[6],8,received[7],
00135                     9,received[8],10,received[9],11,received[10],12,received[11]);
00136             counter = 0;
00137         }
00138         if(to_send) {
00139             to_send = 0;
00140             if(can.write(txMsg)) {
00141                 printf("sent message");
00142                 
00143             } else {
00144                 
00145                 printf("transmission error\n\r");
00146                 //to_send=1;
00147             }
00148         }
00149     }
00150 
00151 }
00152 
00153 
00154