BASIC4MCU | 질문게시판 | 초기화 어떻게 하나요?
페이지 정보
작성자 끵끵끵 작성일2019-12-15 17:01 조회4,026회 댓글1건본문
#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=>music2
DDRA=0xff; FND2();
SetTone(music2[seq].melody); //노래 2=>music2
Cnt=(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 bps
EICRB=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; }
}
}
리셋버튼 안눌러도 노래가 끝나면 초기화가 되서 그냥 hypertrm에 다른 입력넣으면 그 입력에 맞는 곡이 나오도록 어떻게 하죠?
제 코드대로 하니까 리셋버튼을 누르고 입력을 넣어야지 곡이 나오더라구요 ㅠㅠ
댓글 1
조회수 4,026master님의 댓글
master 작성일
지난번 질문에서 답변의 소스로 구동하면 증상이 어떻게 달라지든가요?
보드를 가지고 있지 않으므로 증상을 볼 수가 없어서 원하는대로 한번에 해드릴 방법이 없습니다.
모든 테스트를 질문하신 분이 하셔서 결과를 피드백해야지 질/답이 이어집니다.