找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2436|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

stm32f103 DMA串口驅(qū)動(dòng)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:46381 發(fā)表于 2020-6-18 09:10 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
/******************************************************************
文檔名:Timer
編譯器:MDK5.24.0
M C  U:STM32F103RB
使用庫:stm32f10x_xxx  V3.5.0
作        用:配置TIM2 TIM3 TIM4 TIM5的base。椋恪 
說        明:例程寫在STM32F103RB上 應(yīng)該適用于103全系列
******************************************************************/
#include "Config.h"
#include "stm32f10x.h"
TIME_TYDEDEFS  time_structs;        
/******************************************************************
函數(shù)名: void Timer_RCC_APB1_Config(uint32_t RCC_APB1Periph_Tim,uint32_t RCC_APB1Periph_Port)
參        數(shù):
                uint32_t RCC_APB1Periph_Tim    APB1上的Tim時(shí)鐘
                uint32_t RCC_APB1Periph_Port         相關(guān)端口
返回值:無
作        用:配置TIM3的RCC和GPIOA時(shí)鐘
說        明:
******************************************************************/
void Timer_RCC_APB1_Config(uint32_t RCC_APB1Periph_Tim,uint32_t RCC_APB1Periph_Port)
{
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_Tim, ENABLE);                                         //時(shí)鐘使能
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_Port, ENABLE);                                  //使能GPIOA時(shí)鐘
}
/******************************************************************
函數(shù)名: void Timer_RCC_APB2_Config(uint32_t RCC_APB2Periph_Tim,uint32_t RCC_APB1Periph_Port)
參        數(shù):
                uint32_t RCC_APB2Periph_Tim    APB1上的Tim時(shí)鐘
                uint32_t RCC_APB1Periph_Port         相關(guān)端口
返回值:無
作        用:配置掛在
說        明:
******************************************************************/
void Timer_RCC_APB2_Config(uint32_t RCC_APB2Periph_Tim,uint32_t RCC_APB1Periph_Port)
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_Tim, ENABLE);                                         //時(shí)鐘使能
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_Port, ENABLE);                                  //使能GPIOA時(shí)鐘
}
/******************************************************************
函數(shù)名:extern                void                Timer3_Init(u16 arr,u16 psc);
參        數(shù):
                        u16 arr   重裝值
                        u16 psc                預(yù)分頻值
返回值:無
作        用:初始化timer3
說        明:主頻72MHZ  周期為(0.1ms * arr)         
                                Timerx_Init(5000,7199); 為500ms
                                TIMclk        =        1/10000         s  相當(dāng)于一步 100us
******************************************************************/
void Timer_Base_Init(TIM_TypeDef* TIMx,u16 arr,u16 psc)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

        TIM_TimeBaseStructure.TIM_Period                                 =         arr;                                                                                         //設(shè)置在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值         計(jì)數(shù)到5000為500ms
        TIM_TimeBaseStructure.TIM_Prescaler                 =                (psc-1);                                                                 //設(shè)置用來作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值  10Khz的計(jì)數(shù)頻率  
        TIM_TimeBaseStructure.TIM_ClockDivision =         TIM_CKD_DIV1;                                                                         //設(shè)置時(shí)鐘分割:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode         =         TIM_CounterMode_Up;  //TIM向上計(jì)數(shù)模式
        TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);                                                 //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時(shí)間基數(shù)單位
}
/******************************************************************
函數(shù)名:void Timer3_Cap_IO_Config(void)
參        數(shù):無
返回值:無
作        用:
說        明:
******************************************************************/
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);
}

/******************************************************************
函數(shù)名:void Timer_OC_Init(TIM_TypeDef* TIMx,u16 channle)        
參        數(shù):
                                TIM_TypeDef* TIMx                        定時(shí)器選擇  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;                                                                                                                                         //選擇定時(shí)器模式:TIM脈沖寬度調(diào)制模式2
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;                                                                                         //比較輸出使能
        TIM_OCInitStructure.TIM_Pulse = 0;                                                                                                                                                                                                         //設(shè)置待裝入捕獲比較寄存器的脈沖值
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;                                                                                                         //輸出極性:TIM輸出比較極性高
        TIM_OC2Init(TIM3, &TIM_OCInitStructure);                                                                                                                                                                          //根據(jù)TIM_OCInitStruct中指定的參數(shù)初始化外設(shè)TIMx
        TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable);                                                                                                                                          //使能TIMx在CCR2上的預(yù)裝載寄存器
}
/******************************************************************
函數(shù)名:void Timer3_Cap_IC_Config(u16        channle,u16 tim_icfilter,u16 tim_icpolarity)
參        數(shù):
                                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);
}
/******************************************************************
函數(shù)名:void Timer3_NVIC_Config(void)
參        數(shù):無
返回值:無
作        用:配置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;  //先占優(yōu)先級(jí)0級(jí)
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                                  //從優(yōu)先級(jí)3級(jí)
        NVIC_InitStructure.NVIC_IRQChannelCmd = NewState;                                                 //IRQ通道被使能
        NVIC_Init(&NVIC_InitStructure);  
}
/******************************************************************
函數(shù)名: void Timer3_RCC_Config(void)
參        數(shù):無
返回值:無
作        用:配置TIM3的RCC和GPIOA時(shí)鐘
說        明:
******************************************************************/
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外設(shè)
                        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
                        memset(&time_structs.time2_struct,0,sizeof(time_structs.time2_struct));
}
/******************************************************************
函數(shù)名: void TIM2_IRQHandler(void)
參        數(shù):無
返回值:無
作        用:配置TIM3的RCC和GPIOA時(shí)鐘
說        明:
******************************************************************/
void TIM2_IRQHandler(void)                                                                                                                                           //TIM3中斷
{

        if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)                                        //檢查指定的TIM中斷發(fā)生與否: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)                                                // 識(shí)別前進(jìn)
                {                                                         
                                TIM_ClearITPendingBit(TIM2, TIM_IT_CC2 );                                          //
                                time_structs.time2_struct.timer_cc2_count                ++;

                }
        if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET)                                        //檢查指定的TIM中斷發(fā)生與否:TIM 中斷源
                {
                                TIM_ClearITPendingBit(TIM2, TIM_IT_CC3 );                                          //清除TIMx的中斷待處理位:TIM 中斷源
                                time_structs.time2_struct.timer_cc3_count                ++;                                
                }
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表