AVR > 스위치 8개 피아노-코드비젼-프로테우스

TODAY1,489 TOTAL2,701,394
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

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


BASIC4MCU | AVR | 타이머 | 스위치 8개 피아노-코드비젼-프로테우스

페이지 정보

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

첨부파일

본문

3660040649_NJfG4Arw_25BF25C025B825A325B025A3.JPG3Ftype3Dw740

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Sound Test ===================
#include <mega128.h>
#include <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
 
 
unsigned int ocr1a[7][12]={
    30577,28860,27240,25711,24268,22906,21620,20407,19261,18180,17160,16197,
    15288,14429,13619,12855,12133,11452,10809,10203963090898579,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,102039630908985798098,
     764372146809642760665725540451014814454442894048,
     360634043213303228622701255024062271214420231910
// , 1802, 1701, 1606, 1515, 1430
};
//
void sound(char octave, char pitch){
    TCCR1B=8
    if(octave>6)octave=6;
    if(pitch>11)pitch=11;
    TCCR1B&=~7if((octave*12+pitch)<23)TCCR1B|=2else TCCR1B|=1;
    OCR1A=ocr1a[octave][pitch];
}
 
void main(void){
    DDRB=0x20;  //포트 B.5 펄스출력
    PORTA=0xFF//스위치 포트 내부 풀업 저항 사용
    TCCR1A=0x40; TCCR1B=8; TCCR1C=0x80; OCR1A=0;
    while(1){
        if(!PINA.0){ sound(4, Do); while(!PINA.0); } 
        if(!PINA.1){ sound(4, Re); while(!PINA.1); }
        if(!PINA.2){ sound(4, Mi); while(!PINA.2); }
        if(!PINA.3){ sound(4, Fa); while(!PINA.3); }
        if(!PINA.4){ sound(4,Sol); while(!PINA.4); }
        if(!PINA.5){ sound(4, La); while(!PINA.5); } // 440Hz
        if(!PINA.6){ sound(4, Ti); while(!PINA.6); }
        if(!PINA.7){ sound(5, Do); while(!PINA.7); }
        if(PINA==0xFF){ TCCR1B=0; } // off
    }
}
cs

//
아래는 PE5(OC3C)로 변경한 코드입니다.
(테스트 해보지 않음)

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Sound Test ===================
#include <mega128.h>
#include <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
 
 
unsigned int ocr3c[7][12]={
    30577,28860,27240,25711,24268,22906,21620,20407,19261,18180,17160,16197,
    15288,14429,13619,12855,12133,11452,10809,10203963090898579,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,102039630908985798098,
     764372146809642760665725540451014814454442894048,
     360634043213303228622701255024062271214420231910
// , 1802, 1701, 1606, 1515, 1430
};
//
void sound(char octave, char pitch){
    TCCR1B=8
    if(octave>6)octave=6;
    if(pitch>11)pitch=11;
    TCCR3B&=~7if((octave*12+pitch)<23)TCCR3B|=2else TCCR3B|=1;
    OCR3CH=ocr3c[octave][pitch]>>8; OCR3CL=ocr3c[octave][pitch]&0xFF;
}
 
void main(void){
    DDRE=0x20;  //포트 E.5 펄스출력
    PORTA=0xFF//스위치 포트 내부 풀업 저항 사용
    TCCR3A=0x04; TCCR3B=8; TCCR3C=0x20;
    while(1){
        if(!PINA.0){ sound(4, Do); while(!PINA.0); } 
        if(!PINA.1){ sound(4, Re); while(!PINA.1); }
        if(!PINA.2){ sound(4, Mi); while(!PINA.2); }
        if(!PINA.3){ sound(4, Fa); while(!PINA.3); }
        if(!PINA.4){ sound(4,Sol); while(!PINA.4); }
        if(!PINA.5){ sound(4, La); while(!PINA.5); } // 440Hz
        if(!PINA.6){ sound(4, Ti); while(!PINA.6); }
        if(!PINA.7){ sound(5, Do); while(!PINA.7); }
        if(PINA==0xFF){ TCCR3B=0; } // off

댓글 0

조회수 5,093

등록된 댓글이 없습니다.

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
모바일버전으로보기