开箱完成之后,开始体验第一个外设串口通信,因为平时做的项目都是基于freertos,那么也来体验下这款单片机跑freertos。从提供的资料里面我们可以找到基于freertos的工程:
打开项目简单看下工程内容:
下载进去看看现象:
那么添加我们的串口代码部分:
void uart4_init(u32 bound4){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3|RCC_APB
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2P
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
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_
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_BaudRate = bound4;
USART_Init(UART4, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
USART_Cmd(UART4, ENABLE);
}
__attribute__((interrupt("WCH-Interrupt-fast")))
void UART4_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET)
{
Res =USART_ReceiveData(UART4);
USART_SendData(UART4, Res);
}
}
沁恒的这个串口初始化风格还是和st的很相似的。但是刚开始使用还是遇到了点问题,通过开发板提供的原理图并不能知道哪个是串口4 只能查找数据手册了
编译下再进去看看效果:
很顺利实现了基于freertos的串口通信
|