在线看毛片网站电影-亚洲国产欧美日韩精品一区二区三区,国产欧美乱夫不卡无乱码,国产精品欧美久久久天天影视,精品一区二区三区视频在线观看,亚洲国产精品人成乱码天天看,日韩久久久一区,91精品国产91免费

<menu id="6qfwx"><li id="6qfwx"></li></menu>
    1. <menu id="6qfwx"><dl id="6qfwx"></dl></menu>

      <label id="6qfwx"><ol id="6qfwx"></ol></label><menu id="6qfwx"></menu><object id="6qfwx"><strike id="6qfwx"><noscript id="6qfwx"></noscript></strike></object>
        1. <center id="6qfwx"><dl id="6qfwx"></dl></center>

            新聞中心

            stm32f407的TIM1的中斷和PWM

            作者: 時(shí)間:2016-11-10 來(lái)源:網(wǎng)絡(luò) 收藏
            調(diào)試了多半天TIM1中斷PWM,終于算是調(diào)通了。遇到了不少問(wèn)題,也深入了解了TIM1的配置過(guò)程。代碼如下:

            /************************************************************
            Copyright (C), 2012-2022, yin.
            FileName: main.c
            Author: ycw Version : 1.0 Date: 2012.04.25
            Description: TIM1 PWM NVIC
            Version: V1.0
            Function List: TIM1 PWM NVIC
            History:

            本文引用地址:http://www.biyoush.com/article/201611/317474.htm

            #include

            static __IO uint32_t TimingDelay;//__IO為volatile的宏定義
            int8_t LED_Flag = 1;//LED燈翻轉(zhuǎn)標(biāo)志位

            void GPIO_Config(void);
            void TIM_Config(void);
            void NVIC_Config(void);
            void Delay(__IO uint32_t nTime);

            main ()
            {
            /*在主函數(shù)main之前通過(guò)調(diào)用啟動(dòng)代碼運(yùn)行了SystemInit函數(shù),而
            這個(gè)函數(shù)位于system_stm32f4xx.c”。程序運(yùn)行起始于啟動(dòng)文件的第
            175行(LDR R0, =SystemInit)。sys時(shí)鐘為HSE頻率/PLL_M*PLL_N/PLL_P,
            定義HSE為25M,則sys時(shí)鐘頻率為168M */

            GPIO_Config();
            TIM_Config();
            NVIC_Config();
            /*SystemCoreClock / 1000時(shí)基為1ms*/
            if (SysTick_Config(SystemCoreClock / 1000))
            {
            /* Capture error */
            while (1);
            }
            while (1)
            {
            /*產(chǎn)生一個(gè)軟件中斷
            TIM_GenerateEvent(TIM1,TIM1_UP_TIM10_IRQn);
            Delay(1000);
            */
            if (LED_Flag != 1)
            {
            GPIO_SetBits(GPIOG, GPIO_Pin_6); //setbits使能IO,當(dāng)前下指輸出(此時(shí)為滅)
            }
            else
            {
            GPIO_ResetBits(GPIOG, GPIO_Pin_6); //Resetbits屏蔽IO,當(dāng)前下指不輸出(此時(shí)為亮)
            }
            }
            }

            /*************************************************
            Function: void GPIO_Config(void)
            Description: GPIO配置函數(shù)
            Input: 無(wú)
            Output:無(wú)
            Return:無(wú)
            *************************************************/
            void GPIO_Config(void)
            {
            /*定義了一個(gè)GPIO_InitStructure的結(jié)構(gòu)體,方便一下使用 */
            GPIO_InitTypeDef GPIO_InitStructure;
            /* 使能GPIOG時(shí)鐘(時(shí)鐘結(jié)構(gòu)參見(jiàn)“stm32圖解.pdf”)*/
            RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
            /*僅設(shè)置結(jié)構(gòu)體中的部分成員:這種情況下,用戶應(yīng)當(dāng)首先調(diào)用函數(shù)PPP_SturcInit(..)
            來(lái)初始化變量PPP_InitStructure,然后再修改其中需要修改的成員。這樣可以保證其他
            成員的值(多為缺省值)被正確填入。
            */
            GPIO_StructInit(&GPIO_InitStructure);
            /* 初始化GPIOG的Pin_6為推挽輸出*/
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //指定第六引腳
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //模式為輸出
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //頻率為快速
            GPIO_Init(GPIOG, &GPIO_InitStructure); //調(diào)用IO初始化函數(shù)

            /*配置GPIOA_Pin_8,作為TIM1_Channel2 PWM輸出*/
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_10 | GPIO_Pin_11; //指定復(fù)用引腳
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //模式必須為復(fù)用!
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //頻率為快速
            GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉與否對(duì)PWM產(chǎn)生無(wú)影響
            GPIO_Init(GPIOA, &GPIO_InitStructure);

            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //GPIOB_Pin13作為TIM1_CH1N輸出
            GPIO_Init(GPIOB, &GPIO_InitStructure);

            GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM1); //復(fù)用GPIOA_Pin7為TIM1_Ch1N,效果不理想高電平不到0.5V
            GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1); //復(fù)用GPIOA_Pin8為TIM1_Ch1,
            GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);//復(fù)用GPIOA_Pin10為TIM1_Ch3,
            GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_TIM1);//復(fù)用GPIOA_Pin11為TIM1_Ch4,注意沒(méi)有CH4N
            GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_TIM1);//復(fù)用GPIOB_Pin13為TIM1_Ch1N,效果很好,可以和GPIOA_Pin7同時(shí)復(fù)用
            }

            /*************************************************
            Function: void TIM_Config(void)
            Description: 定時(shí)器配置函數(shù)
            Input: 無(wú)
            Output: 無(wú)
            *************************************************/
            void TIM_Config(void)
            {
            TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
            TIM_OCInitTypeDef TIM_OCInitStructure;
            RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
            TIM_DeInit(TIM1);//初始化TIM1寄存器

            /*分頻和周期計(jì)算公式:
            Prescaler = (TIMxCLK / TIMx counter clock) - 1;
            Period = (TIMx counter clock / TIM3 output clock) - 1
            TIMx counter clock為你所需要的TXM的定時(shí)器時(shí)鐘
            */
            TIM_TimeBaseStructure.TIM_Period = 10000-1; //查數(shù)據(jù)手冊(cè)可知,TIM1與TIM8為16位自動(dòng)裝載
            /*在system_stm32f4xx.c中設(shè)置的APB2 Prescaler = 2 ,可知
            *APB1時(shí)鐘為168M/2*2,因?yàn)槿绻鸄PB1分頻不為1,則定時(shí)時(shí)鐘x2
            */
            TIM_TimeBaseStructure.TIM_Prescaler = 16800-1; //分頻為10K
            TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
            TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上計(jì)數(shù)
            TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
            TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

            /*配置輸出比較,產(chǎn)生占空比為20%的PWM方波*/
            TIM_OCStructInit(&TIM_OCInitStructure); //填入缺省值,其中后幾項(xiàng)只對(duì)TIM1和TIM8有效
            TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1為正常占空比模式,PWM2為反極性模式
            TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //輸出允許
            TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //互補(bǔ)輸出允許
            TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //High為占空比高極性,此時(shí)占空比為20%;Low則為反極性,占空比為80%
            TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//互補(bǔ)輸出,與以上相反
            TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

            TIM_OCInitStructure.TIM_Pulse = 2000; //輸入通道1 CCR1(占空比數(shù)值)
            TIM_OC1Init(TIM1, &TIM_OCInitStructure); //Ch1初始化
            TIM_OCInitStructure.TIM_Pulse = 4000;
            TIM_OC3Init(TIM1, &TIM_OCInitStructure);
            TIM_OCInitStructure.TIM_Pulse = 7000;
            TIM_OC4Init(TIM1, &TIM_OCInitStructure);
            TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);//CCR自動(dòng)裝載默認(rèn)也是打開(kāi)的
            TIM_ARRPreloadConfig(TIM1, ENABLE); //ARR自動(dòng)裝載默認(rèn)是打開(kāi)的,可以不設(shè)置

            TIM_ClearFlag(TIM1, TIM_FLAG_Update);
            TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
            TIM_Cmd(TIM1, ENABLE); //使能TIM1定時(shí)器
            TIM_CtrlPWMOutputs(TIM1, ENABLE);//使能TIM1的PWM輸出,TIM1與TIM8有效
            }

            /*************************************************
            Function: void NVIC_Config(void)
            Description: 嵌套中斷斷配置函數(shù)
            Input: 無(wú)
            Output: 無(wú)
            *************************************************/
            void NVIC_Config(void)
            {
            /*配置定時(shí)器TIM1中斷嵌套*/
            NVIC_InitTypeDef NVIC_InitStructure;
            NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //中斷優(yōu)先級(jí)分組為1
            NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn; //嵌套中斷通道為TIM1
            NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //搶占優(yōu)先級(jí)為1
            NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)優(yōu)先級(jí)為0
            NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中斷
            NVIC_Init(&NVIC_InitStructure);
            }

            /*************************************************
            Function: void Delay(__IO uint32_t nTime)
            Description: 精確延時(shí)函數(shù),時(shí)基根據(jù)前面設(shè)定,當(dāng)前
            為1ms
            Input: 需要延時(shí)的時(shí)間,單位ms
            Output:無(wú)
            *************************************************/
            void Delay(__IO uint32_t nTime)
            {
            TimingDelay = nTime;
            while (TimingDelay != 0);
            }

            /*************************************************
            Function: void TimingDelay_Decrement(void)
            Description: SysTick中斷服務(wù)函數(shù),加在_it.h中的
            void SysTick_Handler(void)函數(shù)內(nèi)
            Input: 無(wú)
            Output:無(wú)
            *************************************************/
            void TimingDelay_Decrement(void)
            {
            if (TimingDelay != 0x00)
            {
            TimingDelay--;
            }
            }



            關(guān)鍵詞: stm32f407TIM1中斷PW

            評(píng)論


            技術(shù)專區(qū)

            關(guān)閉