打印
[技术问答]

定时器计数/PWM输出/输出翻转/输入捕获

[复制链接]
729|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
mnynt121|  楼主 | 2023-4-23 21:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


/********************************************************************************* [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 CCR1_Val = 5000;
uint16_t CCR2_Val = 2500;
uint16_t CCR3_Val = 1250;
uint16_t CCR4_Val = 625;
uint16_t PrescalerValue = 0;void RCC_Configuration(void);
void GPIO_Configuration(void);
void TIM_Config(void);
void GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin);
void TIM6_IRQHandler(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_TIM6 , ENABLE);
}
/*配置GPIO*/
void GPIO_Configuration(void)
{GPIO_InitTypeDef GPIO_InitStructure;//GPIOA,PA1,PA2,PA3GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3 ;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;GPIO_Init(GPIOA, &GPIO_InitStructure);        //GPIOD,PD4GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/*配置TIMER*/
void TIM_Config(void)
{TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;NVIC_InitTypeDef NVIC_InitStructure;// 预分频器值 // SystemCoreClock = 48M,计数器时钟counter clock = 1 MHz
//  PrescalerValue = (uint16_t) (SystemCoreClock  / 1000000) - 1;// 32MHzPrescalerValue = (uint16_t) (SystemCoreClock  / 10000) - 1;// 3200/* Enable the TIM1 gloabal Interrupt */NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;NVIC_InitStructure.NVIC_IRQChannelPriority = 0; // 中断优先级NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);/* Time Base configuration */// 公式:Tout= ((arr+1)*(psc+1))/Tclk// 1s = ((10000+1)*(3200+1))/32000000// 0.5s = ((5000+1)*(3200+1))/32000000TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;// 预分频系数 3200TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;// 计数模式:向上计数TIM_TimeBaseStructure.TIM_Period = 5000; // 自动重载值 10000,计数模式为向上计数时,定时器从0开始计数,计数超过到arr时触发定时中断服务函数TIM_TimeBaseStructure.TIM_ClockDivision = 0;TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);/* TIM Interrupts enable */TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);/* TIM1 counter enable */TIM_Cmd(TIM6, 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; // 输出翻转
}uint16_t capture = 0;void TIM6_IRQHandler(void)
{if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET){TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //每次进入中断都要清空中断标志,否则主函数将无法正常执行/* PA1 toggling with frequency = 50 Hz */GPIO_TogglePin(GPIOD ,GPIO_Pin_4);// PD4 翻转 //                capture = TIM_GetCapture1(TIM1);
//               
//    TIM_SetCompare1(TIM1, capture);}
}#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 */
/********************************************************************************* @file           : main.c* @brief          : 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, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 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_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
}
/*配置GPIO*/
void GPIO_Configuration(void)
{//GPIOA Configuration: Channel 1N, 2N, 3NGPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3 ;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 ;GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_3);        GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_3);        GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_3);        //GPIOC Configuration: Channel 1, 2GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;GPIO_Init(GPIOC, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_3);        GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_3);        /*PD3 = TIMER Channel 3*///GPIOD Configuration: Channel 3GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOD,GPIO_PinSource3,GPIO_AF_3);        /*PD4 = TIMER Channel 4*///GPIOD Configuration: Channel 4GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;GPIO_Init(GPIOD, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOD,GPIO_PinSource4,GPIO_AF_3);//        //GPIOB Configuration: BreakGPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;GPIO_Init(GPIOB, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_3);        //        /*PC3 = TIMER Channel 3*/
//        GPIO_IOMUX_PinAFConfig(GPIOC,GPIO_PinSource3,IOMUX_PC3_TIM1CH3);
//        //GPIOC Configuration: Channel 3 PC3
//  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
//  GPIO_Init(GPIOC, &GPIO_InitStructure);
//        GPIO_PinAFConfig(GPIOC,GPIO_PinSource3,GPIO_AF_3);       
}
/*配置TIMER*/
void TIM_Config(void)
{TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;TIM_OCInitTypeDef  TIM_OCInitStructure;//TIM_BDTRInitTypeDef TIM_BDTRInitStructure;TimerPeriod = (SystemCoreClock / 3200 ) - 1;//SystemCoreClock=32MHZ  // 自动重载值是10000 /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);/* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);/* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);/* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);/* Time Base configuration */TIM_TimeBaseStructure.TIM_Prescaler = 0;// 预分频系数TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数TIM_TimeBaseStructure.TIM_Period = TimerPeriod;// 自动重载值TIM_TimeBaseStructure.TIM_ClockDivision = 0;//一般不使用,默认TIM_CKD_DIV1TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);/* Channel 1, 2,3 and 4 Configuration in PWM mode */TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//设置待装入捕获比较寄存器的脉冲值TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;TIM_OC1Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;TIM_OC2Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;TIM_OC3Init(TIM1, &TIM_OCInitStructure);TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;TIM_OC4Init(TIM1, &TIM_OCInitStructure);
//  /* Automatic Output enable, Break, dead time and lock configuration*/
//  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
//  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
//  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
//  TIM_BDTRInitStructure.TIM_DeadTime = 50;
//  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
//  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
//  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//  TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);/* TIM1 counter enable */TIM_Cmd(TIM1, ENABLE);/* TIM1 Main Output Enable */TIM_CtrlPWMOutputs(TIM1, 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 */


使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

3076

帖子

2

粉丝