|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
|
#define uchar unsigned char
#define SDA P1.0 /* microcontroller's I/O lines */
#define SCL P1.1 /* assigned to I2C lines */
/****************************************************
Issuing of START condition.
****************************************************/
void start(void)
{
SDA = SCL = 1;
SDA = 0;
_opc(0); /* it places NOP instruction */
_opc(0); /* into executable code */
_opc(0);
_opc(0);
_opc(0);
SCL = 0;
}
/****************************************************
Issuing of STOP condition.
****************************************************/
void stop(void)
{
SDA = 0;
SCL = 1;
_opc(0);
_opc(0);
_opc(0);
_opc(0);
_opc(0);
SDA = 1;
}
/****************************************************
Clock pulse generation. The function returns data
or acknowledgment bit.
****************************************************/
bit clock(void)
{
bit level; /* state of SDA line */
SCL = 1;
_opc(0);
while(!SCL); /* if a pulse was stretched */
_opc(0);
_opc(0);
_opc(0);
level = SDA;
_opc(0);
_opc(0);
SCL = 0;
return(level);
}
/****************************************************
Writing a byte to a slave, with most significant
bit first. The function returns acknowledgment bit.
****************************************************/
bit write(uchar byte)
{
uchar mask = 0x80;
while(mask)
{
if (byte & mask)
SDA = 1;
else
SDA = 0;
clock();
mask >>= 1; /* next bit to send */
}
SDA = 1; /* releasing of the line */
return(clock()); /* a slave should acknowledge */
}
/****************************************************
Reading byte from a slave, with most significant
bit first. The parameter indicates, whether to
acknowledge (1) or not (0).
****************************************************/
uchar read(bit acknowledgment)
{
uchar mask = 0x80,
byte = 0x00;
while(mask)
{
if (clock())
byte |= mask;
mask >>= 1; /* next bit to receive */
}
if (acknowledgment)
{
SDA = 0;
clock();
SDA = 1;
}
else
{
SDA = 1;
clock();
}
return(byte);
}
|
|||||||||||||||||||||||||||||||||
|
Copyright © 2003 ChipCenter-QuestLink About ChipCenter-Questlink |
||||||||||||||||||||||||||||||||||