NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » I2C Nerdkit Library

November 08, 2009
by tech20
tech20's Avatar

I have just modified a project from www.avrbeginner.net to work as a addition to the nerdkit library. If anyone's interested, I'll post it in the pastebin.

November 09, 2009
by jbremnant
jbremnant's Avatar

Hi tech20

I am interested. I've been mucking around with TWI (I2C) code from arduino source on NK. That code is implemented with TWI_vect interrupt, but I couldn't get it to work. I wrote a non-interrupt based Master-to-Slave I2C routines, and it works, but I would ideally like an interrupt version working..

November 09, 2009
by tech20
tech20's Avatar

Mine is not interrupt based, but i have one that is. Mine that i revised is setup mainly for basic device use and simplicity, as in data-logging using EEPROM

November 22, 2009
by martelr
martelr's Avatar

I would be interested. Where is the pastebin?

June 08, 2010
by n3ueaEMTP
n3ueaEMTP's Avatar

I too am interested... Any links to the Pastebin page?

Thanks

June 09, 2010
by tech20
tech20's Avatar

Hmm...more interest than before ; ). I'll see if I can dig out some source for it with a modified makefile, hope it works out for you.

June 09, 2010
by treymd
treymd's Avatar

very nice

June 17, 2010
by tech20
tech20's Avatar

Wow this was on my stolen flash drive, I will try to find a possible alternate source of my source code and look through a lot of other places, wish me luck :)

September 09, 2010
by Ralphxyz
Ralphxyz's Avatar

Just for the record, in case anyone else wonders where the/a pastebin lies.

http://nerdkits.pastebin.com/ look in the Sub Domain Archive!

Ralph

September 09, 2010
by tech20
tech20's Avatar

http://nerdkits.pastebin.com/fa884005 and http://nerdkits.pastebin.com/f23917fdb , thank everyone at pastebin for a great archive

September 09, 2010
by Rick_S
Rick_S's Avatar

If you are looking for good general purpose libraries including an I2C (TWI) library, check out the ones done by Peter Fluery. I've used them and they work well and are documented well.

Rick

September 10, 2010
by Rick_S
Rick_S's Avatar

Here is some sample code I wrote using the library from Peter Fluery to display the temerature read with the nerdkit on a 2 digit 7 segment display using a Phillips saa1064 I2C Display driver IC. Don't know if it'll help or not, but it may give you an idea how the library can be used.

#include <avr/io.h>
#include <inttypes.h>
#include "i2cmaster.h"
#include "adc.h"

#define Devsaa1604 0x70

double sampleToFahrenheit(uint16_t sample) {
  // conversion ratio in DEGREES/STEP:
  // (5000 mV / 1024 steps) * (1 degree / 10mV)
  //    ^^^^^^^^^^^      ^^^^^^^^^^
  //     from ADC         from LM34
  return sample * (4950.0 / 1024.0 / 10.0);  
}

int main(void)
{
    unsigned char ret;
    uint16_t x;
    unsigned int ones, tens, hundreds, thousands;

        // holder variables for temperature data
    uint16_t last_sample = 0;
    double this_temp;
    double temp_avg;
    uint8_t i;

    uint8_t control=0x16;  // set display at minimum brightness

        // Set up array for digit masks

    unsigned int digits[10];
        digits[0] = 0x7D; 
        digits[1] = 0x48;
        digits[2] = 0x3E;
        digits[3] = 0x6E;
        digits[4] = 0x4B;
        digits[5] = 0x67;
        digits[6] = 0x77;
        digits[7] = 0x4C;
        digits[8] = 0x7F;
        digits[9] = 0x6F;

   unsigned int dp_on = 0x80;

    DDRB  = 0xff;                              // use all pins on port B for output 
    PORTB = 0xff;                              // (active low LED's )

    i2c_init();                                // init I2C interface
    adc_config(0);

    while(1){
            // Get current temperature

            temp_avg = 0.0;
            for(i=0; i<200; i++) {
                last_sample = adc_read();
                this_temp = sampleToFahrenheit(last_sample);

                // add this contribution to the average
                temp_avg = temp_avg + this_temp/200.0;
            }

            x=temp_avg;

            thousands = x / 1000;
            hundreds = x % 1000 / 100;
            tens = x % 100 / 10;
            ones = x % 10;

            //Send Data to SAA1064 I2C display driver
            /* write 0x75 to eeprom address 0x05 (Byte Write) */
            ret = i2c_start(Devsaa1604+I2C_WRITE);       // set device address and write mode
            if ( ret ) {
                /* failed to issue start condition, possibly no device found */
                i2c_stop();
                PORTB=0x00;                            // activate all 8 LED to show error */
            }else {
                /* issuing start condition ok, device accessible */
                i2c_write(0x00);                       
                i2c_write(control);
                i2c_write( digits[tens]);
                i2c_write( digits[ones]|dp_on);     
                i2c_stop();                            
            }   
    }
    for(;;);

    return 0;

}

Rick

October 30, 2010
by hipitarius
hipitarius's Avatar

Rick_s

I have downloaded the i2cmaster library from Peter Fluery's website. But I am still having trouble getting anything to compile.

Where should I put the i2cmaster.h and i2cmaster.s files? Do I need to change my makefile for the project? Should I add them to the libnerdkits folder, and modify that makefile and then compile it there and include the same as the uart library with (#include "../libnerdkits/uart.h")?

Any help is greatly appreciated.

Thanks,

Todd

October 30, 2010
by Ralphxyz
Ralphxyz's Avatar

Hi Todd, the i2cmaster.h goes in your project folder along with the source that calls it

(#include "i2cmaster.h").

It can go else where but then you have to deal with the path.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that a motor's no-load current at a given voltage is much less than it's resistance would suggest? Learn more...