Software modbus, display, knoppen, bewegingssensor en mbed OS

Dependents:   testSoftware8_alles_display

setup.cpp

Committer:
mikevd1
Date:
2018-06-08
Revision:
1:92f9d6cec962
Parent:
0:d603d31b42dc
Child:
2:ca3d2395b6d3

File content as of revision 1:92f9d6cec962:

#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
            }
            else if ( data == 0x02 && buffer == 1)
            {
                dataReceived[buffer++] = data ;     // FunctionID checks out proceed to receiving data 
            } 
            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 = 0 ; 
    unsigned static int count = 0 ; 
    dataSend[maxData++] = slaveID ; 
    dataSend[maxData++] = dataReceived[1];
    dataSend[maxData++] = 0x01 ;        // byte count
    count++; 
    if ( count > 12 )
    {
        dataSend[maxData++] = 0x00 ;     
              
    }
    else 
    {
        dataSend[maxData++] = 0x01 ; 
    }
    if ( count == 25 )
    { 
        count = 0 ; 
    }

    uint16_t temp = calculate_crc16_Modbus(dataSend, maxData ) ; 
    dataSend[maxData++] = (uint8_t)temp ; 
    dataSend[maxData++] = ((uint16_t)temp>>8); 
    activateSN65 = 1 ; 
    wait_ms(3);
    led1 = !led1; 
    for(i = 0 ; i < maxData ; i++ )
    {
        bus.putc(dataSend[i]);
        pc.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;
}