can_harurobo

Dependencies:   uw_28015 mbed

main.cpp

Committer:
yuki0701
Date:
2018-12-14
Revision:
0:69a91ad75895

File content as of revision 0:69a91ad75895:

#include "mbed.h"
#include "uw.h"

//CAN can1(p30,p29,1000000);
CAN can1(PA_11,PA_12,1000000);
DigitalOut led(LED1);
Ticker ticker;
Uw uw1(PF_1);
Uw uw2(PF_0);

char can_data[2]={0,0};
float x;
int data;

void can_read(){
    
    CANMessage msg;
    
    if(can1.read(msg)){
      
       // led = 1;    
      
      if(msg.id == 1){
        led = 1;
        x = (short)((msg.data[0]<<8) | msg.data[1]);
       printf("%d:%d,%d\n\r",msg.data[0],msg.data[1],x);
      }
    }
   // led = 0;
}
    
void can_send(){
   data = 1000 * uw1.get_dist();
    
    can_data[0] = data >> 8;
    can_data[1] = data &255;
    
    
    if(can1.write(CANMessage(1,can_data,2))){
        led = 1;
        printf("send\n\r");
    }else{
        led = 0;
    
    }
}
    
    
    
int main() {


ticker.attach(&can_send,0.1);
 //ticker.attach(&can_read,0.1);
 
 while(1);
  //printf("%f\n\r",uw1.get_dist());   
     
 
}