如何提高“压电蜂鸣器”的性能?

问题描述

我看过很多视频,人们在演奏带有压电的不同音乐……我一直在尖叫。我不知道如何开始至少进行几次吱吱声? (使用AT90USB647)

int Start_Beep() {
    
    DDRB = 0b01000000; // DDRB |= (1<<PB6)
    
    for(int i = 0; i < 2000; i++) {
        PORTB ^= 0b01000000;
        char delay_cnt = 0xAA;
        while(delay_cnt){
            --delay_cnt;    
        }
    }
    return 5;
        
}

我正在做一个小记忆游戏,其中使用两个二极管向您显示某种顺序,并且玩家必须使用操纵杆来重复该顺序。

我的整个代码,以防万一...

#define F_cpu 2000000UL
#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

// Global
uint8_t Game[8];
int i;

uint16_t n = 500;



void ADC_Init() {
     // It is not necessary that the ADC uses 10 Bit so should configure
    // left adjustment (ADLAR = 1)
    ADMUX = (1<<MUX1 | 1<<REFS0 | 1<<ADLAR);
    // ADEN --> ADC Enable | ADSC --> ADC Start Conversion | ADPS --> ADC Prescaler Select Bits (16)
    ADCSRA = (1<<ADEN)|(1<<ADSC)|(1<<ADPS2);    
}
void PWM_Init() {
    OCR1A = 200;
    TCCR1A |= (1<<COM1A1);
    TCCR1B |= (1<<WGM13);   
}


unsigned int NewDelayValue() {

    while (ADCSRA & (1<<ADSC));

    unsigned char data = ADCH;

    return 100 * ((data>>5) + 1);


}



void delay(uint16_t time_ms) {
    for(uint16_t i = 0; i < time_ms ; i++) {
        for(uint16_t j = 0; j < 200 ; j++) {
            asm volatile ("nop");
        }
    }
}

int Start_Beep() {
    
    DDRB = 0b01000000; // DDRB |= (1<<PB6)
    
    for(int i = 0; i < 2000; i++) {
        PORTB ^= 0b01000000;
        char delay_cnt = 0xAA;
        while(delay_cnt){
            --delay_cnt;    
        }
    }
    return 5;
        
}

void RandomNumber() {
    
    srand((unsigned int)time(NULL));
    for(unsigned int i = 0; i < sizeof(Game)/sizeof(Game[0]); i++) {
        int v = rand() % 2;
        Game[i] = v;
    }
}

void PlayDemo() {
    Start_Beep();
    for(i = 0; i < 8; i++) {
        if(Game[i] == 1) {
            PORTA = 0x80;
            delay(NewDelayValue());
            PORTA = 0x00;
            delay(NewDelayValue());
            //return;
        }
        else if (Game[i] == 0) {
            PORTA = 0x01;
            delay(NewDelayValue());
            PORTA = 0x00;
            delay(NewDelayValue());
            //return;
        }
        else {
            PORTA = 0x00;
        }
    }
}


uint8_t isRightButtonpressed(uint8_t PortValue) {
    if(PortValue & 0x08) {
        return 0;
    } else {
        return 1;
    }
}

uint8_t isLeftButtonpressed(uint8_t PortValue) {
    if(PortValue & 0x20) {
        return 0;
    } else {
        return 1;
    }
}

uint8_t isButtonpressed(uint8_t PortValue) {
    return isLeftButtonpressed(PortValue) || isRightButtonpressed(PortValue);
}

enum Button{
    LEFT = 1,RIGHT= 0    
};


enum Button waitForPress() {

    uint8_t x = PINF;

    while(!isButtonpressed(x)) {
        x = PINF;
    }
    // Debouncing
    delay(50); 
    while(isButtonpressed(PINF)) {
    }

    if(isRightButtonpressed(x)) {
        return RIGHT;   
    } else {
        return LEFT;
    }
}

int main(void) {
    MCUCR |= 0x80;
    MCUCR |= 0x80;
    DDRA = 0xFF;
    DDRB = 0xFF;
    // 0x20 / 0x08 --> Joystick
    PORTF = 0x28;
    
    ADC_Init();
    
    
    PWM_Init();
    
    
    
    RandomNumber();
    PlayDemo();
    
    while(1)
    {
        /*
        if(isRightButtonpressed(PINF)) {
            PORTA = 0x01;
        } else {
            PORTA = 0x00;   
        }
        
        if(isLeftButtonpressed(PINF)) {
            PORTA = 0x80;
        } else {
            PORTA = 0x00;   
        }
        */
        for(uint8_t index = 0; index < 8; index++) {
            enum Button btn = waitForPress();
            if(btn == LEFT && Game[index] == LEFT) {
                // Good
                PORTA = 0xFF;
                delay(n);
                PORTA = 0x00;
                //PlayDemo();
            } else if (btn == RIGHT && Game[index] == RIGHT) {
                // Good
                PORTA = 0xFF;
                delay(n);
                PORTA = 0x00;
                //PlayDemo();
            } else if (btn == RIGHT && Game[index] != RIGHT) {
                PORTA = 0xFF;
                break;
            } else if (btn == LEFT && Game[index] != LEFT) {
                PORTA = 0xFF;
                break;
            }
        }
    }
}

解决方法

相应地更改delay_cnt:翻转PORTB ^= 0b01000000;状态之间的更多延迟意味着较低的频率,较少的延迟意味着较高的频率。

不过,通常情况下,取决于您的芯片,可能没有那么密集的声音播放方式,例如某些PWM硬件。

,

似乎delay_cnt的初始化存在问题,因为它的类型为char。 char类型的范围是-128到+127,因此值0xAA(十进制170)将变为负数。因此,尝试将delay_cnt更改为键入unsigned char:

int Start_Beep() {
    
    DDRB |= (1<<PB6)
    
    for(unsigned int i = 0; i < 2000; i++) {
        PORTB ^= (1<<PB6);

        unsigned char delay_cnt = 0xAA;

        while(delay_cnt){
            --delay_cnt;
        }
    }
    return 5;       
}

让我知道它是否有效...