TWI/I2C

I have been programming for a couple of years now, I cut my teeth on an arduino then moved over to learning 'proper' C using an STM32F4

I have done a lot of work using a bare ATMega328making standalone PCB's and for the most part its analogue electronics that I have played with

I really enjoy electronics and I have done quite a bit mostly making my own motor drives which can be very technical

Anyway I keep encountering sensors that use I2C or SPI and I have absolutely no experience with these but I think its about time I started using them as they are very useful

What I want to do is get a sensor and learn how to use it with a raw ATMega 328 using AS6

For example this sensor

I have had a look around but all the tutorials I can find use an EEPROM and the example seems like its more complex than I need

I just need to use the sensor in slave mode and I would like to write my own functions to use it so that I can gain a real understanding but obviously this is easier said than done

So if anyone can guide me on the best way to attempt this, how did other members learn how to do this stuff?

Thanks in advance

Res

This is a must read for I2C - http://www.gammon.com.au/forum/?id=10896 -

Thank you

I have read that last week sometime and I must say Nick does a great tutorial, well done Nick

However I would like to write my code in C using AS6 and Nick's code is all done on an Arduino, I imagine for a relatively inexperienced programmer like me it would be difficult to port it over to pure C

I will read it again, and again, and again!

Thanks

I imagine for a relatively inexperienced programmer like me it would be difficult to port it over to pure C

I don't understand this statement, or this one:

then moved over to learning 'proper' C using an STM32F4

The Arduino can be programmed in C, although a lot of things are more easily handled in C++. Since C++ is a superset of C, and adds classes and Object Oriented Programming, I fail to see what your objections to learning C++ are.

What I was saying is that I am not using an Arduino, I have a standalone ATMega328 on a home made PCB and I program it using AS6

The examples use the Arduino libraries which I always thought was 'wiring'

I don't care if its C or C++, I know some of both but I will not be using Arduino or any of its libraries

The 2nd statement was in reference to going from the Arduino environment with all its simplifications like Analog read to using a proper eclipse based compiler which uses C/C++

It was a big step for me at the time but it was well worth it, I learnt lots about C programming that I couldn't of done with an Arduino

These days I am using AS6 for my AVR needs and its much better for me to learn C, I don't want any simplifications but it does make it harder to pick up more advance things

So you want to learn C better. Great choice it gives you a really solid base of microcontrollers programming, since it's standard (and so easily portable out of the Arduino world) and used for every microcontrollers and makes you able to read efficiently datasheets.
If I undesrtand well, you have already programmed using Atmel Studio but you've never used I2C? Well, if you know C programming, it's simply matter of reading chapter related to I2C on datasheet.

Keep in mind that if these reading are the firsts, you may pass some days w/out making something work. It's normal, but after that period, you can read with some ability every mcu datasheet. But seems this is not your case.

Sorry if I misunderstand.

Yeah, I am always looking to improve my C programming skills, I am a electrical engineer who works in product development and being able to program really opens up a lot of doors, I am also a power electronics student so controlling hardware and real time control is whats important to me hence C is the weapon of choice

I have done quite a lot of programming using Attollic for the STM32F4 and AS6 for my AVR's, like I said I build motor drives and they require some advanced programming techniques but I really enjoy it all

Your right I just haven't used I2C/TWI or SPI before and I keep encountering it so I really need to learn it but obviously going back to an Arduino is not something I will do

I have ordered a few different sensors and they seem really good, no averaging readings or hardware filters and even two sensors in one with temperature compensation!

I will keep on reading up but any input is welcome

It was a big step for me at the time but it was well worth it, I learnt lots about C programming that I couldn't of done with an Arduino

Why not? If you don't want to use a library or a core function, you are free to operate at a lower level. Nothing in C knows anything about the hardware, so any C coding you do is still at least one level removed from the hardware.

I guess I don't really understand the purpose of your post. You want to now start using libraries that you formerly sneered at?

Why not? If you don't want to use a library or a core function, you are free to operate at a lower level. Nothing in C knows anything about the hardware, so any C coding you do is still at least one level removed from the hardware.

For lots of reasons, the Arduino IDE simplifies so much but the biggest help to my learning was being able to debug with single step

Besides when I learn something I am more hands on and to properly learn I really need something I am trying to do if you understand what I mean, there needs to be something driving me to learn and that driving force for me was my Uni work, my research requires building inverter drives and complex modulation techniques are beyond the grasp of an 8bit AVR

So by going to a proper compiler with a 32 bit microcontroller I learnt things that I just would never of done using the Arduino IDE then by learning C like that it was relatively easy to buy an AVR dragon and program using AS6 for all my AVR needs

I guess I don't really understand the purpose of your post.

The purpose of my post was to see if any other members had any guidance on learning TWI but not using the Arduino IDE?, to maybe start a discussion on what others did as like I said the vast majority of tutorials are aimed at EEPROMS and they seem more complex than I need

You want to now start using libraries that you formerly sneered at?

