📄 iic_host_sim.c
字号:
#include "ATT7025.h"
#include <intrins.h>
sbit SCL=P3^0;
sbit SDA=P3^1;
bit bdata SystemError;
unsigned char idata addbuf[4];
unsigned char xdata sendbuf[10]; // 数据发送缓冲区;
unsigned char xdata receivebuf[10]; // 数据接收缓冲区;
void System_Initialize(void); //The system run in high frequency mode
void Variable_Initialize(void); //Initialize user's variables
void E2PROM_Written_Enable(void); //Open E2PROM written-protection
void writeNbyte(unsigned char slave_add, unsigned int word_address, unsigned char n);
void receiveNbyte(unsigned char idata slave_add, unsigned int word_address, unsigned char n);
void iic_start(void);
void iic_stop(void);
void Delay(void); //Waiting for writting E2PROM
void delayNOP(void);// ajust I2C bus
void delay(unsigned int N);
unsigned char count;
main()
{
System_Initialize();
Variable_Initialize();
E2PROM_Written_Enable();
writeNbyte(0xA0,0x0010,4);
delay(500);
receiveNbyte(0xA0,0x0010,4);
_nop_();
_nop_();
_nop_();
for(count=1; count<5; count++)
{
}
_nop_();
_nop_();
_nop_();
Delay();
_nop_();
_nop_();
_nop_();
Delay();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
/*
Discription: The system run in high frequency mode
*/
void System_Initialize(void)
{
BWPR = 0xC3;
BWPR = 0x9B; //enable the register-written
BORCFG |= 0x80; //high frequency mode
BWPR = 0xA8; //disable the register-written
while((SYSSCR&0x80) != 0x80); //Waiting for high frequency stabilizing
BWPR = 0xC3;
BWPR = 0x9B; //enable the register-written
CLKCFG |= 0x02; //SYSCK1=1,fsys=fpri
BWPR = 0xA8; //disable the register-written
}
void Variable_Initialize(void)
{
unsigned char i;
for(i=0; i<10; i++)
{
sendbuf[i]=i+0x50 ; // 数据发送缓冲区;
receivebuf[i]=0 ; //clear data receive buffer
}
}
/*
Discription: Setup I2C registers
//时钟保持高,数据线从高到低一次跳变,I2C通信开始
*/
void iic_start(void)
{
EA = 1; //disable global interrupt
BWPR = 0xC3;
BWPR = 0x9B; //enable the registers written
SUPDC = SUPDC |0x02; //PDI2C = 1 Close I2C sector. Open GPIO function
BWPR = 0xA8; //disable the register written
DDRP3=DDRP3|(0X03); // 将p3.0\p3.1设置成输出
SDA = 1;
SCL = 1;
delay(5); // 延时50us
SDA = 0;
delay(5); // 延时50us
SCL = 0;
delay(5);
}
/*
Discription: Stop I2C transmission with GPIO function
//时钟保持高,数据线从低到高一次跳变,I2C通信停止
*/
void iic_stop(void)
{
SDA = 0;
delay(1);
SCL = 1;
delay(2);
SDA = 1;
delay(1);
//SCL = 0;
}
//--------------------------------------------------------------------------------------------------
// 函数名称: slave_ACK
// 函数功能: 从机发送应答位子程序
//--------------------------------------------------------------------------------------------------
void slave_ACK(void)
{
SDA = 0;
delay(1);
SCL = 1;
delay(1);
SCL = 0;
delay(1);
SDA = 1;
}
//--------------------------------------------------------------------------------------------------
// 函数名称: slave_NOACK
// 函数功能: 从机发送非应答位子程序,迫使数据传输过程结束
//--------------------------------------------------------------------------------------------------
void slave_NOACK(void)
{
SDA = 1;
SCL = 1;
delayNOP();
SDA = 0;
SCL = 0;
}
//--------------------------------------------------------------------------------------------------
// 函数名称: check_ACK
// 函数功能: 主机应答位检查子程序,迫使数据传输过程结束
//--------------------------------------------------------------------------------------------------
void check_ACK(void)
{
DDRP3=DDRP3&(0XFD); // 将p3.1设置成输入
// SDA = 1;
SCL = 1;
delay(1);
SCL = 1;
F0 = 0;
delay(1);
if(SDA == 1) // 若SDA=1表明非应答,置位非应答标志F0
F0 = 1;
delay(1);
SCL = 0;
DDRP3=DDRP3|(0X02); // 将p3.1设置成输出
SDA = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
//--------------------------------------------------------------------------------------------------
// 函数名称: IICSendByte
// 入口参数: ch
// 函数功能: 发送一个字节
//--------------------------------------------------------------------------------------------------
void IICSendByte(unsigned char ch)
{
unsigned char idata n=8; // 向SDA上发送一位数据字节,共八位
while(n--)
{
if((ch&0x80) == 0x80) // 若要发送的数据最高位为1则发送位1
{
SDA = 1; // 传送位1
delay(1);
SCL = 1;
delay(4);
SCL = 0;
delay(1);
SDA = 0;
}
else
{
SDA = 0; // 否则传送位0
delay(1);
SCL = 1;
delay(4);
SCL = 0;
delay(1);
}
ch = ch<<1; // 数据左移一位
}
}
//--------------------------------------------------------------------------------------------------
// 函数名称: IICreceiveByte
// 返回接收的数据
// 函数功能: 接收一字节子程序
//--------------------------------------------------------------------------------------------------
unsigned char IICreceiveByte(void)
{
unsigned char idata n=8; // 从SDA线上读取一上数据字节,共八位
unsigned char tdata;
while(n--)
{
DDRP3=DDRP3&(0XFD); // 将p3.1设置成输入
delay(1);
SCL = 1;
_nop_();
tdata = tdata<<1; // 左移一位,或_crol_(temp,1)
if(SDA == 1)
tdata = tdata|0x01; // 若接收到的位为1,则数据的最后一位置1
else
tdata = tdata&0xfe; // 否则数据的最后一位置0
delay(1);
SCL=0;
}
DDRP3=DDRP3|(0X02); // 将p3.1设置成输出
return(tdata);
}
//--------------------------------------------------------------------------------------------------
// 函数名称: writeNbyte
// 入口参数: slave_add从机地址,n要发送的数据个数
// 函数功能: 发送n位数据子程序
//--------------------------------------------------------------------------------------------------
void writeNbyte(unsigned char slave_add, unsigned int word_address, unsigned char n)
{
unsigned char idata send_da,j=0,i=0,h=3;
unsigned char high_address, low_address;
high_address = (word_address & 0xFF00) >> 8;
low_address = word_address & 0x00FF;
addbuf[0]=slave_add;
addbuf[1]=high_address;
addbuf[2]=low_address;
iic_start(); // 启动I2C
while(h--)
{
IICSendByte(addbuf[i++]);
check_ACK();
if (F0 == 1)
{
SystemError=1;
return; // 若非应答表明器件错误或已坏,置错误标志位SystemError
}
}
i=0;
while(n--)
{
send_da = sendbuf[i++];
IICSendByte(send_da);
check_ACK(); // 检查应答位
if (F0 == 1)
{
SystemError=1;
return; // 若非应答表明器件错误或已坏,置错误标志位SystemError
}
}
iic_stop(); // 全部发完则停止
}
//--------------------------------------------------------------------------------------------------
// 函数名称: receiveNbyte
// 入口参数: slave_add从机地址,n要接收的数据个数
// 函数功能: 接收n位数据子程序
//--------------------------------------------------------------------------------------------------
void receiveNbyte(unsigned char idata slave_add, unsigned int word_address, unsigned char n)
{
unsigned char idata receive_da,j=0,i=0,h=3;
unsigned char high_address, low_address;
high_address = (word_address & 0xFF00) >> 8;
low_address = word_address & 0x00FF;
addbuf[0]=slave_add;
addbuf[1]=high_address;
addbuf[2]=low_address;
addbuf[3]=slave_add|(0x01);
iic_start(); // 启动I2C
while(h--)
{
IICSendByte(addbuf[i++]);
check_ACK();
if (F0 == 1)
{
SystemError=1;
return; // 若非应答表明器件错误或已坏,置错误标志位SystemError
}
}
iic_start();
IICSendByte(addbuf[3]);
i=0;
check_ACK();
if (F0 == 1)
{
SystemError=1;
return; // 若非应答表明器件错误或已坏,置错误标志位SystemError
}
while(n--)
{
receive_da=IICreceiveByte();
receivebuf[i++]=receive_da;
slave_ACK(); // 收到一个字节后发送一个应答位
}
slave_NOACK(); // 收到最后一个字节后发送一个非应答位
iic_stop();
}
/*
Discription: Open E2PROM written-protection
*/
void E2PROM_Written_Enable(void)
{
BWPR = 0xC3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -