Mega4809 Uno Wifi Rev2 i2c

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

Hi all,

First off I'm rather new to the whole world of microcontrollers. I come from a chemical engineering background, so my apologies if my terminology is a little off.

I have an Arduino Uno for which the following code runs, shamelessly acquired from Joop Brokking's Quadcopter youtube series. 

  Serial.println("Initializing I2C clock speed...");
  TWBR = 12;                                                                //Set the I2C clock speed to 400kHz.
  #if F_CPU == 16000000L                                                    //If the clock speed is 16MHz include the next code line when compiling
    clockspeed_ok = 1;                                                      //Set clockspeed_ok to 1
  #endif    

From what I understand, it sets the I2C clock speed to 400kHz (thanks, comments), then checks that the clock speed is running at the usual 16MHz before executing some other code. I'm currently busy with a control systems project, where I need some wifi goodness to store data on my laptop. Enter the Uno Wifi Rev2, which I picked up for free from a colleague.

I've read somewhere that the I2C clock defaults to 100kHz, and from that I gather I need to set the speed to 400kHz so that the current setup runs. I've delved a little into the Datasheet for the ATmega4809, and I've found that I can set the TWIn.MBAUD register to a value that results in the TWI bus clock frequency to run at 400kHz, however, I cannot find any code examples on this (I'm starting to regret not majoring in electronic engineering) or an easy way of calculating the correct value. The datasheet has some equations for calculating f_SCL (25.3.4.2.1), but with the wrong background, I'm cautious to dive into a rabbit hole and getting stuck for too long.

If it's of any help at all, I'm building a quadcopter and need the gyro data to do some modelling.

 

Any suggestions? :D 

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

If you're using Arduino code/libraries, you can call Wire.SetClock(frequency).  If you running bare code on Arduino compatible hardware, SetClock() is doing this:

  TWBR = ((F_CPU / frequency) - 16) / 2;