I2C Connection

mikroC, mikroBasic and mikroPascal for PRO ARM® MCUs, supporting STM32, Tiva, Kinetis, & CEC devices
Post Reply
Author
Message
STM_Learner
Posts: 3
Joined: 04 May 2017 06:57

I2C Connection

#1 Post by STM_Learner » 08 May 2017 07:26

Hi There,

Can some one guide me with simple example of I2C connection
I2C2_Init_Advanced(100000, &_GPIO_MODULE_I2C2_PF01); //I2C initiate

Below is the example code of Adrno and I am trying to use same hardware with STM32F407ZG

Thank you

JP







#include "Wire.h"

#define I2C_ADDR 0x20 // 0x20 is the address with all jumpers removed

void setup()
{
Serial.begin( 38400 );
Serial.println("RELAY8 demonstration starting up");

Wire.begin(); // Wake up I2C bus

// Set I/O bank A to outputs
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x00); // IODIRA register
Wire.write(0x00); // Set all of bank A to outputs
Wire.endTransmission();

Serial.println("Ready. Type 0 to turn off relays, 1 - 8 to activate a relay.");
}

void loop()
{
int command = 0;
if (Serial.available()) {
command = Serial.read();
if( command == '0' )
{
sendValueToLatch(0);
Serial.println("Resetting all relays");
}
if( command == '1' )
{
sendValueToLatch(1);
Serial.println("Activating relay 1");
}
if( command == '2' )
{
sendValueToLatch(2);
Serial.println("Activating relay 2");
}
if( command == '3' )
{
sendValueToLatch(4);
Serial.println("Activating relay 3");
}
if( command == '4' )
{
sendValueToLatch(8);
Serial.println("Activating relay 4");
}
if( command == '5' )
{
sendValueToLatch(16);
Serial.println("Activating relay 5");
}
if( command == '6' )
{
sendValueToLatch(32);
Serial.println("Activating relay 6");
}
if( command == '7' )
{
sendValueToLatch(64);
Serial.println("Activating relay 7");
}
if( command == '8' )
{
sendValueToLatch(128);
Serial.println("Activating relay 8");
}
}
}

void sendValueToLatch(int latchValue)
{
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select GPIOA
Wire.write(latchValue); // Send value to bank A
Wire.endTransmission();
}

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: I2C Connection

#2 Post by darko.ilijevski » 08 May 2017 16:47

Hello,

The Arduino code is much different from the code you'd write to program STM32 using mikroC PRO for ARM compiler. You can't simply translate the code like that, because of the differences in the architecture. So I could give you a small example on how to read and write data to an address of one particular sensor (accelerator), but there are many preparations beforehand - for the clock of the MCU mostly:

Code: Select all

I2C1_Init_Advanced(400000, &_GPIO_MODULE_I2C1_PB67);  //  inits the I2C1 module on the pins PB6 and PB7 with the clock speed of 400 KHz

unsigned short LIS3DSH_Read(unsigned short address) {
  I2C1_Start();              // issue I2C start signal
  data_[0] = address;
  I2C1_Write(LIS3DSH_ADDRESS, data_, 1, END_MODE_RESTART);  //  Writes data_ on the LIS3DSH_ADDRESS (the IC's address) and sets end mode to END_MODE_RESTART  
  I2C1_Read(ACCEL_ADDR, data_, 1, END_MODE_STOP);  //  Reads data from the set address, it's already in the "restart" mode,bcs of the END_MODE_RESTART parameter
  return data_[0];
}

void LIS3DSH_Write(unsigned short address, unsigned short data1) {
  I2C1_Start();              //  issue I2C start signal
  data_[0] = address;
  data_[1] = data1;
  I2C1_Write(LIS3DSH_ADDRESS, data_, 2, END_MODE_STOP); // Writes I2C1 data_ to LIS3DSH_ADDRESS, it makes 2 iterations (data_ is an array, so it's possible) and stops 
}

There you go. That is a simple function for reading/writing data to LIS3DSH acceleration sensor.
BR,
Darko

Post Reply

Return to “ARM PRO Compilers”