i2c.h File Reference

I2C software routines. More...

Include dependency graph for i2c.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define __i2c_h   defined
#define i2c_read_sda()   set_bit(tris_array[i2c_sda_port - PORTA], i2c_sda_pin);
#define i2c_setup()   i2c_setup_io()
#define i2c_write_sda()   clear_bit(tris_array[i2c_sda_port - PORTA], i2c_sda_pin);

Functions

uns8 i2c_read_eeprom (uns8 device_address, uns8 mem_address)
 Read an 8 bit byte over I2C buss.
uns16 i2c_read_eeprom_16bit (uns8 device_address, uns8 mem_address)
 Read 16 bits of data over I2C buss.
uns8 i2c_receive_byte ()
 Receive byte from I2C buss.
void i2c_send_ack (void)
 Send an ACK.
void i2c_send_byte (uns8 data)
 Send a byte to I2C buss.
void i2c_setup_io ()
 Setup ports and pins for I2C communication.
void i2c_start (void)
 Send start signal to I2C buss.
void i2c_stop (void)
 Send stop signal to I2C buss.
void i2c_write_eeprom (uns8 device_address, uns8 mem_address, uns8 data)
 Write an 8 bit byte ove I2C buss.
void i2c_write_eeprom_16bit (uns8 device_address, uns8 mem_address, uns16 data)
 Write a 16 bit value over I2C buss.

Detailed Description

I2C communication routines. Although all standard functions are provided, you should only need to use i2c_setup, i2c_read_eeprom and i2c_write_eeprom


Define Documentation

#define __i2c_h   defined
 
#define i2c_read_sda (  )     set_bit(tris_array[i2c_sda_port - PORTA], i2c_sda_pin);

Change SDA line to read mode

 
#define i2c_setup (  )     i2c_setup_io()
 
#define i2c_write_sda (  )     clear_bit(tris_array[i2c_sda_port - PORTA], i2c_sda_pin);

Change SDA line to write mode


Function Documentation

uns8 i2c_read_eeprom ( uns8  device_address,
uns8  mem_address 
)

Read an 8 bit byte from the specified device at the memory address

00077 {
00078 uns8 data;
00079     //i2c_ack_polling(device_address);
00080 
00081     i2c_start();
00082     i2c_send_byte(device_address);
00083     //send_byte(address.high8); // Upper Address - Needed for >= 32k EEProms
00084     i2c_send_byte(mem_address);
00085     i2c_stop();
00086 
00087     i2c_start();
00088     i2c_send_byte(device_address | 0b00000001); // Read bit must be set
00089     data = i2c_receive_byte(); // reuse local variable
00090 
00091     // write an nack
00092     clear_pin(i2c_scl_port, i2c_scl_pin);
00093     i2c_write_sda();
00094     delay_us(DELAY_AMOUNT);
00095     set_pin(i2c_sda_port, i2c_sda_pin);
00096     set_pin(i2c_scl_port, i2c_scl_pin);
00097     delay_us(DELAY_AMOUNT);
00098 
00099     i2c_stop();
00100     return(data);
00101 }

Here is the call graph for this function:

Here is the caller graph for this function:

uns16 i2c_read_eeprom_16bit ( uns8  device_address,
uns8  mem_address 
)

Read a 16 bit chunk of data from the given device and memory address

00104 {
00105     uns16 data;
00106     
00107     //i2c_ack_polling(device_address);
00108     i2c_start();
00109     i2c_send_byte(device_address);
00110     //send_byte(address.high8); // Upper Address - Needed for >= 32k EEProms
00111     i2c_send_byte(mem_address);
00112 
00113     i2c_start();
00114     i2c_send_byte(device_address | 0b00000001); // Read bit must be set
00115     data = i2c_receive_byte(); // reuse local variable
00116     i2c_write_sda();
00117     
00118     clear_pin(i2c_scl_port, i2c_scl_pin);
00119     delay_us(DELAY_AMOUNT);
00120     clear_pin(i2c_sda_port, i2c_sda_pin); //ack
00121     delay_us(DELAY_AMOUNT);
00122     set_pin(i2c_scl_port, i2c_scl_pin);
00123     delay_us(DELAY_AMOUNT);
00124     
00125     data = data << 8 | i2c_receive_byte(); // reuse local variable
00126 
00127     // write an nack
00128     clear_pin(i2c_scl_port, i2c_scl_pin);
00129     i2c_write_sda();
00130     delay_us(DELAY_AMOUNT);
00131     set_pin(i2c_sda_port, i2c_sda_pin);
00132     set_pin(i2c_scl_port, i2c_scl_pin);
00133     delay_us(DELAY_AMOUNT);
00134 
00135     i2c_stop();
00136     
00137     return(data);
00138 }

Here is the call graph for this function:

Here is the caller graph for this function:

uns8 i2c_receive_byte (  ) 

Clock in a byte over I2C lines. Note that an acknowledge is not sent/received

