main.c
/*********************************************
標(biāo)題:操作USART的練習(xí)
軟件平臺(tái):MDK-ARM Standard Version4.70
硬件平臺(tái):stm32f4-discovery
主頻:168M
Periph_Driver_version: V1.0.0
描述:用一個(gè)定時(shí)器(TIM3),實(shí)現(xiàn)四路不同頻率、占空比可調(diào)的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 上面已經(jīng)將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);//對(duì)84M進(jìn)行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
#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;
/**
實(shí)際上當(dāng)CCR1_Val每次都變化時(shí),就可以用來改變占空比
*/
void TIM3_IRQHandler(void)
{
/*7500為一個(gè)循環(huán),周期為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后,到達(dá)這條語句。
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)基礎(chǔ)上加5000,則再過5000,又會(huì)觸發(fā)中斷
//另外,有個(gè)問題,進(jìn)入中斷時(shí),當(dāng)ARR計(jì)數(shù)器快加到65535,而又不足5000時(shí),不是會(huì)有數(shù)據(jù)多余,而產(chǎn)生波形的移動(dòng)嗎?
//回答:不用擔(dān)心。例如進(jìn)入中斷是,ARR=65000,65000+5000=70000>65535,那么高位會(huì)舍去,即為70000-65536=4464
//等于是來了個(gè)循環(huán),兩次中斷間ARR的增量還是5000。所以為了波形的穩(wěn)定,初始化時(shí),必須要有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的定時(shí)器1之7路PWM輸出
下一篇:stm32f4,pwm控制電機(jī)自己的疑惑
推薦閱讀
史海拾趣
設(shè)計(jì)資源 培訓(xùn) 開發(fā)板 精華推薦
- 神經(jīng)形態(tài)芯片可能是革新機(jī)器人實(shí)時(shí)電機(jī)控制的未來
- 從三個(gè)方面理解ARM嵌入式系統(tǒng)
- 自動(dòng)報(bào)警 基于MCU的家庭防盜報(bào)警系統(tǒng)的設(shè)計(jì)
- 存儲(chǔ)控制器及其訪問外設(shè)的原理
- 基于51系列單片機(jī)的智能照明控制系統(tǒng)設(shè)計(jì)方案
- 基于STM32的四旋翼飛行器控制系統(tǒng)
- 單片機(jī)應(yīng)用編程技巧解析
- 基于89C52的教室智能節(jié)能照明系統(tǒng)設(shè)計(jì)
- 一種新型的雨量光照傳感器的設(shè)計(jì)
- 保障工業(yè)核心命脈:深度解讀工業(yè)交換機(jī)QoS的“智能流量治理”之道
- WAIC 直擊|Arm 鄒挺:突破基礎(chǔ)設(shè)施、數(shù)據(jù)安全與人才三重挑戰(zhàn),釋放 AI 發(fā)展新潛能
- 鎧俠第九代 BiCS FLASH? 512Gb TLC 存儲(chǔ)器開始送樣
- 基于PPEC32系列芯片的10kW純正弦波車載逆變器應(yīng)用方案
- 新唐科技推出全新KM1M4BF6x系列單電機(jī)控制MCU
- 伺服電機(jī)中的回原點(diǎn)方式
- 芯科集成CX3288系列車規(guī)MCU在旋轉(zhuǎn)變壓器的應(yīng)用
- AI玩具DC-DC芯片,安全與成本雙突圍
- 訂單暴增800%!消費(fèi)芯片新風(fēng)口:東北裝空調(diào)
- 機(jī)器人也能純視覺控制?MIT華人博士團(tuán)隊(duì)登上Nature主刊
- 您的無人機(jī)安全嗎?使用電路保護(hù)應(yīng)對(duì)安全隱患
- CadenceLIVE China 2022中國線上用戶大會(huì) 報(bào)名中!
- 【摸黑搶樓】贏大禮,關(guān)于示波器的狂想曲!正式開搶?。?!
- 微波單片集成電路簡介
- 【六一兒童節(jié)】說說你童年的夢(mèng)想!
- 3小時(shí)實(shí)戰(zhàn)+剖析:TI工程師手把手帶你全面入門MSP430(限量售$4.3開發(fā)套件,助你參與動(dòng)手實(shí)驗(yàn))
- 芯幣競(jìng)拍:感恩教師節(jié)專場(chǎng)
- 【EEWORLD第二十九屆】2011年08月社區(qū)明星人物揭曉!
- 新活動(dòng)上線!ST NUCLEO-U575ZI-Q免費(fèi)測(cè)評(píng)來了~
- 技術(shù)文章:壓力傳感器元件壓力測(cè)量方法
- 華為郭平:5G的重點(diǎn)已轉(zhuǎn)移到如何釋放網(wǎng)絡(luò)紅利上
- 100W+功率,5分鐘充滿50%,高通QC 5.0快充技術(shù)有這么牛?
- 禍不單行,英特爾首席工程師離職 ,技術(shù)部門一分為五
- 5G、ASIC齊發(fā)力, 聯(lián)發(fā)科預(yù)計(jì)營收連續(xù)增長
- 基于TFP401A為核心的DVI接口應(yīng)用系統(tǒng)
- 晉江市副市長李自力:打造全球重要的內(nèi)存生產(chǎn)基地
- 嵌入式ARM處理器的節(jié)能優(yōu)勢(shì)
- 聯(lián)發(fā)科淡季效應(yīng)現(xiàn) 1月營收月減近1成
- 基于LPC2292與CAN總線的電子結(jié)算終端設(shè)計(jì)