一、前期准备
单片机:STM32F103ZET6
开发环境:MDK5.14
库函数:标准库V3.5
FreeRTOS:V9.0.0(网上下载,官网上目前是V10.0.1)
二、实验效果
总共6个任务,串口1、串口2、4个RGB小灯以不同频率闪烁。5050 RGB LED驱动请参考RGB驱动,串口1、2同时打印测试信息。
三、移植准备
下载好的FreeRTOS源码中,只需要把Source文件夹加入工程。
1.FreeRTOS内核文件,如下图所示,里面所有C文件加入到工程里面
2.将portable中无关文件夹删除掉,只留下如图所示的三个文件夹。Keil文件夹不用管,MemMang里面是heap1.c~heap5.c文件,这个是内存管源码,这里移植将heap4.c加入工程。RVDS中ARM_M3的C文件加入工程
3.Keil工程
4.修改stm32启动文件
四、测试代码
完整工程请加QQ:1002521871,验证:呵呵
uart.h
#ifndef __UART_H__
#define __UART_H__
#include "stm32f10x.h"
#include #include #include #include "message.h" /* UART IOpin definirion */ #define TX1 GPIO_Pin_9 #define RX1 GPIO_Pin_10 #define TX2 GPIO_Pin_2 #define RX2 GPIO_Pin_3 #define TX3 GPIO_Pin_10 #define RX3 GPIO_Pin_11 #define RecDataLen 128 extern char RecData[RecDataLen]; extern char *pIndex; extern char *pWrite; extern char *pRead; extern int USART_PRINTF_FLAG; extern void USARTConfiguration(int Baudrate); extern void USART_Send(USART_TypeDef* USARTx, char c); extern char USART_Received(USART_TypeDef* USARTx); #endif uart.c,多串口通用printf程序请参考多串口共用printf讲解 #include "uart.h" char RecData[RecDataLen] = {0}; char *pIndex = RecData; char *pWrite = RecData; char *pRead = RecData; int USART_PRINTF_FLAG = 0; void USARTConfiguration(int Baudrate) { USART_InitTypeDef MyUSART; GPIO_InitTypeDef GPIO; NVIC_InitTypeDef MyNVIC; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO.GPIO_Pin = TX1; GPIO.GPIO_Mode = GPIO_Mode_AF_PP; GPIO.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO); /* Configure USART1 Rx (PA.10) as input floating */ GPIO.GPIO_Pin = RX1; GPIO.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); MyNVIC.NVIC_IRQChannel = USART1_IRQn; MyNVIC.NVIC_IRQChannelSubPriority = 0; MyNVIC.NVIC_IRQChannelPreemptionPriority = 0; MyNVIC.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&MyNVIC); MyUSART.USART_BaudRate = Baudrate; MyUSART.USART_WordLength = USART_WordLength_8b; MyUSART.USART_StopBits = USART_StopBits_1; MyUSART.USART_Parity = USART_Parity_No; MyUSART.USART_HardwareFlowControl = USART_HardwareFlowControl_None; MyUSART.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART1, &MyUSART); USART_Cmd(USART1, ENABLE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); /* Configure USART2 Tx (PA.02) as alternate function push-pull */ GPIO.GPIO_Pin = TX2; GPIO.GPIO_Mode = GPIO_Mode_AF_PP; GPIO.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO); /* Configure USART2 Rx (PA.3) as input floating */ GPIO.GPIO_Pin = RX2; GPIO.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); MyNVIC.NVIC_IRQChannel = USART2_IRQn; MyNVIC.NVIC_IRQChannelSubPriority = 0; MyNVIC.NVIC_IRQChannelPreemptionPriority = 1; MyNVIC.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&MyNVIC); MyUSART.USART_BaudRate = Baudrate; MyUSART.USART_WordLength = USART_WordLength_8b; MyUSART.USART_StopBits = USART_StopBits_1; MyUSART.USART_Parity = USART_Parity_No; MyUSART.USART_HardwareFlowControl = USART_HardwareFlowControl_None; MyUSART.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART2, &MyUSART); USART_Cmd(USART2, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); /* Configure USART3 Tx (PB.10) as alternate function push-pull */ GPIO.GPIO_Pin = TX3; GPIO.GPIO_Mode = GPIO_Mode_AF_PP; GPIO.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO); /* Configure USART3 Rx (PB.11) as input floating */ GPIO.GPIO_Pin = RX3; GPIO.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); MyNVIC.NVIC_IRQChannel = USART3_IRQn; MyNVIC.NVIC_IRQChannelSubPriority = 0; MyNVIC.NVIC_IRQChannelPreemptionPriority = 2; MyNVIC.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&MyNVIC); MyUSART.USART_BaudRate = Baudrate; MyUSART.USART_WordLength = USART_WordLength_8b; MyUSART.USART_StopBits = USART_StopBits_1; MyUSART.USART_Parity = USART_Parity_No; MyUSART.USART_HardwareFlowControl = USART_HardwareFlowControl_None; MyUSART.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART3, &MyUSART); USART_Cmd(USART3, ENABLE); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); USART_ITConfig(USART3, USART_IT_IDLE, ENABLE); } void USART_Send(USART_TypeDef* USARTx, char c) { USART_SendData(USARTx, c); /* loop until the end of transmission */ while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); } char USART_Received(USART_TypeDef* USARTx) { char dat; //while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); dat = (char)(USART_ReceiveData(USARTx)&0xff); return dat; } void USART1_IRQHandler(void) { uint8_t clear = clear; USART_PRINTF_FLAG = 1; if(USART_GetFlagStatus(USART1,USART_IT_RXNE) != RESET ) { if ( (*(pWrite-1) != 0x0A) && ((pWrite - pIndex) < RecDataLen) ) { *pWrite = USART_Received(USART1); pWrite ++; } else { USART_Received(USART1); } } else if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) { if ( *(pWrite - 1) == 0x0A && *(pWrite - 2) == 0x0D) { cmdDealFlag = 1; } else { printf("%s", RecFlowMess); } pWrite = pIndex; clear = USART1->SR; clear = USART1->DR; } } void USART2_IRQHandler(void) { uint8_t clear = clear; USART_PRINTF_FLAG = 2; if(USART_GetFlagStatus(USART2,USART_IT_RXNE) != RESET ) { if ( (*(pWrite-1) != 0x0A) && ((pWrite - pIndex) < RecDataLen) ) { *pWrite = USART_Received(USART2); pWrite ++; } else { USART_Received(USART2); } } else if (USART_GetITStatus(USART2, USART_IT_IDLE) != RESET) { if ( *(pWrite - 1) == 0x0A && *(pWrite - 2) == 0x0D) { cmdDealFlag = 1; } else { printf("%s", RecFlowMess); } pWrite = pIndex; clear = USART2->SR; clear = USART2->DR; } } void USART3_IRQHandler(void) { uint8_t clear = clear; USART_PRINTF_FLAG = 3; if(USART_GetFlagStatus(USART3,USART_IT_RXNE) != RESET ) { if ( (*(pWrite-1) != 0x0A) && ((pWrite - pIndex) < RecDataLen) ) { *pWrite = USART_Received(USART3); pWrite ++; } else { USART_Received(USART3); } } else if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET)
上一篇:STM32F103ZE FreeRTOS任务创建与删除
下一篇:LCD12864驱动(STM32)5V和3.3V
推荐阅读最新更新时间:2024-11-11 01:43
设计资源 培训 开发板 精华推荐
- 用于 BLDC/PMSM 电机控制驱动器的 MC56F8006DBUM、MC56F8006 控制器子板
- 使用 ROHM Semiconductor 的 BD33GA5WEFJ 的参考设计
- MCIMX28LCD、i.MX28 套件评估 (EVK) 板以及连接器和开关的位置
- TDA2005 20W 汽车收音机桥式放大器不带升压器的典型桥式放大器
- LT1301,闪存 VPP 发生器参考设计
- ADM8828 具有关断功能的开关电容器电压逆变器的典型应用
- LT1172CN8、12V/1.25A 负至正降压-升压转换器的典型应用
- MCP43XXEV,MCP4231 数字电位器器件评估板
- 【毕设】一转四TTL2.0
- NHS3152THADADK: NHS3152治疗依从性入门套件