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 "CAN.h" 00003 00004 DigitalOut led1(LED1); 00005 DigitalOut led2(LED2); 00006 DigitalOut led3(LED3); 00007 00008 Serial pc(USBTX, USBRX); // tx, rx default settings (9600 8N1) 00009 00010 00011 CAN can1(p9, p10); // CAN1 uses pins 9 and 10 (tx, rx) 00012 CAN can2(p30, p29); // CAN2 uses pins 30 and 29 (tx, rx) 00013 00014 DigitalOut can1_SleepMode(p11); // Use pin 11 to control the sleep mode of can1 00015 DigitalOut can2_SleepMode(p28); // Use pin 28 to control the sleep mode of can2 00016 00017 CANMessage can_MsgTx; 00018 CANMessage can_MsgRx; 00019 00020 char msg[14]; 00021 char counter = 0; 00022 00023 int main() 00024 { 00025 pc.baud(115200); // change serial interface to pc to 115200, 8N1 00026 00027 can1.frequency(500000); // many frequencies are supported 00028 00029 can2.frequency(83000); // last time I checked 83000 was not, so.... 00030 LPC_CAN2->BTR = 0x52001C; // I had to manually change the "registers" that control the frequency 00031 // the registers are coded weird, so mbed tried to hide that 00032 // so usually you only need to issue the can2.frequency() command 00033 00034 can1_SleepMode = 0; // Set the Sleep Mode to 0 or low or false... this makes sure the transceiver is on 00035 can2_SleepMode = 0; // Set the Sleep Mode to 0 or low or false... this makes sure the transceiver is on 00036 00037 while (1) 00038 { 00039 // send received messages to the pc via serial line 00040 if (can2.read(can_MsgRx)) 00041 { 00042 // When sending a message to the PC, send a couple of sync bytes so that when the PC looks, it finds the start of a message 00043 // putc will write a character/byte to the pc, getc reads a character/byte from the pc 00044 pc.putc(0xff); 00045 pc.putc(0x00); 00046 pc.putc(0xff); 00047 00048 // Now, start sending the data of the message 00049 00050 // The id is 2 bytes long, so I have to break it up 00051 pc.putc((can_MsgRx.id & 0xff00) >> 8); // select the first byte... and write it 00052 pc.putc((can_MsgRx.id & 0x00ff)); // select the second byte... and write it 00053 pc.putc(can_MsgRx.len); // send the length of the can message 00054 for (int i = 0; i < can_MsgRx.len; i++) 00055 { 00056 pc.putc(can_MsgRx.data[i]); // send each of the bytes... from the first byte to the last byte. 00057 } 00058 00059 // any incoming message: toggle led2 00060 led2 = !led2; 00061 } 00062 00063 // send received messages to the pc via serial line 00064 if (can1.read(can_MsgRx)) 00065 { 00066 // When sending a message to the PC, send a couple of sync bytes so that when the PC looks, it finds the start of a message 00067 // putc will write a character/byte to the pc, getc reads a character/byte from the pc 00068 pc.putc(0xff); 00069 pc.putc(0x00); 00070 pc.putc(0xff); 00071 00072 // Now, start sending the data of the message 00073 00074 // The id is 2 bytes long, so I have to break it up 00075 pc.putc((can_MsgRx.id & 0xff00) >> 8); // select the first byte... and write it 00076 pc.putc((can_MsgRx.id & 0x00ff)); // select the second byte... and write it 00077 pc.putc(can_MsgRx.len); // send the length of the can message 00078 for (int i = 0; i < can_MsgRx.len; i++) 00079 { 00080 pc.putc(can_MsgRx.data[i]); // send each of the bytes... from the first byte to the last byte. 00081 } 00082 00083 // any incoming message: toggle led3 00084 led3 = !led3; 00085 } 00086 00087 // repeat this section of code if there is any bytes coming from the PC 00088 while (pc.readable()) 00089 { 00090 msg[counter] = pc.getc(); // read the byte from the pc, and put it into a buffer at the next available position 00091 counter++; // update the next available position 00092 00093 if (counter == 14) // if we received a full message (14 bytes) from the PC, then break out of the loop 00094 { 00095 break; 00096 } 00097 00098 if (counter == 3) // if we have received the first 3 bytes, check to see if they are the sync 00099 { 00100 if ((msg[0] != 0xff) || (msg[1] != 0x00) || (msg[2] != 0xff)) 00101 { 00102 counter = 2; // if they aren't the sync, skip the first byte by... 00103 msg[0] = msg[1]; // moving the second and third bytes into the first and second spots 00104 msg[1] = msg[2]; 00105 } 00106 } 00107 00108 // Only stop repeating if all 14 bytes have been received from the pc, or if the pc isn't sending any more bytes 00109 } 00110 00111 // So... if the pc sent a full message 00112 if (counter > 13) 00113 { 00114 counter = 0; // reset the buffer 00115 00116 // doublecheck to see if the sync was in the pc message 00117 if ((msg[0] == 0xff) && (msg[1] == 0x00) && (msg[2] == 0xff)) 00118 { 00119 // Start filling in the data into the can tx message... 00120 can_MsgTx.id = (((short)msg[3]) << 8) + msg[4]; // make the id from the 2 bytes 00121 can_MsgTx.len = msg[5]; // set the size of the message 00122 for (int i = 0; i < 8; i++) // fill in all 8 bytes 00123 { 00124 can_MsgTx.data[i] = msg[6 + i]; // make sure to use the right bytes offset from the start 00125 } 00126 00127 led1 = !led1; // toggle the led to show that we sending a can message on the second CAN transceiver 00128 can2.write(can_MsgTx); //send the message 00129 00130 memset(msg, 0, 14); // clear out the pc interface's buffer 00131 } 00132 } 00133 } 00134 }
Generated on Wed Jul 13 2022 08:05:45 by
1.7.2