avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Hi need some help with i2c


From: Johan Nilsson
Subject: [avr-gcc-list] Hi need some help with i2c
Date: Sat, 07 Aug 2004 17:23:00 +0200
User-agent: Mozilla Thunderbird 0.7.2 (Windows/20040707)

Hi
I have been trying for awhile now to communicate with a AVR 2313 to a DS1621 thruu i2c but nothing happens is there anyone that can look on this code. I have been looking for sourcecode to see if that works but i cant find any good that compiles. I have read the spec but nothing.

CODE START
#include <avr/io.h>
#include <avr/delay.h>
#include "i2c.h"

#define I2CPORT PORTD
#define SDA PD4
#define SCL PD5

#define SCL_0 cbi(DDRD,SCL)
#define SCL_1 sbi(DDRD,SCL)
#define SDA_0 cbi(DDRD,SDA)
#define SDA_1 sbi(DDRD,SDA)

#define R_SDA inp(I2CPORT) & (1<<SDA)

#define HDEL _delay_loop_1(10);
#define QDEL _delay_loop_1(5);

void i2c_init()
{
 sbi(DDRD,SDA);
 sbi(DDRD,SCL);
}

void i2c_start (void)
{
   SDA_1;                            // Set data line high
   HDEL;
   SCL_1;                            // Set clock line high
   QDEL;
   SDA_0;                            // Set data line low (START SIGNAL)
   SCL_0;                                // Set clock line low
}

void i2c_stop (void)
{

   SCL_0;                            // Set clock line low
   SDA_0;                            // Set data line low
   SCL_1;                            // Set clock line high
   QDEL;
   SDA_1;                            // Set data line high (STOP SIGNAL)
}

void i2c_write (unsigned char output_data)
{
   unsigned char index;

   for(index = 0; index < 8; index++)      // Send 8 bits to the I2C Bus
   {
// Output the data bit to the I2C Bus
       //SDATA = ((output_data & 0x80) ? 1 : 0);
       if(output_data > 127) SDA_1; else SDA_0;
output_data <<= 1; // Shift the byte by one bit
       SCL_1;                           // Clock the data into the I2C Bus
       QDEL;
SCL_0; }
   cbi(I2CPORT, SDA);
   index = R_SDA;                            // Put data pin into read mode
   SCL_1;                               // Clock the ACK from the I2C Bus
   QDEL;
SCL_0; sbi(I2CPORT, SDA);
}

unsigned char i2c_read (void)
{
   unsigned char index, input_data;
   cbi(I2CPORT, SDA);
index = SDA; // Put data pin into read mode

   input_data = 0x00;
   for(index = 0; index < 8; index++)      // Send 8 bits to the I2C Bus
   {
       input_data <<= 1;                    // Shift the byte by one bit
       SCL_1;                           // Clock the data into the I2C Bus
input_data |= R_SDA; // Input the data from the I2C Bus
       QDEL;
       SCL_0;
   }
  sbi(I2CPORT, SDA);
  return input_data;
}
CODE END

--
Johan Nilsson / Edberg Nilsson Data HB
Mobil: 070-261 94 01
Email: address@hidden
URL: http://www.endata.se



reply via email to

[Prev in Thread] Current Thread [Next in Thread]