娇小w搡bbbb搡bbb,《第一次の人妻》,中国成熟妇女毛茸茸,边啃奶头边躁狠狠躁视频免费观看

歷史上的今天

今天是:2024年09月20日(星期五)

正在發生

2018年09月20日 | STM32一個Timer輸出4路不同頻率、可調占空比的PWM

發布者:Huanle 來源: eefocus關鍵字:STM32  Timer  不同頻率  可調占空比  PWM 手機看文章 掃描二維碼
隨時隨地手機看文章

main.c


/********************************************* 

    標題:操作USART的練習 

    軟件平臺:MDK-ARM Standard Version4.70 

    硬件平臺:stm32f4-discovery   

    主頻:168M 

    Periph_Driver_version: V1.0.0 

     

    描述:用一個定時器(TIM3),實現四路不同頻率、占空比可調的PWM 

          代碼參考自STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\TIM_TimeBase 

 

    author:大舟 

    data:2013-04-13 

**********************************************/  

  

#include "stm32f4_discovery.h"  

  

  

  

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  

TIM_OCInitTypeDef  TIM_OCInitStructure;  

__IO uint16_t CCR1_Val = 5000;//54618  

__IO uint16_t CCR2_Val = 27309;  

__IO uint16_t CCR3_Val = 13654;  

__IO uint16_t CCR4_Val = 6826;  

uint16_t PrescalerValue = 0;  

  

void TIM_Config(void);  

  

  

int main(void)  

{  

  /*!< At this stage the microcontroller clock setting is already configured,  

       this is done through SystemInit() function which is called from startup 

       file (startup_stm32f4xx.s) before to branch to application main. 

       To reconfigure the default setting of SystemInit() function, refer to 

       system_stm32f4xx.c file 

     */  

  

  /* TIM Configuration */  

  TIM_Config();  

  

  /** ----------------------------------------------------------------------- 

    TIM3 Configuration: Output Compare Timing Mode: 

     

    In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1),  

    since APB1 prescaler is different from 1.    

      TIM3CLK = 2 * PCLK1   

      PCLK1 = HCLK / 4  

      => TIM3CLK = HCLK / 2 = SystemCoreClock /2 

           

    To get TIM3 counter clock at 500 KHz, the prescaler is computed as follows: 

       Prescaler = (TIM3CLK / TIM3 counter clock) - 1 

       Prescaler = ((SystemCoreClock /2) /50 MHz) - 1 

                                               

    CC1 update rate = TIM3 counter clock / CCR1_Val = 9.154 Hz  @note 上面已經將CCR1_Val改為了5000,具體頻率,見中斷的注釋 

    ==> Toggling frequency = 4.57 Hz 

     

    C2 update rate = TIM3 counter clock / CCR2_Val = 18.31 Hz 

    ==> Toggling frequency = 9.15 Hz 

     

    CC3 update rate = TIM3 counter clock / CCR3_Val = 36.62 Hz 

    ==> Toggling frequency = 18.31 Hz 

     

    CC4 update rate = TIM3 counter clock / CCR4_Val = 73.25 Hz 

    ==> Toggling frequency = 36.62 Hz 

 

    Note:  

     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. 

     Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() 

     function to update SystemCoreClock variable value. Otherwise, any configuration 

     based on this variable will be incorrect.     

  ----------------------------------------------------------------------- */    

  

  /* Compute the prescaler value */  

  PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 500000) - 1;//=168  

  

  /* Time base configuration */  

  TIM_TimeBaseStructure.TIM_Period = 65535;//65535  

  TIM_TimeBaseStructure.TIM_Prescaler = 0;  

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;  

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  

  

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  

  

  /* Prescaler configuration */  

  TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);//對84M進行168分頻,即為500KHz  

  

  /* Output Compare Timing Mode configuration: Channel1 */  

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;  

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  

  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;  

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  

  TIM_OC1Init(TIM3, &TIM_OCInitStructure);  

  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);  

  

  /* Output Compare Timing Mode configuration: Channel2 */  

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  

  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;  

  TIM_OC2Init(TIM3, &TIM_OCInitStructure);  

  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);  

  

  /* Output Compare Timing Mode configuration: Channel3 */  

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  

  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;  

  TIM_OC3Init(TIM3, &TIM_OCInitStructure);  

  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);  

  

  /* Output Compare Timing Mode configuration: Channel4 */  

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  

  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;  

  TIM_OC4Init(TIM3, &TIM_OCInitStructure);  

  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);  

     

  /* TIM Interrupts enable */    

  TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);  

      

  /* TIM3 enable counter */  

  TIM_Cmd(TIM3, ENABLE);  

  

  while (1);  

}  

  

  

  

