스탑와치
//STOP SWATCH 실습 ..1번 누르면 시작...2번 누르면 스톱 //
#include "ddr.h"
#include <avr/signal.h>
#include <avr/interrupt.h>
#define CPU_CLOCK 16000000 // cpu clodk = 16,000,000 Hz
#define TICKS_PER_SEC 1000 // Ticks per sec = 1,000
#define PRESCALER 64 // 클럭의 배수 (크기,오버헤드)
#define DDR_LED DDRF
#define DATA_TO_LED PORTF
#define DATA_TO_FND PORTE
#define DDR_SENSOR DDRD
#define DATA_FROM_SENSOR PIND
#define DDR_FND DDRE
#define DDR_SW DDRC
#define SWITCH_INPUT PORTC
#define INPUT_C PINC
volatile unsigned int g_elapsed_time; // 시간 변수
volatile unsigned int led_state=0;
void initLED(); // LED 초기화
void initFND(); //FND 초기화...
void setTCCR0();
void initTCNT0();
void setTIMSK();
sleep(unsigned int elapsed_time); // 1초대기
SIGNAL(SIG_OVERFLOW0);
void ACT_FND();
int main()
{
initLED();
initFND();
setTCCR0();
initTCNT0();
setTIMSK();
sei(); // 인터럽트 활성화
//DDR_SW=0x00; //c포트를 스위치 입력으로 ...
volatile unsigned char a;
while(1)
{
char a;
a=~PINC;
a=(a<<6);
if(a==(0b01000000))
{
initLED();
initFND();
led_state=8;
while(1)
{
ACT_FND();
DATA_TO_LED=(1<<led_state);
a=~PINC;
a=(a<<6);
if(a==(0b10000000))
{
break;
}
}
}
}
return 1;
}
initLED() //LED 초기화 함수
{
DDR_LED=0xFF;
DATA_TO_LED=0x00;
}
initFND() //FND 초기화 함수
{
DDR_FND=0xFF;
DATA_TO_FND=0xff; // 전부 꺼짐....OFF..
}
setTCCR0() //분주비 64로 설정
{
TCCR0=0b00000100;
}
initTCNT0() // 카운터 6으로 초기화
{
TCNT0=0b00000110;
}
setTIMSK() // 오버플로우 사용하겠다.
{
TIMSK=0b00000001;
}
sleep(unsigned int elapsed_time) // elapsed_time = 1000
{
g_elapsed_time=0;
while(1)
{
if(elapsed_time==g_elapsed_time)
{
break;
}
}
}
SIGNAL(SIG_OVERFLOW0)
{
initTCNT0() ;
g_elapsed_time=g_elapsed_time+1;
}
ACT_FND()
{
int iCount=0;
int jCount=0;
int FndCount=0;
char a=0;
for(iCount=0;iCount<=9;++iCount)
{
for(jCount=0;9>=jCount;++jCount)
{
DATA_TO_FND=FndCount;
sleep(10);
++FndCount;
}
FndCount=FndCount+6;
a=~PINC;
a=(a<<6);
if(a==(0b10000000))
{
break;
}
}
led_state++;
if(led_state>=8)
{
led_state=0;
}
}