François . / Mbed 2 deprecated testcanOK
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers can01.cpp Source File

can01.cpp

00001 #include "can01.h"
00002 
00003  Ticker ticker;
00004  
00005  DigitalOut led1(LED1);
00006  DigitalOut led2(LED2);
00007  
00008  // CAN_RS pin at Philips PCA82C250 can bus controller.
00009  // activate transceiver by pulling this pin to GND.
00010  // (Rise and fall slope controlled by resistor R_s)
00011  // (+5V result in tranceiver standby mode)
00012  // For further information see datasheet page 4
00013  DigitalOut can_Pca82c250SlopePin(p28);
00014  
00015  // second can controller on these pins. Not used here.
00016  // CAN can1(p9, p10);
00017  
00018  // We use can on mbed pins 29(CAN_TXD) and 30(CAN_RXD).
00019  CAN can2(p30, p29);
00020 
00021 
00022 void tCan01 ::  send() 
00023  {
00024      static char counter = 0;
00025      if (can2.write(CANMessage(0x200, &counter, 1))) {
00026          printf("CanTx--> id: 0x200  dlc: 1  data: %x\n\r", counter);
00027          counter++;
00028      }
00029      // toggle led1 after every transmission
00030      led1 = !led1;
00031  }
00032 
00033 
00034 void tCan01 :: ConfigurerCan2Envoie()
00035 {
00036  char compteur=1;
00037  // 500kbit/s
00038  can2.frequency(10000);
00039  // activate external can transceiver
00040  can_Pca82c250SlopePin = 0;
00041  // every 500ms
00042  ticker.attach(&send, 0.5);
00043  /// create message object for message reception
00044  CANMessage can_MsgRx;
00045  can2.write(CANMessage(0x200, &compteur, 1));
00046  while (1) 
00047  {
00048  // send received messages to the pc via serial line (9k6, 8n1)
00049     if (can2.read(can_MsgRx)) 
00050     {
00051         printf("CanRx--> id: 0x%x  dlc: %d  data: ", can_MsgRx.id, can_MsgRx.len);
00052             for (char i=0; i<can_MsgRx.len; i++) 
00053             {
00054                 printf("%x ", can_MsgRx.data[i]);
00055             }
00056             printf("\n\r");
00057              // any incoming message: toggle led2
00058              led2 = !led2;
00059     }
00060  }
00061 }
00062 
00063 /*void tcan01 ::Can1Recoit()
00064 {
00065     static char msg=0;
00066     if (can1.read(CANMessage(0x200,msg)))
00067     {
00068     printf("CanTx
00069     }
00070 }*/