#include "project.h" static int period; static int place; int ringing; #define C6 1047 #define E6 1319 void buzzer_on(void) { gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_TIM1_CH1); gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_TIM1_CH1N); timer_reset(TIM1); timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, TIM_CR1_DIR_UP); timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM2); timer_enable_oc_output(TIM1, TIM_OC1); timer_enable_oc_output(TIM1, TIM_OC1N); timer_enable_break_main_output(TIM1); timer_set_oc_value(TIM1, TIM_OC1, period/2); timer_set_period(TIM1, period); timer_enable_counter(TIM1); } void buzzer_off(void) { gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO8); gpio_clear(GPIOA,GPIO8); gpio_set_mode (GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO13); gpio_clear(GPIOB,GPIO13); timer_disable_counter(TIM1); } void buzzer_set_freq(int hz) { period=(48000000/4)/hz; timer_set_oc_value(TIM1, TIM_OC1, period/2); timer_set_period(TIM1, period); } void buzzer_init(void) { /*buzzer*/ buzzer_off(); buzzer_set_freq(440); } void ring_tick(void) { static int t; if (!ringing) return; ringing--; if (!ringing) { place=0; buzzer_off(); return; } t++; if (t<50) return; t=0; switch(place){ case 1: case 3: case 5: case 7: case 13: case 15: case 17: case 19: place++; buzzer_set_freq(C6); break; case 0: case 12: place++; buzzer_set_freq(E6); buzzer_on(); break; case 2: case 4: case 6: case 14: case 16: case 18: place++; buzzer_set_freq(E6); break; case 8: case 20: place++; buzzer_off(); break; default: place++; break; case 59: place=0; } } void ring_off(void) { place=0; ringing=0; buzzer_off(); } void ring(int l) { //mb(); ringing=l; }