LoginSignup
1

More than 5 years have passed since last update.

arduinoでi2c その4

Last updated at Posted at 2018-01-16

概要

フルスクラッチでmaster書いてみた。

写真

CIMG2522.JPG

回路図

image

サンプルコード

#include <inttypes.h>
#include <compat/twi.h>

#define F_CPU               16000000UL
#define SCL_CLOCK           50000L
#define PCF8574_ADDRESS     0x27 << 1 + 0
void i2c_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;
}
void i2c_stop(void)
{
    TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
    while (TWCR & (1 << TWSTO));
}
unsigned char i2c_write(unsigned char data)
{
    uint8_t twst;
    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 send(uint8_t d)
{
    uint8_t s;
    i2c_start(PCF8574_ADDRESS);
    i2c_write(d);
    s = d | 0x04;
    i2c_write(s);
    delayMicroseconds(1);
    s = d & 0xfb;
    i2c_write(s);
    delayMicroseconds(1);
    i2c_stop();
}
void set(uint8_t d)
{
    uint8_t s;
    uint8_t f1 = d & 0xf0;
    uint8_t f2 = d << 4;
    i2c_start(PCF8574_ADDRESS);
    s = f1;
    i2c_write(s);
    s = f1 | 0x04;
    i2c_write(s);
    delayMicroseconds(1);
    s = f1 & 0xfb;
    i2c_write(s);
    i2c_stop();
    i2c_start(PCF8574_ADDRESS);
    s = f2;
    i2c_write(s);
    s = f2 | 0x04;
    i2c_write(s);
    delayMicroseconds(1);
    s = f2 & 0xfb;
    i2c_write(s);
    i2c_stop();
}
void data(uint8_t d)
{
    uint8_t s;
    uint8_t f1 = d & 0xf0;
    uint8_t f2 = d << 4;
    i2c_start(PCF8574_ADDRESS);
    s = f1;
    i2c_write(s);
    s = f1 | 0x05;
    i2c_write(s);
    delayMicroseconds(1);
    s = f1 & 0xfa;
    i2c_write(s);
    i2c_stop();
    i2c_start(PCF8574_ADDRESS);
    s = f2;
    i2c_write(s);
    s = f2 | 0x05;
    i2c_write(s);
    delayMicroseconds(1);
    s = f2 & 0xfa | 0x08;
    i2c_write(s);
    i2c_stop();
}
void setup()
{
    unsigned char c;
    unsigned char address;
    int nDevices;
    Serial.begin(115200);
    i2c_init();
    send(0x03);
    delayMicroseconds(50);
    send(0x03);
    delayMicroseconds(50);
    send(0x03);
    delayMicroseconds(15);
    send(0x02);
    set(0x28);
    set(0x0c);
    set(0x01);
    set(0x06);
    data(0x68);
    data(0x65);
    data(0x6c);
    data(0x6c);
    data(0x6f);
    data(0x2c);
    data(0x20);
    data(0x77);
    data(0x6f);
    data(0x72);
    data(0x6c);
    data(0x64);
    data(0x21);
    Serial.println("ok");
}
void loop()
{
    delay(100);
}



以上。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1