您的位置 首页 IOT

stm32F407(CORTEX-M4)之RCC装备与TIM2的PWM

之前我已经发过了关于TIM2的PWM,但是没有自己配置RCC时钟,用到的系统时钟、HCLK、PCLK1与PCLK2都是系统启动代码里默认设置的,但是由于应

之前我现已发过了关于TIM2的PWM,可是没有自己装备RCC时钟,用到的体系时钟、HCLK、PCLK1与PCLK2都是体系发动代码里默许设置的,可是因为运用场合不同,默许的设置或许不满足用户需求,这时便需求手动装备。仍是相同的问题,M4的材料太少,我只能参阅函数的英文阐明(我现已习气看英文了,呵呵)来自己装备,不敢独享,现共享如下:

(我的外部时钟为25MHz)

#include

static __IO uint32_t TimingDelay;//__IO为volatile的宏界说
int8_t LED_Flag = 1;//LED灯翻转标志位

void RCC_Config(void);
void GPIO_Config(void);
void TIM_Config(void);
void NVIC_Config(void);
void Delay(__IO uint32_t nTime);

main ()
{
RCC_Config();
GPIO_Config();
TIM_Config();
NVIC_Config();

RCC_MCO1Config(RCC_MCO1Source_PLLCLK, RCC_MCO1Div_1);

if (SysTick_Config(SystemCoreClock / 1000))
{

while (1);
}
while (1)
{

if (LED_Flag != 1)
{
GPIO_SetBits(GPIOG, GPIO_Pin_6); //setbits使能IO,当时下指输出(此刻为灭)
}
else
{
GPIO_ResetBits(GPIOG, GPIO_Pin_6); //Resetbits屏蔽IO,当时下指不输出(此刻为亮)
}
}
}

void RCC_Config(void)
{
RCC_DeInit(); //RCC寄存器初始化
RCC_HSEConfig(RCC_HSE_ON); //运用外部时钟
if (RCC_WaitForHSEStartUp() == SUCCESS)//等候外部时钟发动
{
RCC_PLLCmd(DISABLE); //装备PLL前应先封闭主PLL
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //挑选PLL时钟为体系时钟
RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK(AHB)时钟为体系时钟1分频
RCC_PCLK1Config(RCC_HCLK_Div4); //PCLK1(APB1)时钟为HCLK时钟8分频,则TIM2时钟为HCLK时钟4分频
RCC_PCLK2Config(RCC_HCLK_Div2); //PCLK2(APB2)时钟为HCLK时钟2分频
RCC_PLLConfig(RCC_PLLSource_HSE, 25, 336, 2, 7); //PLL时钟装备,公式详见‘system_stm43f4xx.c’ Line149
RCC_PLLCmd(ENABLE);//PLL时钟敞开
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } //等候PLL时钟准备好
}
}


void GPIO_Config(void)
{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOA, ENABLE);

GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //指定第六引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //形式为输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //频率为快速
GPIO_Init(GPIOG, &GPIO_InitStructure); //调用IO初始化函数

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //指定榜首引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //形式有必要为复用!
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //频率为快速
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉与否对PWM发生无影响
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);//复用GPIOA_Pin1为TIM2_Ch2

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO);
}

void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_DeInit(TIM2);//初始化TIM2寄存器

TIM_TimeBaseStructure.TIM_Period = 10000-1; //查数据手册可知,TIM2与TIM5为32位主动装载

TIM_TimeBaseStructure.TIM_Prescaler = 8400-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

TIM_OCStructInit(&TIM_OCInitStructure); //填入缺省值,其间后几项只对TIM1和TIM8有用
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1为正常占空比形式,PWM2为反极性形式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 2000; //输入CCR(占空比数值)
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //High为占空比高极性,此刻占空比为20%;Low则为反极性,占空比为80%
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);//CCR主动装载默许也是翻开的
TIM_ARRPreloadConfig(TIM2, ENABLE); //ARR主动装载默许是翻开的,能够不设置

TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2, ENABLE); //使能TIM2定时器
}

void NVIC_Config(void)
{

NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //中止优先级分组为1
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;//嵌套中止通道为TIM2
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //呼应优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中止
NVIC_Init(&NVIC_InitStructure);
}

void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while (TimingDelay != 0);
}

void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay–;
}
}

中止服务函数:

void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
LED_Flag= ~LED_Flag;


}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/iot/259768.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部