Nucleo by Cach
Dependencies: C12832 CANnucleo LM75B mbed
Fork of CANnucleo_Hello by
main.cpp
00001 /* 00002 * An example showing how to use the CANnucleo library: 00003 * 00004 * Two affordable (less than $4 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash), 00005 * compatible with the NUCLEO-F103RB platform (20kB SRAM, 128kB Flash), 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_Nucleo_Hello/> 00011 * 00012 * NOTE: If you'd like to use the official NUCLEO-F103RB boards 00013 * comment out the line #define TARGET_STM32F103C8T6 1 00014 * 00015 * The same code is used for both NUCLEO boards, but: 00016 * For board #1 compile the example without any change. 00017 * For board #2 comment out the line #define BOARD1 1 before compiling 00018 * 00019 * Once the binaries have been downloaded to the boards reset board #1. 00020 * 00021 */ 00022 00023 #include "mbed.h" 00024 #include "CAN.h" 00025 #include "C12832.h" 00026 #include "LM75B.h" 00027 00028 #define BOARD1 1 // comment out this line when compiling for board #2 00029 00030 #if defined(BOARD1) 00031 #define RX_ID 0x100 00032 #define TX_ID 0x7FF 00033 #else 00034 #define RX_ID 0x101 00035 #define TX_ID 0x7FF 00036 #endif 00037 00038 // See wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Nucleo_Hello/> 00039 //#define TARGET_STM32F103C8T6 1 // comment out this line if you'd like to use the official NUCLEO-F103RB boards 00040 00041 #if defined(TARGET_STM32F103C8T6) 00042 DigitalOut led(PC_13); 00043 #else 00044 DigitalOut led(LED1); 00045 #endif 00046 00047 DigitalIn fire(D4); 00048 C12832 lcd(D11, D13, D12, D7, D10); 00049 LM75B sensor(D14,D15); 00050 AnalogIn pot1 (A0); 00051 AnalogIn pot2 (A1); 00052 PwmOut r (D5); 00053 PwmOut g (D8); 00054 PwmOut b (D9); 00055 00056 int ledReceived; 00057 Timer timer; 00058 CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name, Automatic recovery from bus-off state enabled by default 00059 CANMessage rxMsg; 00060 CANMessage txMsg; 00061 int counter = 0; 00062 volatile bool msgAvailable = false; 00063 00064 /** 00065 * @brief 'CAN receive-complete' interrup handler. 00066 * @note Called on arrival of new CAN message. 00067 * Keep it as short as possible. 00068 * @param 00069 * @retval 00070 */ 00071 00072 void onMsgReceived() { 00073 msgAvailable = true; 00074 } 00075 00076 /** 00077 * @brief Main 00078 * @note 00079 * @param 00080 * @retval 00081 */ 00082 int main() { 00083 can.frequency(1000000); // set bit rate to 1Mbps 00084 can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive-complete' interrupt handler 00085 char buff_2[8]; 00086 #if defined(BOARD1) 00087 #if defined(TARGET_STM32F103C8T6) 00088 led = 0; // turn LED on 00089 #else 00090 led = 1; // turn LED on 00091 #endif 00092 timer.start(); 00093 #else 00094 #if defined(TARGET_STM32F103C8T6) 00095 led = 1; // turn LED off 00096 #else 00097 led = 0; // turn LED off 00098 #endif 00099 #endif 00100 lcd.cls(); 00101 while(1) { 00102 wait (0.1); 00103 lcd.cls(); 00104 lcd.locate(0,3); 00105 lcd.printf("%d %d %d",char(sensor.temp()),char(pot1*255.0), char(pot2*255.0)); 00106 lcd.locate(0,14); 00107 lcd.printf("I:%dF:%dl:%d Data:", rxMsg.id, rxMsg.type, rxMsg.len); 00108 for(int i = 0; i < rxMsg.len; i++) 00109 { 00110 lcd.printf("%x", buff_2[i]); 00111 } 00112 if(fire) { // check for timeout 00113 timer.stop(); // stop timer 00114 timer.reset(); // reset timer (to avaoid repeated send) 00115 counter++; // increment counter 00116 txMsg.clear(); // clear Tx message storage 00117 txMsg.id = TX_ID; // set ID 00118 txMsg << char(sensor.temp()); // append first data item (make sure that CAN message total data lenght <= 8 bytes!) 00119 txMsg << char(0x00); 00120 txMsg << char(pot1*255.0); // append second data item (make sure that CAN message total data lenght <= 8 bytes!) 00121 txMsg << char(pot2*255.0); 00122 can.write(txMsg); // transmit message 00123 printf("CAN message sent\r\n"); 00124 00125 00126 00127 #if defined(TARGET_STM32F103C8T6) 00128 led = 1; // turn LED off 00129 #else 00130 led = 0; // turn LED off 00131 #endif 00132 while(fire); 00133 } 00134 if(msgAvailable) { 00135 msgAvailable = false; // reset flag for next use 00136 can.read(rxMsg); // read message into Rx message storage 00137 printf("CAN message received:\r\n"); 00138 printf(" ID = %#x\r\n", rxMsg.id); 00139 printf(" Type = %d\r\n", rxMsg.type); 00140 printf(" Format = %d\r\n", rxMsg.format); 00141 printf(" Length = %d\r\n", rxMsg.len); 00142 printf(" Data ="); 00143 lcd.locate(0,14); 00144 lcd.printf("I:%dF:%dl:%d", rxMsg.id, rxMsg.type, rxMsg.len); 00145 00146 for(int i = 0; i < rxMsg.len; i++) 00147 { 00148 lcd.printf(" %x", rxMsg.data[i]); 00149 buff_2[i] = rxMsg.data[i]; 00150 } 00151 r = (rxMsg.data[0]/255.0); 00152 g = (rxMsg.data[1]/255.0); 00153 b = (rxMsg.data[2]/255.0); 00154 if(rxMsg.data[0] == 1) lcd.cls(); 00155 printf("\r\n"); 00156 if(rxMsg.id == RX_ID) { // if ID matches 00157 rxMsg >> counter; // extract first data item 00158 rxMsg >> ledReceived; // extract second data item 00159 led = ledReceived; // set LED 00160 printf("counter = %d\r\n", counter); 00161 timer.start(); 00162 } 00163 } 00164 } 00165 } 00166
Generated on Fri Jul 22 2022 12:28:22 by 1.7.2