本帖最后由 sqqdfny 于 2015-10-22 15:14 编辑
同一个板子,之前用STM32F103C8T6 定时器 TIM2 CH(PA1)输出 PWM,换用GD之后,没输出了。软件没做任何更改,其它功能都是OK的。
static u32 buzzerOnCount = 0;
static u32 buzzerOnTimeTick = 0;
static _bool buzzerOnFlag = 0;
//===================================================================
//蜂鸣器
void BUZZER_ON()
{
TIM2->CR1 |= BIT0; //start
buzzerOnFlag = 1;
}
void BUZZER_OFF()
{
TIM2->CR1 &= ~BIT0; //stop
TIM2->CNT = 0;
buzzerOnFlag = 0;
}
#define GetBeerStatus() (buzzerOnFlag > 0 ? 1 : 0)
//蜂鸣器控制
void BuzzerTimeHook(void)
{
if(buzzerOnCount)
{
if(buzzerOnTimeTick)
{
buzzerOnTimeTick --;
return;
}
if(!GetBeerStatus())
{
BUZZER_ON();
buzzerOnTimeTick = SYSTEM_TICK / 10;
}
else
{
BUZZER_OFF();
buzzerOnCount -- ;
buzzerOnTimeTick = (SYSTEM_TICK / 20);
}
}
}
void BuzzerOn(u32 cnt)
{
buzzerOnCount = (cnt > 10) ? 10 : cnt;
}
void BuzzerInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM2->PSC = 23; //1us
TIM2->ARR = 250; //250us
TIM2->CR1 = (0 << 4) | (1 << 7); //
TIM2->CR2 = 0;
TIM2->SMCR = 0; //
TIM2->DIER = 0; //
TIM2->CCER = 0;
TIM2->CCMR1 = (1<<10) | (7<<12);
TIM2->CCMR2 = 0;
TIM2->CCER = (1<<4) | (0<<5);
TIM2->CCR2 = 125;
}
//=================================================================
void BuzzerEnterSleep(void)
{
TIM2->CR1 &= ~BIT0; //stop
TIM2->CNT = 0;
buzzerOnFlag = 0;
}
//=================================================================
//end files
|