以下是PWM.c
#include "pwm.h"
void TIM3_PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //????GPIO???è??AFIO???????????é?±??????
//?è??????????????????????,????TIM3 CH3==>
B0 CH4==>
B1 ??PWM?????¨??
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7; //??????GPIO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //???????ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; //?¨???è??
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //????????·?
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //???????????÷????????
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //?????¤·??????????????????à????????????×???????????????????????±??????????????????á±??
//±???????4·????ò????????????????×??????????????ù??????????±???????·±???é???????????????í??±?????????????????
TIM_ICInitStructure.TIM_ICFilter = 0x0; //???¨?è???????ú??????????±????¨?¨?????¨0x0~0xf
TIM_PWMIConfig(TIM3, &TIM_ICInitStructure); //?ù???????è??TIM???è????
TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); //????IC2????????·???
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); //TIM??????????·??????????????????????????÷????·??????÷???ü??????
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); //?????¨?±?÷??±?????·?
TIM_Cmd(TIM3, ENABLE); //????TIM3
TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); //?ò??????
}
u16 IC2Value = 0;
u16 DutyCycle = 0;
u16 Frequency = 0;
void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);//????TIM???????????í??
IC2Value=TIM_GetCapture2(TIM3); //????IC2?????????÷??????????PWM????????????
if(IC2Value!=0)
{
DutyCycle=(TIM_GetCapture1(TIM3)*100)/IC2Value; //????IC1?????????÷????????????????±?
Frequency=72000000/IC2Value; //????PWM????
}
else
{
DutyCycle=0;
Frequency=0;
}
}
以下是main.c
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "exti.h"
#include "wdg.h"
#include "pwm.h"
extern void TIM3_IRQHandler(void);
extern void TIM3_PWM_Init(void);
extern u16 IC2Value;
extern u16 DutyCycle ;
extern u16 Frequency ;
int main(void)
{
SystemInit();
delay_init(72); //???±??????
NVIC_Configuration(); //????????
uart_init(9600); //?®????????
while(1)
{
delay_ms(1004);
if(!IC2Value)
{
printf("CO2 = %d
",DutyCycle);
printf("CO2 = %d
",Frequency);
}
}
}
为何读取不到数据呢?用的是PA7 TIM3_CH2
一周热门 更多>