一、前期准备
单片机: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-17 18:57
设计资源 培训 开发板 精华推荐
- 适用于DFN8封装的双运放系列裸PCB评估板
- LT1560-1 微型 1MHz 低通滤波器不使用电感器
- 5.25.1.
- EVAL-AD7781EBZ,用于 AD7781、20 位、16.7SPS ADC 仪表的评估板
- ADR4550 双极性输出电压基准的典型应用
- AD8066AR-EBZ,AD8066ARZ 双通道高速运算放大器评估板,采用 8 引脚 SOIC 封装
- 使用 ROHM Semiconductor 的 BD45361 的参考设计
- 适用于气体流量测量的超声波传感子系统参考设计
- ADR434B 4.096 Vout 超低噪声 XFET 电压基准的典型应用,具有灌电流和拉电流能力
- 10W、5V、12V 交流转直流多输出电源
- KOL实战分享|无刷直流电机(BLDC)的驱动及控制方法
- EEworld新春感恩回馈之ST新出道“高富帅”STM32F746G-DISCO 199元包邮
- 助力初创公司~21种Maxim评估板来了!免费领取进行中!
- 技术直播:TI 60G毫米波传感器概述和应用介绍,种草封装了天线的智能传感器IWR6843
- 考眼力:高速数字设计的秘籍 藏在哪里? 走近Keysight PATHWAVE,提升工作效率
- 有奖问答:基于A2B的音频信号解决方案
- 直播已结束【大唐恩智浦具有阻抗检测功能的新能源锂电芯电池管理方案】
- 直播已结束【使用 TI 低成本 MSPM0 MCU 快速开发 – 生态详解系列之二】(9:45开始入场)