![](/media/cache/profiles/e53ba686b7ad2ec7825f0f4afff80a1b.50x50_q85.jpg)
(CAN) using any type of character and strings with a maxim of 8 bits
main.cpp@1:328932c34ba1, 2013-01-08 (annotated)
- Committer:
- sherckuith
- Date:
- Tue Jan 08 06:44:56 2013 +0000
- Revision:
- 1:328932c34ba1
- Parent:
- 0:8ef426505c79
(CAN) using any type of character and strings with a maxim of 8 bits
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sherckuith | 0:8ef426505c79 | 1 | #include "mbed.h" |
sherckuith | 0:8ef426505c79 | 2 | #include "CAN.h" |
sherckuith | 0:8ef426505c79 | 3 | |
sherckuith | 0:8ef426505c79 | 4 | DigitalOut led1(LED1); |
sherckuith | 0:8ef426505c79 | 5 | DigitalOut led2(LED2); |
sherckuith | 0:8ef426505c79 | 6 | DigitalOut led3(LED3); |
sherckuith | 0:8ef426505c79 | 7 | DigitalOut led4(LED4); |
sherckuith | 0:8ef426505c79 | 8 | CAN can1(p9, p10); |
sherckuith | 0:8ef426505c79 | 9 | CAN can2(p30, p29); |
sherckuith | 0:8ef426505c79 | 10 | Serial pc(USBTX,USBRX); //tx. rx |
sherckuith | 0:8ef426505c79 | 11 | |
sherckuith | 0:8ef426505c79 | 12 | int main() |
sherckuith | 0:8ef426505c79 | 13 | { |
sherckuith | 0:8ef426505c79 | 14 | char a[8] = {0x6D,0x62,0x65,0x64,0x00,0x00,0x00,0x00}; |
sherckuith | 0:8ef426505c79 | 15 | char b[5] = "hola"; |
sherckuith | 0:8ef426505c79 | 16 | char c[8] = {"1234567"}; |
sherckuith | 0:8ef426505c79 | 17 | const char d[8] = {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x00}; |
sherckuith | 0:8ef426505c79 | 18 | CANMessage msg; |
sherckuith | 0:8ef426505c79 | 19 | pc.baud(9600); |
sherckuith | 0:8ef426505c79 | 20 | can1.frequency(125000); |
sherckuith | 0:8ef426505c79 | 21 | can2.frequency(125000); |
sherckuith | 0:8ef426505c79 | 22 | while(1) { |
sherckuith | 1:328932c34ba1 | 23 | if(can1.read(msg)) |
sherckuith | 0:8ef426505c79 | 24 | led1 = 1; |
sherckuith | 1:328932c34ba1 | 25 | if(can2.read(msg)) |
sherckuith | 1:328932c34ba1 | 26 | led2 = 1; |
sherckuith | 0:8ef426505c79 | 27 | |
sherckuith | 1:328932c34ba1 | 28 | /***********inicio de comunicacion CAN1 -> CAN2 ****************************/ |
sherckuith | 0:8ef426505c79 | 29 | pc.printf("************************************\n\r"); |
sherckuith | 0:8ef426505c79 | 30 | can1.write(CANMessage(01, &a[0], 8)); |
sherckuith | 0:8ef426505c79 | 31 | pc.printf("write a: %s ",&a[0]); |
sherckuith | 0:8ef426505c79 | 32 | if(can2.read(msg)) { |
sherckuith | 0:8ef426505c79 | 33 | led1 = 1;led2 = 0;led3 = 0;led4 = 0; |
sherckuith | 0:8ef426505c79 | 34 | pc.printf("read a:-%s-\n",&msg.data[0]); |
sherckuith | 0:8ef426505c79 | 35 | } |
sherckuith | 1:328932c34ba1 | 36 | else { |
sherckuith | 1:328932c34ba1 | 37 | printf("CAN1 RD error: %d, TD error: %d\r\n", can1.rderror(), can1.tderror() ); |
sherckuith | 1:328932c34ba1 | 38 | printf("CAN2 RD error: %d, TD error: %d\r\n", can2.rderror(), can2.tderror() ); |
sherckuith | 1:328932c34ba1 | 39 | } |
sherckuith | 1:328932c34ba1 | 40 | |
sherckuith | 0:8ef426505c79 | 41 | can1.write(CANMessage(01, &b[0], 4)); |
sherckuith | 0:8ef426505c79 | 42 | pc.printf("write b: %s ",&b[0]); |
sherckuith | 0:8ef426505c79 | 43 | if(can2.read(msg)) { |
sherckuith | 1:328932c34ba1 | 44 | led1 = 0;led2 = 1;led3 = 0;led4 = 0; |
sherckuith | 0:8ef426505c79 | 45 | pc.printf("read b:-%s-\n",&msg.data[0]); |
sherckuith | 0:8ef426505c79 | 46 | } |
sherckuith | 1:328932c34ba1 | 47 | else { |
sherckuith | 1:328932c34ba1 | 48 | printf("CAN1 RD error: %d, TD error: %d\r\n", can1.rderror(), can1.tderror() ); |
sherckuith | 1:328932c34ba1 | 49 | printf("CAN2 RD error: %d, TD error: %d\r\n", can2.rderror(), can2.tderror() ); |
sherckuith | 1:328932c34ba1 | 50 | } |
sherckuith | 1:328932c34ba1 | 51 | |
sherckuith | 0:8ef426505c79 | 52 | can1.write(CANMessage(01, &c[0], 8)); |
sherckuith | 0:8ef426505c79 | 53 | pc.printf("write c: %s ",&c[0]); |
sherckuith | 0:8ef426505c79 | 54 | if(can2.read(msg)) { |
sherckuith | 1:328932c34ba1 | 55 | led1 = 0;led2 = 0;led3 = 1;led4 = 0; |
sherckuith | 0:8ef426505c79 | 56 | pc.printf("read c:-%s-\n",&msg.data[0]); |
sherckuith | 0:8ef426505c79 | 57 | } |
sherckuith | 1:328932c34ba1 | 58 | else { |
sherckuith | 1:328932c34ba1 | 59 | printf("CAN1 RD error: %d, TD error: %d\r\n", can1.rderror(), can1.tderror() ); |
sherckuith | 1:328932c34ba1 | 60 | printf("CAN2 RD error: %d, TD error: %d\r\n", can2.rderror(), can2.tderror() ); |
sherckuith | 1:328932c34ba1 | 61 | } |
sherckuith | 1:328932c34ba1 | 62 | |
sherckuith | 0:8ef426505c79 | 63 | can1.write(CANMessage(01, &d[0], 8)); |
sherckuith | 0:8ef426505c79 | 64 | pc.printf("write d: %s ",&d[0]); |
sherckuith | 0:8ef426505c79 | 65 | if(can2.read(msg)) { |
sherckuith | 1:328932c34ba1 | 66 | led1 = 0;led2 = 0;led3 = 0;led4 = 1; |
sherckuith | 0:8ef426505c79 | 67 | pc.printf("read d:-%s-\n",&msg.data[0]); |
sherckuith | 0:8ef426505c79 | 68 | } |
sherckuith | 1:328932c34ba1 | 69 | else { |
sherckuith | 1:328932c34ba1 | 70 | printf("CAN1 RD error: %d, TD error: %d\r\n", can1.rderror(), can1.tderror() ); |
sherckuith | 1:328932c34ba1 | 71 | printf("CAN2 RD error: %d, TD error: %d\r\n", can2.rderror(), can2.tderror() ); |
sherckuith | 1:328932c34ba1 | 72 | } |
sherckuith | 1:328932c34ba1 | 73 | |
sherckuith | 1:328932c34ba1 | 74 | //wait(1); |
sherckuith | 1:328932c34ba1 | 75 | led1 = 0;led2 = 0;led3 = 0;led4 = 0; |
sherckuith | 1:328932c34ba1 | 76 | |
sherckuith | 1:328932c34ba1 | 77 | /************************fin**********************************/ |
sherckuith | 1:328932c34ba1 | 78 | |
sherckuith | 1:328932c34ba1 | 79 | /***********inicio de comunicacion CAN2 -> CAN1 ****************************/ |
sherckuith | 1:328932c34ba1 | 80 | pc.printf("************************************\n\r"); |
sherckuith | 1:328932c34ba1 | 81 | can2.write(CANMessage(01, &a[0], 8)); |
sherckuith | 1:328932c34ba1 | 82 | pc.printf("write a: %s ",&a[0]); |
sherckuith | 1:328932c34ba1 | 83 | if(can1.read(msg)) { |
sherckuith | 1:328932c34ba1 | 84 | led1 = 0;led2 = 0;led3 = 0;led4 = 1; |
sherckuith | 1:328932c34ba1 | 85 | pc.printf("read a:-%s-\n",&msg.data[0]); |
sherckuith | 1:328932c34ba1 | 86 | } |
sherckuith | 1:328932c34ba1 | 87 | else { |
sherckuith | 1:328932c34ba1 | 88 | printf("CAN1 RD error: %d, TD error: %d\r\n", can1.rderror(), can1.tderror() ); |
sherckuith | 1:328932c34ba1 | 89 | printf("CAN2 RD error: %d, TD error: %d\r\n", can2.rderror(), can2.tderror() ); |
sherckuith | 1:328932c34ba1 | 90 | } |
sherckuith | 1:328932c34ba1 | 91 | |
sherckuith | 1:328932c34ba1 | 92 | can2.write(CANMessage(01, &b[0], 4)); |
sherckuith | 1:328932c34ba1 | 93 | pc.printf("write b: %s ",&b[0]); |
sherckuith | 1:328932c34ba1 | 94 | if(can1.read(msg)) { |
sherckuith | 1:328932c34ba1 | 95 | led1 = 0;led2 = 0;led3 = 1;led4 = 0; |
sherckuith | 1:328932c34ba1 | 96 | pc.printf("read b:-%s-\n",&msg.data[0]); |
sherckuith | 1:328932c34ba1 | 97 | } |
sherckuith | 1:328932c34ba1 | 98 | else { |
sherckuith | 1:328932c34ba1 | 99 | printf("CAN1 RD error: %d, TD error: %d\r\n", can1.rderror(), can1.tderror() ); |
sherckuith | 1:328932c34ba1 | 100 | printf("CAN2 RD error: %d, TD error: %d\r\n", can2.rderror(), can2.tderror() ); |
sherckuith | 1:328932c34ba1 | 101 | } |
sherckuith | 1:328932c34ba1 | 102 | |
sherckuith | 1:328932c34ba1 | 103 | can2.write(CANMessage(01, &c[0], 8)); |
sherckuith | 1:328932c34ba1 | 104 | pc.printf("write c: %s ",&c[0]); |
sherckuith | 1:328932c34ba1 | 105 | if(can1.read(msg)) { |
sherckuith | 1:328932c34ba1 | 106 | led1 = 0;led2 = 1;led3 = 0;led4 = 0; |
sherckuith | 1:328932c34ba1 | 107 | pc.printf("read c:-%s-\n",&msg.data[0]); |
sherckuith | 1:328932c34ba1 | 108 | } |
sherckuith | 1:328932c34ba1 | 109 | else { |
sherckuith | 1:328932c34ba1 | 110 | printf("CAN1 RD error: %d, TD error: %d\r\n", can1.rderror(), can1.tderror() ); |
sherckuith | 1:328932c34ba1 | 111 | printf("CAN2 RD error: %d, TD error: %d\r\n", can2.rderror(), can2.tderror() ); |
sherckuith | 1:328932c34ba1 | 112 | } |
sherckuith | 1:328932c34ba1 | 113 | |
sherckuith | 1:328932c34ba1 | 114 | can2.write(CANMessage(01, &d[0], 8)); |
sherckuith | 1:328932c34ba1 | 115 | pc.printf("write d: %s ",&d[0]); |
sherckuith | 1:328932c34ba1 | 116 | if(can1.read(msg)) { |
sherckuith | 1:328932c34ba1 | 117 | led1 = 1;led2 = 0;led3 = 0;led4 = 0; |
sherckuith | 1:328932c34ba1 | 118 | pc.printf("read d:-%s-\n",&msg.data[0]); |
sherckuith | 1:328932c34ba1 | 119 | } |
sherckuith | 1:328932c34ba1 | 120 | else { |
sherckuith | 1:328932c34ba1 | 121 | printf("CAN1 RD error: %d, TD error: %d\r\n", can1.rderror(), can1.tderror() ); |
sherckuith | 1:328932c34ba1 | 122 | printf("CAN2 RD error: %d, TD error: %d\r\n", can2.rderror(), can2.tderror() ); |
sherckuith | 1:328932c34ba1 | 123 | } |
sherckuith | 1:328932c34ba1 | 124 | |
sherckuith | 1:328932c34ba1 | 125 | //wait(1); |
sherckuith | 0:8ef426505c79 | 126 | led1 = 0;led2 = 0;led3 = 0;led4 = 0; |
sherckuith | 0:8ef426505c79 | 127 | |
sherckuith | 0:8ef426505c79 | 128 | /************************fin**********************************/ |
sherckuith | 0:8ef426505c79 | 129 | } |
sherckuith | 0:8ef426505c79 | 130 | } |
sherckuith | 0:8ef426505c79 | 131 | |
sherckuith | 0:8ef426505c79 | 132 | |
sherckuith | 0:8ef426505c79 | 133 | /* |
sherckuith | 0:8ef426505c79 | 134 | (NXP TJA 1051/3) |
sherckuith | 0:8ef426505c79 | 135 | mbed TJA1 TJA2 |
sherckuith | 0:8ef426505c79 | 136 | ---------------------- |
sherckuith | 0:8ef426505c79 | 137 | 5V Vcc Vcc |
sherckuith | 0:8ef426505c79 | 138 | GND GND GND |
sherckuith | 0:8ef426505c79 | 139 | p9 RXD - |
sherckuith | 0:8ef426505c79 | 140 | p10 TXD - |
sherckuith | 0:8ef426505c79 | 141 | p29 - TXD |
sherckuith | 0:8ef426505c79 | 142 | p30 - RXD |
sherckuith | 0:8ef426505c79 | 143 | - CANL CANL |
sherckuith | 0:8ef426505c79 | 144 | - CANH CANH |
sherckuith | 0:8ef426505c79 | 145 | 3.3v Vio Vio |
sherckuith | 0:8ef426505c79 | 146 | |
sherckuith | 0:8ef426505c79 | 147 | GND -> 60Ohm-CANL |
sherckuith | 0:8ef426505c79 | 148 | GND -> 60Ohm-CANH |
sherckuith | 0:8ef426505c79 | 149 | GND -> 10KOhm-S-TJA1 |
sherckuith | 0:8ef426505c79 | 150 | GND -> 10KOhm-S-TJA2 |
sherckuith | 0:8ef426505c79 | 151 | */ |