/****************************************************************
Copyright(c) 2005 Tsinghua Tongfang Optical Disc Co., Ltd.
Developed by Storage Product Department, Beijing, China
*****************************************************************
Filename: i2c.c
Description: the operation of i2c interface i2c接口操作
****************************************************************/
#include "arm.h"
#include "global.h"
#include "timer.h"
#define SDAIN define_as_input(0,SDA); // SDAIN
#define SDAOUT define_as_output(0,SDA); // SDAOUT
#define SDA__0 clear_pio_output(0,SDA); // SDA__0
#define SDA__1 set_pio_output(0,SDA); // SDA__1
#define SCL__0 clear_pio_output(0,SCL); // SCL__0
#define SCL__1 set_pio_output(0,SCL); // SCL__1
#define WPE__0 clear_pio_output(0,EEWP); // WPE__0
#define WPE__1 set_pio_output(0,EEWP); // WPE__1
//char logoarray[20];
//char *logo = logoarray;
BOOL InitI2C(void) //初始化i2c
{
WPE__1
SCL__0
SDA__1
return TRUE;
}
void I2Cstart(void) //i2c开始
{
SCL__0
SDA__1
SCL__1
SDA__0
SCL__0
}
void I2Cstop(void) //i2c停止
{
SCL__0
SDA__0
SCL__1
SDA__1
SCL__0
}
void I2Cack(BOOL ack) //i2c应答
{
SCL__0
if (ack) SDA__0 else SDA__1
SCL__1
SCL__0
if(ack) SDA__1
}
void I2Csendbyte(BYTE b) //i2c发送字节
{
BYTE i;
for (i=0;i<8;i++) {
SCL__0
if (b & 0x80) SDA__1 else SDA__0
b <<= 1;
SCL__1
SCL__0
}
I2Cack(TRUE);
}
BYTE I2Creadbyte(void) //i2c读取字节
{
BYTE i,b = 0;
SCL__0
SDAIN
for (i=0;i<8;i++) {
SCL__0
SCL__1
b <<= 1;
if (read_pio_pin(0) & SDA) b |= 0x01;
}
SCL__0
SDAOUT
return b;
}
BOOL I2CWriteByte(WORD address,BYTE b) //i2c写入字节
{
WPE__0
I2Cstart();
I2Csendbyte(0xa0 | ((address & 0x700) >> 7) );
I2Csendbyte(address & 0xff);
I2Csendbyte(b);
I2Cstop();
DelayTimer(9000); //6ms it is necessary for inner logic to write data
WPE__1
return TRUE;
}
BYTE I2CReadByte(WORD address) //i2c读取字节
{
BYTE b;
I2Cstart();
I2Csendbyte(0xa0 | ((address & 0x700) >> 7));
I2Csendbyte(address & 0xff);
I2Cstart();
I2Csendbyte(0xa0 | ((address & 0x700) >> 7) | 0x01);
b = I2Creadbyte();
I2Cack(FALSE);
I2Cstop();
return b;
}
void Renew_the_string(char *pt) //更新字符串
{
BYTE i = 0;
char *st;
st=pt;
for(i = 0; i < 20; i++) {
if(*st == 0x1b){
if(*(st+1) == 0x5b)
if(*(st+2) == 0x44){
pt--;
st += 3;
i += 3;
}
}
*pt=*st;
pt++;
st++;
}
}
void StoreData2I2C(WORD address, char *pt) //存储数据到i2c总线
{
BYTE i = 0;
Renew_the_string(pt);
while((*pt != '\0') && (*pt != 0x0d) && (i < 15))
{
I2CWriteByte(address, *pt);
pt++;
address++;
i++;
}
I2CWriteByte(address, '\0');
}
void ReadDataFromI2c(WORD address, char *pt) //从i2c中读取数据
{
BYTE i = 0, byte_temp;
do {
byte_temp = I2CReadByte(address);
if((byte_temp == '\0') || (byte_temp == 0x0d) || (i >= 15))
break;
*pt = byte_temp;
pt++;
address++;
i++;
} while(1);
*pt = '\0';
}
i2c.rar_at91m4
版权申诉
73 浏览量
2022-09-19
13:18:43
上传
评论
收藏 3KB RAR 举报
刘良运
- 粉丝: 67
- 资源: 1万+
最新资源
- python-leetcode面试题解之第186题反转字符串中的单词II-题解.zip
- 一个基于python的web后端高性能开发框架,下载可用
- python-leetcode面试题解之第179题最大数-题解.zip
- python-leetcode面试题解之第170题两数之和III数据结构设计-题解.zip
- python-leetcode面试题解之第168题Excel表列名称-题解.zip
- python-leetcode面试题解之第167题两数之和II输入有序数组-题解.zip
- python-leetcode面试题解之第166题分数到小数-题解.zip
- python-leetcode面试题解之第165比较版本号-题解.zip
- python-leetcode面试题解之第163题缺失的区间-题解.zip
- python-leetcode面试题解之第162题寻找峰值-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