Busan IT/AVR 컨트롤러2015. 4. 15. 12:44

AVR 컨트롤러 - Interrupt와 난수를 활용한 주사위 만들기

 

학습목표 - Interrupt와 난수를 활용한 주사위를 만들 수 있다.

 

Interrupt를 활용한 주사위 던지기

 

순서

 

1. 회로를 만든다.

- ATmega128a로 스위치와 7segment(common anode)를 이용한다.(PORT A 사용)

- switch를 활용하여 interrupt를 만든다.

 

2. 코딩을 한다.

- switch를 누르고 있는 동안 8표시

- switch를 떼면 무작위 숫자(rand()%6)+1 출력

 

! 입력핀은 PIN *, 출력은 PORT *

 


코드


/**** main.c ****/



#include
 <atm128.h>
//#include <avr/interrupt.h>
#include "SMART.h"
#include <Turboc.h>

int main(void)
{
  unsigned char FND[10= {0x400x790x240x300x190x120x020x580x000x10};
  volatile unsigned int uiCnt;


  DDRA = 0xFF;  //포트 A 전부 출력 
  DDRD = 0xFE;  //포트 D 1번 핀만 입력
  DDRC = 0xFF;  //포트 C 전부 출력
  
  SREG &= 0x7F;  //interrupt disable, 설정 중 오동작을 막기 위해
  EICRA = 0x02;  //스위치의 트리거 방식 설정 - 하강 에지 방식 
  EIMSK = 0x01;  //외부 인터럽트 허용 레지스터 - 인터럽트 0 외부 인터럽트 허용 
  SREG |= 0x80

  while(1)
  {

      
    while(PIND==1)
    {
      
      PORTA = FND[8];
      PORTC = 0x01;
    
    }

    
  }
        

  return 0;
}


void __vector_1(void)
{
  volatile unsigned int uiCnt;

  unsigned char FND[10= {0x400x790x240x300x190x120x020x580x000x10};

  while(1)
  {

    while(PIND==1)
    {
      while(1)
      {
          
        PORTA = FND[8];
        PORTC = 0x01;
        
          if(PIND==0)
            break;
      }

      PORTA = FND[((random(6))+1)];
      PORTC = 0x01;

      if(PIND==1)
        break;
        
      
        
      }
      

  }

  

      
}  

/*** SMART.h ***/


#ifndef __SMART_H__
#define __SMART_H__

/**** General Purpose Register A - L ****/

#define  PORTA  (*((volatile unsigned char*)0x3B))
#define   DDRA   (*((volatile unsigned char*)0x3A))

#define  PORTC  (*((volatile unsigned char*)0x35))
#define   DDRC  (*((volatile unsigned char*)0x34))
#define   PINC  (*((volatile unsigned char*)0x33))

#define  PORTD  (*((volatile unsigned char*)0x32))
#define   DDRD  (*((volatile unsigned char*)0x31))
#define   PIND   (*((volatile unsigned char*)0x30))

#define  PORTG  (*((volatile unsigned char*)0x65))
#define   DDRG   (*((volatile unsigned char*)0x64))
#define   PING   (*((volatile unsigned char*)0x63))


/* 인터럽트 사용을 위한 레지스터 */

#define EICRA  (*((volatile unsigned char*)0x6A))
#define EICRB  (*((volatile unsigned char*)0x5A))
#define EIMSK  (*((volatile unsigned char*)0x59))
#define EIFR    (*((volatile unsigned char*)0x58))
#define SREG    (*((volatile unsigned char*)0x5F)) 


/* CPU 동작시간을 맞춰주기 위한 Dealy문과 값 지정 */

#define  Delay(x)    for(uiCnt=0; uiCnt<(x); ++uiCnt)

void __vector_1(void)__attribute__((signal,used,externally_visible));

/* 난수 출력  */

#define randomize() srand((unsigned)time(NULL))
#define random(n) (rand() % (n))


#endif //__SMART_H__



 

반응형
Posted by newind2000