반응형

/*     avr studio4

win avr gcc

atmega8a

*/

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/sleep.h>

 unsigned int cnt=0; //선택스위치 count
 int timer0ck = 0;
 char offcnt = 0; // sleep모드 변수
 volatile unsigned int flag0 =0; //전원스위치 flag

ISR(TIMER0_OVF_vect){//1초에 한번 실행하는 timer
  if(++TCNT0> 600000){//10분
  TCNT0 = 0;
  timer0ck++;
  }
 }
void timerin(void){ //타이머 설정
  TIMSK = 0x01; // Timer/Counter0 Overflow Interrupt Enable
 TCNT0 = 250;//1.024ms
 TCCR0 = 0x07;// timer/conter control resister 분주비 1024
}
void timerout(void){ //타이머 초기화
  TIMSK = 0x00;
 TCNT0 = 0;//1.024ms
 TCCR0 = ~0x07;// timer/conter control resister  분주비 1024
}
void buzzer(void){// 부저
 PORTB = 0x02;
  _delay_ms(100);
  PORTB = ~0x02;
  }
ISR(INT0_vect){ // 전원스위치 인터럽트
  buzzer();
 volatile unsigned int flag0 = (flag0++)%2;
   if((PIND = 0x04)==0xff){
   offcnt++;
   if(offcnt >2){
    sleep_cpu(); //sleep모드
   offcnt = 0;}
  }// 2~3초간 누를시 sleep모드   
}
ISR(INT1_vect){ // 선택스위치 인터럽트
  buzzer();
   cnt++;
  switch(cnt){
    case 0:
    PORTD=0x20; // led1
      break;
    case 1: 
    PORTD=0x60; // led2
      break;
    case 2:
    PORTD=0xd0; // led3
     break;
    default:
    PORTD=0x20; // led1
     cnt=0;
      break;
   }
 }
int main(){
 PORTD = 0x00;//초기값
 PORTC = 0x00; // 초기값
 DDRB = 0x02; // 부저
 DDRC = 0x01; // 후면 led
 DDRD = 0x47; // 전면 led 출력 & 스위치 입력 & 인터럽트 INT0

 MCUCR = 0xb0; //MCU control resister
 sleep_enable();
 sleep_cpu(); // sleep mode 진입
 
 while(1){
  while(flag0==0){ //flag 0 일경우 sw누를경우 flag 증가
   PORTD = 0x20;
   PORTC = ~0x01;
   void timerout();
  }
  while(flag0 ==1){//flag 1 일경우
   if((PIND &0x20)==0xff){
    PORTC = 0x01;
   void timerin();
    if(timer0ck%4 ==1){
     void timerout();
    }
   // 타이머를 10분
   }
  else if((PIND & 0x60) ==0xff){
   PORTC = 0x01;
   void timerin();
    if(timer0ck%4 ==2){
     void timerout();
    }
   // 타이머를 20분
   }
  if((PIND & 0xd0) == 0xff){
   PORTC = 0x01;
   void timerin();
    if(timer0ck%4 ==3){
     void timerout();
    }
   // 타이머를 30분
   }
  }
  }
 return 0;
}

 

 

반응형

+ Recent posts