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 / problem with Interfacing ds1307 with atmega 32.

problem with Interfacing ds1307 with atmega 32.

|

Microcontroller › AVR › problem with Interfacing ds1307 with atmega 32.

  • This topic has 2 replies, 3 voices, and was last updated 13 years, 10 months ago by romel emperado.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • July 20, 2011 at 4:47 am #1203
    sunilkumar
    Participant

    Hello everyone..
    i have a problem here. i am using avr studio/avr gcc,16x2LCD(8-bit i/o mode),atmega32. i have compiled the below code ..
    also i am not using any keys.(will use it later). i just want to see running clock.(i.e running from default time.). but it is showing “hello world” “00:00:00 AM”. and time wont update at all..my connections are LCD to Port A& B,SCL,SDA are as usual. please help.. thanks.
    please forgive if this is not the right section to post my doubt..
     

    Code:
    #include <avr/io.h>
    #include <util/delay.h>

    #define FALSE 0
    #define TRUE 1

    #define LCD_DATA PORTA
     
    #define ctrl PORTB
    #define en PB2
    #define rw PB1
    #define rs PB0
    #define CH 7
    void LCD_cmd(  char cmd);
    void init_LCD(void);
    void LCD_write( char data);
    void LCD_write_string( char *str);
    uint8_t DS1307Read(uint8_t address,uint8_t *data);
    uint8_t DS1307Write(uint8_t address,uint8_t data);
    uint8_t I2CReadByte(uint8_t *data,uint8_t ack);
    uint8_t I2CWriteByte(uint8_t data);
    void I2CInit();
    void I2CClose();
    void I2CStart();
    void I2CStop();

    int main(void)
    {
        char Time[12];
       uint8_t data;
       
       DDRA=0xFF;
       DDRB=0xFF;
       init_LCD();
       I2CInit();
        uint8_t temp;
       DS1307Read(0x00,&temp);

       //Clear CH Bit
       temp&=(~(1<<CH));

       DS1307Write(0x00,temp);

       //Set 12 Hour Mode
       DS1307Read(0x02,&temp);

       //Set 12Hour BIT
       temp|=(0b01000000);

       //Write Back to DS1307
       DS1307Write(0x02,temp);

       LCD_write_string(“hello world”);
          while(1)
       {
             DS1307Read(0x00,&data);
          Time[8]=’’;
          Time[7]=(0x30)+(data & 0b00001111);
          Time[6]=(0x30)+((data & 0b01110000)>>4);
          Time[5]=’:’;
       
          DS1307Read(0x01,&data);
       
          Time[4]=(0x30)+(data & 0b00001111);
          Time[3]=(0x30)+((data & 0b01110000)>>4);
          Time[2]=’:’;
       
          DS1307Read(0x02,&data);
       
          Time[1]=(0x30)+(data & 0b00001111);
          Time[0]=(0x30)+((data & 0b00010000)>>4);
          LCD_cmd(0xC0);
           LCD_write_string(Time);
          if(data & 0b00100000)
          LCD_write_string(“PM”);
          else
          LCD_write_string(“AM”);
       }      
          
       return 0;
    }
    uint8_t DS1307Read(uint8_t address,uint8_t *data)
    {
       uint8_t res;   //result
       
       //Start
       I2CStart();
       
       //SLA+W (for dummy write to set register pointer)
       res=I2CWriteByte(0b11010000);   //DS1307 address + W
       
       //Error
       if(!res)   return FALSE;
       
       //Now send the address of required register
       res=I2CWriteByte(address);
       
       //Error
       if(!res)   return FALSE;
       
       //Repeat Start
       I2CStart();
       
       //SLA + R
       res=I2CWriteByte(0b11010001);   //DS1307 Address + R
       
       //Error
       if(!res)   return FALSE;
       
       //Now read the value with NACK
       res=I2CReadByte(data,0);
       
       //Error
       if(!res)   return FALSE;
       
       //STOP
       I2CStop();
       
       return TRUE;
    }

    uint8_t DS1307Write(uint8_t address,uint8_t data)
    {
       uint8_t res;   //result
       
       //Start
       I2CStart();
       
       //SLA+W
       res=I2CWriteByte(0b11010000);   //DS1307 address + W
       
       //Error
       if(!res)   return FALSE;
       
       //Now send the address of required register
       res=I2CWriteByte(address);
       
       //Error
       if(!res)   return FALSE;
       
       //Now write the value
       res=I2CWriteByte(data);
       
       //Error
       if(!res)   return FALSE;
       
       //STOP
       I2CStop();
       
       return TRUE;
    }

    void I2CInit()
    {
       //Set up TWI Module
       TWBR = 2;
       TWSR |=((1<<TWPS1)|(1<<TWPS0));

       //Enable the TWI Module
       TWCR|=(1<<TWEN);

    }

    void I2CClose()
    {
       //Disable the module
       TWCR&=(~(1<<TWEN));
    }

    void I2CStart()
    {
       //Put Start Condition on Bus
       TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTA);

       //Poll Till Done
       while(!(TWCR & (1<<TWINT)));

    }

    void I2CStop()
    {
       //Put Stop Condition on bus
       TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
       
       //Wait for STOP to finish
       while(TWCR & (1<<TWSTO));
       //_delay_loop_2(250);
    }

    uint8_t I2CWriteByte(uint8_t data)
    {
       
       TWDR=data;   

       //Initiate Transfer
       TWCR=(1<<TWEN)|(1<<TWINT);

       //Poll Till Done
       while(!(TWCR & (1<<TWINT)));

       //Check Status
       if((TWSR & 0xF8) == 0x18 || (TWSR & 0xF8) == 0x28 || (TWSR & 0xF8) == 0x40)
       {
          //SLA+W Transmitted and ACK received
          //or
          //SLA+R Transmitted and ACK received
          //or
          //DATA Transmitted and ACK recived

          return TRUE;
       }
       else
          return FALSE;   //Error
    }

    uint8_t I2CReadByte(uint8_t *data,uint8_t ack)
    {
       //Set up ACK
       if(ack)
       {
          //return ACK after reception
          TWCR|=(1<<TWEA);
       }
       else
       {
          //return NACK after reception
          //Signals slave to stop giving more data
          //usually used for last byte read.
          TWCR&=(~(1<<TWEA));
       }

       //Now enable Reception of data by clearing TWINT
       TWCR|=(1<<TWINT);

       //Wait till done
       while(!(TWCR & (1<<TWINT)));

       //Check status
       if((TWSR & 0xF8) == 0x58 || (TWSR & 0xF8) == 0x50)
       {
          //Data received and ACK returned
          //   or
          //Data received and NACK returned

          //Read the data

          *data=TWDR;
          return TRUE;
       }
       else
          return FALSE;   //Error
       
    }

       
       

    void init_LCD(void)      //LCD Initialization
    {
    LCD_cmd(0x38);
    _delay_ms(1);
    LCD_cmd(0x01);
    _delay_ms(1);
    LCD_cmd(0x0F);
    _delay_ms(1);
    LCD_cmd(0x80);
    _delay_ms(1);
    return;
    }
     
     
    void LCD_cmd( char cmd) //LCD command write
    {
    LCD_DATA=cmd;
    ctrl =(0<<rs)|(0<<rw)|(1<<en);
    _delay_ms(1);
    ctrl =(0<<rs)|(0<<rw)|(0<<en);
    _delay_ms(50);
    return;
    }
     
     
    void LCD_write( char data) //LCD write character
    {
    LCD_DATA= data;
    ctrl = (1<<rs)|(0<<rw)|(1<<en); // making RW as LOW and RS, EN as HIGH
    _delay_ms(1);
    ctrl = (1<<rs)|(0<<rw)|(0<<en); // making EN and RW as LOW and RS HIGH
    _delay_ms(50); // give a 10 milli second delay to get thigs executed
    return ;
    }
     
    void LCD_write_string(char *str) //LCD Write String
    {
    int i=0;
    while(str!=’’)
    {
    LCD_write(str
    );
    i++;
    }

     
    July 30, 2011 at 1:08 pm #6557
    mreza
    Participant

    i have another code that works with avr studio , ds1307 , and mega32 .

     

    contact me if you want that yet .

     

     

    [email protected]

    August 3, 2011 at 1:10 pm #6567
    romel emperado
    Participant

    very good example of interfacing RTC: check this link and it has already the code

     

    http://adf.ly/2GyVB

     

    code:

     

    http://adf.ly/2GyW8

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

RSS Recent Posts

  • Curved lines in PCB design June 15, 2025
  • Wideband matching an electrically short bowtie antenna; 50 ohm, 434 MHz June 15, 2025
  • using a RTC in SF basic June 15, 2025
  • PIC KIT 3 not able to program dsPIC June 15, 2025
  • Siemens large industrial PLC parts June 15, 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