找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1812|回复: 0
打印 上一主题 下一主题
收起左侧

stm32f103 DMA串口驱动

[复制链接]
跳转到指定楼层
楼主
ID:46381 发表于 2020-6-18 09:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/******************************************************************
文档名:Timer
编译器:MDK5.24.0
M C  U:STM32F103RB
使用库:stm32f10x_xxx  V3.5.0
作        用:配置TIM2 TIM3 TIM4 TIM5的base ic  
说        明:例程写在STM32F103RB上 应该适用于103全系列
******************************************************************/
#include "Config.h"
#include "stm32f10x.h"
TIME_TYDEDEFS  time_structs;        
/******************************************************************
函数名: void Timer_RCC_APB1_Config(uint32_t RCC_APB1Periph_Tim,uint32_t RCC_APB1Periph_Port)
参        数:
                uint32_t RCC_APB1Periph_Tim    APB1上的Tim时钟
                uint32_t RCC_APB1Periph_Port         相关端口
返回值:无
作        用:配置TIM3的RCC和GPIOA时钟
说        明:
******************************************************************/
void Timer_RCC_APB1_Config(uint32_t RCC_APB1Periph_Tim,uint32_t RCC_APB1Periph_Port)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_Tim, ENABLE);                                         //时钟使能
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_Port, ENABLE);                                  //使能GPIOA时钟
}
/******************************************************************
函数名: void Timer_RCC_APB2_Config(uint32_t RCC_APB2Periph_Tim,uint32_t RCC_APB1Periph_Port)
参        数:
                uint32_t RCC_APB2Periph_Tim    APB1上的Tim时钟
                uint32_t RCC_APB1Periph_Port         相关端口
返回值:无
作        用:配置挂在
说        明:
******************************************************************/
void Timer_RCC_APB2_Config(uint32_t RCC_APB2Periph_Tim,uint32_t RCC_APB1Periph_Port)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_Tim, ENABLE);                                         //时钟使能
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_Port, ENABLE);                                  //使能GPIOA时钟
}
/******************************************************************
函数名:extern                void                Timer3_Init(u16 arr,u16 psc);
参        数:
                        u16 arr   重装值
                        u16 psc                预分频值
返回值:无
作        用:初始化timer3
说        明:主频72MHZ  周期为(0.1ms * arr)         
                                Timerx_Init(5000,7199); 为500ms
                                TIMclk        =        1/10000         s  相当于一步 100us
******************************************************************/
void Timer_Base_Init(TIM_TypeDef* TIMx,u16 arr,u16 psc)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

        TIM_TimeBaseStructure.TIM_Period                                 =         arr;                                                                                         //设置在下一个更新事件装入活动的自动重装载寄存器周期的值         计数到5000为500ms
        TIM_TimeBaseStructure.TIM_Prescaler                 =                (psc-1);                                                                 //设置用来作为TIMx时钟频率除数的预分频值  10Khz的计数频率  
        TIM_TimeBaseStructure.TIM_ClockDivision =         TIM_CKD_DIV1;                                                                         //设置时钟分割:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode         =         TIM_CounterMode_Up;  //TIM向上计数模式
        TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);                                                 //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
}
/******************************************************************
函数名:void Timer3_Cap_IO_Config(void)
参        数:无
返回值:无
作        用:
说        明:
******************************************************************/
void Timer_Cap_IO_Config(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin,GPIOMode_TypeDef GPIO_Mode)
{
        GPIO_InitTypeDef GPIO_InitStructure;
               
        GPIO_InitStructure.GPIO_Pin                 =          GPIO_Pin_6;                                                //需要修改
        GPIO_InitStructure.GPIO_Mode                 =         GPIO_Mode_IN_FLOATING;                        
        GPIO_InitStructure.GPIO_Speed         =         GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        GPIO_Init(GPIOx, &GPIO_InitStructure);
}

