Skip to content

A library for interfacing the DS3234 (SPI) RTC to an AVR microcontroller

License

Notifications You must be signed in to change notification settings

adam-dej/avr-lib-ds3234

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 

Repository files navigation

lib-ds3234

A library for interfacing the DS3234 (SPI) RTC to an AVR microcontroller.

Usage

Before you use this library you need to implement functions for SPI communication. This is because of portability and flexibility. Those functions are really easy to write. See the comments in ds3234.h on how to implement them. Then call ds3234_init() with pointers to those functions as parameters. Then you can use all functions of the library.

Examples

Initializing the library

Those SPI functions were tested on the ATmega8. Chip Select of DS3234 is connected to the SS pin of the ATmega.

#define DD_MOSI 3
#define DD_MISO 4
#define DD_SCK 5
#define DD_SS 2
#define DDR_SPI DDRB

#define DDR_DS3234_SS DDRB
#define PORT_DS3234_SS PORTB
#define DS_3234_SS 2

void spi_init() {
    DDR_SPI |= (1<<DD_MOSI) | (1<<DD_SCK) | (1<<DD_SS);
    DDR_SPI &= ~(1<<DD_MISO);
    
    DDR_DS3234_SS |= (1<<DS_3234_SS);
    
	SPCR = (1<<MSTR) | (1<<CPOL) | (1<<CPHA);
	SPCR |= (1<<SPE);
    
	PORT_DS3234_SS &= ~(1<<DS_3234_SS);
	PORT_DS3234_SS |= (1<<DS_3234_SS);
}

uint8_t spi_transfer(uint8_t data) {
	SPDR = data;
	while(!(SPSR & (1 <<SPIF)));
	return SPDR;
}

void ds3234_spi_slave_select() {
	PORT_DS3234_SS &= ~(1<<DS_3234_SS);
}

void ds3234_spi_slave_unselect() {
	PORT_DS3234_SS |= (1<<DS_3234_SS);
}

int main(void) {

    ds3234_init(spi_init, spi_transfer, ds3234_spi_slave_select, ds3234_spi_slave_unselect);
    
    ...

All other examples are assuming that initialization like this was performed.

Reading time stored in the DS3234

DS3234_TIME *time = malloc(sizeof(DS3234_TIME));
ds3234_read_time(time);

Reading 2 bytes of DS3234's RAM

uint8_t data[2];
ds3234_read_RAM(0, 2, data);

Disabling the 32KHz square output (changing the Control/Status register, see DS3234s datasheet for details)

uint8_t constat_register; //Short for control-status register
constat_register = ds3234_read_register(DS3234_REG_CTRL_STATUS);
constat_register &= ~(1 << DS3234_CONSTAT_EN32KHZ);
ds3234_write_register(DS3234_REG_CTRL_STATUS, constat_register);

About

A library for interfacing the DS3234 (SPI) RTC to an AVR microcontroller

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published