답변 : 답변 : atmega128변환도와주세요....ㅠㅠ
페이지 정보
작성자 꾸꾹 작성일18-12-11 17:16 조회8,658회 댓글2건본문
밑에 게시글을 통해서 참고하여서 Lcd판에 input number를 띄웠습니다
그리고 segment판에 9부터 0까지 숫자가 감소 하면서 세어집니다. 감사합니다ㅠㅠ
근데...이제 첫숫자를 누르고 keymatrix A를 누르면 난수와 비교하여 lcd판에 작으면 up 크면 down으로 나타고 싶은데
시리얼통신 부분의 소스를 어떻게 수정하면서 시작해야 할 지 모르겠습니다.....ㅠㅠ
#include <mega128.h>
#include <delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <lcd.h>
#include <string.h>
#asm
.equ __lcd_port=0x1B; //포트A
#endasm
//
#define SEG PORTD //Port D 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;
char buf[20];
DDRD=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 bps
TCNT0=131; TIMSK=0x01;
SREG=0x80;
while(1){
lcd_init(16);
lcd_gotoxy(0,0);
sprintf(buf,"input number");
lcd_puts(buf);
seed++;
KEY_C=0xFe; //Port B에 1111 1110 출력 3 7 B C line Enable
delay_ms(1); //시간지연 함수 호출
buff=KEY_L; //buff에 PortC 값을 저장한다.
buff=buff&0xF0; //buff값에 0x0f AND Mask
switch(buff>>4){
case 0x0e: Putch('0'); user_number[up++]='0'; delay_ms(200); break; // 1110
case 0x0d: Putch('4'); user_number[up++]='4'; delay_ms(200); break; // 1101
case 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 Enable
delay_ms(1); //시간지연 함수 호출
buff=KEY_L; //buff에 PortC 값을 저장한다.
buff=buff&0xF0; //buff값에 0x0f AND Mask
switch(buff>>4){
case 0x0e: Putch('1'); user_number[up++]='1'; delay_ms(200); break; // 1110
case 0x0d: Putch('5'); user_number[up++]='5'; delay_ms(200); break; // 1101
case 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 Enable
delay_ms(1); //시간지연 함수 호출
buff=KEY_L; //buff에 PortC 값을 저장한다.
buff=buff&0xF0; //buff값에 0x0f AND Mask
switch(buff>>4){
case 0x0e: Putch('2'); user_number[up++]='2'; delay_ms(200); break; // 1110
case 0x0d: Putch('6'); user_number[up++]='6'; delay_ms(200); break; // 1101
case 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 Enable
delay_ms(1); //시간지연 함수 호출
buff=KEY_L; //buff에 PortC 값을 저장한다.
buff=buff&0xF0; //buff값에 0x0f AND Mask
switch(buff>>4){
case 0x0e: Putch('3'); user_number[up++]='3'; delay_ms(200); break; // 1110
case 0x0d: Putch('7'); user_number[up++]='7'; delay_ms(200); break; // 1101
}
delay_ms(1);
}
}
댓글 : 2
조회수 : 8,658
꾸꾹님의 댓글
꾸꾹 작성일지금 하이퍼터미널에 keymatrix를 A누를때마다 led 하나씩 줄어드는데 Lcd로 바꾸면 A누를때마다 줄어드는 것도 가능할까요?ㅠㅠ
master님의 댓글
master
16문자 넘치지 않도록 잘 표시 해보세요