如何消除PB5/6的时间限制?

#include "stm32f10x.h" #include "buzzer.h" void Delay_ms(uint32_t ms) { volatile uint32_t i, j; for (i = 0; i < ms; i++) { for (j = 0; j < 1000; j++); } } int main(void) { // ????? SystemInit(); Buzzer_Init(); while (1) { // ?????? for (uint16_t freq = 200; freq <= 2000; freq += 100) { Change_Tone(freq); Delay_ms(300000); } for (uint16_t freq = 2000; freq >= 200; freq -= 100) { Change_Tone(freq); Delay_ms(300000); } // ???? Play_Song(birthday_notes, birthday_durations, 25); Delay_ms(300000); // ???? } return 0; } #ifndef __BUZZER_H #define __BUZZER_H #include "stm32f10x.h" // ?????(Hz) extern const uint16_t tone_freq[]; // ???? extern const uint8_t birthday_notes[]; extern const uint8_t birthday_durations[]; // ???? void Buzzer_Init(void); void Change_Tone(uint16_t freq); void Play_Song(const uint8_t* notes, const uint8_t* durations, uint8_t len); #endif /* __BUZZER_H */ #include "buzzer.h" // C??????? const uint16_t tone_freq[] = { 0, 262, 294, 330, 349, 392, 440, 494, 523 }; // ??????? const uint8_t birthday_notes[] = { 1,1,2,1,5,4, 1,1,2,1,6,5, 1,1,8,6,4,3,2, 7,7,6,4,5,4 }; const uint8_t birthday_durations[] = { 2,2,2,2,4,4, 2,2,2,2,4,4, 2,2,4,4,4,4,2, 2,2,4,4,4,4 }; // ????(??SysTick????????) static void Delay_ms(uint32_t ms) { uint32_t i; for (i = 0; i < ms; i++) { __asm("nop"); __asm("nop"); __asm("nop"); } } void Buzzer_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; // ?????? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // ??PB10??????? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); // ??????????200Hz-2000Hz???? uint16_t prescaler = 72 - 1; // 72MHz / 72 = 1MHz???? uint16_t max_arr = 1000000 / 200 - 1; // 200Hz???ARR?(4999) // ?????? TIM_TimeBaseStructure.TIM_Period = max_arr; TIM_TimeBaseStructure.TIM_Prescaler = prescaler; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // ??PWM?? TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = max_arr / 2; // 50%?????? TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC3Init(TIM2, &TIM_OCInitStructure); TIM_Cmd(TIM2, ENABLE); } // ?????? #define MIN_FREQ 100 // ??????(Hz) #define MAX_FREQ 10000 // ??????(Hz) void Change_Tone(uint16_t freq) { // ?????? if (freq == 0) { TIM_SetCompare3(TIM2, 0); // ??PWM?? return; } // ?????? if (freq < MIN_FREQ) { freq = MIN_FREQ; // ?????? // ????????? } else if (freq > MAX_FREQ) { freq = MAX_FREQ; // ?????? // ????????? } // ???????????? uint32_t arr_calc = 1000000 / freq; // 1MHz / ?? // ?????? if (arr_calc > 65535) { arr_calc = 65535; // ????? // ????????? } uint16_t arr = (uint16_t)arr_calc; uint16_t compare = arr / 2; // 50%??? // ??????? TIM_SetAutoreload(TIM2, arr); TIM_SetCompare3(TIM2, compare); } // ???? void Play_Song(const uint8_t* notes, const uint8_t* durations, uint8_t len) { for (uint8_t i = 0; i < len; i++) { uint16_t freq = tone_freq[notes[i]]; Change_Tone(freq); Delay_ms(durations[i] * 500); // ?????? Change_Tone(0); // ???? Delay_ms(1000); } } 为什么蜂鸣器不发出声响,可以正常烧录,也没有报错
最新发布
05-28
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值