반응형

 

// 소스 코드 :

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000UL
#include <avr/interrupt.h>
 
#define OFF 0
#define ON 1
volatile unsigned char digit_c[10= {0x000x270x090x030x260x120x100x060x000x02}; // 0 ~ 10
volatile unsigned char digit_b[10= {0x040x04, ~0x04, ~0x04, ~0x04, ~0x04, ~0x040x04, ~0x04, ~0x04}; // 0 ~ 10
volatile unsigned char fnd_sel[4= {0x100x200x400x80}; // fnd_sel c1, c2, c3, c4
 
volatile unsigned int msec, sec,min,hour = 0// time
volatile unsigned int play = ON; // play ON/OFF
 
 
 
int main(){
 
 
 DDRB = 0x04;
 DDRC = 0xff;
 DDRD = 0xf0;
 
 
 while(1){
  
  if ((PIND & 0x01== 0x00){ 
  play = ON; continue;}
  if ((PIND & 0x02== 0x00){
  play = OFF; continue;}
    
  if(play == ON){
  
  msec++
  if(msec>40){msec=0; sec++;}
  if(sec>59){sec=0; min++;}
  if(min>59){min=0; hour++;}
  if(hour>23){hour=0;}
 
  PORTC = digit_c[hour/10]; PORTB = digit_b[hour/10]; PORTD = fnd_sel[3];_delay_ms(3);
  PORTC = digit_c[hour%10]; PORTB = digit_b[hour%10]; PORTD = fnd_sel[2];_delay_ms(3);
  PORTC = digit_c[min/10]; PORTB = digit_b[min/10]; PORTD = fnd_sel[1];_delay_ms(3);
  PORTC = digit_c[min%10]; PORTB = digit_b[min%10]; PORTD = fnd_sel[0];_delay_ms(3);
  }
 
  if(play == OFF){
 
  cli();
  GICR = 0xc0;
  MCUCR = 0x0a;
  GIFR = 0x00;
  sei();
  
  PORTC = digit_c[hour/10]; PORTB = digit_b[hour/10]; PORTD = fnd_sel[3]; _delay_ms(3);
  PORTC = digit_c[hour%10]; PORTB = digit_b[hour%10]; PORTD = fnd_sel[2]; _delay_ms(3);
  PORTC = digit_c[min/10]; PORTB = digit_b[min/10]; PORTD = fnd_sel[1]; _delay_ms(3);
  PORTC = digit_c[min%10]; PORTB = digit_b[min%10]; PORTD = fnd_sel[0]; _delay_ms(3);
 
  }
 }
 
}
 
ISR(INT0_vect){
 if(min <= 59){
 min++;
 }
 if(min >59){
 min = 0; hour++;
 }
}
ISR(INT1_vect){
 if(hour <= 23){hour++;
 }
 if(hour >23){
 hour =0;
 }
}
 
 
/*
SW 1,2,3,4,
SW 1: Play Mode
SW 2: STOP Setting Mode
SW 3: Hour ++;
SW 4: Min ++;
 
스위치 4개로 시간을 설정/재생 할 수 있다.
 
시간측정을해보니 약 5~10초정도 오차가 있다. 몇달 몇년으로 따지면 엄청난 오차.
 
차후엔 ds3231 rtc모듈을 이용하여 만들어 볼 예정.
*/
 

// 그전에 Timer/Counter로 먼저 만들어 봐야 할 것 같다. 

 
cs

 

 

http://cafe.naver.com/carroty/291041

 

반응형

'Study > 8-bit MCUs' 카테고리의 다른 글

[NPAVR Board] AVR -Atmega128 (FND, Switch)  (0) 2018.09.03
[NPAVR Board] AVR -Atmega128 (FND)  (0) 2018.09.03
B/T LED 부품 선정 및 견적서  (1) 2017.04.10
B/T LED Control circuit  (0) 2017.04.10
led 제어 현재 상황  (0) 2016.02.26

+ Recent posts