/** 

  * @brief  Configure the TIM IRQ Handler. 

  * @param  None 

  * @retval None 

  */  

void TIM_Config(void)  

{  

  NVIC_InitTypeDef NVIC_InitStructure;  

  

  /* TIM3 clock enable */  

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);  

  

  /* Enable the TIM3 gloabal Interrupt */  

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  

  NVIC_Init(&NVIC_InitStructure);  

  

  /* Initialize Leds mounted on STM32F4-Discovery board */  

  STM_EVAL_LEDInit(LED4);  

  STM_EVAL_LEDInit(LED3);  

  STM_EVAL_LEDInit(LED5);  

  STM_EVAL_LEDInit(LED6);  

  

  /* Turn on LED4, LED3, LED5 and LED6 */  

  STM_EVAL_LEDOn(LED4);  

  STM_EVAL_LEDOn(LED3);  

  STM_EVAL_LEDOn(LED5);  

  STM_EVAL_LEDOn(LED6);  

}  

  

  

  

  

  

  

#ifdef  USE_FULL_ASSERT  

  

/** 

  * @brief  Reports the name of the source file and the source line number 

  *         where the assert_param error has occurred. 

  * @param  file: pointer to the source file name 

  * @param  line: assert_param error line source number 

  * @retval None 

  */  

void assert_failed(uint8_t* file, uint32_t line)  

{  

  /* User can add his own implementation to report the file name and line number, 

     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */  

  

  while (1)  

  {}  

}  

#endif  

  

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/  



stm32f4xx_it.c




[cpp] view plaincopy

#include "stm32f4xx_it.h"  

#include "stm32f4_discovery.h"  

  

uint16_t capture = 0;  

extern __IO uint16_t CCR1_Val;  

extern __IO uint16_t CCR2_Val;  

extern __IO uint16_t CCR3_Val;  

extern __IO uint16_t CCR4_Val;  

  

  

/** 

實際上當CCR1_Val每次都變化時,就可以用來改變占空比 

*/  

void TIM3_IRQHandler(void)  

{  

    /*7500為一個循環,周期為7500/500000=0.015,頻率為66.67Hz*/  

    if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)  

    {  

        TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);  

          

        if(CCR1_Val==5000)  

        {  

            STM_EVAL_LEDOn(LED4);   //PD12=0。ARR增加5000后,到達這條語句。  

            CCR1_Val=2500;          //所以低電平的百分比為5000/7500=2/3。即占空比為1/3  

        }  

        else  

        {  

            STM_EVAL_LEDOff(LED4);  //PD12=1  

            CCR1_Val=5000;  

        }  

        capture = TIM_GetCapture1(TIM3);  

        TIM_SetCompare1(TIM3, capture + CCR1_Val);//在原來的CCR1(即capture)基礎上加5000,則再過5000,又會觸發中斷  

        //另外,有個問題,進入中斷時,當ARR計數器快加到65535,而又不足5000時,不是會有數據多余,而產生波形的移動嗎?  

        //回答:不用擔心。例如進入中斷是,ARR=65000,65000+5000=70000>65535,那么高位會舍去,即為70000-65536=4464  

        //等于是來了個循環,兩次中斷間ARR的增量還是5000。所以為了波形的穩定,初始化時,必須要有TIM_Period = 65535  

    }  

    else if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)  

    {  

        TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);  

  

        /* LED3 toggling with frequency = 9.15 Hz */  

        STM_EVAL_LEDToggle(LED3);//PD13取反  

        capture = TIM_GetCapture2(TIM3);  

        TIM_SetCompare2(TIM3, capture + CCR2_Val);  

    }  

    else if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)  

    {  

        TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);  

  

        /* LED5 toggling with frequency = 18.31 Hz */  

        STM_EVAL_LEDToggle(LED5);//PD14取反  

        capture = TIM_GetCapture3(TIM3);  

        TIM_SetCompare3(TIM3, capture + CCR3_Val);  

    }  

    else  

    {  

        TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);  

  

        /* LED6 toggling with frequency = 36.62 Hz */  

        STM_EVAL_LEDToggle(LED6);//PD15取反  

        capture = TIM_GetCapture4(TIM3);  

        TIM_SetCompare4(TIM3, capture + CCR4_Val);  

    }  

}  

  

  

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/  





