应广单片机读写24C02程序代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
应广单片机读写24C02程序代码
#include "extern.h"
#include "main.h"
//***************************************************
//***************************************************
//??ÒåI2C?Ó?Ú
I2C_SDA equ pb.2
I2C_SCL equ pb.0
I2C_SDA_DIR equ pbc.2
I2C_SCL_DIR equ pbc.0
I2C_LONG_DLY equ 50
I2C_SHORT_DLY equ 20
I2C_SDA_HIGH equ set1 I2C_SDA I2C_SDA_LOW equ set0 I2C_SDA
I2C_SCL_HIGH equ set1 I2C_SCL I2C_SCL_LOW equ set0 I2C_SCL
I2C_SDA_OUTPUT equ set1 I2C_SDA_DIR I2C_SDA_INPUT equ set0 I2C_SDA_DIR I2C_SCL_OUTPUT equ set1 I2C_SCL_DIR I2C_SCL_INPUT equ set0 I2C_SCL_DIR //??ÒåI2C?äÁ?
uchar i2c_rw_addr; //?ÁÐ?µØÖ? uchar i2c_rw_byte; //?ÁÐ?Êý?Ý uchar
i2c_rw_cmd; //?ÁÐ?µÄÆ??þµØÖ? uchar i2c_rw_temp; //?ÁÐ??ý?ÌÖÐ?ä?äÁ? uchar i2c_rw_cnt; //?ÁÐ??ý?ÌÖÐ?ä?äÁ?
//---------------------------- //?úÉúSTARTÐźÅ
//---------------------------- i2c_start:
I2C_SDA_OUTPUT
I2C_SCL_OUTPUT
I2C_SDA_HIGH
delay I2C_LONG_DLY
I2C_SCL_HIGH
delay I2C_LONG_DLY
I2C_SDA_LOW
delay I2C_LONG_DLY
I2C_SCL_LOW
delay I2C_LONG_DLY
ret
//---------------------------- //?úÉúSTOPÐźÅ
//---------------------------- i2c_stop:
I2C_SCL_LOW
delay I2C_LONG_DLY
I2C_SDA_LOW
delay I2C_LONG_DLY
I2C_SCL_HIGH
delay I2C_LONG_DLY
I2C_SDA_HIGH
delay I2C_LONG_DLY
ret
//----------------------------
//?ì?éSALVE ACKÐźŠ//---------------------------- i2c_slave_ack:
//don't check ACK
I2C_SDA_INPUT
delay I2C_SHORT_DLY
I2C_SCL_HIGH
delay I2C_SHORT_DLY
I2C_SCL_LOW
delay I2C_SHORT_DLY
I2C_SDA_OUTPUT
I2C_SDA_LOW
delay I2C_SHORT_DLY
ret
//----------------------------
//Êä?öMASTER ACKÐźŠ//---------------------------- i2c_master_ack:
I2C_SDA_OUTPUT
I2C_SDA_LOW
delay I2C_SHORT_DLY
I2C_SCL_HIGH
delay I2C_SHORT_DLY
I2C_SCL_LOW
delay I2C_SHORT_DLY
ret
//---------------------------- //Êä?öMASTER NACKÐźŠ//---------------------------- i2c_master_nack:
I2C_SDA_OUTPUT
I2C_SDA_HIGH
delay I2C_SHORT_DLY
I2C_SCL_HIGH
delay I2C_SHORT_DLY
I2C_SCL_LOW
delay I2C_SHORT_DLY
ret
//------------------------------
//Ð?Ò??ö×Ö?Ú
//Input: i2c_rw_temp
//Used: i2c_rw_cnt
//------------------------------
i2c_write_8bit:
i2c_rw_cnt = 8;
i2c_write_8bit_loop:
slc i2c_rw_temp
swapc I2C_SDA
delay I2C_SHORT_DLY
I2C_SCL_HIGH
delay I2C_SHORT_DLY
I2C_SCL_LOW