航顺的库。。
目前是 HK32F030Mxx_ExampleV1.0.5
PWM部分的demo还没做好,这里可以参照STM32F030的搞进去
这里输出 125K 定时器 就是 8us了
/**
******************************************************************************
* [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 PrescalerValue = 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_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
/*PD4 = TIM2 Channel 1*/
//GPIOD Configuration: Channel 1
/*
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_4);
*/
/*PD3 = TIM2 Channel 2*/
//GPIOD Configuration: Channel 2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource3,GPIO_AF_4);
/*PC3 = TIM2 Channel 1*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource3,GPIO_AF_4);
//PA3,IO翻转输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*TIMER配置*/
void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* -----------------------------------------------------------------------
TIM2 Configuration: Encoder mode1:
In this example TIM2 input clock (TIM2CLK) is set to APB1 clock (PCLK1).
TIM2CLK = PCLK1
PCLK1 = HCLK
=> TIM2CLK = HCLK = SystemCoreClock
To get TIM2 counter clock at 32 MHz, the prescaler is computed as follows:
Prescaler = (TIM2CLK / TIM2 counter clock) - 1
Prescaler = ((SystemCoreClock) /32 MHz) - 1
TIM2 is configured to interface with an encoder:
- The encoder mode is encoder mode1: Counter counts up/down on TI2 rising edge
depending on TI1 level
- The Autoreload value is set to 10, so the encoder round is 10 TIM counter clock.
Note:
SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.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 ) / 32000000) - 1;//配置频率为32M
PrescalerValue = 32-1; /* 和下面设置预分频一样 */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 8-1 ; /* 自动重装 数值*/
TIM_TimeBaseStructure.TIM_Prescaler = 0; /* 预分频 */
TIM_TimeBaseStructure.TIM_ClockDivision = 0; /* 不分频 */
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Prescaler configuration */
/* 先配好ARR ,再设置预分频,PSC有预重装功能只能等更新事件来弄,不像ARR CRR立即更新重载 */
TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate);//每次UPdate重新初始化计数器
/*选择编码器模式1,根据TIFP1的电平,计数器在TI2FP2的边沿上下计数*/
// TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
/* TIM Interrupts enable */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM2, ENABLE);
/* Enable the TIM2 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_OCInitTypeDef TIM_OCInitStructure;
/* 频道1,2,3,4的PWM 模式设置 */
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
// TIM_OCInitStructure.TIM_Pulse = 500;//使能频道1配置
// TIM_OC1Init(TIM1, &TIM_OCInitStructure);
/* 通道1 */
TIM_OCInitStructure.TIM_Pulse = 4;//使能频道2配置
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
/* 通道2 */
TIM_OCInitStructure.TIM_Pulse = 4;//使能频道2配置
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
/* TIM1 主输出使能 */
TIM_CtrlPWMOutputs(TIM2, ENABLE);
}
/*配置IO翻转*/
void GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->ODR ^= GPIO_Pin;
}
#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 */
转自https://www.cnblogs.com/XZHDJH/articles/13523787.html
|