BASIC4MCU | 질문게시판 | 답변 : Atmega128 기억력테스트 구성중....
페이지 정보
작성자 master 작성일2018-06-27 09:27 조회4,519회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=2057
본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-06-27 오전 9:30:53// by Ok-Hyun Park//#include <avr/interrupt.h>#include <avr/io.h>#include <avr/pgmspace.h>#include <stdlib.h>#include <stdio.h>#define F_CPU 16000000UL // 16 MHz#include <util/delay.h>////bit제어를위해 설정#define sbit(x,y) (x|=(1<<y))#define cbit(x,y) (x&=~(1<<y))#define tbit(x,y) (x&(1<<y))//volatile uint8_t data_table[4]={0x10,0x20,0x40,0x80};volatile uint8_t key1,key1_temp,key_flag,start_flag,key1_on,key1_up,key1_down;volatile uint8_t msec,m00,data[128],data_count,data_count_end,key_data,timer;volatile int16_t seed;//void LCD_wCmd(char c){ PORTD=0x08; PORTC=c; _delay_us(1); PORTD&=~8; _delay_ms( 3); }void LCD_Char(char d){ PORTD=0x0A; PORTC=d; _delay_us(1); PORTD&=~8; _delay_us(50); }void LCD_Str(char*s){ while(*s)LCD_Char(*s++); }//void keyread(void){unsigned char i,j;//key1=~PINF&0xf0; //값을 읽어와 필요부분만 유효화한다.//------------------------------------------------------------------if(key1_temp==key1){ //2회 동일값이면//---------------------------------------------키를 누를때 1회 oni=key1_on; //이전값j=key1_temp; //현재값j=(i^j)&j; //새로운값만 유효key1_down=j;//---------------------------------------------키를 눌렀다 뗄때 1회ONj=key1_temp; //현재값j=(i^j)&i; //off된 이전값만 유효key1_up=j;//---------------------------------------------키가눌려져 있는 동안 onkey1_on=key1_temp; //현재값}//---------------------------------------------이전 값과 일치하지 않으면else key1_temp=key1;}//void plc(void){ //10ms 마다 이함수로 진입uint8_t i;//if(data_count_end>100)start_flag=0; //최대 100회 반복//if(start_flag==0){ //startsrand(seed);data_count=0; data_count_end=0; start_flag=1; key1_down=0;for(i=0; i<100; i++)data[i]=rand()%4; //난수 100개를 저장}//data 표시if(start_flag==1){if(++timer<50){ m00=pgm_read_byte(&data_table[data[data_count_end]]); }else { m00=0; timer=0; start_flag=2; }}//if(start_flag==2){ //key 변환if(key1_down>0){if (tbit(key1_down,4))key_data=0;else if(tbit(key1_down,5))key_data=1;else if(tbit(key1_down,6))key_data=2;else key_data=3;start_flag=3;}}//if(start_flag==3){ //checkif(key_data==data[data_count]){start_flag=2;if(++data_count>data_count_end){ start_flag=1; data_count=0; data_count_end++; }}else{ start_flag=4; }}//if(start_flag==4){ //errorif (++timer<100){ m00=0xff; }else if(timer<150){ timer=0; m00=0; start_flag=0; }}//PORTD=(PORTD&0x0f)|m00; //out//key1_down=0; key1_up=0;}//---------------------------------------------------------------------ISR(TIMER0_COMP_vect){ // 1msif(++msec==10){ msec=0; // 10msseed++; //난수의 씨앗keyread();plc(); // 10ms를 초과하지말것.(가능하면 5ms이내로할것.)}}//=======================================================================int main(void){DDRD=0xFE; // PD7-PD4=LED output // PD1,PD2,PD3 // RS RW EDDRC=0xFF; // LCD data busPORTF=0xFF; // PORTF=KEY_delay_ms(100);LCD_wCmd(0x38); LCD_wCmd(0x38); LCD_wCmd(0x38); LCD_wCmd(0x38);LCD_wCmd(0x0c); LCD_wCmd(0x01);LCD_wCmd(0x80); LCD_Str("START"); _delay_ms(3000); //3초동안 표시//TCCR0=0x0C; OCR0=249; TIMSK=2; // 16MHz/64/(1+249)=1ms // enable OC0 interruptsei();while(1){}}잘못 누른 부분에서 아래 코드를 추가하세요LCD_wCmd(0x80); LCD_Str("GAME OVER"); while(1); // 게임종료, 리셋버튼 누를 때까지 대기
3초 표시 후에 START를 지우고 싶으면
LCD_wCmd(0x80); LCD_Str("START"); _delay_ms(3000); //3초동안 표시LCD_wCmd(0x01); // <-- 화면 clear 컴맨드를 실행하면 됩니다.그 후에 RUN 표시를 하려면
LCD_wCmd(0x80); LCD_Str("START"); _delay_ms(3000); //3초동안 표시LCD_wCmd(0x01);LCD_wCmd(0x80); LCD_Str("RUN");
한 가지 문제점은
LCD 제어핀과 LED출력이 포트D에 있습니다.
이 경우 LED와 LCD 제어핀을 모두 비트 마스크 처리해줘야하는데
소스코드를 덜 건드리려면
LED를 사용하지 않는 남는 포트로 변경하세요
댓글 0
조회수 4,519등록된 댓글이 없습니다.