답변 : ATMEGA 128 부저 스위치
페이지 정보
작성자 master 작성일18-12-02 16:41 조회4,357회 댓글0건본문
// MCU BASIC: https://www.basic4mcu.com
// DateTime : 2018-12-02 오후 4:47:12
// by Ok-Hyun Park
안녕하세요!!
버튼을 누르면 부저에서 노래가나오는 코드를 짜고있는데
코드돌리면 부저에서 반복적으로 노래가 계속나오긴 하는데
버튼눌렀을때 노래가나오게 하고싶어서용..
버튼코드를 어떻게 넣을지 모르겠습니다 ㅜㅜ 혹시 알려주실수 있나요,,?
//
#define F_CPU 16000000L
#include <avr/io.h>
#include <util/delay.h>
//
#define D5 587
#define E5 659
#define S_D5 622
#define B4 493
#define C5 523
#define A4 440
//
#define PAUSE 0
#define PRESCALER 1
//
const int melody[]={E5,S_D5,E5,S_D5,E5,B4,D5,C5,A4,PAUSE};
//
void init_music(){
DDRE|=(1<<PE3);
TCCR3A|=_BV(COM3A0);
TCCR3B|=_BV(WGM32)|_BV(CS30);
}
//
void play_music(const int *pMusicNotes){
int note,ocr_value;
while(*pMusicNotes){
note=*pMusicNotes;
pMusicNotes++;
if(note==PAUSE){ OCR3A=0; }
else { ocr_value=F_CPU/2/PRESCALER/note; OCR3A=ocr_value; }
_delay_ms(500);
}
OCR3A=0;
}
//
int main(void){
init_music();
while(1){
if(!(PINA&1)){ play_music(melody); }
}
return 0;
}
댓글 : 0
조회수 : 4,357
등록된 댓글이 없습니다.