初级会员

- 积分
- 69
- 金钱
- 69
- 注册时间
- 2014-5-7
- 在线时间
- 0 小时
|
5金钱
void uart4_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB, ENABLE);
USART_DeInit(UART4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//UART4_TX PC.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PC.10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//UART4_RX   C.11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4,ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4,DISABLE);
//Usart4 NVIC //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 我的中断分组为1组
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//UART4
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(UART4, &USART_InitStructure);
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
USART_Cmd(UART4, ENABLE);
RS485_UART4_TX_EN = 0; //   B12 = 0; 使能接收
}
void UART4_IRQHandler(void)
{
OSIntEnter();
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
{
if (UART_DATA_LEN == UART4_CUR_RX_POS)
UART4_CUR_RX_POS = 0;
UART4_RX_BUF[UART4_CUR_RX_POS++] = USART_ReceiveData(UART4);
}
OSIntExit();
}
|
|