Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rtc.c - github.com/ClusterM/intercom.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/rtc.c
blob: 80fdd8aa15c407ec4ccabf8755e944b11bfc0dc5 (plain)
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
#include <avr/io.h>
#include <avr/interrupt.h>
#include "rtc.h"
#include "bits.h"

volatile char rtc_ok = 0;
volatile int year;
volatile int month;
volatile int mday;
volatile int hour;
volatile int min;
volatile int sec;

DWORD get_fattime(void)
{
	if (!rtc_ok) return 0;

	return	  ((DWORD)(year - 1980) << 25)
			| ((DWORD)month << 21)
			| ((DWORD)mday << 16)
			| ((DWORD)hour << 11)
			| ((DWORD)min << 5)
			| ((DWORD)sec >> 1);
}

void set_time(int s_year, int s_month, int s_mday, int s_hour, int s_min, int s_sec)
{
	year = s_year;
	month = s_month;
	mday = s_mday;
	hour = s_hour;
	min = s_min;
	sec = s_sec;
	rtc_ok = 1;
}

void time_init()
{
	OCR2 = 0; // Запускаем часы
	set_bit(ASSR, AS0);
	set_bit2(TCCR0, CS22, CS20); // x128, каждую секунду
	set_bit(TIMSK, TOIE0);
}

int days(int month, int year)
{
	switch (month)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:		
			return 31;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
		case 2:
			return (year % 4 == 0) ? 29 : 28;
	}
	return 0;
}

ISR(TIMER0_OVF_vect)
{
	sec++;
	if (sec > 59)
	{
		min++;
		sec = 0;
	}
	if (min > 59)
	{
		hour++;
		min = 0;
	}
	if (hour > 23)
	{
		mday++;
		hour = 0;
	}
	if (mday > days(month, year))
	{
		month++;
		mday = 1;
	}
	if (month > 12)
	{
		year++;
		month = 1;
	}
}