Atmega128 - I2C to MD23

I2C, Serial, Wireless etc.

Moderator: chris

Atmega128 - I2C to MD23

Postby tjr » Mon Jul 14, 2008 12:11 pm

Hi all,

I've just received a great RD01 drive system to play around with for a few projects i'm thinking about. I guess the reason i'm thinking about them is because i'm pretty inexperienced with robots and the computer engineering side of things, so bear with me if i ask some silly things!

So, my first step is to get I2C communication working between my Atmega128 (on the compact Robostix board) and the MD23 - and i'm looking for some guidance. I've read through the i2c-tutorial you have on offer (thanks for that) and I'm using the example Colin Sauze's code for the AVR using GCC-AVR for the LCD03 (obviously cutting out the LCD stuff and adapting it for the MD23). I've gotten as far as compiling and building the i2c.c and i2c.h code he has, and then i'm trying a simple bit of code in a main.c file to get her and the green LCD working. Unfortunately, that's where my luck has run out.

I'm running this code below to try and get some action:
Code: Select all
#include "i2c.h"
#include <avr/io.h>
#include <stdio.h>

int main(void);


int main(void){
   
   i2c_init();
   i2c_start();
   i2c_transmit(0xB0); //address of the MD23
   i2c_transmit(0x00); //First register
   i2c_transmit(0x00); //Send it any old thing
   i2c_stop();
   do                          // just a loop so i can try and test!
   {
   i2c_start();
   status=i2c_transmit(0xB0);
   i2c_stop();
   }while (status != I2C_NO_ERROR);
   return;
}


Annnd.. the LED light doesn't blink! I've done a bit of poking around, a CRO shows my SCL is low, while i get a waveform on the SDA (which actually looks reasonable, but nothing on the clock). While i'm almost sure that pull-ups are on board, i've tried with a 10k resistor to check if pullups are the problem and yeah, they don't seem to be. So i'm a bit stuck, and unfortunately a bit clueless as to where to go from here.

Any help appreciated - don't assume i've done anything right :)

Edit: Also just to help, the i2c.c and i2c.h files are downloaded fresh from the /examples page with no changes :)
tjr
 
Posts: 7
Joined: Mon Jul 14, 2008 11:52 am

Re: Atmega128 - I2C to MD23

Postby tjr » Wed Jul 16, 2008 8:45 am

Well (thanks to all the help :p)... I've been able to get I2C waveforms appearing on my output pins on the Robostix. I've changed over to the following code:

Code: Select all
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <compat/twi.h>

#include "i2cmaster.h"

#define DDR_SPI DDRB
#define DD_MOSI DDB3
#define DD_SCK DDB1
#define DD_MISO DDB2


void init_all(void){

PORTA=0x00;
DDRA=0x00;

PORTB=0x00;
DDRB=0x00;

PORTC=0x04;
DDRC=0xFF;

PORTD=0xFF;
DDRD=0x00;

PORTE=0x00;
DDRE=0x00;

PORTF=0x00;
DDRF=0x00;

PORTG=0x00;
DDRG=0x00;

ASSR=0x00;
TCCR0=0x01;
TCNT0=0x00;
OCR0=0x50;

TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;

TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;

EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;

TIMSK=0x02;
ETIMSK=0x00;

ACSR=0x80;
SFIOR=0x00;

}

#define SCL_CLOCK  100000L

void twi_init(void)
{

 
  TWSR = 0;                         
  TWBR = ((F_CPU/SCL_CLOCK)-16)/2; 

}

unsigned char i2c_start(unsigned char address)
{
    uint8_t   twst;

   TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);

   while(!(TWCR & (1<<TWINT)));

   twst = TW_STATUS & 0xF8;
   if ( (twst != TW_START) && (twst != TW_REP_START))
      return 1;

   TWDR = address;
   TWCR = (1<<TWINT) | (1<<TWEN);

   while(!(TWCR & (1<<TWINT)));

   twst = TW_STATUS & 0xF8;
   if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) )
      return 1;

   return 0;

}
unsigned char i2c_write( unsigned char data )
{   
    uint8_t   twst;
   
   // send data to the previously addressed device
   TWDR = data;
   TWCR = (1<<TWINT) | (1<<TWEN);
   while(!(TWCR & (1<<TWINT)));
   twst = TW_STATUS & 0xF8;
   if( twst != TW_MT_DATA_ACK) return 1;
   return 0;

}
void i2c_stop(void)
{
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
   while(TWCR & (1<<TWSTO));

}

void init_usart0(void)
{
   UCSR0A=0x00;
   UCSR0B=0xD8;
   UCSR0C=0x06;
   UBRR0H=0x00;
   UBRR0L=0x08;

}
 void uart_putc(char data)
 {
     while (!(UCSR0A&(1<<UDRE)));    // Wait for previous transmissions
     UDR0 = data;    // Send data byte
     while (!(UCSR0A&(1<<UDRE)));    // Wait for previous transmissions
 }



