EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / DS1624+ATMEGA32+AVR GCC

DS1624+ATMEGA32+AVR GCC

|

Microcontroller › AVR › DS1624+ATMEGA32+AVR GCC

  • This topic has 3 replies, 3 voices, and was last updated 13 years, 6 months ago by Amrith.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • May 4, 2012 at 2:35 pm #1784
    Ivan
    Participant

    i 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 0x00

    unsigned 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 #7600
    AJISH ALFRED
    Participant

    Please share what exact problem are you facing with the code?

    May 9, 2012 at 7:22 am #7661
    Amrith
    Participant

    Hi 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 #7682
    Amrith
    Participant

    Hi 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

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Manually actuate fuel tank selector solenoid November 9, 2025
  • strange laptop problem November 9, 2025
  • JLCPBC are using a different shipping company = less $$$$$$$$ November 8, 2025
  • Help please! BLDC driver circuit using the IR2136s and the STP80NF06 MOSFETS November 8, 2025
  • need two ICs November 8, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise