#include<lpc21xx.h>
#include<i2c.h>
//#include<lcd.h>
void i2c_init()
{
PINSEL0 |= 0x00000050; //config. P0.2->scl & P0.3->sda
I2CONCLR = 0x000000FF; //CLR ALL CNTL FLAG INCLUDING ACK
I2SCLL = 75;
I2SCLH = 75;
}
void i2c_tx_data(int device,int add,int data)
{
i2c_start();
I2DAT = device;
I2CONCLR = 0x08;
I2CONCLR = 0x20;
wait_for_ack(0x18);
I2DAT = add;
I2CONCLR = 0x08;
wait_for_ack(0x28);
I2DAT = data;
I2CONCLR = 0x08;
wait_for_ack(0x28);
I2CONCLR = 0x08;
i2c_stop();
}
void i2c_start(void)
{
I2CONCLR = 0x08; // I2C INTERRUPT CLEAR
delay1(10);
I2CONSET = 0x40; //enable I2C interface
delay1(10);
I2CONSET = 0x20; //to set start flag,& to enter in Master mode
wait_for_ack(0x08);
}
void i2c_stop(void)
{
I2CONSET =0x10 ;
delay1(10);
I2CONCLR = 0x40;
/* I2CONCLR = 0x08; //clear I2C interrupt bit
delay(10);
I2CONCLR = 0x40; //clear I2C interface bit */
}
void i2c_setpointer(int device ,int ptr)
{
i2c_start();
I2CONCLR = 0x20; // clear the START bit to avoid retransmit of START
I2DAT = device; // transmit slave address with write bit
I2CONCLR = 0x08;
wait_for_ack(0x18);
I2DAT = ptr;
I2CONCLR = 0x08;
wait_for_ack(0x28);
i2c_stop();
}
void wait_for_ack(unsigned int status)
{
while(1)
{
if(I2CONSET & 0x08)
{
delay1(20);
if(I2STAT == status)
{
break;
}
else
{
I2CONSET = 0x80;
I2CONCLR = 0xFF;
i2c_stop();
i2c_start();
break;
}
}
}
}
int i2c_rx_data(int device, int add)
{
int DATA;
i2c_setpointer(device,add);
i2c_start();
I2DAT = device|0x01;
I2CONCLR = 0x20;
I2CONCLR = 0x08;
wait_for_ack(0x40);
I2CONSET =0x04;
I2CONCLR = 0x08;
wait_for_ack(0x50);
DATA = I2DAT;
I2CONSET =0x04;
i2c_stop();
return DATA;
}
void i2c_tx_data_eeprom(int device,int ptr1,int ptr2,char data)
{
i2c_start();
I2DAT = device; //transmit slave address with write bit
I2CONCLR = 0x08;
I2CONCLR = 0x20;
wait_for_ack(0x18);
I2DAT = ptr1;
I2CONCLR = 0x08; //clear int flag
wait_for_ack(0x28);
I2DAT = ptr2;
I2CONCLR = 0x08;
wait_for_ack(0x28);
I2DAT = data;
I2CONCLR = 0x08;
wait_for_ack(0x28);
I2CONCLR = 0x08;
I2CONSET = 0x10; //enable stop condition
while(!(I2CONSET & 0x08))
{
I2CONSET = 0x20;
delay1(100);
I2DAT = 0xA1;
while(1)
{
if(I2CONSET & 0x08) break;
}
}
I2CONCLR = 0xFF;
i2c_stop();
}
char i2c_rx_data_eeprom(int device,int add1,int add2)
{
char temp;
i2c_setpointer_eeprom(device,add1,add2);
i2c_start();
I2DAT = device | 0x01;
I2CONCLR = 0x20;
I2CONCLR = 0x08;
wait_for_ack(0x40);
I2CONSET = 0x04;
I2CONCLR = 0x08;
wait_for_ack(0x50);
temp = I2DAT;
I2CONSET = 0x04;
i2c_stop();
return temp;
}
void delay1(int d)
{
int i;
while(d)
{
for(i=0;i<10000;i++);
d--;
}
}
void i2c_setpointer_eeprom(unsigned int device,unsigned int ptr1,unsigned int ptr2)
{
i2c_start();
I2CONCLR = 0x20;
I2DAT = device;
I2CONCLR = 0x08;
wait_for_ack(0x18);
I2DAT = ptr1;
I2CONCLR = 0x08;
wait_for_ack(0x28);
I2DAT = ptr2;
I2CONCLR = 0x08;
wait_for_ack(0x28);
I2CONCLR = 0x08;
i2c_stop();
}
没有合适的资源?快使用搜索试试~ 我知道了~
i2c.zip_The Program
共3个文件
c:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 195 浏览量
2022-09-21
07:28:26
上传
评论
收藏 2KB ZIP 举报
温馨提示
The program describes about the eeprom read through i2c protocol with the arm7 microcontroller
资源推荐
资源详情
资源评论
收起资源包目录
i2c.zip (3个子文件)
i2c
i2c_eeprom.c 336B
i2c_lib.c 3KB
lcd_lib.c 2KB
共 3 条
- 1
资源评论
朱moyimi
- 粉丝: 63
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功