티스토리 뷰

OS/Banana Pi /BPI-Bit

BPI-Bit : 부저 사용하기

파란크리스마스 2019. 2. 6. 00:23
728x90

출처

BPI-Bit : 부저 사용하기

BPI-Bit에는 사운드 출력을 위한 부저 모듈이 내장 되어 있습니다. 예제를 통해서 간단하게 부저로 사운드를 출력해보겠습니다.

GPIO control of BPI:bit

GPIO control of BPI:bit
Light Sensor(L)GPIO 36Analog Input
Light Sensor(R)GPIO 39Analog Input
Botton AGPIO 35Digital Input
Botton BGPIO 27Digital Input
Temperature SensorGPIO 34Analog Input
BuzzerGPIO 25PWM(Digital Output) / Analog Output
RGB_LEDGPIO 4Digital Output
MPU9250_SCLGPIO 22Digital Output
MPU9250_SDAGPIO 21Digital Output
MPU9250_INTGPIO 16Digital Input
R_LED(SPI_SCK)GPIO 18Digital Output

예제 소스

int adTestFreq = 0;
int BuzzerTestFreq = 0;
int brightness = 0;
int fadeAmount = 5;

//buzzer config
#define Buzzer 25
#define LEDC_CHANNEL_0 0
#define LEDC_TIMER_13_BIT 13
#define LEDC_BASE_FREQ 450
#define bout 3

/*****************************************************************
 * Function      :ledcAnalogWrite                            *
 * Input Parameter :channel / value                           *
 * Output Parameter:None                                      *
 * Features     :Match value to channel and convert to duty   *
 ****************************************************************/
void ledcAnalogWrite(uint8_t channel, uint32_t value)
{
  // calculate duty, 8191 from 2 ^ 13 - 1 (2 ^ LEDC_TIMER_13_BIT - 1)
  //value_MAX = 255
  uint32_t duty = (8191 / 255) * value;
  // write duty to LEDC
  ledcWrite(channel, duty);
}

void setup() {
  Serial.begin(115200); //Serial Port Config 115200-8-N-1
  ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
  ledcAttachPin(Buzzer, LEDC_CHANNEL_0);
}

void loop() {
  /*****************************
   ***Function:Buzzer Test   ***
   ****************************/
  Serial.println();
  Serial.println("Buzzer Test!!!");
  Serial.println("Buzzer is fading");
  for (BuzzerTestFreq = 0; BuzzerTestFreq < bout; BuzzerTestFreq++)
  {
      for (brightness = 0; brightness < 255; brightness++)
      {
          Serial.print(".");
          // set the brightness on LEDC channel 0
          ledcAnalogWrite(LEDC_CHANNEL_0, brightness);
          // change the brightness for next time through the loop:
          brightness = brightness + fadeAmount;
          // reverse the direction of the fading at the ends of the fade:
          if (brightness <= 0 || brightness >= 255)
          {
              fadeAmount = -fadeAmount;
          }
          // wait for 30 milliseconds to see the dimming effect
          delay(30);
      }
      Serial.println();
      ledcAnalogWrite(LEDC_CHANNEL_0, 0);
  }
  delay(1000);
}

실행 결과

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함