大季 矢花 / Mbed 2 deprecated MB2019_main_11_15_ok

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RS485.cpp Source File

RS485.cpp

00001 #include "RS485.h"
00002 #include "mbed.h"
00003 #include "ActuatorHub/ActuatorHub.h"
00004 #include "LineHub/LineHub.h"
00005 #include "../../LED/LED.h"
00006 #include "../../System/Using.h"
00007 
00008 namespace RS485 {
00009     
00010     DigitalOut selectBitT(SELECTBIT_T_PIN);
00011 //  DigitalOut selectBitR(SELECTBIT_R_PIN);
00012     Serial RS485Uart(RS485UART_TX, RS485UART_RX);
00013 //    Serial RS485Uart(USBTX, USBRX);
00014     Serial RS485Line(RS485LINE_TX, RS485LINE_RX);
00015     
00016     bool readFase = 0;
00017     char buffer[RS485_BUFFER_LINE] = {0};
00018 
00019     void Transmit();
00020     void Recieve();
00021 
00022     void RS485::Initialize() {
00023         selectBitT = 1;  //送信固定
00024 //      selectBitR = 0;  //受信固定
00025         RS485Uart.baud(38400);  // 38400
00026         RS485Uart.attach(Transmit, Serial::TxIrq);  //送信割り込み
00027         //RS485Line.baud(9600);
00028         RS485Line.baud(19200);  
00029         RS485Line.attach(Recieve, Serial::RxIrq);  //受信割り込み
00030     }
00031 
00032     void Transmit() {
00033 //        __disable_irq();
00034         static uint8_t count = 0;
00035         RS485Uart.putc(RS485SendBuffer.GetData());
00036         if(count >= 200) {
00037             #ifdef USE_MOTOR
00038             LED_DEBUG2 = !LED_DEBUG2;
00039             #endif
00040 
00041             count = 0;
00042         } else count++;
00043         __enable_irq();
00044     }
00045     
00046     void Recieve() {
00047         __disable_irq();
00048         static uint8_t time = 0;
00049         static uint8_t count = 0;
00050         char data = RS485Line.getc();
00051         if (data == 'S') {
00052             readFase = true;
00053             time = 0;
00054         } else if(data == 'F') {
00055             readFase = false;
00056             for(int i = 0; i < 8; i++) {
00057                 lineData[i] = buffer[i];
00058             }
00059             time = 0;
00060         } else if(readFase) {
00061             buffer[time] = data;
00062             time++;
00063         } else {
00064             readFase = false;
00065             time = 0;
00066         }
00067         if(count >= 200) {
00068             LED_DEBUG1 = !LED_DEBUG1;
00069             count = 0;
00070         } else count++;
00071         __enable_irq();
00072     }
00073 }