Create library for PCF8583 (RTC) and enable it
This commit is contained in:
13
main.c
13
main.c
@@ -1,13 +1,18 @@
|
|||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
#include <util/delay.h>
|
||||||
#include "i2c.h"
|
#include "i2c.h"
|
||||||
|
#include "rtc.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
|
|
||||||
#define I2C_BITRATE 100000UL // 100kHz
|
#define I2C_BITRATE 100000UL // 100kHz
|
||||||
|
#define RTC_I2C_ADDR 0xA2
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
i2c_init(I2C_BITRATE);
|
i2c_init(I2C_BITRATE);
|
||||||
|
rtc_int0_init();
|
||||||
led_init();
|
led_init();
|
||||||
|
|
||||||
sei();
|
sei();
|
||||||
@@ -20,4 +25,12 @@ int main()
|
|||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(INT0_vect)
|
||||||
|
{
|
||||||
|
struct time curr_time = rtc_read_time(RTC_I2C_ADDR);
|
||||||
|
led_hour = curr_time.hour;
|
||||||
|
led_minute = curr_time.minute;
|
||||||
|
led_second = curr_time.second;
|
||||||
}
|
}
|
||||||
37
rtc.c
Normal file
37
rtc.c
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#include <avr/io.h>
|
||||||
|
#include "rtc.h"
|
||||||
|
#include "i2c.h"
|
||||||
|
|
||||||
|
void rtc_int0_init(void)
|
||||||
|
{
|
||||||
|
INT0_DIR &= ~(1<<INT0_PIN);
|
||||||
|
INT0_PORT |= (1<<INT0_PIN);
|
||||||
|
GICR |= (1<<INT0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rtc_int1_init(void)
|
||||||
|
{
|
||||||
|
INT1_DIR &= ~(1<<INT1_PIN);
|
||||||
|
INT1_PORT |= (1<<INT1_PIN);
|
||||||
|
GICR |= (1<<INT1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void rtc_set_time(uint8_t part, uint8_t value)
|
||||||
|
{
|
||||||
|
uint8_t bcd = DEC_2_BCD(value);
|
||||||
|
i2c_writebuf(RTC_I2C_ADDR, part, 1, &bcd);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct time rtc_read_time(void)
|
||||||
|
{
|
||||||
|
uint8_t buffer[3];
|
||||||
|
i2c_readbuf(RTC_I2C_ADDR, 0x02, 3, buffer);
|
||||||
|
|
||||||
|
struct time curr_time = {
|
||||||
|
BCD_2_DEC(buffer[2]),
|
||||||
|
BCD_2_DEC(buffer[1]),
|
||||||
|
BCD_2_DEC(buffer[0])
|
||||||
|
};
|
||||||
|
|
||||||
|
return curr_time;
|
||||||
|
}
|
||||||
33
rtc.h
Normal file
33
rtc.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef __RTC_H__
|
||||||
|
#define __RTC_H__
|
||||||
|
|
||||||
|
#define RTC_I2C_ADDR 0xA2
|
||||||
|
|
||||||
|
#define SECOND 0x02
|
||||||
|
#define MINUTE 0x03
|
||||||
|
#define HOUR 0x04
|
||||||
|
|
||||||
|
#define INT0_PORT PORTD
|
||||||
|
#define INT0_DIR DDRD
|
||||||
|
#define INT0_PIN PD2
|
||||||
|
#define INT1_PORT PORTD
|
||||||
|
#define INT1_DIR DDRD
|
||||||
|
#define INT1_PIN PD3
|
||||||
|
|
||||||
|
|
||||||
|
#define DEC_2_BCD(dec) ((((dec) / 10) << 4) | ((dec) % 10))
|
||||||
|
#define BCD_2_DEC(bcd) (((((bcd) >> 4) & 0x0F) * 10) + ((bcd) & 0x0F))
|
||||||
|
|
||||||
|
struct time
|
||||||
|
{
|
||||||
|
uint8_t hour;
|
||||||
|
uint8_t minute;
|
||||||
|
uint8_t second;
|
||||||
|
};
|
||||||
|
|
||||||
|
void rtc_int0_init(void);
|
||||||
|
void rtc_int1_init(void);
|
||||||
|
void rtc_set_time(uint8_t part, uint8_t value);
|
||||||
|
struct time rtc_read_time(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user