Starting Can Bus interface on LPC1768

Dependencies:   mbed

Committer:
thanhnhiel
Date:
Wed Apr 18 06:05:49 2018 +0000
Revision:
0:31c2288e38eb
Hello Can Bus

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thanhnhiel 0:31c2288e38eb 1 #include "mbed.h"
thanhnhiel 0:31c2288e38eb 2
thanhnhiel 0:31c2288e38eb 3 Ticker ticker;
thanhnhiel 0:31c2288e38eb 4 DigitalOut led1(LED1);
thanhnhiel 0:31c2288e38eb 5 DigitalOut led2(LED2);
thanhnhiel 0:31c2288e38eb 6 CAN can1(p9, p10);
thanhnhiel 0:31c2288e38eb 7 CAN can2(p30, p29);
thanhnhiel 0:31c2288e38eb 8 char counter = 0;
thanhnhiel 0:31c2288e38eb 9
thanhnhiel 0:31c2288e38eb 10 void send() {
thanhnhiel 0:31c2288e38eb 11 printf("send()\n");
thanhnhiel 0:31c2288e38eb 12 if(can1.write(CANMessage(1337, &counter, 1))) {
thanhnhiel 0:31c2288e38eb 13 printf("wloop()\n");
thanhnhiel 0:31c2288e38eb 14 counter++;
thanhnhiel 0:31c2288e38eb 15 printf("Message sent: %d\n", counter);
thanhnhiel 0:31c2288e38eb 16 }
thanhnhiel 0:31c2288e38eb 17 led1 = !led1;
thanhnhiel 0:31c2288e38eb 18 }
thanhnhiel 0:31c2288e38eb 19
thanhnhiel 0:31c2288e38eb 20 int main() {
thanhnhiel 0:31c2288e38eb 21 printf("main()\n");
thanhnhiel 0:31c2288e38eb 22 ticker.attach(&send, 1);
thanhnhiel 0:31c2288e38eb 23 CANMessage msg;
thanhnhiel 0:31c2288e38eb 24 while(1) {
thanhnhiel 0:31c2288e38eb 25 printf("loop()\n");
thanhnhiel 0:31c2288e38eb 26 if(can2.read(msg)) {
thanhnhiel 0:31c2288e38eb 27 printf("Message received: %d\n", msg.data[0]);
thanhnhiel 0:31c2288e38eb 28 led2 = !led2;
thanhnhiel 0:31c2288e38eb 29 }
thanhnhiel 0:31c2288e38eb 30 wait(0.2);
thanhnhiel 0:31c2288e38eb 31 }
thanhnhiel 0:31c2288e38eb 32 }