아트메가128 프로젝트
두더지 게임.
정말 힘들게 완성한 게임.
하지만 버그가 있다는 점.
하이퍼터미널에 보내는 스위치번호가 0아니면 8이라는 점..
일단 이까지만....
//---------USART 레지스트리 설정
#define UDR1 (*(volatile unsigned char *)0x9C)
#define UCSR1A (*(volatile unsigned char *)0x9B)
#define UCSR1B (*(volatile unsigned char *)0x9A)
#define UCSR1C (*(volatile unsigned char *)0x9D)
#define UBRR1L (*(volatile unsigned char *)0x99)
#define UBRR1H (*(volatile unsigned char *)0x98)
//--------- 통신 값 설정
#define CPU_CLOCK 16000000
#define BAUD_RATE 19200
#define BAUD_RATE_L (CPU_CLOCK/(16l*BAUD_RATE))-1
#define BAUD_RATE_H ((CPU_CLOCK/(16l*BAUD_RATE))-1)>>8
//--------- 레지스트리 비트값(UCSR1C)
#define UPM1 5
#define UPM0 4
#define USBS 3
#define UCSZ1 2
#define UCSZ0 1
//--------- 레지스트리 비트값(UCSR1B)
#define TXEN 3
#define RXEN 4
#define UCSZ2 2
//--------- 레지스트리 비트값(UCSR1A)
#define UDRE 5
#define NOP() asm volatile ("nop");
#define DELAY_MS 400 //딜레이 시간 설정.
#define PINC (*(volatile unsigned int *) 0x33)
#define DDRC (*(volatile unsigned int *) 0x34)
#define PORTC (*(volatile unsigned int *) 0x35)
#define PIND (*(volatile unsigned int *) 0x30)
#define DDRD (*(volatile unsigned int *) 0x31)
#define PORTD (*(volatile unsigned int *) 0x32)
#define PINE (*(volatile unsigned char *)0x21)
#define DDRE (*(volatile unsigned char *)0x22)
#define PORTE (*(volatile unsigned char *)0x23)
#define PINF (*(volatile unsigned char *)0x20)
#define DDRF (*(volatile unsigned char *)0x61)
#define PORTF (*(volatile unsigned char *)0x62)
#define PINB (*(volatile unsigned char *)0x36)
#define DDRB (*(volatile unsigned char *)0x37)
#define PORTB (*(volatile unsigned char *)0x38)
#define NOP() asm volatile ("nop");
#define DELAY_MS 400 //딜레이 시간 설정.
#include <stdio.h>
#include <stdlib.h>
#define SET_FND(num) PORTE=((((num/10)<<4)%10)+(num%10))
void uart_send_string(const char* str);
void Delay_us (unsigned int time_us)
{
register unsigned int i;
for (i=0; i < time_us; i++)
{
asm volatile(" PUSH R0 ");
asm volatile(" POP R0 ");
asm volatile(" PUSH R0 ");
asm volatile(" POP R0 ");
asm volatile(" PUSH R0 ");
asm volatile(" POP R0 ");
}
}
void Delay_ms (unsigned int time_ms)
{
register unsigned int i;
for (i=0; i < time_ms; i++)
{
Delay_us(250);
Delay_us(250);
Delay_us(250);
Delay_us(250);
}
}
void uart_send_byte(unsigned int byte)
{
char num;
while(!(UCSR1A &(1<<UDRE))); //전송버퍼가 빌때까지 기다린다.
num=byte+'0';
UDR1 = num; //문자 1개를 전송
}
int count;
int FND_CNT1 = 0;
int FND_CNT2 = 0;
void FND_SCORE(void)
{
FND_CNT2 +=5;
if(FND_CNT2 ==10)
{
FND_CNT2 =0;
FND_CNT1++;
if(FND_CNT1 ==10)
{
FND_CNT1 =0;
}
}
PORTE = (FND_CNT1<<4)+FND_CNT2;
}
int pin_value(int pin_num) //핀관련 된 함수.
{
int cnt = 0;
while(pin_num!=0)
{
pin_num = pin_num/2;
cnt++;
}
return cnt;
}
void send_result(int flag, int ran_num, int pin_num)
{
uart_send_string("[LED");
uart_send_byte(ran_num+1); //ran_num번호 출력.
uart_send_string("]_[스위치 ");
uart_send_byte(pin_num); //누르는 핀 값을 출력
uart_send_string("]");
switch(flag)
{
case 1:
uart_send_string("OK!! \n\r");
break;
case 0:
uart_send_string("FAIL!! \n\r");
break;
}
}
int main(void)
{
DDRF = 0XFF; //led
PORTF = 0XFF;
DDRE = 0xff;
PORTE = 0x00; //FND
DDRC = 0x00;
PORTC = 0xFF;// 스위치
int flag = 0;
unsigned char num = 0;
int ran_num;
int pin_num;
int i;
int end=0;
unsigned char buf;
UBRR1L = (unsigned char)BAUD_RATE_L;
UBRR1H = (unsigned char)BAUD_RATE_H;
UCSR1C = (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0);
UCSR1B = (1<<TXEN) | (1<<RXEN) | (0<<UCSZ2);
for(end =0;end <20 ;end++)
{
flag = 0;
pin_num = 0;
ran_num = rand()%8;
PORTF = ~(1 << (ran_num)); //LED 램덤으로 돌림
for(i=0; i<1000; i++)
{
Delay_ms(1);
if(PORTF == PINC) //포트값이랑 PINC값이 같다면
{
FND_SCORE(); //FND 올려주는 함수
flag = 1; //스위치 성공, 실패 알리는 변수
pin_num = ~PINC; //pin_num에 PINC값의 반전 값을 받고
pin_num = pin_value (pin_num);
if(PINC!=0Xff)
{
Delay_ms(1000-i);
break;
}
}
}
send_result(flag, ran_num,pin_num); //결과 출력하는 함수.
}
uart_send_string("You got a ");
uart_send_byte(FND_CNT1); //10자리
uart_send_byte(FND_CNT2); //1자리
if(FND_CNT1 == 3 && FND_CNT2 ==0)
{
while(1)
{
PORTF = 0x00;
Delay_ms(1000);
PORTF = 0xff;
Delay_ms(1000);
}
}
}
void uart_send_string(const char* str)
{
int i;
for(i=0;*(str+i)!=0;i++)
{
//UDR1 = str[i];
while(!(UCSR1A &(1<<UDRE)));
UDR1 = str[i];
}
}
|