mega169 I2C hair-pulling session

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

I need to talk to an I2C proximity sensor, using a meag169 at the uC. I'm using Studio 7. I have a fairly complex project started in Studio 7 that originally used a different sensor, but now require changing to an I2C sensor. It seems that most of what I can find is aimed at the Arduino environment, and I'm not using that and don't want to - I need some real debugging capability.

I found an I2C package by Peter Fleury, but I cannot seem to find a way to make the system build my test application.  From what I can tell, this particular package is fairly old, and not compatible with the mega169, as the register definitions used don't match the USI in the mega169.

So, I am getting toward my wits end. What library (or source/header combo) can I use with AS7 that has functions for I2C on a mega169 with the USI?

This topic has a solution.
Last Edited: Tue. Mar 19, 2019 - 01:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've no experience with the mega169, but you could use the bit-bash i2c code that has no reliance on TWI or USI hardware. This might speed your progress.

 

 

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

Kartman wrote:

I've no experience with the mega169, but you could use the bit-bash i2c code that has no reliance on TWI or USI hardware. This might speed your progress.

 

 

I am not sure which packages would do this. I know that there are some libraries for Arduino that apparently do this kind of I2C simulation, but I'm hoping for something that can use the serial interface already in the mega169.

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

I believe the Peter Fleury package has the bit-bash option. There's probably not much advantage from using USI in an I2C master application. Using bit-bash - you have a greater choice of what port pins you can use.

 

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

Surely the mega169 has got TWI and USI
Which means that you can compare the behaviour / capabilities of both peripherals.

.
Typed on a tablet from memory. The Butterfly has an mega169.
Even if I am wrong, you can bit-bang an I2C Master with Fleury's i2cmaster.S
.
David.

 

Edit.  I should not type directly from my bottom.   The datasheet shows that the m169 does not have TWI.

Regarding USI and TWS.   The Tiny1634 does have both of these peripherals

Last Edited: Tue. Mar 5, 2019 - 08:33 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to point out that the manual for Peter Fleury's I2C is here:

 

http://homepage.hispeed.ch/peter...

 

As it says there:

This I2c library is implemented as a compact assembler software implementation of the I2C protocol which runs on any AVR (i2cmaster.S) and as a TWI hardware interface for all AVR with built-in TWI hardware (twimaster.c). Since the API for these two implementations is exactly the same, an application can be linked either against the software I2C implementation or the hardware I2C implementation.

So on AVR that have TWI you use twimaster.c but on those that don't, like the 169, you use i2cmaster.S instead. So you still (like test_i2cmaster.c) include i2cmaster.h but in the list of files you build it's i2cmaster.S and not twimaster.c. By default that .S file has:

;******----- Adapt these SCA and SCL port and pin definition to your target !!
;
#define SDA             4           // SDA Port D, Pin 4   
#define SCL             5           // SCL Port D, Pin 5
#define SDA_PORT        PORTD       // SDA Port D
#define SCL_PORT        PORTD       // SCL Port D         

You need to adjust these to the PORT/pins you want to use for SDA and SCL.

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

There is little point in using USI as an I2C Master. Fleury works fine.
The USI is ok for an I2C Slave.
.
Most USI chips have debugWIRE. It is much nicer to have JTAG on the mega169.
.
David.

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

Sorry I didn't post earlier, it has been a busy week for me. At any rate - kudos to those who recommended Fleury's library - I was able to get it up and running with fairly minor tweaks, and my sensor board is working as expected.

Thanks to all for the recommendations!