/******************************************************************
函数名:void Timer_OC_Init(TIM_TypeDef* TIMx,u16 channle)        
参        数:
                                TIM_TypeDef* TIMx                        定时器选择  x=0到17
                                u16 channle                                                通道选择
返回值:无
作        用:使能捕获比较输出模块
说        明:
******************************************************************/
void Timer_OC_Init(TIM_TypeDef* TIMx,u16 channle)        
{
        TIM_OCInitTypeDef  TIM_OCInitStructure;
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;                                                                                                                                         //选择定时器模式:TIM脉冲宽度调制模式2
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;                                                                                         //比较输出使能
        TIM_OCInitStructure.TIM_Pulse = 0;                                                                                                                                                                                                         //设置待装入捕获比较寄存器的脉冲值
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;                                                                                                         //输出极性:TIM输出比较极性高
        TIM_OC2Init(TIM3, &TIM_OCInitStructure);                                                                                                                                                                          //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
        TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable);                                                                                                                                          //使能TIMx在CCR2上的预装载寄存器
}
/******************************************************************
函数名:void Timer3_Cap_IC_Config(u16        channle,u16 tim_icfilter,u16 tim_icpolarity)
参        数:
                                u16        channle                                        通道值
                                u16 tim_icfilter                滤波值0x00 -0x0f可选
                                u16 tim_icpolarity        TIM_ICPolarity_Rising        上升沿
返回值:无                  TIM_ICPolarity_Falling                          下降沿
作        用:                    TIM_ICPolarity_BothEdge                         双沿
说        明:配置输入捕获的方式
******************************************************************/
void Timer_Cap_IC_Config(TIM_TypeDef* TIMx,u16        channle,u16 tim_icfilter,u16 tim_icpolarity)
{
                TIM_ICInitTypeDef  TIM_ICInitStructure;
                TIM_ICInitStructure.TIM_Channel                         = channle; //CC1S=01         选择输入端 IC1映射到TI1上
          TIM_ICInitStructure.TIM_ICPolarity         = tim_icpolarity;        //上升沿捕获
          TIM_ICInitStructure.TIM_ICSelection         = TIM_ICSelection_DirectTI; //映射到TI1上
          TIM_ICInitStructure.TIM_ICPrescaler         = TIM_ICPSC_DIV1;         //配置输入分频,不分频
          TIM_ICInitStructure.TIM_ICFilter                        = 0x0F;//IC1F=0000 配置输入滤波器 不滤波
          TIM_ICInit(TIMx, &TIM_ICInitStructure);
}
/******************************************************************
函数名:void Timer3_NVIC_Config(void)
参        数:无
返回值:无
作        用:配置TIM3  NVIC
说        明:
******************************************************************/
void Timer_NVIC_Config(TIM_TypeDef* TIMx,FunctionalState NewState)
{
        u8        IRQ        ;
        NVIC_InitTypeDef NVIC_InitStructure;
        if(TIMx        ==        TIM2)
        {
                IRQ        =        TIM2_IRQn;
        }
        else if(TIMx        ==        TIM3)
        {
                IRQ        =        TIM3_IRQn;
        }
else if(TIMx        ==        TIM4)
        {
                IRQ        =        TIM4_IRQn;
        }
        NVIC_InitStructure.NVIC_IRQChannel = IRQ;                                                                          //TIM中断
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                                  //从优先级3级
        NVIC_InitStructure.NVIC_IRQChannelCmd = NewState;                                                 //IRQ通道被使能
        NVIC_Init(&NVIC_InitStructure);  
}
/******************************************************************
函数名: void Timer3_RCC_Config(void)
参        数:无
返回值:无
作        用:配置TIM3的RCC和GPIOA时钟
说        明:
******************************************************************/
void Timer2_Config(void)
{
                        Timer_RCC_APB1_Config(RCC_APB1Periph_TIM2,RCC_APB2Periph_GPIOA)        ;
                        Timer_Base_Init(TIM2,TIM_CYCLE_10US,TIM_UNIT_1US);                                                                                                                                        //20ms  
//                        Timer2_Cap_IO_Config(GPIOA,GPIO_Pin_1);
//                        Timer2_Cap_IC_Config(TIM_Channel_2,0x00,TIM_ICPolarity_Falling);
//                        Timer2_Cap_IO_Config(GPIOA,GPIO_Pin_2);
//                        Timer2_Cap_IC_Config(TIM_Channel_3,0x00,TIM_ICPolarity_Falling);
                        Timer_NVIC_Config(TIM2,ENABLE);
                        TIM_ITConfig(TIM2,TIM_IT_Update,         ENABLE );                                                                 //只使能UPdate 中断   
                        TIM2_START();                                                                                                                                                                                          //使能TIMx外设
                        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
                        memset(&time_structs.time2_struct,0,sizeof(time_structs.time2_struct));
}
/******************************************************************
函数名: void TIM2_IRQHandler(void)
参        数:无
返回值:无
作        用:配置TIM3的RCC和GPIOA时钟
说        明:
******************************************************************/
void TIM2_IRQHandler(void)                                                                                                                                           //TIM3中断
{

        if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)                                        //检查指定的TIM中断发生与否:TIM 中断源
                {
                        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);                                          //清除TIMx的中断待处理位:TIM 中断源
                        time_structs.time2_struct.timer_base_count++;
                        EPS8266_01_Time_Delay(TIM2,time_structs.time2_struct.timer_base_count);
                }
        if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)                                                // 识别前进
                {                                                         
                                TIM_ClearITPendingBit(TIM2, TIM_IT_CC2 );                                          //
                                time_structs.time2_struct.timer_cc2_count                ++;

                }
        if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)                                        //检查指定的TIM中断发生与否:TIM 中断源
                {
                                TIM_ClearITPendingBit(TIM2, TIM_IT_CC3 );                                          //清除TIMx的中断待处理位:TIM 中断源
                                time_structs.time2_struct.timer_cc3_count                ++;                                
                }
}


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表