
Comunicação CAN entre joystick e LCD
main.cpp@0:ea7a2f35caf4, 2020-06-10 (annotated)
- Committer:
- matheus_duarte
- Date:
- Wed Jun 10 04:05:50 2020 +0000
- Revision:
- 0:ea7a2f35caf4
APS_2_Matheus
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
matheus_duarte | 0:ea7a2f35caf4 | 1 | #include "mbed.h" |
matheus_duarte | 0:ea7a2f35caf4 | 2 | #include "TextLCD.h" |
matheus_duarte | 0:ea7a2f35caf4 | 3 | |
matheus_duarte | 0:ea7a2f35caf4 | 4 | TextLCD lcd(D8, D9, D4, D5, D6, D7); //rs,e,d0,d1,d2,d3 |
matheus_duarte | 0:ea7a2f35caf4 | 5 | AnalogIn xAxis(A0); |
matheus_duarte | 0:ea7a2f35caf4 | 6 | AnalogIn yAxis(A1); |
matheus_duarte | 0:ea7a2f35caf4 | 7 | Serial pc(USBTX, USBRX); |
matheus_duarte | 0:ea7a2f35caf4 | 8 | CAN can1(PB_8, PB_9); |
matheus_duarte | 0:ea7a2f35caf4 | 9 | |
matheus_duarte | 0:ea7a2f35caf4 | 10 | static char info[6]= {0x55, 0x54, 0x53, 0x52, 0x51, 0x50}; |
matheus_duarte | 0:ea7a2f35caf4 | 11 | |
matheus_duarte | 0:ea7a2f35caf4 | 12 | int main() |
matheus_duarte | 0:ea7a2f35caf4 | 13 | { |
matheus_duarte | 0:ea7a2f35caf4 | 14 | CANMessage cima(5, &info[0], 8, CANData, CANStandard); |
matheus_duarte | 0:ea7a2f35caf4 | 15 | CANMessage baixo(5, &info[1], 8, CANData, CANStandard); |
matheus_duarte | 0:ea7a2f35caf4 | 16 | CANMessage esquerda(5, &info[2], 8, CANData, CANStandard); |
matheus_duarte | 0:ea7a2f35caf4 | 17 | CANMessage direita(5, &info[3], 8, CANData, CANStandard); |
matheus_duarte | 0:ea7a2f35caf4 | 18 | CANMessage parado(5, &info[4], 8, CANData, CANStandard); |
matheus_duarte | 0:ea7a2f35caf4 | 19 | CANMessage ativar(5, &info[5], 8, CANData, CANStandard); |
matheus_duarte | 0:ea7a2f35caf4 | 20 | |
matheus_duarte | 0:ea7a2f35caf4 | 21 | int x,y; |
matheus_duarte | 0:ea7a2f35caf4 | 22 | lcd.locate(3,0); |
matheus_duarte | 0:ea7a2f35caf4 | 23 | lcd.printf("Retrovisor"); |
matheus_duarte | 0:ea7a2f35caf4 | 24 | |
matheus_duarte | 0:ea7a2f35caf4 | 25 | while(1) |
matheus_duarte | 0:ea7a2f35caf4 | 26 | { |
matheus_duarte | 0:ea7a2f35caf4 | 27 | wait(0.1); |
matheus_duarte | 0:ea7a2f35caf4 | 28 | can1.write(ativar); |
matheus_duarte | 0:ea7a2f35caf4 | 29 | x = xAxis.read()*1000; // float (0 -> 1) to int (0 -> 1000) |
matheus_duarte | 0:ea7a2f35caf4 | 30 | y = yAxis.read()*1000; |
matheus_duarte | 0:ea7a2f35caf4 | 31 | if (x < 200){ |
matheus_duarte | 0:ea7a2f35caf4 | 32 | can1.write(esquerda); |
matheus_duarte | 0:ea7a2f35caf4 | 33 | } |
matheus_duarte | 0:ea7a2f35caf4 | 34 | if (x > 800){ |
matheus_duarte | 0:ea7a2f35caf4 | 35 | can1.write(direita); |
matheus_duarte | 0:ea7a2f35caf4 | 36 | } |
matheus_duarte | 0:ea7a2f35caf4 | 37 | if (y < 200){ |
matheus_duarte | 0:ea7a2f35caf4 | 38 | can1.write(baixo); |
matheus_duarte | 0:ea7a2f35caf4 | 39 | } |
matheus_duarte | 0:ea7a2f35caf4 | 40 | if (y > 800){ |
matheus_duarte | 0:ea7a2f35caf4 | 41 | can1.write(cima); |
matheus_duarte | 0:ea7a2f35caf4 | 42 | } |
matheus_duarte | 0:ea7a2f35caf4 | 43 | if (x > 300 && x < 700 && y > 300 && y < 700){ |
matheus_duarte | 0:ea7a2f35caf4 | 44 | can1.write(parado); |
matheus_duarte | 0:ea7a2f35caf4 | 45 | } |
matheus_duarte | 0:ea7a2f35caf4 | 46 | |
matheus_duarte | 0:ea7a2f35caf4 | 47 | if(can1.read(ativar)){ |
matheus_duarte | 0:ea7a2f35caf4 | 48 | if (ativar.data[0] == 0x55){ |
matheus_duarte | 0:ea7a2f35caf4 | 49 | lcd.locate(5,1); |
matheus_duarte | 0:ea7a2f35caf4 | 50 | lcd.printf("Cima "); |
matheus_duarte | 0:ea7a2f35caf4 | 51 | pc.printf("Cima"); |
matheus_duarte | 0:ea7a2f35caf4 | 52 | } |
matheus_duarte | 0:ea7a2f35caf4 | 53 | if (ativar.data[0] == 0x54){ |
matheus_duarte | 0:ea7a2f35caf4 | 54 | lcd.locate(5,1); |
matheus_duarte | 0:ea7a2f35caf4 | 55 | lcd.printf("Baixo "); |
matheus_duarte | 0:ea7a2f35caf4 | 56 | pc.printf("Baixo"); |
matheus_duarte | 0:ea7a2f35caf4 | 57 | } |
matheus_duarte | 0:ea7a2f35caf4 | 58 | if (ativar.data[0] == 0x53){ |
matheus_duarte | 0:ea7a2f35caf4 | 59 | lcd.locate(5,1); |
matheus_duarte | 0:ea7a2f35caf4 | 60 | lcd.printf("Direita "); |
matheus_duarte | 0:ea7a2f35caf4 | 61 | pc.printf("Direita"); |
matheus_duarte | 0:ea7a2f35caf4 | 62 | } |
matheus_duarte | 0:ea7a2f35caf4 | 63 | if (ativar.data[0] == 0x52){ |
matheus_duarte | 0:ea7a2f35caf4 | 64 | lcd.locate(5,1); |
matheus_duarte | 0:ea7a2f35caf4 | 65 | lcd.printf("Esquerda"); |
matheus_duarte | 0:ea7a2f35caf4 | 66 | pc.printf("Esquerda "); |
matheus_duarte | 0:ea7a2f35caf4 | 67 | } |
matheus_duarte | 0:ea7a2f35caf4 | 68 | if (ativar.data[0] == 0x51){ |
matheus_duarte | 0:ea7a2f35caf4 | 69 | lcd.locate(5,1); |
matheus_duarte | 0:ea7a2f35caf4 | 70 | lcd.printf("Parado "); |
matheus_duarte | 0:ea7a2f35caf4 | 71 | pc.printf("Parado"); |
matheus_duarte | 0:ea7a2f35caf4 | 72 | } |
matheus_duarte | 0:ea7a2f35caf4 | 73 | ativar.data[0]=0; |
matheus_duarte | 0:ea7a2f35caf4 | 74 | wait(0.1); |
matheus_duarte | 0:ea7a2f35caf4 | 75 | } |
matheus_duarte | 0:ea7a2f35caf4 | 76 | } |
matheus_duarte | 0:ea7a2f35caf4 | 77 | } |