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
*/
上一篇:stm32f407之數字濾波(操作寄存器)
下一篇:STM32作為主機I2C,讀寫24C02 EEPROM
推薦閱讀
史海拾趣
近年來,輝芒微積極籌備上市工作,旨在通過資本市場進一步拓展融資渠道和品牌影響力。盡管在上市過程中經歷了兩次撤回申請資料的波折,但公司始終保持著對資本市場的熱情和信心。通過不斷優化內部管理、提升產品競爭力和加強市場拓展,輝芒微最終贏得了市場的廣泛認可。其產品被廣泛應用于智能家居、生活電器、智能穿戴等領域,并與小米、飛科、中興等眾多知名品牌建立了長期穩定的合作關系。
在自動駕駛技術快速發展的同時,AURORA公司也積極尋找商業化突破口。公司創始人認為,自動駕駛卡車商業化是一個具有巨大潛力的市場。因此,AURORA優先瞄準了自動駕駛卡車商業化領域,與FedEx、Paccar、Schneider、Werner和Xpress等多家平臺公司建立了試點合作關系。通過在實際場景中測試和驗證自動駕駛技術,AURORA不斷積累經驗和數據,為未來的商業化落地奠定了堅實基礎。
這五個故事展示了AURORA公司在電子行業中的發展歷程和取得的成就。從初創時期的挑戰與機遇到技術實力的不斷提升,再到與汽車制造商的深度合作、垂直產業鏈的整合與布局以及自動駕駛卡車商業化的探索,AURORA公司憑借其卓越的技術實力和前瞻性的戰略眼光,在電子行業中逐漸嶄露頭角。未來,我們有理由相信AURORA將繼續在自動駕駛領域取得更多突破和創新。
為了保持技術領先地位,柯愛亞不斷加大研發投入,引進先進的研發設備和人才。公司在功率電感、變壓器等領域取得了多項技術成果,并申請了多項專利。這些技術成果不僅提升了柯愛亞產品的競爭力,還推動了整個電子行業的發展。
隨著ETC技術的不斷完善和產品性能的提升,速通科技開始積極拓展市場。公司積極參與全國ETC聯網工作,與各地交通運輸管理部門和高速公路運營商建立了良好的合作關系。在政府的支持下,速通科技成功推動了ETC系統的全國聯網工作,實現了ETC卡的跨地區、跨省份通用。
EUCHNER公司一直將質量和服務視為其生命線。公司嚴格控制產品質量,所有核心部件均自行生產,以確保產品的可靠性和穩定性。同時,公司還注重客戶服務體驗,為客戶提供具有競爭力的價格及優質服務。無論是為大型汽車制造企業還是小型研究機構提供解決方案,公司都堅持與客戶緊密合作并傾聽他們的需求。通過持續提升質量和服務水平,EUCHNER公司逐漸贏得了客戶的信任和忠誠。
隨著全球市場的不斷擴大,Captive Fastener公司意識到國際合作的重要性。公司積極尋求與國際知名電子制造商的合作機會,通過提供高質量的緊固件產品和技術支持,成功打入國際市場。同時,公司還與國外同行開展技術交流與合作,不斷提升自身的技術水平和市場競爭力。
以手機為代表的電池供電電路的興起,為便攜式儀表開創了一個新的紀元。超低功耗電路系統(包括超低功耗的電源、單片機、放大器、液晶顯示屏等)已經對電路設計人員形成了極大的誘惑。毫無疑問,超低功耗電路設計已經對低功耗電路提出了挑戰,并將擴展 ...… 查看全部問答∨ |
在作硬件之前,需要看的資料有: 1.芯片數據手冊,描述該器件的引腳信號、片上資源、電氣指標和機械特性(如封裝等),在做硬件前必看(TMS320F281x數據手冊SPRS174J) 2.某一系列DSP的CPU和指令集用戶指南,描述該系列DSP的CPU結構、內部寄存器 ...… 查看全部問答∨ |
關于pxa270 wince5.0 系統使用sdio wifi (mavell 8686 芯片)的問題 使用sdio 接口,接上wifi 卡,系統能檢測到wifi 卡,并能完成 MrvDrvInitialize ,彈出wifi的設置對話框后,能夠檢測到AP,但是始終鏈接不上:下面是調試信息,有沒有什么建議『我摘取了從對話框彈出到鏈接一個AP不成功的信息』: ...… 查看全部問答∨ |
|
最近幫朋友做調查拉項目,關于電子芯片網站的,有幾個網站已經確定了,還要了解一個達普電子芯片網,網址是www.ic72.com 不太知道底細,誰了解的話幫忙介紹一下?要公司詳細介紹和其網站架構,最好是權威一點,非常感謝!… 查看全部問答∨ |
|
為什么可以把系統從掛起狀態的設備不能通過DevicePowerNotify申請進入D3狀態呢? 小弟看了windows mobile 5.0的文檔,但是看得一頭霧水,請各位大哥大姐指教,本人不勝感激!以下是mobile的部分文檔: Devices that can wake the s ...… 查看全部問答∨ |
最近再做一個FPGA通過并口與PC機進行通信的實驗:數據從FPGA傳向PC機。給FPGA下載代碼后,PC機接收不到數據,當打開示波器的一瞬間或者用示波器的探頭地接觸電路板的地時,PC機可以接收到數據! 我在網上看了一些資料,發現可能是“浮地”的問題, ...… 查看全部問答∨ |
設計資源 培訓 開發板 精華推薦
- STM32一文通(6) 外部中斷
- ESP32玩轉MicroPython(一) 燒入固件, 初識
- ESP32玩轉MicroPython(二) 連接WIFI網絡 webperl文件傳輸
- ESP32玩轉MicroPython(三) 延時、計時 和GPIO操作
- 玩轉 ESP32 + Arduino (一) 開發環境搭建
- 玩轉 ESP32 + Arduino (四) 電容按鍵 霍爾傳感器 外部中斷 延時 脈沖檢測
- 玩轉 ESP32 + Arduino (五) platformIO引入類庫 自開發庫 串口打印字符串
- 玩轉 ESP32 + Arduino (六) 硬件定時器, IIC, SPI
- 玩轉 ESP32 + Arduino (八) U8G2驅動OLED