Skip to content

i2c_send

gicking edited this page Feb 10, 2018 · 5 revisions

back to Command Reference / I2C

Description

Send data to an I2C slave via pins SCLand SDA. Before the transmisstion, a start condition must be generated by i2c_start(). The transmission is terminated by a stop condition, which is generated via i2c_stop().

Inclusion

  • defined in i2c.h
  • not loaded by main_general.h
  • for custom I2C interrupts set set #define USE_I2C_ISR in config.h. The corresponding interrupt function name is I2C_ISR

Syntax

i2c_send(addr,num,buf)

Parameters

  • input:

    • addr: I2C address of slave to send data to (uint8_t)
    • num: number of bytes to send (uint8_t)
    • buf: buffer to send (uint8_t array)
  • output:

    • none

Returns

  • 0: acknowledge received, 1: timeout occurred

Example Code

The below function controls a digital potentiometer AD5280. Specifically it sets the resistance between terminal A and washer to R=Rmax/255*res

#include "i2c.h"

...
void setAD5280(uint8_t addr, uint8_t res) {
  uint8_t  buf[2] = {0x00, res};
  i2c_start();
  i2c_send(addr,2,buf);
  i2c_stop();
}

setup() {
  ...
  i2c_init();
  ...
}

Relevant Tutorial

Notes and Warnings

To avoid damage

  • do not expose I/Os to voltages outside [-0.3V; Vdd+0.3V], else limit injection currents to the specificied limits
  • for OUTPUT pins assert that sink and source currents are below the specificied limits
  • do not directly connect two OUTPUT pins. If e.g. half-duplex is required, use a pull-up and OUTPUT_OPENDRAIN, instead

See also

Clone this wiki locally