Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "tft.h" 00003 00004 #define TX_ID (0x7e0) 00005 #define RX_ID (0x7e1) 00006 00007 DigitalOut led1(LED1); 00008 00009 CAN can(PB_8, PB_9); // CAN Rx pin name, CAN Tx pin name 00010 00011 char counter = 0; 00012 00013 #define NUMBER_TEXT_LINES ( 23 ) 00014 00015 int filterhandle=0, index =0; 00016 char buffer[NUMBER_TEXT_LINES][200]; 00017 bool available = false; 00018 00019 /** 00020 * @brief 'CAN receive-complete' interrup handler. 00021 * @note Called on arrival of new CAN message. 00022 * Keep it as short as possible. 00023 * @param 00024 * @retval 00025 */ 00026 void onMsgReceived() 00027 { 00028 CANMessage msg; 00029 00030 memset ( &msg, 0, sizeof ( msg ) ); 00031 00032 if ( can.read ( msg ) ) { 00033 00034 sprintf(buffer[index%NUMBER_TEXT_LINES],"0x%03x %d %02x %02x %02x %02x %02x %02x %02x %02x", msg.id,msg.len , 00035 msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.data[4],msg.data[5], msg.data[6], msg.data[7] 00036 ); 00037 00038 available = true; 00039 index++; 00040 } 00041 } 00042 00043 int main() 00044 { 00045 int error; 00046 00047 unsigned char q = 0; 00048 set_back(0); 00049 tft_init(); 00050 00051 tft_clear(TFT_BLUE); 00052 tft_set_window(32, 8, TFT_WIDTH + 32 - 1, TFT_HEIGHT + 8 - 1); 00053 00054 set_back(1); 00055 00056 CANMessage msg,txMsg; 00057 restart: 00058 ; 00059 00060 // can reset messes up the can (more init?) 00061 //can.reset(); 00062 00063 error = can.frequency(500000); // set bit rate to 1Mbps 00064 if(error == 0) { 00065 tft_text(0,0,"can.frequency",0xfff,0); 00066 wait(10); 00067 goto restart; 00068 } 00069 00070 00071 00072 error = can.mode(CAN::Silent); 00073 if(error == 0 ) { 00074 tft_text(0,0,"can.mode",0xfff,0); 00075 wait(10); 00076 goto restart; 00077 } 00078 00079 //can.monitor(true); 00080 00081 // filterhandle = can.filter(0xff,0x07ff,CANAny,0); 00082 00083 can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler 00084 00085 tft_clear(TFT_BLACK); 00086 00087 tft_text(0,0," ID LEN ------ Data ------ Waiting",0xff0,0); 00088 00089 while(1) { 00090 00091 00092 #if 0 00093 char ind[ 12 ]; 00094 00095 memset ( &msg, 0, sizeof ( msg ) ); 00096 00097 error = can.read ( msg,0 ) ; 00098 00099 if(error == 1) { 00100 sprintf(buffer[index%NUMBER_TEXT_LINES],"0x%03x %d %02x %02x %02x %02x %02x %02x %02x %02x", msg.id,msg.len , 00101 msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.data[4],msg.data[5], msg.data[6], msg.data[7] 00102 ); 00103 index++; 00104 index%=NUMBER_TEXT_LINES; 00105 } 00106 00107 if( can.rderror() ) { 00108 00109 tft_text(100,0,"can.rderror",0xfff,0); 00110 wait(1); 00111 tft_clear(TFT_BLACK); 00112 00113 can.reset(); 00114 can.frequency(500000); 00115 can.mode(CAN::Normal); 00116 q= 0; 00117 00118 } 00119 00120 sprintf(ind,"%03d %01d %d",index,error, can.rderror() ); 00121 00122 tft_text(0,0,ind,0xfff,0); 00123 00124 #endif 00125 //for(unsigned char q=0;q< NUMBER_TEXT_LINES;q++) 00126 { 00127 tft_text(0,8+((q)*8),buffer[q],0xfff,0); 00128 00129 } 00130 00131 q++; 00132 q%=NUMBER_TEXT_LINES; 00133 00134 led1 = !led1; 00135 } 00136 00137 00138 }
Generated on Tue Aug 2 2022 04:45:39 by
1.7.2