sneer (snîr)
n.

  1. A scornful facial expression characterized by a slight raising of one corner of the upper lip.
  2. A contemptuous facial expression, sound, or statement.
    v. sneered, sneer·ing, sneers
    v.tr.
    To utter with a sneer or in a sneering manner.
    v.intr.
  3. To assume a scornful, contemptuous, or derisive facial expression.
  4. To speak in a scornful, contemptuous, or derisive manner.

Sneer is certainly not something I have done, and it seems like a bit of a confrontational word to use, are you trying to say that I look down on an Arduino or something as I can assure you this is not the case. The Arduino is a fantastic piece of hardware that opens up lots doors for lots of people and I have personally started a few people out programming using one, the libraries I certainly wouldn't 'sneer' at as what they do is amazing

I will use any library I can that will assist me on my learning, what library did I 'sneer' at that I am now considering using? this really confuses me, I just don't want to have to use an Arduino bootloader or IDE, I have my hardware board built, I program using AS6 and a dragon and I am happy with everything the way it is, I have lots of different programs for different projects and I want to be able to swap out analogue techniques for these i2C sensors, I am not a programming guru or anything like that so I don't fully understand all things programming

So I don't think I understand what it is you are saying, are you advising me to move back to the Arduino IDE? or are you saying use the Ardunio libraries in AS6?

I will use any library I can that will assist me on my learning

There are already libraries for both TWI and I2C. Use them or reinvent them.

If you are going to use them, there is plenty of documentation about how. If you are going to reinvent them, then it seems like you should already know enough about how they work. I still don't see the purpose of your post. But, I'm over it.

In my opinion, if someone want to use something different from Arduino Language like C, it's mandatory reading the datasheet and make proper libraries maybe, if you want, copying pieces of code from the arduino core or using google. According to Resinator purposes. :slight_smile:

something different from Arduino Language like C

The "Arduino language" IS C (and C++). Get over it.

PaulS:

something different from Arduino Language like C

The "Arduino language" IS C (and C++). Get over it.

You've right if you modify the Arduino core files as we talked about few days ago. The final user have a different approach, he/she will use the SW layer developed in C/C++, but this layer is made to hide this C language. It could be written a pure C too, but it's not mandatory to make a working sketch. So it can't be C. The C programming NEVER require a setup() and loop() functions, which are mandatory in a Wiring language. The C programming require a lot of headers added somewhere and the final user should know them. Here are hidden in wiring.c/h files and so on. in the IDE there is not C, there is C-like.
People like Davide Gomba, which have worked along Massimo Banzi and responsible for some big Maker spaces, said that you can program it w/out having any knowledge in programming. Do you think you able to program in C w/out knowledge? The C work is made by the Arduino team and is made for people that have different goals than learn C or are new in this field. Like I did years ago.
People that make thing done with Arduino, even the prettiest, can't say that they are able to use C if they never studied it.

If you want, you can apply the scientific method. Take AS6 and write your project written in Arduino IDE inside the Atmel IDE and see what you have to do to make it work. Could be very educational for you knowledge, maybe one day you will understand this too.

Your "get over it" make a sense of little irritation against me, I hope not, I simply said a fact. If someone don't want to understand (I hope not you), is not my business to insist on it.

Btw, I hope Resinator tell us something when he will solve his though, helping community inside this forum. :wink:

So it can't be C.

You can call it what you want. It IS C. The IDE imposes some rules. The compiler does not. If you want to get rid of setup() and loop(), feel free to replace them with main(). Don't forget to do all the things that the IDE does for you, like calling init() and creating function prototypes and adding header files.

Resinator:
I have read that last week sometime and I must say Nick does a great tutorial, well done Nick

However I would like to write my code in C using AS6 and Nick's code is all done on an Arduino, I imagine for a relatively inexperienced programmer like me it would be difficult to port it over to pure C

Thanks for the compliments. :slight_smile:

I am bemused however at the references to porting it over to pure C. The code is in C (well, C++ really), so it doesn't need any porting.

Sure, it uses the Arduino "Wire" library but that doesn't make it "not C". If you program in "pure C" you are almost certainly using libraries like stdlib, string, math, etc.

So it can't be C. The C programming NEVER require a setup() and loop() functions, which are mandatory in a Wiring language.

Not at all. Go to your Arduino IDE, and enter this:

int main ()
  {
  return 0;
  }

That compiles:

Binary sketch size: 176 bytes (of a 32,256 byte maximum)

No setup, no loop, and it generates short code. So they aren't mandatory, eh?

All that the Wiring stuff does is simplify things for you, like setting up Timer 0 to let you time events (eg. using millis() and micros() ). You don't have to use that.

But that's like saying "Pfft! I'm not going to use sprintf, I'm going to write my own!". Well, you can, if you have a good reason.

We get asked every few weeks "why does it take 1000 bytes to blink an LED?" so I've prepared a stock response.

In that I demonstrate that it actually takes 8 bytes to blink an LED. And that is within the Arduino IDE.