?? i2c_in_c.txt
字號:
#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);
}
//=========================================================================
#define EEPROM 0xAE /* slave address, data direction
bit = 0 */
bit EEPROM_byte_write(uchar address, uchar byte)
{
bit status;
status = 0; /* failure by default */
start();
if (!write(EEPROM)) /* write operation */
if (!write(address)) /* byte address */
if (!write(byte))
status = 1; /* success */
stop();
return(status);
}
//=========================================================================
bit EEPROM_acknowledge_polling(void)
{
bit status;
start();
status = write(EEPROM);
stop();
return(status); /* if 1, the write cycle is in progress */
}
void EEPROM_busy(void)
{
while(EEPROM_acknowledge_polling())
delay(1,164); /* about 1 msec */
}
//=========================================================================
#define T1_SETP_ADDR 0 /* starting locations of set point */
#define T2_SETP_ADDR 4 /* values of temperatures */
:
/****************************************************
Global variables.
****************************************************/
float t1_setp, /* temperatures' set points */
t2_setp;
:
/****************************************************
Initialization of the system.
****************************************************/
void initialization(void)
{
:
EEPROM_sequential_read(&t1_setp,T1_SETP_ADDR,4);
EEPROM_sequential_read(&t2_setp,T2_SETP_ADDR,4);
:
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -