BASIC4MCU | AVR | 타이머 | 스위치 8개 피아노-코드비젼-프로테우스
페이지 정보
작성자 키트 작성일2017-08-29 09:43 조회5,093회 댓글0건첨부파일
본문
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 // 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 11unsigned 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};//void sound(char octave, char pitch){TCCR1B=8;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];}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); } // 440Hzif(!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)로 변경한 코드입니다.(테스트 해보지 않음)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 // 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 11unsigned int ocr3c[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};//void sound(char octave, char pitch){TCCR1B=8;if(octave>6)octave=6;if(pitch>11)pitch=11;TCCR3B&=~7; if((octave*12+pitch)<23)TCCR3B|=2; else 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); } // 440Hzif(!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등록된 댓글이 없습니다.