RS-485 + modbus protocol: remote fire detection system

Preparatory

1.cortex-M3 master module 2

2. The flame sensor 1

Achieve phenomenon

M3 LED8S collected on the end of the scintillator module, control module M3 monitor queries the fire end of every two seconds, LED 9 on for 0.5 second and off for 1.5 seconds, when there is

When generating fire monitoring terminal LED1-LED4 start significant water lamp shown, press the button of the monitor terminal 1 (KEY1) water lights off.

A, STM32CubeMX configuration

1.1 LED1-LED4 (PE7, PE6, PE5, PE4), KEY1 (PC13), LED9 (PB8) configuration where GPIO

 

 

 

 

 1.2 USART2 serial baud rate is set to 115200bps, 8 data bits, 1 stop bit, no parity

 

 1.3 PC9 pin 485 controls communication chip duplexer (low received, sending a high level)

 

 

 1.4 clock tree configuration, frequency 72M

 

 

 

 

 1.5 Configuring the timer operating parameters

 

 

 Generate code

Third, the supplementary code

3.1 Definitions variable

/* USER CODE BEGIN PV */
// LED灯的状态
static uint16_t ledState = 0x0000;    
// 着火标志,默认为0,不着火
static uint8_t flag_fire = 0;
// 接受的数据存放
uint8_t rxBuf[20] = {0};
/* USER CODE END PV */

3.2监测端发送命令

/* USER CODE BEGIN 0 */
void SendCmd(void)
{
        // 监测端查询请求的数据格式
    uint8_t cmd[8] = {0};
    cmd[0] = 0x01;
    cmd[1] = 0x04;
    cmd[2] = 0x00;
    cmd[3] = 0x02;
    cmd[4] = 0x00;
    cmd[5] = 0x01;
    cmd[6] = 0x90;
    cmd[7] = 0x0A;
// 设置PC9引脚为低电平,接受数据    HAL_GPIO_WritePin(CONPIN_GPIO_Port,CONPIN_Pin,GPIO_PIN_SET);
// 发送数据 HAL_UART_Transmit(串口,内容,数据大小,延时时间)
    HAL_UART_Transmit(&huart2,cmd,8,0xffff);
// 设置PC9引脚为低电平,接受数据    
HAL_GPIO_WritePin(CONPIN_GPIO_Port,CONPIN_Pin,GPIO_PIN_RESET);
// 接受数据HAL_UART_Receive_IT(串口,数据,大小)
    HAL_UART_Receive_IT(&huart2,rxBuf,7);
}

/* USER CODE END 0 */

3.3启动定时器的中断

  /* USER CODE BEGIN 2 */
  if(HAL_TIM_Base_Start_IT(&htim6) != HAL_OK)
  {
      Error_Handler();
  }
  if(HAL_TIM_Base_Start_IT(&htim7) != HAL_OK)
  {
      Error_Handler();
  }

  /* USER CODE END 2 */

3.4实现监测端LED9 每隔 2 秒闪烁一 次(亮 0.5 秒,灭 1.5 秒)

 /* USER CODE BEGIN WHILE */
  while (1)
  {
      HAL_GPIO_WritePin(LED9_GPIO_Port,LED9_Pin,GPIO_PIN_RESET);
      HAL_Delay(500);
      HAL_GPIO_WritePin(LED9_GPIO_Port,LED9_Pin,GPIO_PIN_SET);
      HAL_Delay(1500);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

3.5按键中断

/* USER CODE BEGIN 4 */
//
实现按键中的回调函数 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { // 判断按键按下 if(GPIO_Pin & GPIO_PIN_13) { // 判断火情 if(flag_fire == 1) { // 存在火情,将标志位置0 flag_fire = 0; // LED灯置0 ledState = 0; // 所有引脚置于低电平 HAL_GPIO_WritePin(GPIOE,0xffff,GPIO_PIN_SET); } } }

3.6定时器

// 定时器回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
        // 定时器6产生
    if(htim->Instance == TIM6)
    {
                // 发生火情
        if(flag_fire == 1)
        {
            // ledState=0x0000
            // 0x0000H=0000 0000B
            // 0000 0000B>>1=0000 0000B
            ledState = ledState>>1;
            // 0x0080H=1000 0000B
            // 1000 0000B | 0000 0000B->1000 0000B
// LED1点亮
ledState |= 0x0080 // 0x00f8H=1111 1000B if(ledState == 0x00f8) ledState = 0; HAL_GPIO_WritePin(GPIOE,0xffff,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOE,ledState,GPIO_PIN_RESET); } }
//定时器7,每2秒发送一次命令
if(htim->Instance == TIM7) { SendCmd(); } }

 3.7串口接受函数

485通信协议,比如

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART2)
    {
                // 比对地址码和功能码
        if((rxBuf[0] == 0x01) && (rxBuf[1] == 0x04))
        {
                        // 定义无符号整型变量temp
            uint16_t temp = 0;
                        // rxBuf[3]为寄存器的值
                        // 例如:rxBuf[3]=0x00c8H=1100 1000B
                        // 1100 1000B<<8 ->0000 0000B
            temp = ((uint16_t)rxBuf[3])<<8;
                        // 0000 0000B | 1000 1011 1010 0110=0000 0000 0000 0000
            temp = temp | rxBuf[4];
                        // 当temp>100时发出报警(flag_fire = 1)
            if(temp > 100)
            {
                flag_fire = 1;
            }
        }
    }
}
/* USER CODE END 4 */

 

 

 

 

Guess you like

Origin www.cnblogs.com/jsit-dj-it/p/12002932.html