[8/16-bit MCU] [TRK-KEA128]LIN通讯实验

[复制链接]
 楼主| ideafor 发表于 2016-1-14 21:27 | 显示全部楼层 |阅读模式
  1. <div class="blockcode"><blockquote>void LIN_Init(uint32_t baud)
  2. {
  3.         GPIO_InitTypeDef GPIO_InitStructure;
  4.         GPIO_InitStructure.GPIOx = PTB;     //PORT端口选择
  5.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;  //选择输出模式
  6.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4; //PTC 0~3  
  7.         GPIO_InitStructure.GPIO_InitState = Bit_SET;       //低电平
  8.         GPIO_Init(&GPIO_InitStructure);     //初始化
  9.         
  10.         //UART
  11.         UART_InitTypeDef UART_InitStruct;
  12.         UART_InitStruct.UART_BaudRate = baud; //波特率
  13.         UART_InitStruct.UART_WordLength = UART_WordLength_8b ;  //数据长度
  14.         UART_InitStruct.UART_StopBits = UART_StopBits_1;       //停止位
  15.         UART_InitStruct.UART_Parity = UART_Parity_No;          //奇偶校验
  16.         UART_InitStruct.UART_Mode = UART_Mode_Rx|UART_Mode_Tx; //RX TX使能
  17.         UART_InitStruct.UART_PIN = RX_PB0_TX_PB1 ;             //RX_PD6_TX_PD7
  18.         UART_Init(UART0,&UART_InitStruct);                     //初始化UART
  19.         
  20.         UART0->S2 |= UART_S2_BRK13_MASK ;
  21.                 //UART_ITConfig(UART2,UART_IT_RXNE, ENABLE);             //中断使能
  22.         //NVIC_Init(UART2_IRQn,2);                              //中断使能 分组2
  23.         
  24. }

  25. /**
  26.   * [url=home.php?mod=space&uid=247401]@brief[/url] 发送同步间隔段
  27.   * @param   
  28.   * @retval
  29.   */
  30. void LIN_SendBreak(UART_Type* UARTx)
  31. {
  32.   while(!(UARTx->S1 & UART_S1_TDRE_MASK));  //等待发送数据寄存器(缓冲器)为空
  33.   UARTx->C2 |= UART_C2_SBK_MASK  ;           //队列待发送的中止字符
  34.   UARTx->C2 &= ~UART_C2_SBK_MASK;           //返回正常发送操作
  35. }

  36. /**
  37.   * @brief LIN奇偶校验计算
  38.   * @param   
  39.   * @retval
  40.   */
  41. uint8_t LIN_CalcParity(uint8_t id)
  42. {
  43.   uint8_t parity, p0,p1;
  44.   parity=id;
  45.   p0=(BIT(parity,0)^BIT(parity,1)^BIT(parity,2)^BIT(parity,4))<<6;     //偶校验位
  46.   p1=(!(BIT(parity,1)^BIT(parity,3)^BIT(parity,4)^BIT(parity,5)))<<7;  //奇校验位
  47.   parity|=(p0|p1);
  48.   return parity;
  49. }


  50.   /**
  51.    * @brief LIN校验和计算
  52.    * @param   
  53.    * @retval
  54.    */
  55. uint8_t LIN_Checksum(uint8_t id,uint8_t *data,uint8_t length)
  56. {
  57.    uint8_t i;
  58.    uint32_t check_sum = 0;
  59.    if(id != 0x3c && id != 0x7d)  //使用增强型校验
  60.    {
  61.           check_sum  = id ;
  62.    }
  63.    else //使用标准校验
  64.    {
  65.           check_sum = 0 ;
  66.    }
  67.    for (i = 0; i < length; i++)
  68.          {         
  69.            check_sum += *(data++);
  70.            
  71.            if (check_sum > 0xFF)          //进位
  72.            {
  73.                  check_sum -= 0xFF;
  74.            }
  75.          }
  76.          return (~check_sum);  //取反
  77. }

  78.   /**
  79.    * @brief LIN发送函数
  80.    * @param   
  81.    * @retval
  82.    */
  83. uint8_t LIN_Send_Msg(UART_Type* UARTx,LIN_Msg *SendMsg)
  84. {
  85.    uint8_t pid ,i ,check_sum;
  86.    if(SendMsg->ID == 0xff||SendMsg->ID == 0x00)
  87.    return (FALSE);
  88.    //发送间隔同步段
  89.    LIN_SendBreak(UARTx);
  90.          
  91.    //发送同步场
  92.    UART_SendData(UARTx,0x55);
  93.   
  94.    //计算PID
  95.    pid = LIN_CalcParity(SendMsg->ID);
  96.    //发送PID
  97.     UART_SendData(UARTx,pid);
  98.   
  99.    //发送数据场
  100.    for(i=0; i <SendMsg->length ; i++)         
  101.         {
  102.           // 发送数据场
  103.            UART_SendData(UARTx,SendMsg->Data[i]);      

  104.         }
  105.         //计算校验和                        
  106.    check_sum = LIN_Checksum(pid,SendMsg->Data,SendMsg->length);
  107.     UART_SendData(UARTx,check_sum);

  108.          return(TRUE);
  109. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

30

主题

149

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部