下面USART初始化代码:
- /* 初始化指定的UART */
- void Console_Uart_Init(int num) {
- GPIO_InitType GPIO_InitStructure;
- USART_InitType USART_InitStructure;
- NVIC_InitType NVIC_InitStructure;
-
- if(num == 1) {
- //打开串口1所在的IO口时钟
- RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA|RCC_APB2PERIPH_AFIO , ENABLE);
- //打开串口1时钟
- RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_USART1, ENABLE);
-
- //USART1端口配置
- GPIO_PinsRemapConfig(GPIO_Remap_USART1, DISABLE);//使用原始位置
- //RX
- GPIO_InitStructure.GPIO_Pins = GPIO_Pins_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_PU;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- //TX
- GPIO_InitStructure.GPIO_Pins = GPIO_Pins_9;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- } else if(num == 3) {
- //打开串口所在的IO口时钟
- RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOD|RCC_APB2PERIPH_AFIO , ENABLE);
- //打开串口时钟
- RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_USART3, ENABLE);
-
- //USART3端口配置
- GPIO_PinsRemapConfig(GPIO_FullRemap_USART3, ENABLE);//重映射,问题? PD10 11 12能用普通作IO吗?实际测试可以!
- //RX
- GPIO_InitStructure.GPIO_Pins = GPIO_Pins_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_PU;//注:此处要上拉,不然很可能出问题。
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
- //TX
- GPIO_InitStructure.GPIO_Pins = GPIO_Pins_8;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- } else if(num == 6) {
- //打开串口6所在的IO口时钟
- RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOC|RCC_APB2PERIPH_AFIO , ENABLE);
- //打开串口时钟
- RCC_APB1PeriphClockCmd(RCC_APB2PERIPH_USART6, ENABLE);
-
- // //USART6端口配置
- //??? GPIO_PinsRemapConfig(GPIO_Remap_USART6, ENABLE);//重映射,问题?
- //RX
- GPIO_InitStructure.GPIO_Pins = GPIO_Pins_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_PU; //
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- //TX
- GPIO_InitStructure.GPIO_Pins = GPIO_Pins_6;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
-
- // USART初始化设置
- // 波特率设置
- // USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_BaudRate = 57600;
- // 字长为8位数据格式
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- // 一个停止位
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//break;
- // USART_InitStructure.USART_StopBits = USART_StopBits_0_5;break;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- // USART_InitStructure.USART_Parity = USART_Parity_Even;
-
- //todo
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
-
- if(num == 1) {
- USART_Init(USART1, &USART_InitStructure); //初始化串口1
- USART_Cmd(USART1, ENABLE); //使能串口1
- //打开收非空中断
- USART_INTConfig(USART1, USART_INT_RDNE, ENABLE);
- // USART_INTConfig(USART1, USART_INT_IDLEF, ENABLE);
-
- //Usart1 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=10;//抢占优先级10
- NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
- } else if(num == 3) {
- USART_Init(USART3, &USART_InitStructure); //初始化串口
- USART_Cmd(USART3, ENABLE); //使能串口
- //打开收非空中断
- // USART_INTConfig(USART3, USART_INT_RDNE, ENABLE);
- //USART_INTConfig(USART1, USART_INT_IDLEF, ENABLE);
-
- //Usart1 NVIC 配置
- // NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//串口1中断通道
- // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=10;//抢占优先级10
- // NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级
- // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- // NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
- } else if(num == 6) {
- USART_Init(USART6, &USART_InitStructure); //初始化串口
- USART_Cmd(USART6, ENABLE); //使能串口
- //打开收非空中断
- USART_INTConfig(USART6, USART_INT_RDNE, ENABLE);
- // USART_INTConfig(USART6, USART_INT_IDLEF, ENABLE);
- //Usart1 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//串口6中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=10;//抢占优先级10
- NVIC_InitStructure.NVIC_IRQChannelSubPriority =1; //子优先级
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
- }
- }
移植了freeRTOS
代码中关于USART6重映射IO的部分出错, 硬件是单独使用PC6\PC7的,没有占用
|