SHENG-HEN HSIEH / Mbed 2 deprecated CAN_Example

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 This is a example program for using CAN Bus protocol.
00003 Feel free to modified to your own usage.
00004 The function include:
00005 
00006 1.  Interrupt-type recieving program
00007     A better method to cover the small mailbox size limited by hardware ( = 3 )
00008     Might overload the CPU when Bus jammed ( >50% Bus load )
00009     Might occur during any part of main function, so a proper buffering should apply
00010     ( Avoid any varible directly change by intterupt, use a buffer and/or flags instead )
00011 
00012 2.  A TX flag which decide whether a message is broadcast on the CAN Bus
00013     "Charge OK" signal (Sent on CAN Bus frequently, if a charging manager is online, it will start charging the cells.)
00014 
00015 3.  A "Heart-Beat" flag which can stop specific action if specified message absence from CAN Bus
00016     "Balance OK" signal (Sent from charging manager, dedicate the charging condition. In driving condition, no balance is allowed.)
00017 */
00018 
00019 
00020 #include "mbed.h"
00021 
00022 CAN can1(PB_5, PB_13, 1000000);                     //Declare a CAN object
00023 
00024 CANMessage can_msg_1;                               //Use as general beffer
00025 CANMessage can_msg_send;                            //Use as general buffer (Sent out)
00026 
00027 uint8_t CAN_flag_TX = 0;                            //Flag for sending out shit
00028 
00029 uint8_t CAN_flag_RX = 0;                            //Flag when recieving shit
00030 
00031 uint8_t CAN_flag_HeartBeat = 0;                     //Flag if a heart beat signal is require for certain action
00032 
00033 uint8_t Buffer_RX = 0;                              //Genral buffer, can derictly change by interrupt, might change during mian()
00034 uint8_t Data_RX = 0;                                //Genral buffer, only change if condition met, keep as a constant threw whole main()
00035 
00036 char data_msg[8] = {0,0,0,0,0,0,0,0};               //Example varible which might change by CAN message
00037 
00038 void CAN1_RX(void);                                 //Interrupt type recieving program
00039 
00040 
00041 
00042 int main()
00043 {
00044     CAN_flag_TX = 0;                                //Reset flags
00045     CAN_flag_RX = 0;
00046 
00047     can1.attach(&CAN1_RX, CAN::RxIrq);              //Start CAN1 Recieve Irq
00048 
00049     while(1) {
00050         //Transfer data from voltile varible to normal varible ( avoid change during main() )
00051         while(CAN_flag_RX == 1) {                   //Check if varible updated (Recieve new)
00052             Data_RX = Buffer_RX;                    //Transfering
00053             CAN_flag_RX = 0;                        //Reset flag
00054         }
00055 
00056         /*
00057          *General Program
00058          *
00059          *data_msg[i] = something;                  //General calculation
00060          *
00061          *if(CAN_flag_HeartBeat == 1){
00062          *  Do something when heart beat signal arrive
00063          *  CAN_flag_HeartBeat = 0;                 //Reset flag
00064          *  }
00065          *
00066          *CAN_flag_TX = 1;                          //If some condition met or need to sned out some message
00067          */
00068 
00069         while(CAN_flag_TX == 1) {
00070             can_msg_send = CANMessage(0xFA,data_msg,8,CANData,CANStandard);     //Send out data_msg with ID = 0xFA
00071             can1.write(can_msg_send);                                           
00072             CAN_flag_TX = 0;                                                    //Reset flag
00073         }
00074     }
00075 }
00076 
00077 void CAN1_RX(void)
00078 {
00079     if(can1.read(can_msg_1)) {                                              //If a false interrupt then do nothing
00080         switch(can_msg.id) {                                                //Filtered the input message and do corresponding action
00081             case 0xA2 :                                                     //Just an arbitary ID
00082                 Buffer_RX = can_msg_1.data[5] << 8 | can_msg_1.data[4];     //Transfer data from can_msg_1 to target varible
00083                 CAN_flag_RX = 1;                                            //Flag if needed, in this case indicate a new varible arrived
00084                 /*
00085                 if(some bits or similar == shit) {
00086                     CAN_flag_HeartBeat = 1;                                 //Set up a heart beat flag
00087                 }
00088                 */
00089                 break;
00090 
00091 //          case 0xAB :                                                     //Add more if needed
00092 //              for(int i=0; i<8; i++) {
00093 //                  Fault[i] = can_msg.data[i];
00094 //              }
00095 //              Fault_M = Fault[0] | Fault[1] | Fault[2] | Fault[3] | Fault[4] | Fault[5] | Fault[6] | Fault[7];
00096 //              printflag = 1;
00097 //              break;
00098         }
00099     }
00100 }