Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:27339bb50fd6, 2021-07-13 (annotated)
- Committer:
- MarcusEA
- Date:
- Tue Jul 13 01:51:12 2021 +0000
- Revision:
- 0:27339bb50fd6
- Child:
- 1:248c00db9e9d
Initial push
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MarcusEA | 0:27339bb50fd6 | 1 | #include "mbed.h" |
MarcusEA | 0:27339bb50fd6 | 2 | |
MarcusEA | 0:27339bb50fd6 | 3 | #define CAN_TD D2 |
MarcusEA | 0:27339bb50fd6 | 4 | #define CAN_RD D10 |
MarcusEA | 0:27339bb50fd6 | 5 | |
MarcusEA | 0:27339bb50fd6 | 6 | #define INTERRUPT D8 |
MarcusEA | 0:27339bb50fd6 | 7 | |
MarcusEA | 0:27339bb50fd6 | 8 | #define LEFTJOYSTICK true |
MarcusEA | 0:27339bb50fd6 | 9 | |
MarcusEA | 0:27339bb50fd6 | 10 | I2C i2c(I2C_SDA, I2C_SCL); |
MarcusEA | 0:27339bb50fd6 | 11 | InterruptIn interrupt(INTERRUPT); |
MarcusEA | 0:27339bb50fd6 | 12 | Serial pc(USBTX, USBRX); |
MarcusEA | 0:27339bb50fd6 | 13 | |
MarcusEA | 0:27339bb50fd6 | 14 | CAN can1(CAN_RD, CAN_TD); |
MarcusEA | 0:27339bb50fd6 | 15 | |
MarcusEA | 0:27339bb50fd6 | 16 | const int main_addr = 0x81; |
MarcusEA | 0:27339bb50fd6 | 17 | const int alt_addr = 0x83; |
MarcusEA | 0:27339bb50fd6 | 18 | const int control = 0x76; |
MarcusEA | 0:27339bb50fd6 | 19 | |
MarcusEA | 0:27339bb50fd6 | 20 | char rawReading[2]; |
MarcusEA | 0:27339bb50fd6 | 21 | |
MarcusEA | 0:27339bb50fd6 | 22 | int xAxis = 0; |
MarcusEA | 0:27339bb50fd6 | 23 | int yAxis = 0; |
MarcusEA | 0:27339bb50fd6 | 24 | |
MarcusEA | 0:27339bb50fd6 | 25 | int main() { |
MarcusEA | 0:27339bb50fd6 | 26 | |
MarcusEA | 0:27339bb50fd6 | 27 | pc.printf("Starting I2C converter \n"); |
MarcusEA | 0:27339bb50fd6 | 28 | //interrupt.fall(&readSensor); |
MarcusEA | 0:27339bb50fd6 | 29 | |
MarcusEA | 0:27339bb50fd6 | 30 | can1.frequency(250000); |
MarcusEA | 0:27339bb50fd6 | 31 | //can1.filter(0x70,0x70,CANExtended); //CANbus filtering to only accept 0x70 |
MarcusEA | 0:27339bb50fd6 | 32 | |
MarcusEA | 0:27339bb50fd6 | 33 | CANMessage msg; |
MarcusEA | 0:27339bb50fd6 | 34 | |
MarcusEA | 0:27339bb50fd6 | 35 | msg.format = CANExtended; |
MarcusEA | 0:27339bb50fd6 | 36 | msg.id = 0x71; //messageID |
MarcusEA | 0:27339bb50fd6 | 37 | msg.len = 8; //length in bytes |
MarcusEA | 0:27339bb50fd6 | 38 | |
MarcusEA | 0:27339bb50fd6 | 39 | for(int i = 3; i<8; i++) { |
MarcusEA | 0:27339bb50fd6 | 40 | msg.data[i] = 0; |
MarcusEA | 0:27339bb50fd6 | 41 | } |
MarcusEA | 0:27339bb50fd6 | 42 | |
MarcusEA | 0:27339bb50fd6 | 43 | int addr = main_addr; |
MarcusEA | 0:27339bb50fd6 | 44 | |
MarcusEA | 0:27339bb50fd6 | 45 | if(LEFTJOYSTICK) { |
MarcusEA | 0:27339bb50fd6 | 46 | addr = main_addr; |
MarcusEA | 0:27339bb50fd6 | 47 | msg.data[0] = 0; |
MarcusEA | 0:27339bb50fd6 | 48 | } |
MarcusEA | 0:27339bb50fd6 | 49 | else { |
MarcusEA | 0:27339bb50fd6 | 50 | addr = alt_addr; |
MarcusEA | 0:27339bb50fd6 | 51 | msg.data[0] = 1; |
MarcusEA | 0:27339bb50fd6 | 52 | } |
MarcusEA | 0:27339bb50fd6 | 53 | |
MarcusEA | 0:27339bb50fd6 | 54 | while(1) { |
MarcusEA | 0:27339bb50fd6 | 55 | |
MarcusEA | 0:27339bb50fd6 | 56 | i2c.read(addr, rawReading, 2); |
MarcusEA | 0:27339bb50fd6 | 57 | xAxis = (int8_t)(rawReading[0]); |
MarcusEA | 0:27339bb50fd6 | 58 | yAxis = (int8_t)(rawReading[1]); |
MarcusEA | 0:27339bb50fd6 | 59 | msg.data[1] = xAxis; |
MarcusEA | 0:27339bb50fd6 | 60 | msg.data[2] = yAxis; |
MarcusEA | 0:27339bb50fd6 | 61 | |
MarcusEA | 0:27339bb50fd6 | 62 | //pc.printf("X: %d, Y: %d \n", xAxis, yAxis); |
MarcusEA | 0:27339bb50fd6 | 63 | |
MarcusEA | 0:27339bb50fd6 | 64 | can1.write(msg); |
MarcusEA | 0:27339bb50fd6 | 65 | if(can1.read(msg)) { |
MarcusEA | 0:27339bb50fd6 | 66 | //pc.printf("Recv: %d\n", msg.id); |
MarcusEA | 0:27339bb50fd6 | 67 | } |
MarcusEA | 0:27339bb50fd6 | 68 | |
MarcusEA | 0:27339bb50fd6 | 69 | //send out CANBus value |
MarcusEA | 0:27339bb50fd6 | 70 | wait(0.02); |
MarcusEA | 0:27339bb50fd6 | 71 | } |
MarcusEA | 0:27339bb50fd6 | 72 | } |