数据通过485总线发送到485转TTL模块(485转串口),
STM32单片机接受,但无法接受到数据,不知道是什么原因?两边的波特相同,使用USB转ttl接受485转TTL的数据,完全可以显示!求大神指教
下面是代码:
tica">u8 RS485_RX_BUF[64];
u8 RS485_RX_CNT=0;
void RS485_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹÄÜGPIOAʱÖÓ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE) ; //ʹÄÜUSART2ʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍì
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//¸´Î»´®¿Ú2
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//Í£Ö¹¸´Î»
USART_InitStructure.USART_BaudRate = bound;//²¨ÌØÂÊÉèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8λÊý¾Ý³¤¶È
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);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
res =USART_ReceiveData(USART2);
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
if(RS485_RX_CNT<64)
{
RS485_RX_BUF[RS485_RX_CNT]=res;
RS485_RX_CNT++;
}
}
}
void RS485_Receive_Data(u8 *buf)
{
u8 i;
delay_ms(10);
if(RS485_RX_CNT)
{
for(i=0;i<RS485_RX_CNT;i++)
{
buf=RS485_RX_BUF;
}
RS485_RX_CNT=0;
}
}
void BEEP_485(void)
{
u8 resbuf[5] ,i ;
RS485_Receive_Data(resbuf);
for(i=0;i<5;i++)
{
printf("res:%d
",resbuf);
if(resbuf=='1')
{
BEEP=1;
delay_ms(5000);
BEEP=0;
}
}
}
主循环一直运行BEEP_485(); 发送端以字符串的形式每次发送5个1;
一周热门 更多>