File: atmega32_ds1338.c

package info (click to toggle)
simavr 1.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 20,268 kB
  • sloc: ansic: 362,806; makefile: 622; ruby: 70; python: 63
file content (93 lines) | stat: -rw-r--r-- 1,968 bytes parent folder | download | duplicates (2)
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
/*
 atmega32_ds1338.c

 Copyright 2014 Doug Szumski <d.s.szumski@gmail.com>

 This file is part of simavr.

 simavr is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 simavr is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with simavr.  If not, see <http://www.gnu.org/licenses/>.
 */

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

#include <stdio.h>

#undef F_CPU
#define F_CPU 7380000

#include "avr_mcu_section.h"
AVR_MCU(F_CPU, "atmega32");

#include "ds1338.h"
#include "i2cmaster.h"

volatile uint8_t update_flag;

/*
 * This demo consists of a DS1338 RTC connected via the TWI bus
 * of an atmega32. The square wave output of the DS1338 is
 * enabled, with the tick-rate set to 1HZ. This is then fed
 * to pin D3 on the atmega32 which is configured to generate
 * an interrupt on a rising edge. When the interrupt fires the
 * time is read from the DS1338.
 */

int
main()
{
	i2c_init();

	/*
	 * Demonstrate the virtual part functionality.
	 */
	ds1338_init();
	ds1338_time_t time = {
		.date = 31,
		.day = 6,
		.hours = 23,
		.minutes = 59,
		.month = 12,
		.seconds = 56,
		.year = 14,
	};
	ds1338_set_time(&time);

	// Setup pin change interrupt for the square wave output
	cli();
	DDRD &= ~(1 << PD3);
	// Fire INT1 on the rising edge
	MCUCR |= (1 << ISC11) | (1 << ISC10);
	GICR |= (1 << INT1);
	sei();

	while(time.seconds != 2)
	{
		if (update_flag) {
			ds1338_get_time(&time);
			update_flag = 0;
		}
	}

	cli();
	sleep_mode();

}

ISR (INT1_vect)
{
	update_flag = 1;
}