programming micrcontroller using arduino I2C

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, this is my first post . 

I am learning avr micrcontroller I2C protocol

 

I have connected atmega 8l-8pu with arduino using I2c 

 

can I program atmega for simple led lighting using arduino by writing  to the DDR and PORT register of atmega by arduino as master 

 

 

my proteus schematic is given below and atmega8/l datasheet snap

 

i was able to connect it to arduino ( connection was verified by I2C scanner arduino code) .but unable to write the DDR and PORT for lighting led.

 

Can anyone tell me that is there any other condition related to CLock  frequency of slave and master .

 

      ____________________________________________________SLAVE CODE_________________________________________________________

# include<avr/io.h> 

# include<util/delay.h>

 

 int main(void) { 

       

        TWAR=0b00000110;                                                //Assiging address of 3
        TWCR=(1<<TWEA)|(1<<TWEN)|(1<<TWINT);             // enabling TWI
        return 0;

     }

 

____________________________________________MASTER ARDUINO CODE______________________________________________________

 

# include<Wire.h>

void setup() {
 

  Wire.begin();
 

  Wire.beginTransmission(0x03);//Device address
  Wire.write(0x11);                     //  DDRD register Address
  Wire.write(B11111111);          //  Writing to DDRD register
  Wire.endTransmission();

 

 

 Wire.beginTransmission(0x03);   //Device address
 Wire.write(0x12);                       // PORTD register address
 Wire.write(B11111111);             // writing for output mode at all D PORT pins
 Wire.endTransmission();
}

void loop() {

                    // put your main code here, to run repeatedly:
                  }

 

 

 

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you misunderstand how i2c works. First up - where’s the rest if the i2c code for the slave? There’s no inbuilt magic that allows you to access the AVR registers via i2c - you need to add this.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you tell me what part of the process I am missing ?(i.e how to get acess to the Avr register ?)

 

I thought that the above mentioned way was enough to access to the register ( I thought it as same as writing to the EEPROM  in which we follow this way of writing to the registers directly )

 

This is as far as I was able to understand from datasheet of Atmega8/L about I2c.

 

It would be a great help if tell me what do i have to do to light an led using avr as slave but commanded by Arduino

 

 

And by " where’s the rest if the i2c code for the slave?" I think you meant about like writing a code for a slave that if it gets some data use that data for DDRD and PORTD isn't it?

 

--Thanks--

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You thought wrong! Microcontrollers usually aren’t accesed like i2c eeprom - at least not the ones i’ve come across. You need toget yourself a i2c slave library ( or write one yourself - it’s not too difficult) and then write code to make it work like a i2c eeprom.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As Kartman says, when you control and EEPROM or an RTC or something by I2C there's effectively a little I2C slave engine "running" in the EEPROM/RTC/etc that implements the comms interface and also probably defines some kind of "protocol" when command bytes you send like 0x03 or 0x0D or something have meanings like "set address counter", "read alarm register", etc etc.

 

To do this with an AVR you have to implement all the in the AVR slave. So start by implementing just some core I2C software that can simply receive and send bytes. Then think up some kind of protocol for how you are going to control the slave. You might decide that command byte 0x01 means "set DDR for port B" and command byte 0x02 means "set PORT register for port B".

 

From the controlling I2C master you might then send 01 FF 02 AA or something where 01 FF means "set DDRB to FF" and 02 AA means "set PORTB to AA". Now all the pins in B will be output and alternate pins will be 1 or 0.

 

You don't get all this "for free". You have to implement all this.

 

It's actually quite an ambitious project if you are just starting out with MCUs.

Last Edited: Fri. Nov 9, 2018 - 01:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Two more things that is missing from your schematic are:

 Pull up resistors on the SDA & SCL lines, I would suggest 4.7k connecting from each line to VCC

 and you need a current limiting resistor in series with your LED, I would suggest a 330 ohm.

 

Although your "simulation" may work, in real life without these "details" a real circuit will not work (or will work until the smoke is let out!)

 

Jim

 

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274