#include "public.h"
#include "systick.h"
#include "PWM.h"
int main()
{
double fi=5000;
PWM_init(9999,143);
//while(1)
//{
//TIM_SetCompare1(TIM3,ti);//ê1óÃμÄêÇí¨μà2£¬¸ÃoˉêyÎaéèÖÃTIMX2¶»ñ±è½Ï2¼Ä′æÆ÷Ö
//delay_ms(1000);
TIM_SetCompare1(TIM3,fi);//ê1óÃμÄêÇí¨μà2£¬¸ÃoˉêyÎaéèÖÃTIMX2¶»ñ±è½Ï2¼Ä′æÆ÷Ö
}
// }
////////////////////////////////////////
#include "PWM.h"
void PWM_init(u16 per,u16 psc)//PWM3õê¼»ˉ
{
//½á11ìå±äῶ¨òå
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//¶¨ê±Æ÷3ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//led¶Ë¿úê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//Îaê1óÃPWM1|Äü′ò¿a1ü½Å¸′óÃ1|Äü
TIM_TimeBaseStructure.TIM_Period =per ;//Ïμí3ê±ÖóÎa72M£¬×Ô¶ˉÖØ×°ÔØÖμμÄÅäÖÃ
TIM_TimeBaseStructure.TIM_Prescaler =psc;//Ô¤·ÖÆμÖμμÄÅäÖÃ
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure);
TIM_Cmd(TIM3, ENABLE); //ê1Äü
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);//¸Ä±äÖƶ¨òy½ÅμÄó3éä
//PWMÅäÖÃ
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);//ê1ÄüCCR2éÏμÄԤװÔÔ¼Ä′æÆ÷
TIM_ARRPreloadConfig(TIM3,ENABLE);//ê1ÄüԤװÔÔ¼Ä′æÆ÷
//gpio3õê¼»ˉ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_6);
}
#ifndef _PWM_H
#define _PWM_H
#include "stm32f10x.h"
void PWM_init(u16 per,u16 psc);//PWM3õê¼»ˉ
#endif
|