I2c

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey guys,
i have a problem while setting up the I2C on my atmega32.
i am interfacing it with RTC DS1307 IC, which requires setting of the TWBR register, a formula is given in the datasheet, but it doesnt yield results,
i am using a 12Mhz crystal for the MCU and a 32.768kHz crystal for DS1307.
what should be the value of TWBR and TWSR registers,
please help
thanking you in anticipation
Code:


# include
# include
#include "lcd.h"

void init_i2c()
{
	TWSR = 0X00;
	TWBR = 0X2C;
	TWCR = (1<<TWEN);
}

unsigned char read_i2c()
{
	TWCR = (1<<TWINT)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
	return TWDR;
}

void write_i2c(unsigned char ch)
{
	TWDR = ch;
	TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
	while(!(TWCR&(1<<TWINT)));
}

void start()
{
	TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
	while((TWCR &(1<<TWINT))==0);
}

void stop()
{
	TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
	_delay_ms(1);
}

void rtc_write(char dev_addr,char dev_loc,char dev_data)
{
	start();
	write_i2c(dev_addr);
	write_i2c(dev_loc);
	write_i2c(dev_data);
	stop();
}

unsigned char rtc_read(char dev_addr,char dev_loc)
{
	char ch;

	start();

	write_i2c(dev_addr);
	write_i2c(dev_loc);
	
	start();
	write_i2c(dev_addr|0x01);
	ch = read_i2c();

	stop();
	return ch;
}

void disp_time_date()
{
	char ch;
	
	lcd_string("Time: ");

	ch = rtc_read(0xd0 , 0x02);
	lcd_char(ch/16+48);
	lcd_char(ch%16+48);
	lcd_char('-');
	

	ch = rtc_read(0xd0 , 0x01);
	lcd_char(ch/16+48);
	lcd_char(ch%16+48);
	lcd_char('-');

	
	ch = rtc_read(0xd0 , 0x00);
	lcd_char(ch/16+48);
	lcd_char(ch%16+48);


	lcd_char(254);
	lcd_char(192);

	lcd_string("Date: ");

	ch = rtc_read(0xd0 , 0x04);
	lcd_char(ch/16+48);
	lcd_char(ch%16+48);
	lcd_char('-');

	ch = rtc_read(0xd0 , 0x05);
	lcd_char(ch/16+48);
	lcd_char(ch%16+48);
	lcd_char('-');

	ch = rtc_read(0xd0 , 0x06);
	lcd_char(ch/16+48);
	lcd_char(ch%16+48);



}



int main()
{
	DDRC =0XFF;
	DDRD =0XFF;
	init_i2c();
	lcd_init();
	lcd_string("ready");
	
	rtc_write(0xd0,0x00,0x00);
	rtc_write(0xd0,0x01,0x27);
	rtc_write(0xd0,0x02,0x08);

	rtc_write(0xd0,0x04,0x08);
	rtc_write(0xd0,0x05,0x09);
	rtc_write(0xd0,0x06,0x13);

	_delay_ms(1000); 

	while(1)
	{
		disp_time_date();
	}

}


  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

any other insights are also welcome

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tired to make my own code to begin with but i didn't go far with it when i found out i was re inventing the wheel. Peter Fleury has I2C etc library's for what you want to do. Maybe look at his code to see where you maybe going wrong. A scope may help too. Its possible to get yours working but chances are someone else maybe quicker at solving it than me.
I noticed that TWCR = (1<<TWEN); in void init_i2c() wasn't in peters init
Check your clock speed with a 12Mhz crystal & a bus speed of 100khz my calculations say that comes to 0x34 not 0x2C.
but i dont think that will fix all of your problems.