#include <aduc831.h>
#include <stdio.h>
#include <math.h>
#include <intrins.h>
#define HIGH 1
#define LOW 0
int b_ACK = 0;
//sbit I2CDAT = P2^4;
//sbit scl = P2^6;
//sbit LCD_EN = P2^7;
//sbit DIR_245= P2^5;//HC245 DIR pin
/*
void I2C_Delay()
{ char i;
for(i=0;i<50;i++);
}
*/
/******************
start I2C, send I2C start bit
*******************/
void StartBit_temp()
{
MDE=1;
MDO=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
MCO=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
MDO=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
MCO=0;
_nop_();
_nop_();
}
/******************
send the stop bit to end an I2C communication
*******************/
void StopBit_temp()
{
MDE=1;
MDO = LOW;
_nop_();
_nop_();
MCO = HIGH;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
MDE=1;
MDO = HIGH;
_nop_();
_nop_();
_nop_();
}
/*****************
send 8-bits byte to the slave
******************/
void SendByte_temp(unsigned char c)
{
unsigned BitCnt; //8 bits in a byte
//start to send bit, 8 rounds
for(BitCnt=0;BitCnt<8;BitCnt++)
{
MDE=1;
if((c<0));
else
{MDE=1; MDO=0;}
_nop_();
_nop_();
MCO=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
MCO = 0;
}
_nop_();
_nop_();
MDE=1;
MDO = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
MCO = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
MDE=0;
if(MDI)b_ACK = 0;
else b_ACK = 1;
MCO = 0;
_nop_();
_nop_();
}
bit ISendByte(unsigned char sla,unsigned char c,unsigned char d)
{
// StartBit();
// SendByte(sla);
// if(!b_ACK)return(0);
// SendByte(c);
// if(!b_ACK)return(0);
// SendByte(d);
// if(!b_ACK)return(0);
// StopBit();
// return(1);
StartBit_temp();
SendByte_temp(sla);
if(!b_ACK)return(0);
SendByte_temp(c);
if(!b_ACK)return(0);
SendByte_temp(d);
if(!b_ACK)return(0);
StopBit_temp();
return(1);
}
void main()
{
int i;
I2CM=1;
MDE=1;
MDI=1;
SPE = 0; //Disable the SPI function in order to enable T2C function.
I2CCON = 0xA8; //set Master Mode, disables the output drivers on I2CDAT
//scl float high;
SPIM=1;
CPOL=1;
CPHA=1;
StopBit_temp();
MCO=1;
for(i=0;i<100;i++);
ISendByte(0x40,0x03,0x00); //config valve I2C as output
ISendByte(0x40,0x01,0x00); //close all valve output
}