usart多机通信 地址标记检测 静默模式

2019-07-21 08:42发布

参考官方例程做usart多机通信,它是怎么样发送地址的?

只看到 USART_SendData(USARTy, 0x33);那地址怎么发送了的?



int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System Clocks Configuration */
  RCC_Configuration();
      
  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* Initialize Leds, Wakeup and Key Buttons mounted on STM3210X-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_PBInit(BUTTON_WAKEUP, BUTTON_MODE_EXTI);         
  STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); 

/* USARTy and USARTz configuration -------------------------------------------*/
  /* USARTy and USARTz configured as follow:
        - BaudRate = 9600 baud 
        - Word Length = 9 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_InitStructure.USART_BaudRate = 9600;
  USART_InitStructure.USART_WordLength = USART_WordLength_9b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 
  /* Configure USARTy */
  USART_Init(USARTy, &USART_InitStructure);
  /* Configure USARTz */
  USART_Init(USARTz, &USART_InitStructure);
 
  /* Enable the USARTy */
  USART_Cmd(USARTy, ENABLE);
  /* Enable the USARTz */
  USART_Cmd(USARTz, ENABLE);

  /* Set the USARTy Address */
  USART_SetAddress(USARTy, 0x1);
  /* Set the USARTz Address */
  USART_SetAddress(USARTz, 0x2);

  /* Select the USARTz WakeUp Method */
  USART_WakeUpConfig(USARTz, USART_WakeUp_AddressMark);    //被地址标记唤醒
 
  while (1)
  {
    /* Send one byte from USARTy to USARTz */
    USART_SendData(USARTy, 0x33);
    /* Wait while USART1 TXE = 0 */
    while(USART_GetFlagStatus(USARTz, USART_FLAG_TXE) == RESET)
    {
    }
   
    if(USART_GetFlagStatus(USARTz, USART_FLAG_RXNE) != RESET)
    {
      if(USART_ReceiveData(USARTz) == 0x33)
      {
        STM_EVAL_LEDToggle(LED1);
        Delay(0x5FFFF);
        STM_EVAL_LEDToggle(LED2);
        Delay(0x5FFFF);
        STM_EVAL_LEDToggle(LED3);
        Delay(0x5FFFF);
        STM_EVAL_LEDToggle(LED4);
        Delay(0x5FFFF);
      }
    }
  }
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
26条回答
MDZZYUE
1楼-- · 2019-07-21 13:26
qiuxuanbin 发表于 2018-3-16 20:06
你好,这是从哪知道的?固件库上的注释有吗,我没找到!还是说只有手册上有写?

USART 的控制寄存器中有一个叫做WAKE的bit位,当这个位被置位时,数据的最高位为1,说明这个数据是个地址数据,地址被存在低四位中。接受方会将这个低四位数据与自身的数据进行对比,若是相符合的话,就会退出所谓的静默模式,可以接受数据。注意这里的最高位是指你设定的数据格式的最高位 设定为9位的话,就是第九位,是这个意思
正点原子
2楼-- · 2019-07-21 15:17
没用过这个功能,直接做协议吧。
sunday151640
3楼-- · 2019-07-21 16:16
 精彩回答 2  元偷偷看……
正点原子
4楼-- · 2019-07-21 20:23
回复【3楼】sunday151640:
---------------------------------
参见:modbus
sunday151640
5楼-- · 2019-07-21 23:24
回复【4楼】正点原子:
---------------------------------


谢谢 原子哥。。
废墟崛起之厦
6楼-- · 2019-07-21 23:56

我看了下,最近准备用STM32做主控,STM8做从机,发送的地址在寄存器里面,他不像51,自己还得定义一个变量来存地址,然后自己写程序识别,STM32是硬件自己帮你识别的。。。详见ADD设置。。。

一周热门 更多>