3.4.16_プログラムリスト(I2C.c)
//
// I2C EEPROM (WIZ-C) // #include "C:\\WIZ_C\\Ver12\\Projects\\VOICERECORDER\\VOICERECORDER_Auto.h" #include "C:\\WIZ_C\\Ver12\\Projects\\VOICERECORDER\\I2C.h" #include <strings.h> #include <Delays.h> void I2CStart_1(BYTE cont); BYTE I2CWrite_1(BYTE data); BYTE I2CRead_1(BYTE noack); void I2CStop_1(void); void I2CStart_2(BYTE cont); BYTE I2CWrite_2(BYTE data); BYTE I2CRead_2(BYTE noack); void I2CStop_2(void); void I2CStart_3(BYTE cont); BYTE I2CWrite_3(BYTE data); BYTE I2CRead_3(BYTE noack); void I2CStop_3(void); void delay1(void); //************************************************** // No.1 I2Cbus // SDA:RC0 // SCL:RA4 //************************************************** // // start condition process // void I2CStart_1(BYTE cont) { if(cont) { SDA_1 = 1; delay1(); SCL_1 = 1; delay1(); } SDA_1 = 0; delay1(); SCL_1 = 0; } // // I2C byte data output // BYTE I2CWrite_1(BYTE data) { BYTE i, ack; for(i = 0; i < 8; i++) { if(data & 0x80) { // H SDA_1 = 1; } else { // L SDA_1 = 0; } data <<= 1; delay1(); SCL_1 = 1; delay1(); SCL_1 = 0; } SDA_1 = 0; // read ACK SDA_INPUT_1; // SDA input delay1(); SCL_1 = 1; if(PORTC & 1<<0) { // read RC0(SDA) // 1 ack = 1; // no ACK } else { // 0 ack = 0; } delay1(); SCL_1 = 0; #asmline NOP SDA_OUTPUT_1; // SDA output delay1(); return ack; } // // I2C byte data input // BYTE I2CRead_1(BYTE noack) { BYTE sda, dt; BYTE i, ackb; SDA_INPUT_1; // SDA input dt = 0; for(i = 0; i < 8; i++) { dt <<= 1; delay1(); SCL_1 = 1; delay1(); if(PORTC & 0x01) { sda = 1; } else { sda = 0; } dt |= sda; #asmline NOP SCL_1 = 0; } // write ACK SDA_OUTPUT_1; // SDA output SDA_1 = noack; delay1(); SCL_1 = 1; delay1(); SCL_1 = 0; SDA_1 = 0; delay1(); return dt; } // // stop condition process // void I2CStop_1(void) { SCL_1 = 1; delay1(); SDA_1 = 1; delay1(); } //************************************************** // No.2 I2Cbus // SDA:RD1 // SCL:RD0 //************************************************** // // start condition process // void I2CStart_2(BYTE cont) { if(cont) { SDA_2 = 1; delay1(); SCL_2 = 1; delay1(); } SDA_2 = 0; delay1(); SCL_2 = 0; } // // I2C byte data output // BYTE I2CWrite_2(BYTE data) { BYTE i, ack; for(i = 0; i < 8; i++) { if(data & 0x80) { // H SDA_2 = 1; } else { // L SDA_2 = 0; } data <<= 1; delay1(); SCL_2 = 1; delay1(); SCL_2 = 0; } SDA_2 = 0; // read ACK SDA_INPUT_2; // SDA input delay1(); SCL_2 = 1; if(PORTD & 1<<1) { // read RD1(SDA) // 1 ack = 1; // no ACK } else { // 0 ack = 0; } delay1(); SCL_2 = 0; #asmline NOP SDA_OUTPUT_2; // SDA output delay1(); return ack; } // // I2C byte data input // BYTE I2CRead_2(BYTE noack) { BYTE sda, dt; BYTE i, ackb; SDA_INPUT_2; // SDA input dt = 0; for(i = 0; i < 8; i++) { dt <<= 1; delay1(); SCL_2 = 1; delay1(); if(PORTD & 0x02) { sda = 1; } else { sda = 0; } dt |= sda; #asmline NOP SCL_2 = 0; } // write ACK SDA_OUTPUT_2; // SDA output SDA_2 = noack; delay1(); SCL_2 = 1; delay1(); SCL_2 = 0; SDA_2 = 0; delay1(); return dt; } // // stop condition process // void I2CStop_2(void) { SCL_2 = 1; delay1(); SDA_2 = 1; delay1(); } //************************************************** // No.3 I2Cbus // SDA:RD3 // SCL:RD2 //************************************************** // // start condition process // void I2CStart_3(BYTE cont) { if(cont) { SDA_3 = 1; delay1(); SCL_3 = 1; delay1(); } SDA_3 = 0; delay1(); SCL_3 = 0; } // // I2C byte data output // BYTE I2CWrite_3(BYTE data) { BYTE i, ack; for(i = 0; i < 8; i++) { if(data & 0x80) { // H SDA_3 = 1; } else { // L SDA_3 = 0; } data <<= 1; delay1(); SCL_3 = 1; delay1(); SCL_3 = 0; } SDA_3 = 0; // read ACK SDA_INPUT_3; // SDA input delay1(); SCL_3 = 1; if(PORTD & 1<<3) { // read RD3(SDA) // 1 ack = 1; // no ACK } else { // 0 ack = 0; } delay1(); SCL_3 = 0; #asmline NOP SDA_OUTPUT_3; // SDA output delay1(); return ack; } // // I2C byte data input // BYTE I2CRead_3(BYTE noack) { BYTE sda, dt; BYTE i, ackb; SDA_INPUT_3; // SDA input dt = 0; for(i = 0; i < 8; i++) { dt <<= 1; delay1(); SCL_3 = 1; delay1(); if(PORTD & 0x08) { sda = 1; } else { sda = 0; } dt |= sda; #asmline NOP SCL_3 = 0; } // write ACK SDA_OUTPUT_3; // SDA output SDA_3 = noack; delay1(); SCL_3 = 1; delay1(); SCL_3 = 0; SDA_3 = 0; delay1(); return dt; } // // stop condition process // void I2CStop_3(void) { SCL_3 = 1; delay1(); SDA_3 = 1; delay1(); } //************************************************** // // delay // void delay1() { #asm NOP NOP NOP #asmend } トップページヘもどる |