void uart_puts(unsigned char *str)
{
   int i = 0;
   while(str[i] != '\0')
   { // Loop through string, sending each character
      uart_putc(str[i]);
      i++;
   }
}


 
 
SIGNAL(SIG_2WIRE_SERIAL)
{

}

SIGNAL( USART0_RX_vect )
{
}

SIGNAL(USART0_TX_vect)
{
}



unsigned char twi_start(unsigned char address)
{
    uint8_t   twst;

   // send START condition
   TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);

   // wait until transmission completed
   while(!(TWCR & (1<<TWINT)));

   // check value of TWI Status Register. Mask prescaler bits.
   twst = TW_STATUS & 0xF8;
   if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;

   // send device address
   TWDR = address;
   TWCR = (1<<TWINT) | (1<<TWEN);

   // wail until transmission completed and ACK/NACK has been received
   while(!(TWCR & (1<<TWINT)));

   // check value of TWI Status Register. Mask prescaler bits.
   twst = TW_STATUS & 0xF8;
   if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;

   return 0;

}/* twi_start */



void main(void)
{
   init_all();
   twi_init(); 
   init_usart0();

   
   i2c_start(0xB0);
   i2c_write(0x00);
   i2c_write(0x20);
   i2c_stop();
   
   while (1) {
   i2c_start(0xB0);
   i2c_write(0x00);
   i2c_write(0x20);
   i2c_stop();   

   }   
 }
 


This is by no means brilliant coding, just trying to get it working. Anyway, the following issues were preventing anything working:

Atmega128 fuse settings - look at the fuses and try a very simple peice of code to get an LED working. If that code doesn't work, check fuses before moving on (i had compatibility fuses incorrectly set)
Connection issues using AVR ISP mkII - reduce your frequency below 1Mhz if the signature you are reading doesn't match
Make sure your main.c file is included

Now, my issue is that while the CRO shows me the expected waveforms for SCL and SDA - once connected to the MD23, the whole thing collapses and SCL is high while SDA is low. Atm, I can't understand this - the green LED will light up for one pulse but then the waveform collapse. It's as is the uC isn't driving them hard enough.

Any ideas?

Thanks.
TJR,
tjr
 
Posts: 7
Joined: Mon Jul 14, 2008 11:52 am

Re: Atmega128 - I2C to MD23

Postby tjr » Thu Jul 17, 2008 8:55 am

Turns out i had an earthing problem - the power supply earth wasn't sufficient, needed to have an additional earth tied to the Robostix board.

However - still problems - connecting a working SDA and SCL to the MD23 results in a waveform that is fairly nonsense - SDA effectively matches SCL. I'm thinking the MCU on the MD23 (the PIC###) is broken
tjr
 
Posts: 7
Joined: Mon Jul 14, 2008 11:52 am

Re: Atmega128 - I2C to MD23

Postby chris » Thu Jul 17, 2008 9:18 am

Hi TJR,

Just to pose a few basic questions first:

What pull up resistors are you using?
Have you used a common ground between the boards?
How do you know SDA and SCL were working before being connected?
User avatar
chris
 
Posts: 172
Joined: Wed Nov 08, 2006 3:13 pm
Location: Norfolk, England

Re: Atmega128 - I2C to MD23

Postby tjr » Thu Jul 17, 2008 2:14 pm

Hi chris,

Thanks a lot for your response.

To answer:

10k pullups are on board the robostix. However, i wasn't 100% sure of this so i wired in a 5.1k resistor to be sure - this just made the SCL and SDA waveforms a little clearer.
Yes, i believe at some stage the ground wasn't properly connected but this is fixed now.
Using a CRO to look at the SDA and SCL.

The current problem now is that there seems to be a short within the MD23 itself which perhaps was causing problems right from the start. I can't confirm this, however i may be able to get my hands on a spare board to test.
tjr
 
Posts: 7
Joined: Mon Jul 14, 2008 11:52 am

Re: Atmega128 - I2C to MD23

Postby tjr » Fri Jul 18, 2008 2:06 pm

All fixed! Interestingly enough grounding problems were the whole story. The robostix board has a tiny power adapter for it, which actually fits my power adapter for my nokia mobile phone. However - these mobile phone chargers run from a switch-mode power supply without a hardwired ground, just +ve and -ve. It seems that the whole system needed to be earthed more solidly - which now done, and the motors are finally turning.

Thanks for the help, i guess this forum post is a bit more of a blog than anything - hope the ramblings can help others.
tjr
 
Posts: 7
Joined: Mon Jul 14, 2008 11:52 am


Return to Communication devices

Who is online

Users browsing this forum: No registered users and 0 guests

cron