ReadMe.txt




/**

  @page TIM_TimeBase TIM_TimeBase

  

  @verbatim

  ******************************************************************************

  * @file    TIM_TimeBase/readme.txt

  * @author  MCD Application Team

  * @version V1.0.0

  * @date    19-September-2011

  * @brief   Description of the TIM Time Base example.

  ******************************************************************************

  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS

  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE

  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY

  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING

  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE

  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

  ******************************************************************************

   @endverbatim



@par Example Description 



This example shows how to configure the TIM peripheral in Output Compare Timing 

mode with the corresponding Interrupt requests for each channel in order to generate

4 different time bases.



The TIM3CLK frequency is set to SystemCoreClock / 2 (Hz), to get TIM3 counter 

clock at 500 KHz so the Prescaler is computed as following:

   - Prescaler = (TIM3CLK / TIM3 counter clock) - 1



SystemCoreClock is set to 168 MHz for STM32F4xx Devices Revision A.



The TIM3 CC1 register value is equal to 54618, 

CC1 update rate = TIM3 counter clock / CCR1_Val = 9.154 Hz,

so the TIM3 Channel 1 generates an interrupt each 109.2ms



The TIM3 CC2 register is equal to 27309, 

CC2 update rate = TIM3 counter clock / CCR2_Val = 18.31 Hz

so the TIM3 Channel 2 generates an interrupt each 54.6ms



The TIM3 CC3 register is equal to 13654, 

CC3 update rate = TIM3 counter clock / CCR3_Val = 36.62 Hz

so the TIM3 Channel 3 generates an interrupt each 27.3ms



The TIM3 CC4 register is equal to 6826, 

CC4 update rate = TIM3 counter clock / CCR4_Val =  73.25 Hz

so the TIM3 Channel 4 generates an interrupt each 13.65ms.



When the counter value reaches the Output compare registers values, the Output 

Compare interrupts are generated and, in the handler routine, 4 pins(PD.12, PD.13,

PD.14 and PD.15) are toggled with the following frequencies: 



//引腳輸出的頻率

- PD.12:  4.57 Hz (CC1)

- PD.13:  9.15 Hz (CC2)

- PD.14: 18.31 Hz (CC3) 

- PD.15: 36.62 Hz (CC4)



@par Directory contents 



  - TIM_TimeBase/stm32f4xx_conf.h     Library Configuration file

  - TIM_TimeBase/stm32f4xx_it.c       Interrupt handlers

  - TIM_TimeBase/stm32f4xx_it.h       Interrupt handlers header file

  - TIM_TimeBase/main.c               Main program 

  - TIM_TimeBase/system_stm32f4xx.c   STM32F4xx system clock configuration file

  

 



@par Hardware and Software environment 



  - This example runs on STM32F4xx Devices Revision A.

  

  - This example has been tested with STM32F4-Discovery (MB997) RevA and can be

    easily tailored to any other development board.

    



  - STM32F4-Discovery  

    - Use LED4, LED3, LED5 and LED6 connected respectively to PD.12, PD.13,D.14 

    and PD.15 pins and connect them on an oscilloscope to show the different 

    Time Base signals.  



@par How to use it ? 



In order to make the program work, you must do the following :



 + EWARM

    - Open the TIM_TimeBase.eww workspace 

    - Rebuild all files: Project->Rebuild all

    - Load project image: Project->Debug

    - Run program: Debug->Go(F5)



 + MDK-ARM

    - Open the TIM_TimeBase.uvproj project

    - Rebuild all files: Project->Rebuild all target files

    - Load project image: Debug->Start/Stop Debug Session

    - Run program: Debug->Run (F5)    



 + TASKING

    - Open TASKING toolchain.

    - Click on File->Import, select General->'Existing Projects into Workspace' 

      and then click "Next". 

    - Browse to  TASKING workspace directory and select the project "TIM_TimeBase"   

    - Rebuild all project files: Select the project in the "Project explorer" 

      window then click on Project->build project menu.

    - Run program: Select the project in the "Project explorer" window then click 

      Run->Debug (F11)



 + TrueSTUDIO

    - Open the TrueSTUDIO toolchain.

    - Click on File->Switch Workspace->Other and browse to TrueSTUDIO workspace 

      directory.

    - Click on File->Import, select General->'Existing Projects into Workspace' 

      and then click "Next". 

    - Browse to the TrueSTUDIO workspace directory and select the project "TIM_TimeBase" 

    - Rebuild all project files: Select the project in the "Project explorer" 

      window then click on Project->build project menu.

    - Run program: Select the project in the "Project explorer" window then click 

      Run->Debug (F11)

   

 *

© COPYRIGHT 2011 STMicroelectronics

 */


