注:本文是作者以前发表在其个人博客,现在发布到电子发烧友专栏
本例是用针对标准I2C接口EEPROM存储器24C02进行读写操作,只要对例程做适当修改,就可以用到大部分控制I2C接口设备的场合。
应广单片机软件I2C接口例程(MASTER模式)
本例仅供参考,欢迎指正程序中的问题
//-----------------------------------------
.chip p201cs14a
//{{PADAUK_CODE_OPTION
.Code_OptionBootupSlow// 1024 ILRC
.Code_OptionLVD2.79V// Maximum performance = 4 MIPS
.Code_OptionSecurityEnable// Security 3/4 words Enable
//}}PADAUK_CODE_OPTION
//定义I2C接口
I2C_SDAequpa.7
I2C_SCLequpa.6
I2C_SDA_DIRequpac.7
I2C_SCL_DIRequpac.6
I2C_LONG_DLYequ50
I2C_SHORT_DLYequ20
I2C_SDA_HIGHequset1I2C_SDA
I2C_SDA_LOWequset0I2C_SDA
I2C_SCL_HIGHequset1I2C_SCL
I2C_SCL_LOWequset0I2C_SCL
I2C_SDA_OUTPUTequset1I2C_SDA_DIR
I2C_SDA_INPUTequset0I2C_SDA_DIR
I2C_SCL_OUTPUTequset1I2C_SCL_DIR
I2C_SCL_INPUTequset0I2C_SCL_DIR
//定义I2C变量
bytei2c_rw_addr//读写地址
bytei2c_rw_byte//读写数据
bytei2c_rw_cmd//读写的器件地址
bytei2c_rw_temp//读写过程中间变量
bytei2c_rw_cnt//读写过程中间变量
//
byteXms
bytems_cnt
//
bytetest_addr
bytetest_data
.romadr0x000
gotomain0
gotomain1
.romadr0x010
isr_entry:
pushaf
intrq = 0
popaf
reti
//----------------------------
//产生START信号
//----------------------------
i2c_start:
I2C_SDA_OUTPUT
I2C_SCL_OUTPUT
I2C_SDA_HIGH
delayI2C_LONG_DLY
I2C_SCL_HIGH
delayI2C_LONG_DLY
I2C_SDA_LOW
delayI2C_LONG_DLY
I2C_SCL_LOW
delayI2C_LONG_DLY
ret
//----------------------------
//产生STOP信号
//----------------------------
i2c_stop:
I2C_SCL_LOW
delayI2C_LONG_DLY
I2C_SDA_LOW
delayI2C_LONG_DLY
I2C_SCL_HIGH
delayI2C_LONG_DLY
I2C_SDA_HIGH
delayI2C_LONG_DLY
//
I2C_SCL_INPUT
I2C_SDA_INPUT
ret
//----------------------------
//检查SALVE ACK信号
//----------------------------
i2c_slave_ack:
//don't check ACK
I2C_SDA_INPUT
delayI2C_SHORT_DLY
I2C_SCL_HIGH
delayI2C_SHORT_DLY
I2C_SCL_LOW
delayI2C_SHORT_DLY
I2C_SDA_OUTPUT
I2C_SDA_LOW
delayI2C_SHORT_DLY
ret
//----------------------------
//输出MASTER ACK信号
//----------------------------
i2c_master_ack:
I2C_SDA_OUTPUT
I2C_SDA_LOW
delayI2C_SHORT_DLY
I2C_SCL_HIGH
delayI2C_SHORT_DLY
I2C_SCL_LOW
delayI2C_SHORT_DLY
ret
//----------------------------
//输出MASTER NACK信号
//----------------------------
i2c_master_nack:
I2C_SDA_OUTPUT
I2C_SDA_HIGH
delayI2C_SHORT_DLY
I2C_SCL_HIGH
delayI2C_SHORT_DLY
I2C_SCL_LOW
delayI2C_SHORT_DLY
ret
//------------------------------
//写一个字节
//Input: i2c_rw_temp
//Used: i2c_rw_cnt
//------------------------------
i2c_write_8bit:
i2c_rw_cnt = 8
i2c_write_8bit_loop:
slci2c_rw_temp
swapcI2C_SDA
delayI2C_SHORT_DLY
I2C_SCL_HIGH
delayI2C_SHORT_DLY
I2C_SCL_LOW
delayI2C_SHORT_DLY
dzsni2c_rw_cnt
gotoi2c_write_8bit_loop
ret
//------------------------------
//读一个字节
//Used: i2c_rw_cnt
//Output: i2c_rw_temp
//------------------------------
i2c_read_8bit:
i2c_rw_temp = 0
i2c_rw_cnt = 8
delayI2C_SHORT_DLY
i2c_read_8bit_loop:
I2C_SCL_HIGH
delayI2C_SHORT_DLY
swapcI2C_SDA
slci2c_rw_temp
I2C_SCL_LOW
delayI2C_SHORT_DLY
dzsni2c_rw_cnt
gotoi2c_read_8bit_loop
ret
//------------------------------
//Input: i2c_rw_addr
// i2c_rw_cmd
//Used: i2c_rw_cnt
// i2c_rw_temp
//Output: i2c_rw_byte
//------------------------------
i2c_read_byte:
//start
calli2c_start
//write device address(write)
i2c_rw_temp = i2c_rw_cmd
calli2c_write_8bit
//slave ack
calli2c_slave_ack
//write register address
i2c_rw_temp = i2c_rw_addr
calli2c_write_8bit
//slave ack
//don't check ACK
calli2c_slave_ack
//start repeat
calli2c_start
//write device address(read)
i2c_rw_temp = i2c_rw_cmd
i2c_rw_temp.0 = 1
calli2c_write_8bit
//slave ack
//don't check ACK
I2C_SDA_INPUT
delayI2C_SHORT_DLY
I2C_SCL_HIGH
delayI2C_SHORT_DLY
I2C_SCL_LOW
delayI2C_SHORT_DLY//这里为特殊情况I2C_SDA不用转为输出
//read data
i2c_rw_temp = 0
calli2c_read_8bit
i2c_rw_byte = i2c_rw_temp//store data
//master nack
calli2c_master_nack
//stop
calli2c_stop
//retune
delayI2C_LONG_DLY
wdreset
ret
//------------------------------
//Input: i2c_rw_addr
// i2c_rw_byte
// i2c_rw_cmd
//Used: i2c_rw_cnt
// i2c_rw_temp
//------------------------------
i2c_write_byte:
//start
calli2c_start
//write device address
i2c_rw_temp = i2c_rw_cmd
calli2c_write_8bit
//slave ack
//don't check ACK
calli2c_slave_ack
//write register address
i2c_rw_temp = i2c_rw_addr
calli2c_write_8bit
//slave ack
//don't check ACK
calli2c_slave_ack
//write data
i2c_rw_temp = i2c_rw_byte
calli2c_write_8bit
//slave ack
//don't check ACK
calli2c_slave_ack
//stop
calli2c_stop
//retune
delayI2C_LONG_DLY
wdreset
ret
EEPROM_RW_CMDequ0xA0
//------------------------------
//函数名: eeprom_read_byte
//Input: i2c_rw_addr
//Used: i2c_rw_cnt
// i2c_rw_temp
//Output: i2c_rw_byte
//------------------------------
eeprom_read_byte:
i2c_rw_cmd = EEPROM_RW_CMD
gotoi2c_read_byte//注意这里用的是跳转
//------------------------------
//函数名: eeprom_write_byte
//Input: i2c_rw_addr
// i2c_rw_byte
//Used: i2c_rw_cnt
// i2c_rw_temp
//注意: 调用完后需要等待一段时间以保证写操作完成
//------------------------------
eeprom_write_byte:
i2c_rw_cmd = EEPROM_RW_CMD
gotoi2c_write_byte//注意这里用的是跳转
//----------------------------------------
//input: ms
//该函数以4M频率为基准时钟实现延时
//----------------------------------------
delayXms:
while(Xms)
{
wdreset
ms_cnt = 20
while(ms_cnt)
{
delay195
ms_cnt--
}
Xms--
}
ret
main0:
.ADJUST_OTP_IHRCR8MIPS// IHRC/2 = 8MIPS, WatchDog Disable, RAM 0,1 temporary be used
sp = 0x30
disgint
inten = 0
pa = 0b0000_0000
paph = 0b1101_0000
pac = 0b0000_0001
pb = 0b0000_0000
pbph = 0b0000_0000
pbc = 0b1111_1111
I2C_SDA_INPUT
I2C_SCL_INPUT
delay200
mova,0b100_11_111
movt16m,a
clkmd.1 = 1//enable watch dog
wdreset
Xms = 100
calldelayXms
test_data = 0
test_addr = 0
main0_loop:
wdreset
//写E2EPROM
i2c_rw_addr = test_addr
i2c_rw_byte = test_data
calleeprom_write_byte
//调用EEPROM写操作函数后要等待一段时间,以保证数据写操作完成
Xms = 20
calldelayXms
//读E2EPROM
i2c_rw_addr = test_addr
calleeprom_read_byte
if(i2c_rw_byte != test_data)
{
//读回的数据比较出错,判断为读写E2PROM出错
nop
}
test_addr ++
test_data --
gotomain0_loop
//----------------FPPA1-------------------
main1:
sp = 52
main1_loop:
gotomain1_loop
本例代码是从实际程序中移植而来,已编译,未做最终调试
-
单机片
+关注
关注
0文章
50浏览量
11733
发布评论请先 登录
相关推荐
评论