CELIS/APPLICATION/External/Tim_Pwm.c

149 lines
4.2 KiB
C
Raw Normal View History

#include "Tim_Pwm.h"
#include <stdio.h>
#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;
// ʹ<><CAB9> TIM2 ʱ<><CAB1>
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// ʹ<><CAB9> GPIOA ʱ<><CAB1>
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// <20><><EFBFBD><EFBFBD> PA0 Ϊ TIM2_CH1 <20><><EFBFBD><EFBFBD>
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;
// <20><><EFBFBD><EFBFBD> TIM2 ʱ<><CAB1>
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
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);
// <20><><EFBFBD><EFBFBD> TIM2 ͨ<><CDA8>1 Ϊ<><CEAA><EFBFBD>벶׽
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//<2F><><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD><D8B2><EFBFBD>
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//ֱ<><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
// <20><><EFBFBD><EFBFBD> TIM2 ͨ<><CDA8>2 Ϊ<><CEAA><EFBFBD>벶׽
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;//<2F>½<EFBFBD><C2BD>ز<EFBFBD><D8B2><EFBFBD>
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1); // ʹ<><CAB9>ͨ<EFBFBD><CDA8>1<EFBFBD><31>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset); // ѡ<><D1A1><EFBFBD><EFBFBD>λģʽ
// // <20><><EFBFBD><EFBFBD> TIM2 <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>
// TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
TIM2->SR = 0;
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
// <20><><EFBFBD><EFBFBD> NVIC <20>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ȼ<EFBFBD>
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);
// ʹ<><CAB9> TIM2 <20><><EFBFBD>벶׽<EBB2B6>ж<EFBFBD>
// <20><><EFBFBD><EFBFBD> TIM2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־λ
// ʹ<><CAB9> 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 <20>жϴ<D0B6><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void TIM2_IRQHandler(void) {
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) {
// <20><>ȡ<EFBFBD><C8A1>׽<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ֵ
// TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE);
uint16_t capture = TIM_GetCapture1(TIM2);
// printf("capture is %d\r\n",capture);
// <20><><EFBFBD><EFBFBD>PWMƵ<4D>ʺ<EFBFBD>ռ<EFBFBD>ձ<EFBFBD>
pwmFrequency = SystemCoreClock / capture;
pwmDutyCycle = (TIM_GetCapture2(TIM2) * 100) / capture;
// printf("pwmFrequency is %d\r\n",pwmFrequency);
// printf("pwmDutyCycle is %d\r\n",pwmDutyCycle);
// <20><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־
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);
}
}