本帖最后由 micropoints 于 2019-1-1 18:09 编辑
#define MAXPWMFREQ 8000000
#define MINHZ 100
#define UNIT 100
#define MINUNIT 1
#define PWM_CHANNEL_ENABLE PWM2_P05_OUTPUT_ENABLE
#define PWM_CHANNEL_INVERSE PWM2_OUTPUT_INVERSE
#define PWM_CHANNEL_H PWM2H
#define PWM_CHANNEL_L PWM2L
void beep_init(void) {
PWM_CHANNEL_ENABLE;
PWM_IMDEPENDENT_MODE;
PWM_CLOCK_DIV_2;
PWM_CHANNEL_INVERSE;
}
bool beep_work_freq(uint8_t freq,uint8_t duty) {
if((freq >= (MINUNIT + 1) && freq <= UNIT) && (duty >= MINUNIT && duty <= UNIT)) {
uint16_t PwmFreq = ((MAXPWMFREQ / (freq * MINHZ)) - 1);
uint16_t PwmDuty = (PwmFreq / UNIT) * duty;
PWMPH = ((PwmFreq >> 8) & 0xFF);
PWMPL = (PwmFreq & 0xFF);
PWM_CHANNEL_H = ((PwmDuty >> 8) & 0xFF);
PWM_CHANNEL_L = (PwmDuty & 0xFF);
return true;
}
return false;
}
void beep_work_start(void) {
set_LOAD;
set_PWMRUN;
}
void beep_work_stop(void) {
set_CLRPWM;
}
void beep_2K_50_work(void) {
if(PWMRUN) {
beep_work_stop();
}
beep_work_freq(20,50);
beep_work_start();
}
void beep_2K_70_work(void) {
if(PWMRUN) {
beep_work_stop();
}
beep_work_freq(20,70);
beep_work_start();
}
刚刚使用新唐的003 ,写了个蜂鸣器的测试代码,跑了一下,蜂鸣器不响应,各位大大帮忙看看这个pwm的配置对不对?手头上没有示波器量不了输出频率。。 |