Microcontroller › AVR › DS1624+ATMEGA32+AVR GCC
- This topic has 3 replies, 3 voices, and was last updated 12 years, 10 months ago by
Amrith.
-
AuthorPosts
-
May 4, 2012 at 2:35 pm #1784
Ivan
Participanti have a problem with communications ATMEGA32 and DS1624 with I2C/TWI…
anybody helps?
code:
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/delay.h>
#include <compat/twi.h>#define START 0X08
#define PONOVLJENI_START 0X10
#define MASTER_PRIMIO_PODATAK_ACK 0X50
#define MASTER_PRIMIO_PODATAK_NACK 0X58
#define MT_SLA_ACK 0x18
#define MT_DATA_ACK 0x28#define DS1624_W 0x90
#define DS1624_R 0x91
#define ACCESS_MEMORY 0X17
#define ACCESS_CONFIG 0XAC
#define READ_TEMPERATURE 0XAA
#define START_CONVERT 0XEE
#define STOP_CONVERT OX22
#define CONVERSION 0x00unsigned char TWI_master(void);
unsigned char TWI_start(void);
unsigned char TWI_repeat_start(void);
unsigned char TWI_posalji_adresu(unsigned char);
unsigned char TWI_posalji_command_protocol(unsigned char);
unsigned char TWI_conversion(unsigned char);
unsigned char TWI_primi_MSB(unsigned char);
unsigned char TWI_primi_LSB(unsigned char);
unsigned char TWI_stop(void);unsigned char DS1624_init(void);
unsigned char DS1624_convert(void);
unsigned char DS1624_temperaturaH(unsigned char);unsigned char temp,tempH,tempL,C1,C2;
int main(void)
{
DDRB=0xFF;TWI_master();
//_delay_ms(500);
DS1624_init();float tm;
while(1)
{DS1624_convert();
_delay_ms(500);DS1624_temperaturaH(temp);
PORTB=temp;
}
}unsigned char TWI_master(void)
{
TWCR=0x00;
TWBR=0x02;
TWSR=0x00;
TWCR=0x44;
}unsigned char TWI_start(void)
{
TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)));
while((TWSR & 0xF8)!= START);}
unsigned char TWI_repeat_start(void)
{
TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)));
while((TWSR & 0xF8)!= PONOVLJENI_START);
}unsigned char TWI_posalji_adresu(unsigned char adresa)
{
TWDR=adresa;
TWCR=(1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
while((TWSR & 0xF8)!= MT_SLA_ACK);
}unsigned char TWI_posalji_command_protocol(unsigned char data)
{
TWDR=data;
TWCR=(1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
while((TWSR & 0xF8) != MT_DATA_ACK);
}unsigned char TWI_conversion(unsigned char conv)
{
TWDR=conv;
TWCR=(1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
while((TWSR & 0xF8) != MT_DATA_ACK);}
unsigned char TWI_primi_MSB(unsigned char MSB)
{
TWCR = (1<<TWEA)|(1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
while((TWSR & 0xF8) != MASTER_PRIMIO_PODATAK_ACK);
MSB=TWDR;
}unsigned char TWI_primi_LSB(unsigned char LSB)
{
TWCR = (1<<TWEA)|(1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
while((TWSR & 0xF8) != MASTER_PRIMIO_PODATAK_NACK);
LSB=TWDR;
}unsigned char TWI_stop(void)
{
TWCR= (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
while(!(TWCR & (1<<TWSTO)));
}unsigned char DS1624_init(void)
{
TWI_start();
TWI_posalji_adresu(DS1624_W);
TWI_posalji_command_protocol(ACCESS_CONFIG);
TWI_conversion(CONVERSION);
TWI_stop();}
unsigned char DS1624_convert(void)
{
TWI_start();
TWI_posalji_adresu(DS1624_W);
TWI_posalji_command_protocol(START_CONVERT);
TWI_stop();}
unsigned char DS1624_temperaturaH(unsigned char tempC)
{
TWI_start();
TWI_posalji_adresu(DS1624_W);
TWI_posalji_command_protocol(READ_TEMPERATURE);
TWI_repeat_start();
TWI_posalji_adresu(DS1624_R);
TWI_primi_MSB(tempH);
_delay_ms(500);
TWI_primi_LSB(tempL);
_delay_ms(500);
TWI_stop();}
May 6, 2012 at 5:57 pm #7600AJISH ALFRED
ParticipantPlease share what exact problem are you facing with the code?
May 9, 2012 at 7:22 am #7661Amrith
ParticipantHi Ivan,
Looks fine with the program, did you add proper external pull up resistors to the lines SCL & SDA. If yes, can you tell me what problem you re facing.
Regards
Amrith
May 9, 2012 at 7:22 am #7682Amrith
ParticipantHi Ivan,
Looks fine with the program, did you add proper external pull up resistors to the lines SCL & SDA. If yes, can you tell me what problem you re facing.
Regards
Amrith
-
AuthorPosts
- You must be logged in to reply to this topic.