/********************************************************************************* [url=home.php?mod=space&uid=288409]@file[/url] : main.c* [url=home.php?mod=space&uid=247401]@brief[/url] : Main program body******************************************************************************* @attention**//* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "hk32f030m.h"
#include "hk32f030m_gpio.h"
#include <stdio.h>
#include "stdarg.h"uint16_t TimerPeriod = 0; // 自动重载值
uint16_t Channel1Pulse = 0;// 脉冲宽度void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Config(void);int main(void)/* Infinite loop */
{RCC_Configuration();GPIO_Configuration();TIM_Config();while (1){}
}
/*配置时钟*/
void RCC_Configuration(void)
{RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE );RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOD, ENABLE );RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
/*配置GPIO*/
void GPIO_Configuration(void)
{GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;/*PD4 = TIM2 Channel 1*///GPIOD Configuration: Channel 1GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_4);
}
/*配置TIMER*/
void TIM_Config(void)
{TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;TIM_OCInitTypeDef TIM_OCInitStructure;// 频率配置为100KHZ SystemCoreClock = 32Mhz(内部高速时钟) 自动重载值 = 32Mhz/100 000 - 1 = 3199TimerPeriod = (SystemCoreClock / 100000 ) - 1;/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */// 脉宽值 // 5*(3199-1)/10 = 1599// duty cycle 占空比为 1/2 = 50%Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10); /* Time Base configuration */TIM_TimeBaseStructure.TIM_Prescaler = 0;//预分频系数TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数TIM_TimeBaseStructure.TIM_Period = TimerPeriod;//自动重载值3200TIM_TimeBaseStructure.TIM_ClockDivision = 0;TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);/* Channel 1 Configuration in PWM mode */TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//翻转TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;TIM_OC1Init(TIM2, &TIM_OCInitStructure);//TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);/* TIM2 counter enable */TIM_Cmd(TIM2, ENABLE);
}#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,tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}
#endif /* USE_FULL_ASSERT */
|