3.4.7_プログラムリスト(I2C.c)

//
// I2C EEPROM (WIZ-C)
//
#include "C:\\WIZ_C\\Ver12\\Projects\\IRREMO1\\IRREMO1_Auto.h"
#include "C:\\WIZ_C\\Ver12\\Projects\\IRREMO1\\i2c.h"
#include <strings.h>
#include <Delays.h>

void I2CStart(BYTE cont);
BYTE I2CWrite(BYTE data);
BYTE I2CRead(BYTE noack);
void I2CStop(void);
void delay1(void);

//
// start condition process
//
void I2CStart(BYTE cont) {
    if(cont) {
        SDA = 1;
        delay1();
        SCL = 1;
        delay1();
    }
    SDA = 0;
    delay1();
    SCL = 0;
}

//
// I2C byte data output
//
BYTE I2CWrite(BYTE data) {
    BYTE i, ack;

    for(i = 0; i < 8; i++) {
        if(data & 0x80) {
            // H
            SDA = 1;
        } else {
            // L
            SDA = 0;
        }
        data <<= 1;
        delay1();
        SCL = 1;
        delay1();
        SCL = 0;
    }
    SDA = 0;
    // read ACK
    SDA_INPUT;                // SDA input
    delay1();
    SCL = 1;
    if(PORTC & 1<<4) {        // read RC4(SDA)
        // 1
        ack = 1;        // no ACK
    } else {
        // 0
        ack = 0;
    }
    delay1();
    SCL = 0;
#asmline NOP
    SDA_OUTPUT;                // SDA output
    delay1();

    return ack;
}

//
// I2C byte data input
//
BYTE I2CRead(BYTE noack) {
    BYTE sda, dt;
    BYTE i, ackb;
    SDA_INPUT;                // SDA input
    dt = 0;
    for(i = 0; i < 8; i++) {
        dt <<= 1;
        delay1();
        SCL = 1;
        delay1();
        if(PORTC & 0x10) {
            sda = 1;
        } else {
            sda = 0;
        }
        dt |= sda;
#asmline NOP
        SCL = 0;
    }
    // write ACK
    SDA_OUTPUT;                // SDA output
    SDA = noack;
    delay1();
    SCL = 1;
    delay1();
    SCL = 0;
    SDA = 0;
    delay1();
    return dt;
}

//
// stop condition process
//
void I2CStop(void)
{
    SCL = 1;
    delay1();
    SDA = 1;
    delay1();
}

//
// delay
//
void delay1()
{
#asm
    NOP
    NOP
    NOP
#asmend
}





トップページヘもどる