關鍵字:STM32  Timer  不同頻率  可調占空比  PWM 引用地址:STM32一個Timer輸出4路不同頻率、可調占空比的PWM

上一篇:stm32f407之數字濾波(操作寄存器)
下一篇:STM32作為主機I2C,讀寫24C02 EEPROM

推薦閱讀

程序功能將STM32的USB枚舉為HID設備。STM32使用3個端點,端點0用于枚舉用,端點1和2用于數據的發送和接收。端點長度為64,也就是單次最多可以傳輸64個字節數據。STM32獲取上位機下發的數據并將該數據通過USB原樣返回,同時將數據打印輸出。上位機程序通過調用windows的API實現對HID設備的讀寫控制。USB接口原理圖: HID枚舉成功: 程序效果圖 圖一 上位機...
據外媒報道,三星將于9月23日發布Galaxy S20 Fan Edition智能手機。關于這款手機的信息已經被泄露了很多次,可以說,關于它的幾乎所有細節信息都已經被透露了關于這款手機的所有細節。現在,WinFuture透露了Galaxy S20 FE在歐洲的價格。據悉,這款手機已經在該品牌保加利亞和菲律賓的網站上被發現。眾所周知,4G版本將搭載Exynos 990, 5G版則將搭...
9月19日,據韓媒Business Korea報道,消息人士表示,蘋果正在開發兩款折疊手機,一款為左右開蓋,另一種為上下掀蓋,預計蘋果首款折疊機將于2023年問世,目前蘋果正與LG Display開發可折疊面板。事實上,早在去年年底就有消息傳出,蘋果謀劃推出可折疊iPhone,已將樣品送至富士康測試,預計在2022年9月推出。從近兩年蘋果申請的專利也可以看出,蘋果正在...

史海拾趣

問答坊 | AI 解惑

超低功耗電子電路系統設計原則

以手機為代表的電池供電電路的興起,為便攜式儀表開創了一個新的紀元。超低功耗電路系統(包括超低功耗的電源、單片機、放大器、液晶顯示屏等)已經對電路設計人員形成了極大的誘惑。毫無疑問,超低功耗電路設計已經對低功耗電路提出了挑戰,并將擴展 ...…

