|
收到的数据只有一个字节 00,求解
硬件:STM32L152 MAX3485
MAX3485的A和B之间连接120欧电阻,仪表485也有120欧电阻
PB5 做485的控制端
串口1接485的RX和TX,接收中断
串口3输出MODBUS接收到的数据
程序:
/***********************************************************************
* @brief 串口1初始化
*
* @param None
*
* @retval None
*
**********************************************************************/
void usart1_config()
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //----1 串口时钟使能
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //---2 IO时钟使能
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); //--------3 (1) 配置串口管脚
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //------------------- (2) 配置管脚 AF 模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10; //-------------- (3) 配置管脚其他参数
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure); //----------------------------- (4) 调用GPIO初始化函数
USART_InitStructure.USART_BaudRate = 9600; //---------------------4 设置波特率等参数
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);// ---------------------------5 调用串口初始化函数
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// 接收中断
USART_Cmd(USART1, ENABLE); //----------------------------------------6 开启串口
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/************ 配置 USART1 中断向量*************/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
中断入口函数:
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
USART_SendData(USART3,USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART3,USART_FLAG_TXE) == RESET);
}
}
485控制IO配置:
void MODBUS_GPIO()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //GPIO CLOCK Enable
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
主函数:
uint8_t msg[8] = {0x01,0x04,0x00,0x00,0x00,0x05,0x30,0x09};
int main(void)
{
uint8_t i;
usart3_config();
usart1_config();
NVIC_Config();
MODBUS_GPIO();
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_5); //485发送
delay_ms(1);
for(i=0;i<8;i++)
{
USART_SendData(USART1,msg);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
// delay_ms(10);
}
delay_ms(1);
GPIO_ResetBits(GPIOB,GPIO_Pin_5); //485接收
delay_ms(100);
}
}
|
|