新闻中心

EEPW首页>嵌入式系统>设计应用> STM32单片机简易定时器PWM输出

STM32单片机简易定时器PWM输出

作者: 时间:2016-11-30 来源:网络 收藏

void led_init(void);

#endif

//------------------------主函数-----------test.c-----------
#include
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
#include "time.h"

char pwm; //pwm增减量
char cnt; //pwm时间变量

//-----------------------pwm增量按键----------------
void key1_scan()
{
static u8 i,j;
if(key1==0)
{
if(i==0)
{
j++;
if(j>3)
{
i=1;j=0;
pwm+=5;
if(pwm>100)pwm=0;
}
}
}
else
{
i=j=0;
}
}

//--------------------pwm减量按键--------------------
void key2_scan()
{
static u8 i,j;
if(key2==0)
{
if(i==0)
{
j++;
if(j>3)
{
i=1;j=0;
pwm-=5;
if(pwm<10)pwm=0;
}
}
}
else
{
i=j=0;
}
}

void TIM3_IRQHandler(void) //定时器2中断函数
{
if(TIM3->SR&0x0001)
{
cnt++;
if(cnt>100)cnt=0;
if(cnt>pwm) //LED0作为pwm 输出指示
{
LED0=0;
}
else
{
LED0=1;
}
}
TIM3->SR&=~(1<<0); //清除中断标志位
}


int main(void)
{
u16 i,t;
Stm32_Clock_Init(9);
delay_init(72);
uart_init(72,9600);
led_init();
key_init();
time_init(3600,1); //产生10K频率 pwm只是~~100HZ 由于100分频
while(1)
{
key1_scan();
key2_scan();
t++;
if(t>60000)
{
i++;
if(i>11)
{
LED1=!LED1; //系统运行指示约1秒闪烁一次
i=0;
}
t=0;
}

}
}

//--------------------------------关于定时器溢出时间计算--------------------------
1)TIM3时钟使能。
这里我们通过APB1ENR的第1位来设置TIM3的时钟,因为Stm32_Clock_Init函数里面把APB1的分频设置为2了,所以我们的TIM3时钟就是APB1时钟的2被,等于系统时钟。(72M)

Tout= (arr*(psc+1))/Tclk;
其中:
Tclk:TIM3的输入时钟频率(单位为Khz)。
Tout:TIM3溢出时间(单位为ms)。

Tout=(3600*(1+1))/72M=0.1ms

//------------------------------系统时钟初始化函数对于定时器TIM3时钟简易分析-----------------
void Stm32_Clock_Init(u8 PLL)
{
unsigned char temp=0;
MYRCC_DeInit(); //复位并配置向量表
RCC->CR|=0x00010000; //外部高速时钟使能HSEON
while(!(RCC->CR>>17));//等待外部时钟就绪
RCC->CFGR=0X00000400; //APB1=DIV2;APB2=DIV1;AHB=DIV1;
PLL-=2;//抵消2个单位
RCC->CFGR|=PLL<<18; //设置PLL值 2~16
RCC->CFGR|=1<<16; //PLLSRC ON
FLASH->ACR|=0x32; //FLASH 2个延时周期

RCC->CR|=0x01000000; //PLLON
while(!(RCC->CR>>25));//等待PLL锁定
RCC->CFGR|=0x00000002;//PLL作为系统时钟
while(temp!=0x02) //等待PLL作为系统时钟设置成功
{
temp=RCC->CFGR>>2;
temp&=0x03;
}
}


上一页 1 2 下一页

关键词:STM32单片机定时器PWM输

评论


相关推荐

技术专区

关闭