1、问题描述
我使用STM32F103VB的板子的USART2用于接发数据。实验现象是:能够正常发送数据,但是接收的时候不能进入中断服务函数,所以不能接收。在此附上电路图和USART2配置程序,希望路过的大神小试牛刀帮忙看一看,感激不尽~
2、MAX3483连接图
PA0,PA1同时为1是为发送模式,同时为0时为接收模式。
3、RS485.c程序(参照原子哥配置,在此说明并致谢!)
#include "sys.h"
#include "rs485.h"
#include "delay.h"
#ifdef EN_USART2_RX
u8 RS485_RX_BUF[64]; //????????,×??ó64??×???
u8 RS485_RX_CNT = 0; //?????????????¤??
void RS485_Init(u32 bound)
{
//GPIO?????è??
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?±??
USART_DeInit(USART2);
//????RS485_TX_EN0 ?? RS485_TX_EN0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; //PA.0,PA.1
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???????ì????
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART2_TX GPIOA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????ì????
GPIO_Init(GPIOA, &GPIO_InitStructure);//??????GPIOA.2
//USART2_RX GPIOA.3??????
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_Init(GPIOA, &GPIO_InitStructure);//??????GPIOA.3
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3 ;//??????????2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×???????2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????NVIC?????÷
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//?????®??2
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//????????
//USART ???????è??
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); //???????®??2
//Usart1 NVIC ????
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//?????®??????????
USART_Cmd(USART2, ENABLE); //?????®??2
RS485_TX_EN0 = DISABLE;
RS485_TX_EN1 = DISABLE;//??????????????
}
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET)
{
res = USART_ReceiveData(USART2);//Receive Data
if(RS485_RX_CNT < 64)
{
RS485_RX_BUF[RS485_RX_CNT]=res;
RS485_Send_Data(RS485_RX_BUF,RS485_RX_CNT);
RS485_RX_CNT++;
}
}
}
void RS485_Send_Data(u8 *ptr,u8 len)
{
unsigned char i;
RS485_TX_EN0 = ENABLE;
RS485_TX_EN1 = ENABLE;//?è????·???????
USART_ClearFlag(USART2,USART_FLAG_TC);
for(i=0;i<len;i++)
{
USART_SendData(USART2,*ptr);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC) == RESET);
ptr++;
}
RS485_RX_CNT = 0;
RS485_TX_EN0 = DISABLE;
RS485_TX_EN1 = DISABLE;//?è????????????
}
void RS485_Receive_Data(u8 *ptr,u8 *len)
{
u8 rxlen = RS485_RX_CNT;
u8 i=0;
*len = 0;
delay_ms(200);
if(rxlen == (RS485_RX_CNT && rxlen))
{
for(i=0;i<rxlen;i++)
{
ptr
= RS485_RX_BUF;
}
*len = RS485_RX_CNT;
RS485_RX_CNT = 0;
}
}
#endif
一周热门 更多>