Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
Kandalf
PostPosted: Feb 23, 2010 - 11:30 PM
Rookie


Joined: Jan 12, 2010
Posts: 35


I want to interface two atmega32 via TWI, but this is the first time that I work with avr and with TWI.

If anyone have some code or tutorial to do that please send me.

For now I am using the AVR Studio 4 + WinAVR and I am trying to use the example code from atmel (AVR311 and AVR315)

I tried to compile the code but I have always errors, first was the includes that I change from:
Code:
#include <ioavr.h>
#include <inavr.h>
#include "TWI_Master.h"

to
Code:
//#include <ioavr.h>
//#include <inavr.h>
#include <avr/io.h>
#include "TWI_Master.h"
#include "TWI_Master.c"


but now I have this error:

Quote:
../TWI_Master.h:39: error: redefinition of 'union TWI_statusReg'

that refers to this code
Code:
union TWI_statusReg                       // Status byte holding flags.
{ (the line 39 is that)
    unsigned char all;
    struct
    {
        unsigned char lastTransOK:1;     
        unsigned char unusedBits:7;
    };
};

extern union TWI_statusReg TWI_statusReg;


Please a need some help I do not know how to compile this code
 
 View user's profile Send private message  
Reply with quote Back to top
atomicdog
PostPosted: Feb 24, 2010 - 12:31 AM
Resident


Joined: Jan 14, 2008
Posts: 722
Location: San Diego

Don't include c files like this. It only causes many headaches. Instead add the file to the AVR studio project.
In main.c ...
Code:
#include "TWI_Master.c"


Also add include guards to TWI_Master.h

_________________
~~John
WinAVR-20100110
Dragon w/Atmega324P, Xplain
------
TWI C source code
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Feb 24, 2010 - 09:18 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 33138
Location: (using avr-gcc in) Finchingfield, Essex, England

Kandalf,

Suggest you read this tutorial: Large C projects

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Kandalf
PostPosted: Feb 27, 2010 - 07:24 PM
Rookie


Joined: Jan 12, 2010
Posts: 35


I already solved the compilation problems (thanks atomicdog)

But my problem now is that this isn't work.

I want do the basic send a command from the master to a slave and when the slave receive the command turn on a led, can anyone help me with some examples of code.

I already tried alot of things modifying the code from atmel but I can't put this working
 
 View user's profile Send private message  
Reply with quote Back to top
Bingo600
PostPosted: Feb 27, 2010 - 08:22 PM
Raving lunatic


Joined: Apr 25, 2004
Posts: 3034
Location: Denmark

In the TWI appnotes for the Mega , i seem to remember that one or more of the globals needed to be declared volatile. Else i had problems.

It's some time ago , so im not 100% sure.

But worth checking

/Bingo
 
 View user's profile Send private message  
Reply with quote Back to top
stu_san
PostPosted: Feb 28, 2010 - 08:35 PM
Raving lunatic


Joined: Dec 30, 2005
Posts: 2041
Location: Longmont, CO USA

Kandalf wrote:
But my problem now is that this isn't work.
Have you got any idea of how far you are getting in the TWI protocol? This is not descriptive enough to tell us what kind of "don't work" problem you are having.

Recommendations:

If you have JTAG or some other link to the master that allows on-chip debugging, go into the debugger and watch the code. At what step does the TWI protocol fail?

If you do not have anything that allows on-chip debugging, you can either use the serial port on the master (if you have it tied to a terminal), an oscilloscope tied to some debug output pins that you set high or low depending on where you are in the TWI code, or worse case tie LEDs to those debug out pins and watch the LEDs.

Without more information about what is failing, we cannot help you.

Stu

PS: You did remember to pull-up resistors on the SCL and SDA lines between the master and slave, didn't you? Depending on the speed of the TWI interface you are trying to set, any value from 4.6 KOhm to 10 KOhm would do.

_________________
Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.
(My boss always chooses Cheap. Twice.)
 
 View user's profile Send private message  
Reply with quote Back to top
Kandalf
PostPosted: Mar 01, 2010 - 12:24 AM
Rookie


Joined: Jan 12, 2010
Posts: 35


The TWI between the two atmega32 already work. I am able to send data between both AVRs.

I only changed the Datarate to 400kbits and the system starts to work I don't know why.

Tomorrow I will make more some tests
 
 View user's profile Send private message  
Reply with quote Back to top
stu_san
PostPosted: Mar 01, 2010 - 03:23 PM
Raving lunatic


Joined: Dec 30, 2005
Posts: 2041
Location: Longmont, CO USA

Kandalf wrote:
I only changed the Datarate to 400kbits and the system starts to work I don't know why.
To 400 KBits/sec from...?

You should know that the speed of the TWI bus is dependent upon three things:

1 - the capacitance on the bus

2 - the size of the pull-up resistors on SCL and SDA

3 - the speed accepted by all devices on the bus,

There is a page in the ATmega32 datasheet that talks about the size of the pull-up resistors. It's in the TWI section, somewhere. Be sure to estimate the capacitance on the bus, since that really determines the rise and fall times set by the pull-up resistors.

Since you have two ATmega32 devices talking, the third limit is not so severe. We have had experience with other devices (specifically, the Philips I/O Extender) that lie on their datasheet about the maximum acceptable data rate.

Just make sure that your pull-up resistors are sized properly for the speed you want to run at.

Stu

_________________
Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.
(My boss always chooses Cheap. Twice.)
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits