BASIC4MCU | 질문게시판 | 답변 : atmega 128 깔끔하게 정리 부탁드려요 ㅠㅠ
페이지 정보
작성자 master 작성일2019-12-10 17:43 조회9,136회 댓글3건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=11120
작성일
작성일
본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2019-12-10 오후 5:43:25// by Ok-Hyun Park//#include <io.h>#include <delay.h>//unsigned char tccr0_set[24]={ 31,30,30,30,30,30,30,30,29,29,29,29,29,29,29,28,28,28,28,28,28,28,28,27};unsigned char ocr0_set[24]={ 70,252,237,211,188,177,158,141,252,237,211,188,177,158,141,252,237,211,188,177,158,141,252,237};unsigned char LED[24]={0x04,0x02,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x80};//typedef enum{la_0,si_0,do_1,re_1,mi_1,fa_1,sol_1,la_1,si_1,do_2,re_2,mi_2,fa_2,sol_2,la_2,si_2,do_3,re_3,mi_3,fa_3,sol_3,ra_3,si_3,do_4,F} melody_code;//typedef struct{melody_code melody;float rhythm;}MUSIC_CODE;//#define LENGTH1 42//const MUSIC_CODE music1[]={{ do_2,1},{ do_2,1},{sol_2,1},{sol_2,1},{ la_2,1},{ la_2,1},{sol_2,2},//반짝반짝 작은별{ fa_2,1},{ fa_2,1},{ mi_2,1},{ mi_2,1},{ re_2,1},{ re_2,1},{ do_2,2},//아름답게 비치네{sol_2,1},{sol_2,1},{ fa_2,1},{ fa_2,1},{ mi_2,1},{ mi_2,1},{ re_2,2},//동쪽 하늘 에서도{sol_2,1},{sol_2,1},{ fa_2,1},{ fa_2,1},{ mi_2,1},{ mi_2,1},{ re_2,2},//서쪽 하늘 에서도{ do_2,1},{ do_2,1},{sol_2,1},{sol_2,1},{ la_2,1},{ la_2,1},{sol_2,2},//반짝반짝 작은별{ fa_2,1},{ fa_2,1},{ mi_2,1},{ mi_2,1},{ re_2,1},{ re_2,1},{ do_2,2},{ 0,0} //아름답게 비치네};//#define LENGTH2 49//const MUSIC_CODE music2[]={{ do_2,1},{ do_2,0.5},{ do_2,0.5},{ do_2,1},{ do_2,1}, //곰세마리가{ mi_2,1},{ sol_2,0.5},{ sol_2,0.5},{ mi_2,1},{ do_2,1}, //한 집에 있어{sol_2,0.5},{ sol_2,0.5},{ mi_2,1},{ sol_2,0.5},{ sol_2,0.5},{ mi_2,1},{ do_2,1},{ do_2,1},{ do_2,2}, // 아빠곰 엄마곰 애기곰{sol_2,1},{ sol_2,1},{ mi_2,1},{ do_2,1},{ sol_2,1},{ sol_2,1},{ sol_2,2}, //아빠곰은 뚱뚱해{sol_2,1},{ sol_2,1},{ mi_2,1},{ do_2,1},{ sol_2,1},{ sol_2,1},{ sol_2,2}, //엄마곰은 날씬해{sol_2,1},{ sol_2,1},{ mi_2,1},{ do_2,1},{ sol_2,0.5},{ sol_2,0.5},{ sol_2,0.5},{ la_2,0.5},{ sol_2,2}, //애기곰은 너무귀여워{ do_3,1},{ sol_2,1},{ do_3,1},{ sol_2,1},{ mi_2,1},{ re_2,1},{ do_2,2},{ 0,0} //으쓱 으쓱 잘한다};//unsigned char Time2Flag=0;unsigned int Cnt=0,seq=0;//char USART0_GetChar(void) { while(!(UCSR0A&0x80)); return(UDR0); }void USART0_PutChar(char c){ while(!(UCSR0A&0x20)); UDR0=c; }void USART0_PutStr(char *s){ while(*s)USART0_PutChar(*s++); }//void SetTone(unsigned char n){ TCCR0=tccr0_set[n]; OCR0=ocr0_set[n]; DDRB.4=1; PORTA=LED[n]; }//void InitTimer2(){ TCCR2=0x0B; OCR2=249; TCNT2=0; TIFR=2; TIMSK=2; }//void FND1(){ PORTG=(int)(music1[seq].rhythm*4); PORTC=0x3f; }void FND2(){ PORTG=(int)(music2[seq].rhythm*4); PORTC=0x3f; }//void Song1(){InitTimer2();while(1){if(Time2Flag){ Time2Flag=0;if(Cnt){if(Cnt==10)DDRB.4=0;else if(Cnt==1){if(music1[seq].rhythm!=0.0){DDRA=0xff; FND1();SetTone(music1[seq].melody);Cnt=(unsigned int)(music1[seq].rhythm*350*60/LENGTH1);++seq;}}}--Cnt;}}}//void Song2(){InitTimer2();while(1){if(Time2Flag){ Time2Flag=0;if(Cnt){if(Cnt==10)DDRB.4=0;else if(Cnt==1){if(music2[seq].rhythm!=0.0){ //노래 2=>music2DDRA=0xff; FND2();SetTone(music2[seq].melody); //노래 2=>music2Cnt=(unsigned int)(music2[seq].rhythm*350*60/LENGTH2); // 노래 2=>music2++seq;}}}--Cnt;}}}//interrupt[TIM2_COMP]void isr_timer2_comp(){ Time2Flag=1; }//void main(){char ch;DDRG=0xff; DDRC=0xff;UCSR0B=0x18; UBRR0H=1; UBRR0L=160; // 2400 bpsEICRB=0x0a; EIMSK=0x30;SREG=0x80;while(1){ch=USART0_GetChar(); USART0_PutChar(ch); USART0_PutStr(" \r");//if(ch==49){ //숫자 1입력Cnt=1; seq=0; Song1();}else if(ch==50){ //숫자 2입력Cnt=1; seq=0; Song2();}else{ Cnt=0; }}}
댓글 3
조회수 9,136끵끵끵님의 댓글
끵끵끵 작성일혹시 곡이 끝나고 초기화 버튼 안누르고 바로 hypertrm에 다른 번호 눌렀을때 곡이 나올 수 있도록 초기화 하는방법 가르쳐 주실수 있나요?
master님의 댓글
master
초기화 버튼 <-- 리셋 버튼을 말하나요?
끵끵끵님의 댓글
끵끵끵
넵 맞습니다.
리셋버튼 안눌러도 그냥 hypertrm에 바로 다른 입력넣으면 곡이 나오도록이요 ㅎㅎ
제 코드대로 하니까 리셋버튼을 누르고 입력을 넣어야지 곡이 나오더라구요 ㅠㅠ