各位大神:
小弟用的是
STM32L152 RAT6的板子,主频是32Mhz,用下面的代码想控制蜂鸣器从灭到亮。。但不知道为什么蜂鸣器一点反应都没有。。。代码我是照着原子教你玩STM32 F系列改编过来的。。。他的板子主频是72MHz ...不过这应该不会影响蛮大。。下面是完整代码。请各位大神看看是哪里有错。。。控制的是
tiM2的2通道。。gpio是PA1。。。蜂鸣器是好的。。请用过STM32 L152的大神来指导一下
代码如下:
#include "stm32l1xx.h"
#include "stm32l1xx_conf.h"
typedef unsigned long u32;
typedef unsigned short u16;
typedef unsigned int u8;
static __IO u32 TimingDelay;
/*
BUZZERÖ÷º¯Êý
*/
void beep_Init(void);
void TIM3_PWM_Init(u16 arr,u16 psc);
void SysTick_Init(void);
void Delay_us(__IO u32 nTime);
void TimingDelay_Decrement(void) ;
int main(void)
{
u16 led0pwmval=0;
u8 dir=1;
SysTick_Init();
//NVIC_Configuration(); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
//uart_init(9600); //´®¿Ú³õʼ»¯Îª9600
beep_Init(); //
TIM3_PWM_Init(899,0); //
while(1)
{
Delay_us(10);
if(dir)led0pwmval++;
else led0pwmval--;
if(led0pwmval>300)dir=0;
if(led0pwmval==0)dir=1;
TIM_SetCompare2(TIM3,led0pwmval);
}
}
void beep_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //ʹÄÜPA¶Ë¿ÚʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //beep0-->A.8 ¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // ¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾ÝÉ趨²ÎÊý³õʼ»¯GPIOA.1
// GPIO_SetBits(GPIOA,GPIO_Pin_1); //PA.1 Êä³ö¸ß
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // ¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure); //ÍÆÍìÊä³ö £¬IO¿ÚËÙ¶ÈΪ50MHz
//GPIO_SetBits(GPIOB,GPIO_Pin_5); //PB.5 Êä³ö¸ß
}
//TIM3 PWM²¿·Ö³õʼ»¯
//PWMÊä³ö³õʼ»¯
//arr£º×Ô¶¯ÖØ×°Öµ
//psc£ºÊ±ÖÓÔ¤·ÖƵÊý
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // ¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure);
////³õʼ»¯TIM2
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
//³õʼ»¯TIM2 Channel2 PWMģʽ
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_Cmd(TIM2, ENABLE);
}
void SysTick_Init(void)
{
/* SystemFrequency / 1000 1ms????
* SystemFrequency / 100000 10us????
* SystemFrequency / 1000000 1us????
*/
// if (SysTick_Config(SystemFrequency / 100000)) // ST3.0.0???
if (SysTick_Config(SystemCoreClock / 100000)) // ST3.5.0???
{
/* Capture error */
while (1);
}
// ???????
SysTick->CTRL &= ~ SysTick_CTRL_ENABLE_Msk;
}
void Delay_us(__IO u32 nTime)
{
TimingDelay = nTime;
// ???????
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
while(TimingDelay != 0);
}
/* void SysTick_Handler(void)
{
TimingDelay_Decrement();
} */
/*
* »ñÈ¡½ÚÅĺ¯Êý
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
一周热门 更多>