Connect with us

I2C programming ?

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Apr 10, 2015.

  1. vead

    vead

    473
    14
    Nov 27, 2011
    Hello ,

    I need help to understand I2C programming for Microcontroller.
    1. Initializing I2C
    2. start I2C
    3. stop I2C
    4.ACK for I2C
    5. NAK for I2C

    I am writing function one bye one. if there is any mistake. let me know

    1. Initializing I2C
    Code:
    void I2C_Init()
    {
        SDA = 1;
        SCL = 1;
    }
    
    2. start I2C
    Code:
    void I2C_Start()
    {
        SDA = 0;
        SCL = 0;
    }
    

    3. stop I2C
    Code:
    void I2C_Stop()
    {
        SCL = 0;
        SDA = 0;
        SCL = 1;
        SDA = 1;
    }
    
    4.ACK for I2C
    Code:
    void I2C_Ack()
    {
        SDA = 0;
        SCL = 1;
        SCL = 0;
        SDA = 1;
    }
    
    5. NAK for I2C
    Code:
    void I2C_Nak()
    {
        SDA = 1;
        SCL = 1;
        SCL = 0;
        SDA = 1;
    }
    
    If above all points are correct then I want to write another function, so please tell me if there are any mistake ?
     
  2. vead

    vead

    473
    14
    Nov 27, 2011
    actually I am interfacing RTC with microcontroller and LED.
    actually I have prototyping KIt (cortex m0). I only need c code.
    I want to control LED using real time clock via I2c pins

    Set alarm for 5 second (automatically turn on LED for 5 second )
    Set alarm for 3 minute (automatically turn on LED for 3 minute)
    Set alarm for 1 hours (automatically turn on Led for 1 hours )

    SCL connected with P3.5;
    SDA connected with P3.1;
    LED connected with P1.6

    I did google search and I am trying to write some code myself

    Code:
            void I2C_Init()         //
            {
                SDA = 1;
                SCL = 1;
            }
    
            void I2C_Start()       // start I2C
            {
                SDA = 0;
                SCL = 0;
            }
    
            I2C_write()               //  send data to Ds1307
            {
            I2C_write(0xD0);      // address of the DS1307 )
            I2C_write(0x00);      // address of seconds register
            I2C_write(0x10);       // 10 data as 10 second
            }
    
            void I2C_Ack()
            {
                SDA = 0;
                SCL = 1;
                SCL = 0;
                SDA = 1;
    
            void I2C_Stop()    // stop I2C
            {
                SCL = 0;
                SDA = 0;
                SCL = 1;
                SDA = 1;
            }
    
            void I2C_Start()
            {
                SDA = 0;
                SCL = 0;
            }
    
            I2C_read()
            {
            I2C_write(0xD1);      // address of the DS1307 )
            I2C_write(0x00);      // address of seconds register
            I2C_write(0x10);       // 10 data as 10 second
            }
    
            void I2C_Nak()
            {
                SDA = 1;
                SCL = 1;
                SCL = 0;
                SDA = 1;
            }
    
            void I2C_Stop()    // stop I2C
            {
                SCL = 0;
                SDA = 0;
                SCL = 1;
                SDA = 1;
            }
    
           
                  
    
    I don't understand how to set alarm for second , minutes and hours ?
     
  3. JWHassler

    JWHassler

    77
    16
    Dec 22, 2014
    There are a few problems here.
    1. The section of code "I2C_write()" is recursive, (calls itself) and that's not usually helpful.
    2 "I2C_write()" also sends a parameter to a function that does not use one.
    3. ... or maybe it does: there are no prototype-declarations shown
    4 "I2C_read()" returns nothing: in other words does not read. I don't think that's what you want


    If you have a dev-kit for your micro-controller, you should have some sample-code to use. There's no shame in doing so, and you will get going much faster
     
    Arouse1973 likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-