CAN_ex_STM32F103C8T6

Dependencies:   mbed CANMsg

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 mbed CAN API:
00003  *
00004  * Two affordable (about $2 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/CAN_Hello/>
00011  *
00012  * NOTE: When using an STM32F103C8T6 board uncomment line 22 and import the mbed-STM32F103C8T6 library 
00013  *
00014  * The same code is used for both mbed boards, but:
00015  *      For board #1 compile the example without any change.
00016  *      For board #2 comment out line 21 before compiling
00017  *
00018  * Once the binaries have been downloaded to the boards reset both boards at the same time.
00019  *
00020  */
00021 
00022 //#define TARGET_STM32F103C8T6    1       // uncomment this line and import the mbed-STM32F103C8T6 library when using STM32F103C8T6 boards!
00023 
00024 #define BOARD1                  1       // comment out this line when compiling for board #2
00025 
00026 #if defined(TARGET_STM32F103C8T6)
00027 #include "stm32f103c8t6.h"
00028 #define LED_PIN PC_13
00029 const int           OFF = 1;
00030 const int           ON = 0;
00031 #else
00032 #define LED_PIN LED1
00033 const int           OFF = 0;
00034 const int           ON = 1;
00035 #endif
00036 #if defined(BOARD1)
00037 const unsigned int  RX_ID = 0x100;
00038 const unsigned int  TX_ID = 0x101;
00039 #else
00040 const unsigned int  RX_ID = 0x101;
00041 const unsigned int  TX_ID = 0x100;
00042 #endif
00043 #include "mbed.h"
00044 #include "CANMsg.h"
00045 
00046 Serial              pc(USBTX, USBRX);
00047 CAN                 can(PB_8, PB_9);  // CAN Rx pin name, CAN Tx pin name
00048 CANMsg              rxMsg;
00049 CANMsg              txMsg;
00050 DigitalOut          led(LED_PIN);
00051 Timer               timer;
00052 uint8_t             counter = 0;
00053 AnalogIn            analogIn(A0);
00054 float               voltage;
00055 
00056 /**
00057  * @brief   Prints CAN msg to PC's serial terminal
00058  * @note
00059  * @param   CANMessage to print
00060  * @retval
00061  */
00062 void printMsg(CANMessage& msg) {
00063     pc.printf("  ID      = 0x%.3x\r\n", msg.id);
00064     pc.printf("  Type    = %d\r\n", msg.type);
00065     pc.printf("  Format  = %d\r\n", msg.format);
00066     pc.printf("  Length  = %d\r\n", msg.len);
00067     pc.printf("  Data    =");
00068     for(int i = 0; i < msg.len; i++)
00069         pc.printf(" 0x%.2X", msg.data[i]);
00070     pc.printf("\r\n");
00071 }
00072 
00073 /**
00074  * @brief   Main
00075  * @note
00076  * @param
00077  * @retval
00078  */
00079 int main(void)
00080 {
00081 #if defined(TARGET_STM32F103C8T6)
00082     confSysClock();         //Configure system clock (72MHz HSE clock, 48MHz USB clock)
00083 #endif
00084     pc.baud(9600);          // set Serial speed
00085     can.frequency(1000000); // set bit rate to 1Mbps
00086 #if defined(BOARD1)
00087     led = ON;               // turn the LED on
00088     timer.start();          // start timer
00089     pc.printf("CAN_Hello board #1\r\n");
00090 #else
00091     led = OFF;      // turn LED off
00092     pc.printf("CAN_Hello board #2\r\n");
00093 #endif
00094     while(1) {
00095         if(timer.read_ms() >= 1000) {    // check for timeout
00096             timer.stop();                // stop timer
00097             timer.reset();               // reset timer
00098             counter++;                   // increment counter
00099             voltage = analogIn * 3.3f;   // read the small drifting voltage from analog input
00100             txMsg.clear();               // clear Tx message storage
00101             txMsg.id = TX_ID;            // set ID
00102             txMsg << counter << voltage; // append data (total data length must not exceed 8 bytes!)
00103             if(can.write(txMsg)) {       // transmit message
00104                 led = OFF;               // turn the LED off
00105                 pc.printf("-------------------------------------\r\n");
00106                 pc.printf("CAN message sent\r\n");
00107                 printMsg(txMsg);
00108                 pc.printf("  counter = %d\r\n", counter);
00109                 pc.printf("  voltage = %e V\r\n", voltage);
00110             }
00111             else
00112                 pc.printf("Transmission error\r\n");
00113         }
00114 
00115         if(can.read(rxMsg)) {
00116             led = ON;       // turn the LED on
00117             pc.printf("-------------------------------------\r\n");
00118             pc.printf("CAN message received\r\n");
00119             printMsg(rxMsg);
00120 
00121             // Filtering performed by software:
00122             if(rxMsg.id == RX_ID) {
00123                 rxMsg >> counter >> voltage;    // extract data from the received CAN message
00124                 pc.printf("  counter = %d\r\n", counter);
00125                 pc.printf("  voltage = %e V\r\n", voltage);
00126                 timer.start();                  // transmission lag
00127             }
00128         }
00129     }
00130 }