BASIC4MCU | 질문게시판 | 소스수정도와주세요...ㅠㅠ
페이지 정보
작성자 꾸꾹 작성일2018-12-11 16:22 조회8,420회 댓글1건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=6719
본문
시리얼 통신을 이용해서 숫자 맞추기 게임 소스를 설계했는데 하이퍼터미널에서 나오는 "입력한 숫자보다 작습니다" "입력한 숫자보다 큽니다"를하이퍼터미널을 사용하지 않고 LCD판에 나타내고 싶습니다.수정해주실 수 있을까요??.....정말정말 간절합니다......ㅠㅠ#include <mega128.h>#include <delay.h>#include <stdio.h>#include <stdlib.h>//#define SEG PORTA //Port A 7-SEG data로 사용#define Q PORTB#define KEY_C PORTC //Port C Key Matrix 입력신호#define KEY_L PINC //Port C Key Matrix 출력신호//char count=0,up=0,user_number[6]={0,},str[100];int time=10,com_number,life=8,user_num=0,seed=0;//void SegDisplay(unsigned int cmd){unsigned char fnd[17]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x98,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x7f};SEG=fnd[cmd];}//char Getch(){ while((UCSR0A&0x80)==0); return UDR0; }void Putch(char d){ while((UCSR0A&0x20)==0); UDR0=d; }void PutSt(char *s){ while(*s)Putch(*s++); }//void light(int l){switch(l){case 8: PORTE=0b11110000; PORTG=0b00001111; break;case 7: PORTE=0b01110000; PORTG=0b00001111; break;case 6: PORTE=0b00110000; PORTG=0b00001111; break;case 5: PORTE=0b00010000; PORTG=0b00001111; break;case 4: PORTE=0b00000000; PORTG=0b00001111; break;case 3: PORTE=0b00000000; PORTG=0b00000111; break;case 2: PORTE=0b00000000; PORTG=0b00000011; break;case 1: PORTE=0b00000000; PORTG=0b00000001; break;}}//interrupt[EXT_INT0]void ext_int0_isr(void){PutSt("\n\n\r");PutSt("=========================\n\r");PutSt(" Game Start \n\r");PutSt("=========================\n\r");PutSt("0~100 사이의 수를 맞춰보세요!\n\r");life=8; time=10;sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str); delay_ms(500);TCCR0=0x07; srand(seed); com_number=rand()%100+1; light(life);}//interrupt[TIM0_OVF]void timer0_ovf_isr(void){if(++count==125){ count=0;SegDisplay(--time);if(time==0){ time=10;PutSt("\n\rwarning!!입력에 실패했습니다!!\n\r");up=0;life--;if(life>0){ light(life); sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str); }else{PutSt("당신은 패배했습니다.\n\r");sprintf(str,"아쉽네요.정답은%d입니다.\n\r",com_number); PutSt(str);TCCR0=0; // 타이머 정지PutSt("=========================\n\r");PutSt(" Game End \n\r");PutSt("=========================\n\n\r");}}}TCNT0=131;}//void main(void){unsigned char buff=0;DDRA=0xff;DDRB=0x0f;PORTC=0x0F; DDRC=0x0F; //(c0-c3: 출력,c4-c7: 입력)DDRE=0xff;DDRG=0xff;SegDisplay(0); //7-Segment에 '0'출력Q=0x0e; // 모든 7-Seg 선택EICRA=0x0A; EIMSK=0x01; // INT0UCSR0B=0x18; UBRR0L=0x08; // 115200 bpsTCNT0=131; TIMSK=0x01;SREG=0x80;while(1){seed++;KEY_C=0xFe; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 1110 출력 3 7 B C line Enableswitch(buff>>4){case 0x0e: Putch('0'); user_number[up++]='0'; delay_ms(200); break; // 1110case 0x0d: Putch('4'); user_number[up++]='4'; delay_ms(200); break; // 1101case 0x0b: Putch('8'); user_number[up++]='8'; delay_ms(200); break; //1011case 0x07: break; //0111}delay_ms(1); KEY_C=0xFC; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 1101 출력 1 5 9 D line Enableswitch(buff>>4){case 0x0e: Putch('1'); user_number[up++]='1'; delay_ms(200); break; // 1110case 0x0d: Putch('5'); user_number[up++]='5'; delay_ms(200); break; // 1101case 0x0b: Putch('9'); user_number[up++]='9'; delay_ms(200); break; //1011case 0x07: break; //0111}delay_ms(1); KEY_C=0xFB; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 1011 출력 2 6 A E line Enableswitch(buff>>4){case 0x0e: Putch('2'); user_number[up++]='2'; delay_ms(200); break; // 1110case 0x0d: Putch('6'); user_number[up++]='6'; delay_ms(200); break; // 1101case 0x0b:{ //1011// 입력 완료PutSt("\n\r");count=0; time=0; user_number[up++]='\0'; life--;user_num=atoi(user_number);if(life>0){if(user_num==com_number){ PutSt("당신이 이겼습니다.축하합니다!\n\r"); }else if(user_num>com_number){PutSt("정답보다 입력한 숫자가 큽니다.\n\r");sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str);light(life); time=10;}else{PutSt("정답보다 입력한 숫자가 작습니다.\n\r");sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str);light(life); time=10;}}else{PutSt("당신은 패배했습니다.\n\r");TCCR0=0; // 타이머 정지sprintf(str,"아쉽네요.정답은%d입니다.\n\r",com_number); PutSt(str);PutSt("=========================\n\r");PutSt(" Game End \n\r");PutSt("=========================\n\n\r");}up=0; delay_ms(200);break;}case 0x07: break; //0111}delay_ms(1); KEY_C=0xF7; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 0111 출력 3 7 B F line Enableswitch(buff>>4){case 0x0e: Putch('3'); user_number[up++]='3'; delay_ms(200); break; // 1110case 0x0d: Putch('7'); user_number[up++]='7'; delay_ms(200); break; // 1101case 0x0b: break; //1011case 0x07: break; //0111}delay_ms(1);}}#include <mega128.h>#include <delay.h>#include <stdio.h>#include <stdlib.h>//#define SEG PORTA //Port A 7-SEG data로 사용#define Q PORTB#define KEY_C PORTC //Port C Key Matrix 입력신호#define KEY_L PINC //Port C Key Matrix 출력신호//char count=0,up=0,user_number[6]={0,},str[100];int time=10,com_number,life=8,user_num=0,seed=0;//void SegDisplay(unsigned int cmd){unsigned char fnd[17]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x98,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x7f};SEG=fnd[cmd];}//char Getch(){ while((UCSR0A&0x80)==0); return UDR0; }void Putch(char d){ while((UCSR0A&0x20)==0); UDR0=d; }void PutSt(char *s){ while(*s)Putch(*s++); }//void light(int l){switch(l){case 8: PORTE=0b11110000; PORTG=0b00001111; break;case 7: PORTE=0b01110000; PORTG=0b00001111; break;case 6: PORTE=0b00110000; PORTG=0b00001111; break;case 5: PORTE=0b00010000; PORTG=0b00001111; break;case 4: PORTE=0b00000000; PORTG=0b00001111; break;case 3: PORTE=0b00000000; PORTG=0b00000111; break;case 2: PORTE=0b00000000; PORTG=0b00000011; break;case 1: PORTE=0b00000000; PORTG=0b00000001; break;}}//interrupt[EXT_INT0]void ext_int0_isr(void){PutSt("\n\n\r");PutSt("=========================\n\r");PutSt(" Game Start \n\r");PutSt("=========================\n\r");PutSt("0~100 사이의 수를 맞춰보세요!\n\r");life=8; time=10;sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str); delay_ms(500);TCCR0=0x07; srand(seed); com_number=rand()%100+1; light(life);}//interrupt[TIM0_OVF]void timer0_ovf_isr(void){if(++count==125){ count=0;SegDisplay(--time);if(time==0){ time=10;PutSt("\n\rwarning!!입력에 실패했습니다!!\n\r");up=0;life--;if(life>0){ light(life); sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str); }else{PutSt("당신은 패배했습니다.\n\r");sprintf(str,"아쉽네요.정답은%d입니다.\n\r",com_number); PutSt(str);TCCR0=0; // 타이머 정지PutSt("=========================\n\r");PutSt(" Game End \n\r");PutSt("=========================\n\n\r");}}}TCNT0=131;}//void main(void){unsigned char buff=0;DDRA=0xff;DDRB=0x0f;PORTC=0x0F; DDRC=0x0F; //(c0-c3: 출력,c4-c7: 입력)DDRE=0xff;DDRG=0xff;SegDisplay(0); //7-Segment에 '0'출력Q=0x0e; // 모든 7-Seg 선택EICRA=0x0A; EIMSK=0x01; // INT0UCSR0B=0x18; UBRR0L=0x08; // 115200 bpsTCNT0=131; TIMSK=0x01;SREG=0x80;while(1){seed++;KEY_C=0xFe; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 1110 출력 3 7 B C line Enableswitch(buff>>4){case 0x0e: Putch('0'); user_number[up++]='0'; delay_ms(200); break; // 1110case 0x0d: Putch('4'); user_number[up++]='4'; delay_ms(200); break; // 1101case 0x0b: Putch('8'); user_number[up++]='8'; delay_ms(200); break; //1011case 0x07: break; //0111}delay_ms(1); KEY_C=0xFC; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 1101 출력 1 5 9 D line Enableswitch(buff>>4){case 0x0e: Putch('1'); user_number[up++]='1'; delay_ms(200); break; // 1110case 0x0d: Putch('5'); user_number[up++]='5'; delay_ms(200); break; // 1101case 0x0b: Putch('9'); user_number[up++]='9'; delay_ms(200); break; //1011case 0x07: break; //0111}delay_ms(1); KEY_C=0xFB; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 1011 출력 2 6 A E line Enableswitch(buff>>4){case 0x0e: Putch('2'); user_number[up++]='2'; delay_ms(200); break; // 1110case 0x0d: Putch('6'); user_number[up++]='6'; delay_ms(200); break; // 1101case 0x0b:{ //1011// 입력 완료PutSt("\n\r");count=0; time=0; user_number[up++]='\0'; life--;user_num=atoi(user_number);if(life>0){if(user_num==com_number){ PutSt("당신이 이겼습니다.축하합니다!\n\r"); }else if(user_num>com_number){PutSt("정답보다 입력한 숫자가 큽니다.\n\r");sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str);light(life); time=10;}else{PutSt("정답보다 입력한 숫자가 작습니다.\n\r");sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str);light(life); time=10;}}else{PutSt("당신은 패배했습니다.\n\r");TCCR0=0; // 타이머 정지sprintf(str,"아쉽네요.정답은%d입니다.\n\r",com_number); PutSt(str);PutSt("=========================\n\r");PutSt(" Game End \n\r");PutSt("=========================\n\n\r");}up=0; delay_ms(200);break;}case 0x07: break; //0111}delay_ms(1); KEY_C=0xF7; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 0111 출력 3 7 B F line Enableswitch(buff>>4){case 0x0e: Putch('3'); user_number[up++]='3'; delay_ms(200); break; // 1110case 0x0d: Putch('7'); user_number[up++]='7'; delay_ms(200); break; // 1101case 0x0b: break; //1011case 0x07: break; //0111}delay_ms(1);}}#include <mega128.h>#include <delay.h>#include <stdio.h>#include <stdlib.h>//#define SEG PORTA //Port A 7-SEG data로 사용#define Q PORTB#define KEY_C PORTC //Port C Key Matrix 입력신호#define KEY_L PINC //Port C Key Matrix 출력신호//char count=0,up=0,user_number[6]={0,},str[100];int time=10,com_number,life=8,user_num=0,seed=0;//void SegDisplay(unsigned int cmd){unsigned char fnd[17]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x98,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x7f};SEG=fnd[cmd];}//char Getch(){ while((UCSR0A&0x80)==0); return UDR0; }void Putch(char d){ while((UCSR0A&0x20)==0); UDR0=d; }void PutSt(char *s){ while(*s)Putch(*s++); }//void light(int l){switch(l){case 8: PORTE=0b11110000; PORTG=0b00001111; break;case 7: PORTE=0b01110000; PORTG=0b00001111; break;case 6: PORTE=0b00110000; PORTG=0b00001111; break;case 5: PORTE=0b00010000; PORTG=0b00001111; break;case 4: PORTE=0b00000000; PORTG=0b00001111; break;case 3: PORTE=0b00000000; PORTG=0b00000111; break;case 2: PORTE=0b00000000; PORTG=0b00000011; break;case 1: PORTE=0b00000000; PORTG=0b00000001; break;}}//interrupt[EXT_INT0]void ext_int0_isr(void){PutSt("\n\n\r");PutSt("=========================\n\r");PutSt(" Game Start \n\r");PutSt("=========================\n\r");PutSt("0~100 사이의 수를 맞춰보세요!\n\r");life=8; time=10;sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str); delay_ms(500);TCCR0=0x07; srand(seed); com_number=rand()%100+1; light(life);}//interrupt[TIM0_OVF]void timer0_ovf_isr(void){if(++count==125){ count=0;SegDisplay(--time);if(time==0){ time=10;PutSt("\n\rwarning!!입력에 실패했습니다!!\n\r");up=0;life--;if(life>0){ light(life); sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str); }else{PutSt("당신은 패배했습니다.\n\r");sprintf(str,"아쉽네요.정답은%d입니다.\n\r",com_number); PutSt(str);TCCR0=0; // 타이머 정지PutSt("=========================\n\r");PutSt(" Game End \n\r");PutSt("=========================\n\n\r");}}}TCNT0=131;}//void main(void){unsigned char buff=0;DDRA=0xff;DDRB=0x0f;PORTC=0x0F; DDRC=0x0F; //(c0-c3: 출력,c4-c7: 입력)DDRE=0xff;DDRG=0xff;SegDisplay(0); //7-Segment에 '0'출력Q=0x0e; // 모든 7-Seg 선택EICRA=0x0A; EIMSK=0x01; // INT0UCSR0B=0x18; UBRR0L=0x08; // 115200 bpsTCNT0=131; TIMSK=0x01;SREG=0x80;while(1){seed++;KEY_C=0xFe; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 1110 출력 3 7 B C line Enableswitch(buff>>4){case 0x0e: Putch('0'); user_number[up++]='0'; delay_ms(200); break; // 1110case 0x0d: Putch('4'); user_number[up++]='4'; delay_ms(200); break; // 1101case 0x0b: Putch('8'); user_number[up++]='8'; delay_ms(200); break; //1011case 0x07: break; //0111}delay_ms(1); KEY_C=0xFC; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 1101 출력 1 5 9 D line Enableswitch(buff>>4){case 0x0e: Putch('1'); user_number[up++]='1'; delay_ms(200); break; // 1110case 0x0d: Putch('5'); user_number[up++]='5'; delay_ms(200); break; // 1101case 0x0b: Putch('9'); user_number[up++]='9'; delay_ms(200); break; //1011case 0x07: break; //0111}delay_ms(1); KEY_C=0xFB; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 1011 출력 2 6 A E line Enableswitch(buff>>4){case 0x0e: Putch('2'); user_number[up++]='2'; delay_ms(200); break; // 1110case 0x0d: Putch('6'); user_number[up++]='6'; delay_ms(200); break; // 1101case 0x0b:{ //1011// 입력 완료PutSt("\n\r");count=0; time=0; user_number[up++]='\0'; life--;user_num=atoi(user_number);if(life>0){if(user_num==com_number){ PutSt("당신이 이겼습니다.축하합니다!\n\r"); }else if(user_num>com_number){PutSt("정답보다 입력한 숫자가 큽니다.\n\r");sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str);light(life); time=10;}else{PutSt("정답보다 입력한 숫자가 작습니다.\n\r");sprintf(str,"=>기회가%d번 남았습니다.\n\r",life); PutSt(str);light(life); time=10;}}else{PutSt("당신은 패배했습니다.\n\r");TCCR0=0; // 타이머 정지sprintf(str,"아쉽네요.정답은%d입니다.\n\r",com_number); PutSt(str);PutSt("=========================\n\r");PutSt(" Game End \n\r");PutSt("=========================\n\n\r");}up=0; delay_ms(200);break;}case 0x07: break; //0111}delay_ms(1); KEY_C=0xF7; delay_ms(1); buff=KEY_L; buff=buff&0xF0; //Port B에 1111 0111 출력 3 7 B F line Enableswitch(buff>>4){case 0x0e: Putch('3'); user_number[up++]='3'; delay_ms(200); break; // 1110case 0x0d: Putch('7'); user_number[up++]='7'; delay_ms(200); break; // 1101case 0x0b: break; //1011case 0x07: break; //0111}delay_ms(1);}}
댓글 1
조회수 8,420master님의 댓글
master 작성일
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=6718
이 글의 LCD 함수 참고해서 만들어보세요