STM32F107 LIN通信问题(UART4)

a1314521531 2017-07-11 11:52:27
STM32F107VCT6 TJA1021接到串口4
实现LIN的主模式
目前功能仍然没有调试通过,希望有相关经验的能指导一下。


static void lin_gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitGPIOC&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitGPIOC&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
}

static void lin_nvic_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

static void lin_uart_init(void)
{
USART_InitTypeDef USART_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
USART_InitStructure.USART_BaudRate = 19200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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;
USART_Init(macUSARTx, &USART_InitStructure);

USART_LINBreakDetectLengthConfig(macUSARTx, USART_LINBreakDetectLength_11b);

USART_LINCmd(UART4,ENABLE);
USART_ITConfig(UART4, USART_IT_LBD, ENABLE);
//USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
USART_Cmd(UART4, ENABLE);
}

void LINInit(void)
{
lin_gpio_init();
lin_nvic_init();
lin_uart_init();
}

void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch )
{
/* 发送一个字节数据到USART1 */
USART_SendData(pUSARTx, ch);

/* 等待发送完毕 */
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}

static unsigned char LINCalcParity(unsigned char id)
{
unsigned char parity, p0, p1;
parity = id;
p0 = (BIT(parity, 0)^BIT(parity, 1)^BIT(parity, 2)^BIT(parity, 4)) << 6; //偶校验位
p1 = (!(BIT(parity, 1)^BIT(parity, 3)^BIT(parity, 4)^BIT(parity, 5))) << 7; //奇校验位
parity |= (p0 | p1);
return parity;
}

/*************************************************************/
/* 计算和校验位 */
/*************************************************************/
static unsigned char LINCalcChecksum(unsigned char *data)
{
unsigned int sum = 0;
unsigned char i;

for(i = 0; i < 8; i++)
{
sum += data[i];
if(sum & 0xFF00)
{
sum = (sum & 0x00FF) + 1;
}
}
sum ^= 0x00FF;
return (unsigned char)sum;
}

#if 0
msg_send.identifier = 0x30;
msg_send.data[0] = 'F';
msg_send.data[1] = 'R';
msg_send.data[2] = 'E';
msg_send.data[3] = 'E';
msg_send.data[4] = 'F';
msg_send.data[5] = 'L';
msg_send.data[6] = 'Y';
msg_send.data[7] = '!';
#endif
void LINSendTest(void)
{
unsigned char check_sum, parity_id, i;

USART_SendBreak(UART4);
Usart_SendByte(UART4, 0x55);

parity_id = LINCalcParity(msg_send.identifier);

LINSendChar(parity_id);

for(i = 0; i < 8; i++)
{
if(!LINSendChar(msg_send.data[i]))
{
return(FALSE);
}
}

check_sum = LINCalcChecksum(msg_send.data);
// 发送校验和场
LINSendChar(check_sum);

}
...全文
418 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

27,375

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