![](/media/cache/group/Display_Nokia_Insper_v01.jpg.50x50_q85.jpg)
comunicaçao CAN com LCD
main.cpp@0:f45252ccde11, 2020-05-26 (annotated)
- Committer:
- Marcelocostanzo
- Date:
- Tue May 26 17:00:45 2020 +0000
- Revision:
- 0:f45252ccde11
- Child:
- 1:dc5aef755b51
CAN + LCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Marcelocostanzo | 0:f45252ccde11 | 1 | #include "mbed.h" |
Marcelocostanzo | 0:f45252ccde11 | 2 | #include "TextLCD.h" |
Marcelocostanzo | 0:f45252ccde11 | 3 | |
Marcelocostanzo | 0:f45252ccde11 | 4 | TextLCD lcd(D8, D9, D4, D5, D6, D7); // rs, e, d4-d7 |
Marcelocostanzo | 0:f45252ccde11 | 5 | |
Marcelocostanzo | 0:f45252ccde11 | 6 | //-------Declara uso da serial--------------------- |
Marcelocostanzo | 0:f45252ccde11 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
Marcelocostanzo | 0:f45252ccde11 | 8 | |
Marcelocostanzo | 0:f45252ccde11 | 9 | //--------Declara uso da CAN---------------------- |
Marcelocostanzo | 0:f45252ccde11 | 10 | CAN can1(PB_8, PB_9); |
Marcelocostanzo | 0:f45252ccde11 | 11 | |
Marcelocostanzo | 0:f45252ccde11 | 12 | //--------Declara uso do LED nativo da placa------ |
Marcelocostanzo | 0:f45252ccde11 | 13 | DigitalOut LED(PA_5); |
Marcelocostanzo | 0:f45252ccde11 | 14 | |
Marcelocostanzo | 0:f45252ccde11 | 15 | //-----Declara uso do botao nativo da placa-------- |
Marcelocostanzo | 0:f45252ccde11 | 16 | DigitalIn SW(PC_13); |
Marcelocostanzo | 0:f45252ccde11 | 17 | |
Marcelocostanzo | 0:f45252ccde11 | 18 | //------Dado da mensagem chamada de info que sera usada--------------- |
Marcelocostanzo | 0:f45252ccde11 | 19 | static char info = 0x55; |
Marcelocostanzo | 0:f45252ccde11 | 20 | |
Marcelocostanzo | 0:f45252ccde11 | 21 | int main() |
Marcelocostanzo | 0:f45252ccde11 | 22 | { |
Marcelocostanzo | 0:f45252ccde11 | 23 | lcd.setCursor(TextLCD::CurOff_BlkOff); |
Marcelocostanzo | 0:f45252ccde11 | 24 | lcd.cls(); |
Marcelocostanzo | 0:f45252ccde11 | 25 | |
Marcelocostanzo | 0:f45252ccde11 | 26 | //------construção da mensagem CAN (ID, dado, tamanho em bits, Tipo de dado, Modo CAN)--- |
Marcelocostanzo | 0:f45252ccde11 | 27 | CANMessage msg(5, &info, 8, CANData, CANStandard); |
Marcelocostanzo | 0:f45252ccde11 | 28 | |
Marcelocostanzo | 0:f45252ccde11 | 29 | //pisca o led |
Marcelocostanzo | 0:f45252ccde11 | 30 | LED = 1; |
Marcelocostanzo | 0:f45252ccde11 | 31 | wait(0.5); |
Marcelocostanzo | 0:f45252ccde11 | 32 | LED = 0; |
Marcelocostanzo | 0:f45252ccde11 | 33 | wait(0.5); |
Marcelocostanzo | 0:f45252ccde11 | 34 | |
Marcelocostanzo | 0:f45252ccde11 | 35 | //---boas vindas------------ |
Marcelocostanzo | 0:f45252ccde11 | 36 | pc.printf("Ola!!!\n"); |
Marcelocostanzo | 0:f45252ccde11 | 37 | lcd.locate(0,0); |
Marcelocostanzo | 0:f45252ccde11 | 38 | lcd.printf("Ola!!!"); |
Marcelocostanzo | 0:f45252ccde11 | 39 | |
Marcelocostanzo | 0:f45252ccde11 | 40 | while(1) |
Marcelocostanzo | 0:f45252ccde11 | 41 | { |
Marcelocostanzo | 0:f45252ccde11 | 42 | //---checa se foi apertado o botão, com debounce |
Marcelocostanzo | 0:f45252ccde11 | 43 | if(SW == 0) |
Marcelocostanzo | 0:f45252ccde11 | 44 | { |
Marcelocostanzo | 0:f45252ccde11 | 45 | wait_ms(200); |
Marcelocostanzo | 0:f45252ccde11 | 46 | if(SW == 0) |
Marcelocostanzo | 0:f45252ccde11 | 47 | { |
Marcelocostanzo | 0:f45252ccde11 | 48 | //se a mensagem foi enviada |
Marcelocostanzo | 0:f45252ccde11 | 49 | if(can1.write(msg)) |
Marcelocostanzo | 0:f45252ccde11 | 50 | { |
Marcelocostanzo | 0:f45252ccde11 | 51 | lcd.cls(); |
Marcelocostanzo | 0:f45252ccde11 | 52 | pc.printf("Mensagem enviada\n"); |
Marcelocostanzo | 0:f45252ccde11 | 53 | lcd.locate(0,0); |
Marcelocostanzo | 0:f45252ccde11 | 54 | lcd.printf("Msg enviada"); |
Marcelocostanzo | 0:f45252ccde11 | 55 | } |
Marcelocostanzo | 0:f45252ccde11 | 56 | |
Marcelocostanzo | 0:f45252ccde11 | 57 | //se a mensagem nao foi enviada |
Marcelocostanzo | 0:f45252ccde11 | 58 | else |
Marcelocostanzo | 0:f45252ccde11 | 59 | { |
Marcelocostanzo | 0:f45252ccde11 | 60 | lcd.cls(); |
Marcelocostanzo | 0:f45252ccde11 | 61 | pc.printf("Mensagem NAO enviada\n"); |
Marcelocostanzo | 0:f45252ccde11 | 62 | lcd.locate(0,0); |
Marcelocostanzo | 0:f45252ccde11 | 63 | lcd.printf("Msg NAO enviada"); |
Marcelocostanzo | 0:f45252ccde11 | 64 | } |
Marcelocostanzo | 0:f45252ccde11 | 65 | } |
Marcelocostanzo | 0:f45252ccde11 | 66 | } |
Marcelocostanzo | 0:f45252ccde11 | 67 | |
Marcelocostanzo | 0:f45252ccde11 | 68 | //se a mensagem for recebida |
Marcelocostanzo | 0:f45252ccde11 | 69 | if(can1.read(msg)) |
Marcelocostanzo | 0:f45252ccde11 | 70 | { |
Marcelocostanzo | 0:f45252ccde11 | 71 | lcd.cls(); |
Marcelocostanzo | 0:f45252ccde11 | 72 | //printa o valor da mensagem |
Marcelocostanzo | 0:f45252ccde11 | 73 | pc.printf("Mensagem recebida: %x\n", msg.data[0]); |
Marcelocostanzo | 0:f45252ccde11 | 74 | lcd.locate(0,0); |
Marcelocostanzo | 0:f45252ccde11 | 75 | lcd.printf("Mensagem recebida"); |
Marcelocostanzo | 0:f45252ccde11 | 76 | lcd.locate(0,1); |
Marcelocostanzo | 0:f45252ccde11 | 77 | lcd.printf("%x\n", msg.data[0]); |
Marcelocostanzo | 0:f45252ccde11 | 78 | |
Marcelocostanzo | 0:f45252ccde11 | 79 | //pisca o led |
Marcelocostanzo | 0:f45252ccde11 | 80 | for(int i = 0; i < 5; i++) |
Marcelocostanzo | 0:f45252ccde11 | 81 | { |
Marcelocostanzo | 0:f45252ccde11 | 82 | LED = 1; |
Marcelocostanzo | 0:f45252ccde11 | 83 | wait_ms(300); |
Marcelocostanzo | 0:f45252ccde11 | 84 | LED = 0; |
Marcelocostanzo | 0:f45252ccde11 | 85 | wait_ms(300); |
Marcelocostanzo | 0:f45252ccde11 | 86 | } |
Marcelocostanzo | 0:f45252ccde11 | 87 | } |
Marcelocostanzo | 0:f45252ccde11 | 88 | } |
Marcelocostanzo | 0:f45252ccde11 | 89 | } |