00187 {
00188    uns8 count, in_byte;
00189 
00190     clear_pin(i2c_scl_port, i2c_scl_pin);
00191 
00192     i2c_read_sda();
00193     for(count = 0 ; count < 8 ; count++)
00194     {
00195         clear_pin(i2c_scl_port, i2c_scl_pin);
00196         delay_us(DELAY_AMOUNT);
00197         set_pin(i2c_scl_port, i2c_scl_pin);
00198         delay_us(DELAY_AMOUNT);
00199 
00200         in_byte = in_byte << 1;
00201         in_byte.0 = test_pin(i2c_sda_port, i2c_sda_pin);
00202     }
00203     return(in_byte);
00204 }

Here is the caller graph for this function:

void i2c_send_ack ( void   ) 

Sends an I2C acknowledge (bit)

00140                         {
00141 
00142     i2c_write_sda();
00143     
00144     clear_pin(i2c_scl_port, i2c_scl_pin);
00145     delay_us(DELAY_AMOUNT);
00146     clear_pin(i2c_sda_port, i2c_sda_pin); //ack
00147     delay_us(DELAY_AMOUNT);
00148     set_pin(i2c_scl_port, i2c_scl_pin);
00149     delay_us(DELAY_AMOUNT);
00150  
00151 
00152 }

Here is the caller graph for this function:

void i2c_send_byte ( uns8  data  ) 

Clock out a byte from the I2C buss. An acknowledge is "received" (although ignored).

00207 {
00208    uns8 count;
00209 serial_print_str("Sending: ");
00210 serial_print_int_hex(data);
00211 serial_print_nl();
00212     clear_pin(i2c_scl_port, i2c_scl_pin);
00213     i2c_write_sda();
00214 
00215     for( count = 0 ; count < 8 ; count++ )
00216     {
00217         clear_pin(i2c_scl_port, i2c_scl_pin);
00218         change_pin(i2c_sda_port, i2c_sda_pin, data.7);
00219         data = data << 1;
00220         delay_us(DELAY_AMOUNT);
00221         set_pin(i2c_scl_port, i2c_scl_pin);
00222         delay_us(DELAY_AMOUNT);
00223     }
00224 
00225     // read ack (and ignore)
00226     clear_pin(i2c_scl_port, i2c_scl_pin);
00227     i2c_read_sda();
00228     delay_us(DELAY_AMOUNT);
00229     set_pin(i2c_scl_port, i2c_scl_pin);
00230     // read at this point
00231     
00232     delay_us(DELAY_AMOUNT);
00233 }

Here is the call graph for this function:

Here is the caller graph for this function:

void i2c_setup_io (  ) 

Set port and pins correctly for I2C communication

00235                     {
00236     make_output(i2c_scl_port, i2c_scl_pin);
00237     make_input(i2c_sda_port, i2c_sda_pin);
00238 }

void i2c_start ( void   ) 

Signals a start condition on the I2C buss.

00156 {
00157 
00158     clear_pin(i2c_scl_port, i2c_scl_pin);
00159     delay_us(DELAY_AMOUNT);
00160 
00161     i2c_write_sda();
00162 
00163     set_pin(i2c_sda_port, i2c_sda_pin);
00164     delay_us(DELAY_AMOUNT);
00165     set_pin(i2c_scl_port, i2c_scl_pin);
00166     delay_us(DELAY_AMOUNT);
00167     clear_pin(i2c_sda_port, i2c_sda_pin);
00168     delay_us(DELAY_AMOUNT);
00169 }

Here is the caller graph for this function:

void i2c_stop ( void   ) 

Signals a stop condition on the I2C buss.

00172 {
00173     clear_pin(i2c_scl_port, i2c_scl_pin);
00174     delay_us(DELAY_AMOUNT);
00175 
00176     i2c_write_sda();
00177 
00178     clear_pin(i2c_sda_port, i2c_sda_pin);
00179     delay_us(DELAY_AMOUNT);
00180     set_pin(i2c_scl_port, i2c_scl_pin);
00181     delay_us(DELAY_AMOUNT);
00182     set_pin(i2c_sda_port, i2c_sda_pin);
00183     delay_us(DELAY_AMOUNT);
00184 }

Here is the caller graph for this function:

void i2c_write_eeprom ( uns8  device_address,
uns8  mem_address,
uns8  data 
)

Write a byte to a given device address at the memory address

00052 {
00053     //i2c_ack_polling(device_address);
00054     i2c_start();
00055     i2c_send_byte(device_address);
00056 
00057     //send_byte(address.high8); //Uppder Address - Needed for >= 32k EEProms
00058     i2c_send_byte(mem_address);
00059     i2c_send_byte(data);
00060     i2c_stop();
00061 }

Here is the call graph for this function:

Here is the caller graph for this function:

void i2c_write_eeprom_16bit ( uns8  device_address,
uns8  mem_address,
uns16  data 
)

Write a byte to a given device address at the memory address

00064 {
00065     //i2c_ack_polling(device_address);
00066     i2c_start();
00067     i2c_send_byte(device_address);
00068 
00069     //send_byte(address.high8); //Uppder Address - Needed for >= 32k EEProms
00070     i2c_send_byte(mem_address);
00071     i2c_send_byte(data >> 8);
00072     i2c_send_byte(data & 0x00ff);
00073     i2c_stop();
00074 }

Here is the call graph for this function:

Here is the caller graph for this function:


Generated on Fri Aug 19 09:06:03 2011 for Pic Pack Library by  doxygen 1.6.1