AVR > 스위치 8개 피아노 - AVRStudio

TODAY7,213 TOTAL8,132,029
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | AVR | 타이머 | 스위치 8개 피아노 - AVRStudio

페이지 정보

작성자 키트 작성일2017-08-29 09:40 조회5,845회 댓글0건

본문

// Sound Test ===================
#include <avr/io.h>
#include <util/delay.h>
// pitch
#define Do   0
#define DoS  1
#define Re   2
#define ReS  3
#define Mi   4
#define Fa   5
#define FaS  6
#define Sol  7
#define SolS 8
#define La   9
#define LaS  10
#define Ti   11


int ocr1a[7][12]={
    {30577,28860,27240,25711,24268,22906,21620,20407,19261,18180,17160,16197},
    {15288,14429,13619,12855,12133,11452,10809,10203, 9630, 9089, 8579,64791},
    {61155,57722,54482,51424,48538,45814,43242,40815,38524,36362,34321,32395},
    {30577,28860,27240,25711,24268,22906,21620,20407,19261,18180,17160,16197},
    {15288,14429,13619,12855,12133,11452,10809,10203, 9630, 9089, 8579, 8098},
    { 7643, 7214, 6809, 6427, 6066, 5725, 5404, 5101, 4814, 4544, 4289, 4048},
    { 3606, 3404, 3213, 3032, 2862, 2701, 2550, 2406, 2271, 2144, 2023, 1910}
// , 1802, 1701, 1606, 1515, 1430
};
//

int cnt=0;
// 
void sound(int octave, int pitch){
    if(octave>6)octave=6;
    if(pitch>11)pitch=11;
    TCCR1B&=~7; if((octave*12+pitch)<23)TCCR1B|=2; else TCCR1B|=1;
    OCR1A=ocr1a[octave][pitch]; 
    cnt=0;
}

int main(void){
    DDRB=0x20;  //포트 B.5 펄스출력

    PORTA=0xFF; //스위치 포트 내부 풀업 저항 사용
    TCCR1A=0x40; TCCR1B=8; TCCR1C=0x80; OCR1A=0;
    while(1){
        if(!(PINA&0x01))sound(4, Do);
        if(!(PINA&0x02))sound(4, Re);
        if(!(PINA&0x04))sound(4, Mi);
        if(!(PINA&0x08))sound(4, Fa);
        if(!(PINA&0x10))sound(4,Sol);
        if(!(PINA&0x20))sound(4, La); // 440Hz
        if(!(PINA&0x40))sound(4, Ti);
        if(!(PINA&0x80))sound(5, Do);
        if(PINA==0xFF){ _delay_ms(10); if(++cnt>200)TCCR1B=8; } // 2초이상 입력 없으면 off
    }
}

댓글 0

조회수 5,845

등록된 댓글이 없습니다.

AVRHOME > AVR > 전체 목록

게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기