Software modbus, display, knoppen, bewegingssensor en mbed OS
Dependents: testSoftware8_alles_display
Diff: setup.cpp
- Revision:
- 0:d603d31b42dc
- Child:
- 1:92f9d6cec962
diff -r 000000000000 -r d603d31b42dc setup.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.cpp Thu Jun 07 14:28:24 2018 +0000 @@ -0,0 +1,217 @@ +#include "mbed.h" +#include "setup.h" + +unsigned int slaveID = 0x07; +unsigned int functionID0x03 = 0x03; +unsigned int functionID0x05 = 0x05; +unsigned int functionID0x02 = 0x02; + +char TxEnablePin = PA_8; + +Serial bus(PA_9,PA_10); +DigitalOut led1(LED1); +DigitalOut activateSN65(PA_8); +Serial pc(USBTX, USBRX); + +// Timers +Timer intercharacter_timer; +Timeout interframe_timeout; + + +unsigned int T1; +unsigned int T1_5; // inter character time out +unsigned int T3_5; // frame delay +unsigned static int buffer = 0; +unsigned int dataReceived[10] ; +unsigned int dataSend[10] ; + + +void modbus_configure(long baud, int _slaveID ) +{ + pc.baud(115200); + slaveID = _slaveID; + + if (baud == 1000000 ){ + T1_5 = 1; + T3_5 = 10; + } + else if (baud >= 115200 ){ + T1_5 = 75; + T3_5 = 175; + } + else if (baud > 19200){ + T1_5 = 750; + T3_5 = 1750; + } + else { + T1_5 = 15000000/baud; // 1T * 1.5 = T1.5 + T3_5 = 35000000/baud; // 1T * 3.5 = T3.5 + } + T1 = 10000000/baud; +} +int modbus_read2(void) +{ + + //int data ; + //int value = 0 ; + //activateSN65 = 0 ; + + if(bus.readable()) + { + int data = bus.getc(); + pc.putc(data); + } + return 0 ; +} + +int modbus_read(void) +{ + activateSN65 = 0 ; + if(bus.readable()) + { + int data = bus.getc(); + //pc.putc(data); + if ( data == slaveID && buffer == 0 ) + { + dataReceived[buffer++] = slaveID ; // slaveID checks out proceed to checking function ID + //pc.putc(buffer); + } + else if ( data == 0x02 && buffer == 1) + { + dataReceived[buffer++] = data ; // FunctionID checks out proceed to receiving data + //pc.putc(buffer); + } + else if ( buffer == 2 || buffer == 3 ) // get data adress high and low order + { + // adress high and low + if ( buffer == 2 ) + { + dataReceived[buffer++] = data ; + //pc.putc(buffer); + } + else + { + dataReceived[buffer++] = data ; + //pc.putc(buffer); + } + } + else if ( buffer == 4 || buffer == 5 ) + { + // adress high and low + if ( buffer == 4 ) + { + dataReceived[buffer++] = data ; + //pc.putc(buffer); + } + else + { + dataReceived[buffer++] = data ; + //pc.putc(buffer); + } + } + else if ( buffer == 6 || buffer == 7 ) + { + if ( buffer == 6 ) + { + dataReceived[buffer++] = data ; + //pc.putc(buffer); + } + else + { + dataReceived[buffer++] = data ; + unsigned long crc = ((dataReceived[buffer - 1] << 8) | dataReceived[buffer - 2]); // combine the crc Low & High bytes + // check crc + if ( calculate_crc16_Modbus(dataReceived, buffer - 2 ) == crc ) + { + buffer = 8 ; + //pc.putc(buffer); + } + else + { + buffer = 0 ; + //pc.putc(buffer); + // error + } + } + } + else + { + buffer = 0 ; + pc.putc(buffer); + } + } + if ( buffer == 8 ) + { + modbus_sendData(); + } + + return 0 ; +} + +int modbus_sendData(void) +{ + int i = 0 ; + int maxData = 5 ; + unsigned static int count = 0 ; + + dataSend[0] = slaveID ; + dataSend[1] = dataReceived[1]; + dataSend[2] = 0x02 ; // byte count + dataSend[3] = 0x00 ; // data 1 + dataSend[4] = 0x00 ; // data 2 + if (count == 255 ) + { + count = 0 ; + } + uint16_t temp = calculate_crc16_Modbus(dataSend, 5 ) ; + dataSend[6] = (uint8_t)temp ; + dataSend[5] = ((uint16_t)temp>>8); + //pc.putc(dataSend[6]); + //pc.putc(dataSend[5]); + activateSN65 = 1 ; + wait_ms(3); + for(i = 0 ; i < maxData+2 ; i++ ) + { + led1 = !led1; + bus.putc(dataSend[i]); + wait_us(750); + } + buffer = 0 ; + wait_ms(2) ; + activateSN65 = 0 ; + + return 0 ; +} + +uint16_t update_crc16_reflected(const uint16_t *table, uint16_t crc, unsigned int c ) +{ + long short_c; + + short_c = 0x00ff & (long) c; + + /* Reflected form */ + return (crc >> 8) ^ table[(crc ^ short_c) & 0xff]; +} + + +uint16_t update_crc16_A001( long crc, unsigned int c ) +{ + return update_crc16_reflected(crc_tab_8005_reflected,crc,c); +} + + +long calculate_crc16_Modbus(unsigned int *p, unsigned int length) +{ + long crc; + unsigned int i; + + crc = 0xFFFF; + + for (i=0; i < length; i++) + { + crc = update_crc16_A001(crc,*p++); + } + return crc; +} + +