第一次接触485通讯,是自制的电路板,用的芯片是STM32F103RET6和RY485;通过485转串口将板子和PC机连接,在串口调试助手中向板子发送数据,板子将接收到的数据返还给PC机,目前的问题是:
1、将485使能引脚拉低,设置成接收模式;引脚拉高,设置成发送模式;串口调试助手发送数据没有返回;
2、接收状态下,将使能引脚拉高;发送状态下,将使能引脚拉低;(此处描述有点乱)串口调试助手向板子发送数据55,返回55 00;发AA,返回15 00;有数据返回但不正确;
跪求指教
程序如下:
int main(void)
{
Stm32_Clock_Init(9);
delay_init(72);
InitHT1621();
GPIO_USART_Config();
NVIC_USART_Config();
NVIC_Configuration();
USART_Config(2400);
while(1)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_7); //??485?è??????????
}
}
#define RX_485 GPIO_ResetBits(GPIOA,GPIO_Pin_7);
#define TX_485 GPIO_SetBits(GPIOA,GPIO_Pin_7);
void GPIO_USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART2|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void NVIC_USART_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config(u32 bound)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = bound;
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(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
}
void USART_TX485_Puts(uint16_t *str)
{
while(*str)
{
TX_485;
delay_ms(2);
USART_SendData(USART2,*str++);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET);
delay_ms(2);
RX_485;
}
}
void USART2_IRQHandler(void)
{
u8 i;
uint16_t USART_RX_BUF[5] = {0,0,0,0,0};
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET)
{
for(i=0;i<5;i++)
{
USART_RX_BUF = USART_ReceiveData(USART2);
}
delay_ms(10);
USART_TX485_Puts(USART_RX_BUF);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
正常,单片机IO口电平不确定的时候,很容易产生这种数据的.
你程序里面,做下处理就好了.
一周热门 更多>