Руслан Бредун / Mbed 2 deprecated STM32-MC_node

Dependencies:   mbed Watchdog stm32-sensor-base2

main.cpp

Committer:
ommpy
Date:
2020-07-28
Revision:
8:c3cffab85b0d
Parent:
7:2f218add711e
Child:
9:859bcb293e46

File content as of revision 8:c3cffab85b0d:

#include <global.h>

RS485 RS485(UART2_TX,UART2_RX,DE_TXD_2); // Tx, Rx , !RE and DE MAX485 pin
 
volatile bool serialArrived = false;
int n=0;
char a;
bool send_flag = false;

Timer timer;
typedef uint8_t byte;

DigitalOut select(DE_TXD_1);
byte regvalue[1] ;
 byte data[6];
const unsigned char CRC7_POLY = 0x91;
 
unsigned char getCRC(unsigned char message[], unsigned char length)
{
  unsigned char i, j, crc = 0;
 
  for (i = 0; i < length; i++)
  {
    crc ^= message[i];
    for (j = 0; j < 8; j++)
    {
      if (crc & 1)
        crc ^= CRC7_POLY;
      crc >>= 1;
    }
  }
  return crc;
}
int main()
{
# if DEMO_CODE
    
    JSN_SR04 sensor1(TIM1_CH2, TRIG_PA8_OUT);
    E18_D80NK infared1 (IR1_PB12_OUT);
    E18_D80NK infared2 (IR2_PB13_OUT);
    
    select = 1;
    
    sensor1.setRanges(10, 200);
    
    memset(data,0,sizeof(data));
    float distance;
    
    while(true) {      
         if(RS485.readable() >0){
           //memset(regvalue,0,sizeof(regvalue));
           //wait_ms(100);
           RS485.recvMsg(regvalue,sizeof(uint8_t),30);
           }
        //printf("Done\n");
        
        if((int)regvalue[1] == 4 )
        {
            select = 1;
            timer.reset();
            timer.start();
            sensor1.startMeasurement();
            
            distance = sensor1.getDistance_cm();
            data[0] = (uint8_t)(distance >> 24) ;
            data[1] = (uint8_t)(distance >> 16) ; 
            data[2] = (uint8_t)(distance >> 8) ;
            data[3] = (uint8_t)distance  ;
            data[4] = (uint8_t)infared1.checkObstacle();
            data[5] = (uint8_t)infared2.checkObstacle();
            RS485.sendMsg(data,sizeof(data));
            timer.stop();
            send_flag = false;
            wait_ms(20);
            
            select = 0 ;
        }

    }
    


#endif

}