Microcontroller › AVR › I2C (TWI) address transmission to slave (problem)
- This topic has 1 reply, 2 voices, and was last updated 6 years, 10 months ago by
GANEEV SINGH.
-
AuthorPosts
-
February 26, 2016 at 11:31 am #4326
prathap
ParticipantDear Friends,
Hope you all doing well,, I got a probelm in my project with I2C address transmission to slave, Here my master is ATmega32 and Slave is RTC(DS1307), so while transmitting the slave address i am getting correct status code as it is 0x18, and then i have to transmit the internal memory address right? so i transmitted 0x00 for seconds memory address as i referred data shet, but this time i am getting status value is 0x28 but it's the value for data transmission not for memory address transmission, anyway after this memory address transmission i am transferring the data, but guys it's not transmitting at all and i am not getting any status value of it!!!!!
I hope you understand my problem, how to transfer the slave address and memory address for writing values into specific memory location in avr atmega!!!!!!
here is the code!!!! for I2C transmission!!! Please give me a way to Understand and finish it!!! and i don't have debugger with me,, can you please suggest which debugger is good for avr atmega32 in Atmel Studio 6 or 7 IDE.??????
Code:
/*
* I2C_DS1307.c
*
* Created: 25-Feb-16 11:07:35 AM
* Author: sys1
*/#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <inttypes.h>
#include "ADC_LCD.h"
#include <avr/interrupt.h>void TWI_Master_Init(void);
void TWI_Start(void);
void TWI_Write_Address(unsigned char );
void TWI_Write_Data(unsigned char data);
void TWI_Stop(void);//unsigned char address = 0xD0, write = 0, read = 1, write_data = 0xA0;
//unsigned char seconds_address = 0x00;
//unsigned char recv_data;int main(void)
{
//DDRA |= 0b00000111;
//PORTA |= (1<<PINA0) | (1<<PINA1);
//PORTA &= ~(1<<PINA0)| ~(1<<PINA1) | ~(1<<PINA2);
//TWI_Master_Init();
lcd_init();
_delay_ms(2000);
while(1)
{
TWI_Start();
TWI_Write_Address(0xD0 + 0);
TWI_Write_Address(0x00+0);
TWI_Write_Data(0xA0); //i want to write 10 in seconds memory location of DS1307(RTC)
TWI_Stop();
}
}void TWI_Master_Init(void)
{
TWBR = 0x01;
TWSR = (0<<TWPS1) | (0<<TWPS0);
//SCL frequency = F_CPU/(16+2(TWBR).4^TWPS)
}void TWI_Start(void)
{
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
while(!(TWCR &(1<<TWINT)));
while((TWSR & 0xF8) != 0x08);
}void TWI_Write_Address(unsigned char data)
{
TWDR = data;
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR &(1<<TWINT)));
while((TWSR & 0xF8) != 0x18);
}
}void TWI_Write_Data(unsigned char data)
{
TWDR = data;
TWCR = (1<<TWINT) | (1<<TWEN);
while(!(TWCR &(1<<TWINT)));
while((TWSR & 0xF8) != 0x28);
}void TWI_Stop(void)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
while(!(TWCR & (1<<TWSTO)));
}March 20, 2018 at 7:17 am #14805GANEEV SINGH
ParticipantHi
I think I have found out the mistake. When you are sending data i.e. TWI_Write_Data(0xA0); //i want to write 10 in seconds memory location of DS1307(RTC), you are actually enabling the CH(Clock Halt) bit which is why RTC is not responding afterwards.
The thing is while writing seconds onto it's register, the 8th bit or BIT7 is dedicated for CH. So when you are writing 0xA0 you are also activating CH bit which is halting the clock of RTC. It you want to write 10 seconds then try sending 0X10 or 0X0A because RTC's register is divided into two parts BIT0-3 which accept values for 1 x seconds while BIT4-6 accept values for 10 x seconds i.e if you write 2 in former part then it will be 2 seconds but if you do the same in latter part then it will be traeted as 20 seconds.
Refer to its datasheet once agin and confirm.
-
AuthorPosts
- You must be logged in to reply to this topic.