aa
Dependencies: mbed TrapezoidControl QEI
Communication/RS485/RS485.cpp@36:788274604650, 2019-09-25 (annotated)
- Committer:
- yabahiro
- Date:
- Wed Sep 25 11:03:03 2019 +0000
- Revision:
- 36:788274604650
- Parent:
- 21:1f1e9c585da8
aa;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
t_yamamoto | 0:669ef71cba68 | 1 | #include "RS485.h" |
t_yamamoto | 0:669ef71cba68 | 2 | #include "mbed.h" |
t_yamamoto | 0:669ef71cba68 | 3 | #include "ActuatorHub/ActuatorHub.h" |
yabahiro | 21:1f1e9c585da8 | 4 | #include "LineHub/LineHub.h" |
t_yamamoto | 0:669ef71cba68 | 5 | #include "../../LED/LED.h" |
t_yamamoto | 0:669ef71cba68 | 6 | #include "../../System/Using.h" |
t_yamamoto | 0:669ef71cba68 | 7 | |
t_yamamoto | 0:669ef71cba68 | 8 | namespace RS485 { |
yabahiro | 21:1f1e9c585da8 | 9 | |
yabahiro | 21:1f1e9c585da8 | 10 | DigitalOut selectBitT(SELECTBIT_T_PIN); |
yabahiro | 21:1f1e9c585da8 | 11 | // DigitalOut selectBitR(SELECTBIT_R_PIN); |
t_yamamoto | 0:669ef71cba68 | 12 | Serial RS485Uart(RS485UART_TX, RS485UART_RX); |
yabahiro | 21:1f1e9c585da8 | 13 | Serial RS485Line(RS485LINE_TX, RS485LINE_RX); |
yabahiro | 21:1f1e9c585da8 | 14 | |
yabahiro | 21:1f1e9c585da8 | 15 | bool readFase = 0; |
yabahiro | 21:1f1e9c585da8 | 16 | char buffer[RS485_BUFFER_LINE] = {0}; |
t_yamamoto | 0:669ef71cba68 | 17 | |
t_yamamoto | 0:669ef71cba68 | 18 | void Transmit(); |
yabahiro | 21:1f1e9c585da8 | 19 | void Recieve(); |
t_yamamoto | 0:669ef71cba68 | 20 | |
t_yamamoto | 0:669ef71cba68 | 21 | void RS485::Initialize() { |
yabahiro | 21:1f1e9c585da8 | 22 | selectBitT = 1; //送信固定 |
yabahiro | 21:1f1e9c585da8 | 23 | // selectBitR = 0; //受信固定 |
t_yamamoto | 0:669ef71cba68 | 24 | RS485Uart.baud(38400); |
yabahiro | 21:1f1e9c585da8 | 25 | RS485Uart.attach(Transmit, Serial::TxIrq); //送信割り込み |
yabahiro | 21:1f1e9c585da8 | 26 | RS485Line.baud(9600); |
yabahiro | 21:1f1e9c585da8 | 27 | RS485Line.attach(Recieve, Serial::RxIrq); //受信割り込み |
t_yamamoto | 0:669ef71cba68 | 28 | } |
t_yamamoto | 0:669ef71cba68 | 29 | |
t_yamamoto | 0:669ef71cba68 | 30 | void Transmit() { |
yabahiro | 21:1f1e9c585da8 | 31 | // __disable_irq(); |
t_yamamoto | 0:669ef71cba68 | 32 | static uint8_t count = 0; |
t_yamamoto | 0:669ef71cba68 | 33 | RS485Uart.putc(RS485SendBuffer.GetData()); |
t_yamamoto | 0:669ef71cba68 | 34 | if(count >= 200) { |
t_yamamoto | 0:669ef71cba68 | 35 | #ifdef USE_MOTOR |
t_yamamoto | 0:669ef71cba68 | 36 | LED_DEBUG2 = !LED_DEBUG2; |
t_yamamoto | 0:669ef71cba68 | 37 | #endif |
t_yamamoto | 0:669ef71cba68 | 38 | |
t_yamamoto | 0:669ef71cba68 | 39 | count = 0; |
t_yamamoto | 0:669ef71cba68 | 40 | } else count++; |
t_yamamoto | 0:669ef71cba68 | 41 | __enable_irq(); |
t_yamamoto | 0:669ef71cba68 | 42 | } |
yabahiro | 21:1f1e9c585da8 | 43 | |
yabahiro | 21:1f1e9c585da8 | 44 | void Recieve() { |
yabahiro | 21:1f1e9c585da8 | 45 | __disable_irq(); |
yabahiro | 21:1f1e9c585da8 | 46 | static uint8_t time = 0; |
yabahiro | 21:1f1e9c585da8 | 47 | static uint8_t count = 0; |
yabahiro | 21:1f1e9c585da8 | 48 | char data = RS485Line.getc(); |
yabahiro | 21:1f1e9c585da8 | 49 | if (data == 'S') { |
yabahiro | 21:1f1e9c585da8 | 50 | readFase = true; |
yabahiro | 21:1f1e9c585da8 | 51 | time = 0; |
yabahiro | 21:1f1e9c585da8 | 52 | } else if(data == 'F') { |
yabahiro | 21:1f1e9c585da8 | 53 | readFase = false; |
yabahiro | 21:1f1e9c585da8 | 54 | for(int i = 0; i < 8; i++) { |
yabahiro | 21:1f1e9c585da8 | 55 | lineData[i] = buffer[i]; |
yabahiro | 21:1f1e9c585da8 | 56 | } |
yabahiro | 21:1f1e9c585da8 | 57 | time = 0; |
yabahiro | 21:1f1e9c585da8 | 58 | } else if(readFase) { |
yabahiro | 21:1f1e9c585da8 | 59 | RS485LineBuffer.PutData(data); |
yabahiro | 21:1f1e9c585da8 | 60 | buffer[time] = data; |
yabahiro | 21:1f1e9c585da8 | 61 | time++; |
yabahiro | 21:1f1e9c585da8 | 62 | } else { |
yabahiro | 21:1f1e9c585da8 | 63 | readFase = false; |
yabahiro | 21:1f1e9c585da8 | 64 | time = 0; |
yabahiro | 21:1f1e9c585da8 | 65 | } |
yabahiro | 21:1f1e9c585da8 | 66 | if(count >= 200) { |
yabahiro | 21:1f1e9c585da8 | 67 | LED_DEBUG1 = !LED_DEBUG1; |
yabahiro | 21:1f1e9c585da8 | 68 | count = 0; |
yabahiro | 21:1f1e9c585da8 | 69 | } else count++; |
yabahiro | 21:1f1e9c585da8 | 70 | __enable_irq(); |
yabahiro | 21:1f1e9c585da8 | 71 | } |
yabahiro | 21:1f1e9c585da8 | 72 | } |