BASIC4MCU | 질문게시판 | 답변 : atmega128변환도와주세요....ㅠㅠ
페이지 정보
작성자 master 작성일2018-12-11 16:31 조회6,847회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=6723
본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-12-11 오후 4:36:53// by Ok-Hyun Park//#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 fnd[17]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xd8,0x80,0x98,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x7f};unsigned char count=0;int time=10;//unsigned char flag=0;int com_number;int life=8;char user_number[6]={0,};int user_num=0;char up=0;int seed=0;////char Getch(){ while(!(UCSR0A&0x80)); return UDR0; }//void Putch(char d){ while(!(UCSR0A&0x20)); 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=========================\n\r Game Start \n\r=========================\n\r");PutSt("0~100 사이의 수를 맞춰보세요!\n\r");life=8;time=10;printf("=>기회가%d번 남았습니다.\n\r",life); delay_ms(500);TCCR0=0x07;srand(seed);com_number=rand()%100+1;light(life);}//interrupt [TIM0_OVF] void timer0_ovf_isr(void){TCNT0=131;if(++count==125){ count=0;time--;SEG=fnd[time];if(time==0){ time=10;life--;printf("\n\rwarning!!입력에 실패했습니다!!\n\r");up=0;if(life>0){ light(life); printf("=>기회가%d번 남았습니다.\n\r",life); }else{printf("당신은 패배했습니다.\n\r아쉽네요.정답은%d입니다.\n\r",com_number);TCCR0=0; // 타이머 정지PutSt("=========================\n\r Game End \n\r=========================\n\n\r");}}}}//void main(void){unsigned char buff=0;DDRA=0xff;DDRB=0x0f;PORTC=0x0F; DDRC=0x0F; //(c0-c3: 출력,c4-c7: 입력)DDRE=0xff;DDRG=0xff;SEG=fnd[0]; //7-Segment에 '0'출력Q=0x0e; // 모든 7-Seg 선택EICRA=0x0A; EIMSK=0x01; // INT0 하강레벨감지UCSR0B=0x18; UBRR0L=0x08; // 115200 bpsTCNT0=131; TIMSK=0x01;SREG=0x80;while(1){seed++;KEY_C=0xFe; //Port B에 1111 1110 출력 3 7 B C line Enabledelay_ms(1); //시간지연 함수 호출buff=KEY_L; //buff에 PortC 값을 저장한다.buff=buff&0xF0; //buff값에 0x0f AND Maskswitch(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; //1011}delay_ms(1);KEY_C=0xFC; //Port B에 1111 1101 출력 1 5 9 D line Enabledelay_ms(1); //시간지연 함수 호출buff=KEY_L; //buff에 PortC 값을 저장한다.buff=buff&0xF0; //buff값에 0x0f AND Maskswitch(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; //1011}delay_ms(1);KEY_C=0xFB; //Port B에 1111 1011 출력 2 6 A E line Enabledelay_ms(1); //시간지연 함수 호출buff=KEY_L; //buff에 PortC 값을 저장한다.buff=buff&0xF0; //buff값에 0x0f AND Maskswitch(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){printf("정답보다 입력한 숫자가 큽니다.\n\r=>기회가%d번 남았습니다.\n\r",life);light(life);time=10;}else{light(life);printf("정답보다 입력한 숫자가 작습니다.\n\r=>기회가%d번 남았습니다.\n\r",life);time=10;}}else{TCCR0=0; // 타이머 정지printf("당신은 패배했습니다.\n\r아쉽네요.정답은%d입니다.\n\r",com_number);PutSt("=========================\n\r Game End \n\r=========================\n\n\r");}up=0; delay_ms(200);break;}delay_ms(1);KEY_C=0xF7; //Port B에 1111 0111 출력 3 7 B F line Enabledelay_ms(1); //시간지연 함수 호출buff=KEY_L; //buff에 PortC 값을 저장한다.buff=buff&0xF0; //buff값에 0x0f AND Maskswitch(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; // 1101}delay_ms(1);}}
댓글 0
조회수 6,847등록된 댓글이 없습니다.