Re: Procyon Library for Atmel AVR MCU - I²C problems



First of all: Thank you for the code!

The reason why I use the Procyon-Library is because I shall use it - for
following projects will use more and more of it's functions - and it's
deemend unnecessary that I - literally - re-invent the wheel with all the
functions that will be needed with future projects.

On my own feet I got as far as being able to sample analog values and check
input-pins, write those values out over the serial port, recieve them on
another MCU, and reproduce the digital states on GPIO-pins, the analog
states on generic Servos - and I got to admit I still don't understand the
Interrupt-Routine for real, it just works.

I'll first of all will use your code to check that the sensor is working -
and then I'll dig into the I2C libraries of the procyon-libraries and try
to compare and understand what is happening.

One thing I don't understand for example is why, when I want to transmit a
single databyte, I have to use a pointer instead of the actual variable, or
even if I want to read data fro man array I have to use a pointer instead
of simply incrementing through the arrays field.

I learned programming on TI Basic, but the modern Basic dialects are stuffed
with functions doing honors to assembly language and C, so it was decided
that the time to learn using C to be "future-proof" would be more worthy
than trying to learn either Assembly or one of the other more recent Basic
Languages and Dialects.

*sighs*

Anyway.
I'll try your code tomorrow to see if the sensor is talking with me, and
then I'll hook up the osscilloscope and the analyzer and compare what the
two programs are doing differently.

Thank you very much so far.


Vladimir Vassilevsky wrote:



Rüdiger Leibrandt wrote:

Hello!

I am new to programming and am struck with the Manual for the Procyon
Library for Atmel's ATMega MCUs. It basically provides functions for all
the interfaces and functions available on the Atmel MCUs.

[...]


I guess I have a conceptual error in understanding the I²C Protocol and /
or utilizing the Procyon Library.

The conceptual error is in multiplying the entities beyond the
necessity. For something as simple as AVR (tm), you don't need any
libraries, and the only manual required is the AVR data***. All it
takes to run I2C is to program a couple of registers.

The program compiles without problems under avr-gcc.
Any sggestions and hints welcome!

As simple as that:

static void I2C_WaitDone(void)
{
while(!(TWCR&0x80));
}

static void I2C_Stop(void)
{
TWCR = 0x94;
while(TWCR&0x10);
}

static void I2C_Start(void)
{
TWCR = 0xA4;
I2C_WaitDone();
}

static void I2C_Transmit_Byte(u8 byte)
{
TWDR = byte;
TWCR = 0x84;
I2C_WaitDone();
}


//=======================

void I2C_Action(u8 address, u8 *data, u8 length)
{
u8 ci;

I2C_Start();
I2C_Transmit_Byte(address);

if(address & 0x01) // I2C read
{
for(ci = 0; ci < length; ci++)
{
if(ci < (length - 1)) TWCR = 0xC4; // Confirm byte with ACK
else TWCR = 0x84; // Last byte - send NAK
I2C_WaitDone();
data[ci] = TWDR;
}
}
else // I2C write
{
for(ci = 0; ci < length; ci++) I2C_Transmit_Byte(data[ci]);
}

I2C_Stop();
}

//----------------------------------

Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com

--
Sincerely

Ruediger

.


Quantcast