完整的串口转485代码
void usart_config(uint8_t port, uint32_t BaudRate)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_TypeDef *InitPort = USART1; //默认是debug口
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //使能GPIOA的系统时钟
if (port == 1) {
RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1, ENABLE); //打开串口1串口时钟
//GPIO MAP
GPIO_PinAFConfig (GPIOA, GPIO_PinSource9, GPIO_AF_1); //初始化GPIOA的PIN9为串口功能
GPIO_PinAFConfig (GPIOA, GPIO_PinSource10, GPIO_AF_1); //初始化GPIOA的PIN10为串口功能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //初始化GPIOA PIN9,PIN10 口
InitPort = USART1;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
} else if (port == 2) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //打开串口2串口时钟
//GPIO MAP
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1); //初始化GPIOA的PIN2为串口功能;
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); //初始化GPIOA的PIN2为串口功能;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //初始化GPIOA PIN2,PIN3 口
InitPort = USART2;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
}
USART_DeInit (InitPort);
/* Configure pins as AF pushpull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //串口GPIO复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init (GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = BaudRate; //模特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据长度为8bit
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 (InitPort, &USART_InitStructure); //串口使能
USART_ITConfig(InitPort, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_ORE, ENABLE);
USART_Cmd (InitPort, ENABLE);
/* USART1 IRQ Channel configuration */
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler()
{
USART_ClearITPendingBit(USART1,USART_IT_ORE);
USART_ClearITPendingBit(USART1,USART_IT_IDLE);
USART_ClearITPendingBit(USART1,USART_IT_TXE);
USART_ClearITPendingBit(USART1,USART_IT_EOB);
if( USART_GetITStatus(USART1,USART_IT_RXNE) != RESET )
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
while((USART1->ISR&0x40) == 0);
USART2->TDR = USART1->RDR;
}
}
void USART2_IRQHandler()
{
USART_ClearITPendingBit(USART2,USART_IT_ORE);
USART_ClearITPendingBit(USART2,USART_IT_IDLE);
USART_ClearITPendingBit(USART2,USART_IT_TXE);
USART_ClearITPendingBit(USART2,USART_IT_EOB);
if( USART_GetITStatus(USART2,USART_IT_RXNE) != RESET )
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
while((USART1->ISR&0x40) == 0);
USART1->TDR = USART2->RDR;
}
}
|