查看全部問答∨

開始入門DSP的一些注意事項

在作硬件之前,需要看的資料有: 1.芯片數據手冊,描述該器件的引腳信號、片上資源、電氣指標和機械特性(如封裝等),在做硬件前必看(TMS320F281x數據手冊SPRS174J) 2.某一系列DSP的CPU和指令集用戶指南,描述該系列DSP的CPU結構、內部寄存器 ...…

查看全部問答∨

介紹一個實用的數控穩壓電源

各位有那位高手介紹一個實用的0-36V數控實驗穩壓電源的原理圖和程序,謝謝了…

查看全部問答∨

怎么沒有CBitmapButton類啊

來自EEWORLD合作群:arm linux fpga 嵌入0(49900581) 群主:wangkj …

查看全部問答∨

關于pxa270 wince5.0 系統使用sdio wifi (mavell 8686 芯片)的問題

   使用sdio 接口,接上wifi 卡,系統能檢測到wifi 卡,并能完成 MrvDrvInitialize ,彈出wifi的設置對話框后,能夠檢測到AP,但是始終鏈接不上:下面是調試信息,有沒有什么建議『我摘取了從對話框彈出到鏈接一個AP不成功的信息』: ...…

查看全部問答∨

我需要Advanced Archive Password Recovery 注冊碼

那位高手有請給個吧!!!!!!!!!!!…

查看全部問答∨

介紹一下達普電子芯片網站?

    最近幫朋友做調查拉項目,關于電子芯片網站的,有幾個網站已經確定了,還要了解一個達普電子芯片網,網址是www.ic72.com 不太知道底細,誰了解的話幫忙介紹一下?要公司詳細介紹和其網站架構,最好是權威一點,非常感謝!…

查看全部問答∨

關于wince6.0的shell組件

有那位高人能幫我介紹下shell組件…

查看全部問答∨

為什么設備不能申請進入D3狀態呢?

為什么可以把系統從掛起狀態的設備不能通過DevicePowerNotify申請進入D3狀態呢?     小弟看了windows mobile 5.0的文檔,但是看得一頭霧水,請各位大哥大姐指教,本人不勝感激!以下是mobile的部分文檔: Devices that can wake the s ...…

查看全部問答∨

系統不工作,示波器探頭地碰下電路板的地就工作!

最近再做一個FPGA通過并口與PC機進行通信的實驗:數據從FPGA傳向PC機。給FPGA下載代碼后,PC機接收不到數據,當打開示波器的一瞬間或者用示波器的探頭地接觸電路板的地時,PC機可以接收到數據! 我在網上看了一些資料,發現可能是“浮地”的問題, ...…

查看全部問答∨
小廣播
設計資源 培訓 開發板 精華推薦

最新單片機文章
何立民專欄 單片機及嵌入式寶典

北京航空航天大學教授,20余年來致力于單片機與嵌入式系統推廣工作。

 
EEWorld訂閱號

 
EEWorld服務號

 
汽車開發圈

 
機器人開發圈

電子工程世界版權所有 京ICP證060456號 京ICP備10001474號-1 電信業務審批[2006]字第258號函 京公網安備 11010802033920號 Copyright ? 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
主站蜘蛛池模板: 甘肃省| 栖霞市| 双鸭山市| 汤阴县| 如皋市| 会东县| 澳门| 仁寿县| 象山县| 阳西县| 临泉县| 四子王旗| 黄大仙区| 汝南县| 阿鲁科尔沁旗| 财经| 安顺市| 芮城县| 宁远县| 炉霍县| 慈溪市| 葫芦岛市| 平塘县| 遂溪县| 左贡县| 五寨县| 开远市| 古交市| 桐乡市| 盐城市| 麻栗坡县| 随州市| 玛多县| 肥城市| 闻喜县| 凌海市| 伊春市| 青川县| 万年县| 宜宾县| 华亭县|