Author Topic: i2c programming  (Read 2755 times)

m777

  • Newbie
  • *
  • Posts: 10
i2c programming
« on: October 18, 2014, 01:39:45 PM »
Hello,

First of all, I'm new to microcontroller programming and I have a basic understanding of the python programming language.  I heard about the arduino boards but it uses the c language.  I prefer to use python so I did a search and found pymcu.  I work for a semiconductor company and most of our parts have the i2c interface.  Our parts are mostly switching buck/boost regulator IC's.  One example test would be changing the output voltage by sending command bytes.  I would like to write a program preferably using a gui user interface by writing and reading those commands bytes into the IC registers.  I have the following test mode map.

slave address:  0x5B
Register Address:     Command Bytes/Data Bytes:
0x01                                   0x05
0x02                                   0xA1
0x05                                   0x02
0x08                                   0xF4

I need to send 5 total bytes including the slave address at one time to get into the test mode.
How do I write this test mode using the i2cWrite command.  And after writing, I want to be able to read using the i2cRead command.
I appreciate your help on this and thank you in advance.



richard

  • Administrator
  • Jr. Member
  • *****
  • Posts: 95
Re: i2c programming
« Reply #1 on: October 21, 2014, 08:18:43 PM »
Hi,

To do what you need try this:

Code: [Select]
import pymcu

i2cAddr = 0x5B

mb = pymcu.mcuModule()

mb.i2cWrite(i2cAddr, 0x01, 0x05)
mb.i2cWrite(i2cAddr, 0x02, 0xA1)
mb.i2cWrite(i2cAddr, 0x05, 0x02)
mb.i2cWrite(i2cAddr, 0x08, 0xF4)


Because your register addresses are not contiguous you would need to send each one and specify the address, although you could do 1 and 2 in one command.
like this:
mb.i2cWrite(i2cAddr, 0x01, [0x05,0xA1])

if they were all contiguous then you could do this:
mb.i2cWrite(i2cAddr, 0x01, [0x05,0xA1,0x02,0xF4])

Reading is very similar:
readVar = mb.i2cRead(i2cAddr, 0x01)

if you need to read more than one byte then specify the number of bytes you would like to read:
readVar = mb.i2cRead(i2cAddr, 0x01, 3)

Without seeing the data sheet I can't verify if the information is correct so give it a try and let me know how it goes.

For more examples check out some of the code on github:
https://github.com/circuitsforfun/pyMCU/blob/master/Examples/PCA9685_Servo.py
https://github.com/circuitsforfun/pyMCU/blob/master/Examples/LM75B_TempSensor.py
https://github.com/circuitsforfun/pyMCU/blob/master/Examples/BMP085_Barometer.py

or another topic on the forum dealing with i2c and the BlinkM module:
http://www.circuitsforfun.com/smf/index.php?topic=56.0


m777

  • Newbie
  • *
  • Posts: 10
Re: i2c programming
« Reply #2 on: October 28, 2014, 05:31:46 PM »
Hello Richard,

Looks like the I2C SCL frequency for the pymcu board is 100kHz.  Any plans for future update for 400kHz data transfer rate or higher?

richard

  • Administrator
  • Jr. Member
  • *****
  • Posts: 95
Re: i2c programming
« Reply #3 on: October 28, 2014, 10:25:25 PM »
Yes I would like to eventually add a config function to be able to change the clock speed.
That might have to wait for pyMCU 2.0 though.

m777

  • Newbie
  • *
  • Posts: 10
Re: i2c programming
« Reply #4 on: November 13, 2014, 01:06:23 PM »
To have slave address scan function would be nice.  When do you think Pymcu version 2.0 would be available?

riklaunim

  • Newbie
  • *
  • Posts: 48
Re: i2c programming
« Reply #5 on: November 16, 2014, 11:41:14 AM »
To have slave address scan function would be nice.  When do you think Pymcu version 2.0 would be available?

That idea was here since long time. I'm curious too, but in the meantime I have pyboard with MicroPython and WeIO soon on its way :)