#include "Tim_Pwm.h" #include #include "stm32f10x.h" #include "usart.h" void TIM_PWMGET(struct driver *self); void TIM_PWMGET_INIT(struct driver *self); const struct TIM_Get_PWM TIM2_PWM = { .GPIO_NUM = GPIO_Pin_15, .GPIO_Group = GPIOA, }; U_BOOT_DRIVER(TIME2_PWM_CH2) = { .pdata =(void*)&TIM2_PWM, .Init =TIM_PWMGET_INIT, .ops = NULL }; U_BOOT_DEVICE(TIME2_PWM_CH2) = { .name = "TIME2_PWM_CH2", .driver = &GetDRIVER_Name(TIME2_PWM_CH2), }; volatile uint16_t pwmFrequency = 0; volatile uint16_t pwmDutyCycle = 0; void TIM_PWMGET_INIT(struct driver *self) { struct TIM_Get_PWM *Pdata_TIM = (struct TIM_Get_PWM*)self->pdata; GPIO_InitTypeDef GPIO_InitStructure; // 使能 TIM2 时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // 使能 GPIOA 时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 配置 PA0 为 TIM2_CH1 输入 GPIO_InitStructure.GPIO_Pin = Pdata_TIM->GPIO_NUM; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(Pdata_TIM->GPIO_Group, &GPIO_InitStructure); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; // 配置 TIM2 时基 TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // 最大计数值 TIM_TimeBaseStructure.TIM_Prescaler = 72-1; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // 配置 TIM2 通道1 为输入捕捉 TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//上升沿捕获 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//直接输入模式 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM2, &TIM_ICInitStructure); // 配置 TIM2 通道2 为输入捕捉 TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;//下降沿捕获 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;//间接输入模式 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM2, &TIM_ICInitStructure); TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1); // 使用通道1作为触发输入 TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); // 选择复位模式 // // 配置 TIM2 主从模式(使能主从模式) // TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); TIM2->SR = 0; TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); // 配置 NVIC 中断优先级 NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // 使能 TIM2 输入捕捉中断 // 清除 TIM2 的所有中断标志位 // 使能 TIM2 // TIM_Cmd(TIM2, ENABLE); } void Tim_DEVICE_INIT(struct udevice* self) { struct driver* Init_ops = (struct driver*)self->driver; if(!Init_ops->Init) return; Init_ops->Init(Init_ops); } void Init_Tim_PWM(void) { Tim_DEVICE_INIT(&GetDEVICE_Name(TIME2_PWM_CH2)); } void Printf_Frequency() { printf("Frequency is %d\r\n",pwmFrequency); } void Printf_DutyCycle() { printf("DutyCycle is %d\r\n",pwmDutyCycle); } // TIM2 中断处理函数 void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) { // 读取捕捉寄存器的值 // TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE); uint16_t capture = TIM_GetCapture1(TIM2); // printf("capture is %d\r\n",capture); // 计算PWM频率和占空比 pwmFrequency = SystemCoreClock / capture; pwmDutyCycle = (TIM_GetCapture2(TIM2) * 100) / capture; // printf("pwmFrequency is %d\r\n",pwmFrequency); // printf("pwmDutyCycle is %d\r\n",pwmDutyCycle); // 清除中断标志 TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); } else if(TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET) { // TIM_ITConfig(TIM2, TIM_IT_CC2, DISABLE); TIM_ClearITPendingBit(TIM2, TIM_IT_CC2); } }