Maker Pro
Maker Pro

I2C programming ?

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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 ?
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
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 ?
 

JWHassler

Dec 22, 2014
85
Joined
Dec 22, 2014
Messages
85
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
 
Top