|
單片機(jī)源程序如下:- /**
- ******************************************************************************
- * @文件名 : main.c
- * @作者 : strongerHuang
- * @版本 : V1.0.0
- * @日期 : 2018年06月23日
- * @摘要 : 主函數(shù) - Demo軟件工程
- ******************************************************************************/
- /*----------------------------------------------------------------------------
- 更新日志:
- 2018-06-23 V1.0.0:初始版本
- ----------------------------------------------------------------------------*/
- /* 包含的頭文件 --------------------------------------------------------------*/
- #include "bsp.h"
- #include "bsp_timer.h"
- /************************************************
- 函數(shù)名稱 : Delay
- 功 能 : 軟件延時(shí)
- 參 數(shù) : Cnt --- 延時(shí)計(jì)數(shù)
- 返 回 值 : 無
- 作 者 : strongerHuang
- *************************************************/
- static void Delay(uint32_t Cnt)
- {
- uint32_t i;
- while(Cnt--)
- {
- for(i=0; i<0x100000; i++);
- }
- }
- /************************************************
- 函數(shù)名稱 : System_Initializes
- 功 能 : 系統(tǒng)初始化
- 參 數(shù) : 無
- 返 回 值 : 無
- 作 者 : strongerHuang
- *************************************************/
- void System_Initializes(void)
- {
- BSP_Initializes();
- }
- /************************************************
- 函數(shù)名稱 : main
- 功 能 : 主函數(shù)入口
- 參 數(shù) : 無
- 返 回 值 : int
- 作 者 : strongerHuang
- *************************************************/
- int main(void)
- {
- System_Initializes();
- while(1)
- {
- LED_TOGGLE(); //LED變化
- Delay(5); //延時(shí)(約240ms)
- PWM_Output(1000, 20, 10); //1KHz, 20%占空比, 10個(gè)脈沖
- }
- }
復(fù)制代碼- /**
- ******************************************************************************
- * @文件名 : bsp_timer.c
- * @作者 : strongerHuang
- * @版本 : V1.0.0
- * @日期 : 2018年06月23日
- * @摘要 : 定時(shí)器底層源文件
- ******************************************************************************/
- /*----------------------------------------------------------------------------
- 更新日志:
- 2018-06-23 V1.0.0:初始版本
- ----------------------------------------------------------------------------*/
- /* 包含的頭文件 --------------------------------------------------------------*/
- #include "bsp_timer.h"
- /******************************************* PWM輸出 ********************************************/
- /************************************************
- 函數(shù)名稱 : PWM_TIM_Configuration
- 功 能 : PWM輸出定時(shí)器配置
- 參 數(shù) : 無
- 返 回 值 : 無
- 作 者 : strongerHuang
- *************************************************/
- void PWM_TIM_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- /* 時(shí)鐘配置 */
- RCC_APB1PeriphClockCmd(PWM_TIM_CLK, ENABLE);
- RCC_AHB1PeriphClockCmd(PWM_TIM_GPIO_CLK, ENABLE);
- GPIO_InitStructure.GPIO_Pin = PWM_TIM_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(PWM_TIM_GPIO_PORT, &GPIO_InitStructure);
- /* 映射配置 */
- GPIO_PinAFConfig(PWM_TIM_GPIO_PORT, PWM_TIM_SOURCE, PWM_TIM_AF);
- /* 時(shí)基配置 */
- TIM_TimeBaseStructure.TIM_Prescaler = PWM_PRESCALER; //預(yù)分頻值
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //定時(shí)周期
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //分頻因子
- TIM_TimeBaseInit(PWM_TIMx, &TIM_TimeBaseStructure);
- /* PWM模式配置 */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1模式
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能輸出
- TIM_OCInitStructure.TIM_Pulse = 0xFFFF; //脈寬值
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性
- PWM_TIM_OCxInit(PWM_TIMx, &TIM_OCInitStructure);
- TIM_Cmd(PWM_TIMx, DISABLE);
- }
- /************************************************
- 函數(shù)名稱 : CNT_TIM_Configuration
- 功 能 : 計(jì)時(shí)定時(shí)器配置
- 參 數(shù) : 無
- 返 回 值 : 無
- 作 者 : strongerHuang
- *************************************************/
- void CNT_TIM_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- /* 時(shí)鐘配置 */
- RCC_APB1PeriphClockCmd(CNT_TIM_CLK, ENABLE);
- RCC_AHB1PeriphClockCmd(CNT_TIM_GPIO_CLK, ENABLE);
- GPIO_InitStructure.GPIO_Pin = CNT_TIM_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(CNT_TIM_GPIO_PORT, &GPIO_InitStructure);
- /* 映射配置 */
- GPIO_PinAFConfig(CNT_TIM_GPIO_PORT, CNT_TIM_SOURCE, CNT_TIM_AF);
- /* NVIC配置 */
- NVIC_InitStructure.NVIC_IRQChannel = CNT_TIM_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = CNT_TIM_Priority;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /* 使用外部時(shí)鐘源 */
- TIM_ETRClockMode2Config(CNT_TIMx, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0);
- /* 時(shí)基配置 */
- TIM_TimeBaseStructure.TIM_Prescaler = 0; //預(yù)分頻值
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上計(jì)數(shù)
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //定時(shí)周期
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //分頻因子
- TIM_TimeBaseInit(CNT_TIMx, &TIM_TimeBaseStructure);
- TIM_ClearFlag(CNT_TIMx, TIM_FLAG_Update);
- TIM_ITConfig(CNT_TIMx, TIM_IT_Update, ENABLE); //使能"更新"中斷
- TIM_Cmd(CNT_TIMx, DISABLE);
- }
- /************************************************
- 函數(shù)名稱 : PWM_Output
- 功 能 : 輸出PWM
- 參 數(shù) : Frequency --- 頻率
- Dutycycle --- 占空比(12代表占空比為12%)
- NumPulse --- 脈沖個(gè)數(shù)
- 返 回 值 : 無
- 作 者 : strongerHuang
- *************************************************/
- void PWM_Output(uint32_t Frequency, uint32_t Dutycycle, uint32_t NumPulse)
- {
- uint32_t pwm_period;
- uint32_t pwm_pulse;
- /* 輸出PWM */
- pwm_period = PWM_CK_CNT/Frequency - 1; //計(jì)算出計(jì)數(shù)周期(決定輸出的頻率)
- pwm_pulse = (pwm_period + 1)*Dutycycle / 100; //計(jì)算出脈寬值(決定PWM占空比)
- /* 判斷是否為32位定時(shí)器 */
- if((TIM2 == PWM_TIMx) || (TIM5 == PWM_TIMx)) //32位定時(shí)器:
- {
- }
- else
- {
- if((0xFFFF < pwm_period) || (0xFFFF < pwm_pulse)) //16位定時(shí)器:period和pulse都不能超過0xFFFF
- {
- return;
- }
- }
- TIM_Cmd(PWM_TIMx, DISABLE); //失能TIM
- TIM_SetCounter(PWM_TIMx, 0); //計(jì)數(shù)清零
- TIM_SetAutoreload(PWM_TIMx, pwm_period); //更改頻率
- PWM_TIM_SetComparex(PWM_TIMx, pwm_pulse); //更改占空比
- TIM_Cmd(PWM_TIMx, ENABLE); //使能TIM
- /* 脈沖個(gè)數(shù)計(jì)時(shí) */
- TIM_Cmd(CNT_TIMx, DISABLE);
- TIM_SetCounter(CNT_TIMx, 0);
- TIM_SetAutoreload(CNT_TIMx, NumPulse-1); //設(shè)置中斷更新數(shù)
- TIM_ClearFlag(CNT_TIMx, TIM_FLAG_Update);
- TIM_Cmd(CNT_TIMx, ENABLE);
- }
- /************************************************
- 函數(shù)名稱 : TIM_Initializes
- 功 能 : TIM定時(shí)器初始化
- 參 數(shù) : 無
- 返 回 值 : 無
- 作 者 : strongerHuang
- *************************************************/
- void TIM_Initializes(void)
- {
- PWM_TIM_Configuration();
- CNT_TIM_Configuration();
- }
復(fù)制代碼- /**
- ******************************************************************************
- * @文件名 : bsp.c
- * @作者 : strongerHuang
- * @版本 : V1.0.0
- * @日期 : 2018年06月23日
- * @摘要 : BSP底層源文件
- ******************************************************************************/
- /*----------------------------------------------------------------------------
- 更新日志:
- 2018-06-23 V1.0.0:初始版本
- ----------------------------------------------------------------------------*/
- /* 包含的頭文件 --------------------------------------------------------------*/
- #include "bsp.h"
- #include "bsp_timer.h"
- /************************************************
- 函數(shù)名稱 : RCC_Configuration
- 功 能 : 時(shí)鐘配置
- 參 數(shù) : 無
- 返 回 值 : 無
- 作 者 : strongerHuang
- *************************************************/
- void RCC_Configuration(void)
- {
- /* 使能AHB1時(shí)鐘 */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
- RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD |
- RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF |
- RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOH, ENABLE);
- /* 使能AHB2時(shí)鐘 */
- RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_DCMI, ENABLE);
- /* 使能APB1時(shí)鐘 */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* 使能APB2時(shí)鐘 */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- }
- /************************************************
- 函數(shù)名稱 : LED_Initializes
- 功 能 : LED初始化
- 參 數(shù) : 無
- 返 回 值 : 無
- 作 者 : strongerHuang
- *************************************************/
- void LED_Initializes(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 使能時(shí)鐘 */
- RCC_AHB1PeriphClockCmd(LED_GPIO_CLK, ENABLE);
- /* 引腳配置 */
- GPIO_InitStructure.GPIO_Pin = LED_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
- LED_OFF();
- #if 0
- /* MCO時(shí)鐘輸出 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- RCC_MCO1Config(RCC_MCO1Source_PLLCLK, RCC_MCO1Div_2);
- #endif
- }
- /************************************************
- 函數(shù)名稱 : BSP_Initializes
- 功 能 : 底層初始化
- 參 數(shù) : 無
- 返 回 值 : 無
- 作 者 : strongerHuang
- *************************************************/
- void BSP_Initializes(void)
- {
- LED_Initializes(); //LED初始化
- TIM_Initializes();
- }
復(fù)制代碼 本人初學(xué),僅供參考,存在錯(cuò)誤和不足之處,請大家回帖多多指教,切勿照搬,文件下載:
Keil代碼下載:
Keil代碼.7z
(388.56 KB, 下載次數(shù): 34)
2022-7-19 17:05 上傳
點(diǎn)擊文件名下載附件
|
評分
-
查看全部評分
|