三浦 颯太 / Mbed 2 deprecated MB2019_main_ver3

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.attach(Recieve, Serial::RxIrq);  //受信割り込み
00029     }
00030 
00031     void Transmit() {
00032 //        __disable_irq();
00033         static uint8_t count = 0;
00034         RS485Uart.putc(RS485SendBuffer.GetData());
00035         if(count >= 200) {
00036             #ifdef USE_MOTOR
00037             LED_DEBUG2 = !LED_DEBUG2;
00038             #endif
00039 
00040             count = 0;
00041         } else count++;
00042         __enable_irq();
00043     }
00044     
00045     void Recieve() {
00046         __disable_irq();
00047         static uint8_t time = 0;
00048         static uint8_t count = 0;
00049         char data = RS485Line.getc();
00050         if (data == 'S') {
00051             readFase = true;
00052             time = 0;
00053         } else if(data == 'F') {
00054             readFase = false;
00055             for(int i = 0; i < 8; i++) {
00056                 lineData[i] = buffer[i];
00057             }
00058             time = 0;
00059         } else if(readFase) {
00060             buffer[time] = data;
00061             time++;
00062         } else {
00063             readFase = false;
00064             time = 0;
00065         }
00066         if(count >= 200) {
00067             LED_DEBUG1 = !LED_DEBUG1;
00068             count = 0;
00069         } else count++;
00070         __enable_irq();
00071     }
00072 }