📄 i2cbuscode.txt
字号:
#include <iom16v.h>
#include <macros.h>
#include <delay.h>
#define uchar unsigned char
uchar mid;
#define SCL 0x01 // IIC clock
#define SDA 0x02 // IIC data
#define true 1
#define false 0
/*===================================================================
// 函数功能: 模拟I2C接口初始化函数
// 形参: void
// 返回: void
// 编写: 2004/6/27
// 备注: 放于端口初始化之后调用
===================================================================*/
void Lib_I2C_Ini(void)
{
DDRC |= SCL;
DDRC |= SDA;
PORTC |= SCL;
PORTC |= SDA;
}
/*===================================================================
// 函数功能: 模拟I2C接口延时函数
// 形参: void
// 返回: void
// 编写: 2004/6/27
// 备注: 约延时7us,这样,I2C速度大约为100kHz
===================================================================*/
void Lib_I2C_Delay(void)
{
unsigned char tmp;
for(tmp = 0; tmp < 200; tmp++)
_NOP();
}
/*===================================================================
// 函数功能: 模拟I2C接口启动
// 形参: void
// 返回: void
// 编写: 2004/6/27
// 备注: 在SCL为高时间内变化SDA由高到低即为总线启动
===================================================================*/
void Lib_I2C_Start(void)
{
DDRC |= SDA;
PORTC |= SCL;
PORTC |= SDA;
Lib_I2C_Delay(); // START condition setup time
PORTC &= ~SDA;
Lib_I2C_Delay(); // START condition hold time
}
/*===================================================================
// 函数功能: 模拟I2C接口停止
// 形参: void
// 返回: void
// 编写: 2004/6/27
// 备注: 在SCL为高时间内变化SDA由低到高即为总线停止
===================================================================*/
void Lib_I2C_Stop(void)
{
DDRC |= SDA;
PORTC &= ~SDA;
PORTC &= ~SCL;
PORTC |= SCL;
Lib_I2C_Delay(); // STOP condition setup time
PORTC |= SDA;
Lib_I2C_Delay(); // STOP condition hold time
}
void Lib_I2C_CLK(void)
{
PORTC |= SCL;
Lib_I2C_Delay();
PORTC &= ~SCL;
}
/*===================================================================
// 函数功能: 模拟I2C接口接收一个ACK
// 形参: void
// 返回: unsigned char 收到的回复
// 编写: 2004/6/27
===================================================================*/
unsigned char Lib_I2C_ACK(void)
{
PORTC &= ~SCL;
DDRC &= ~SDA;
PORTC |= SCL; // Clock the ACK bit
Lib_I2C_Delay();
if((SDA & PINC) == SDA) // Check the ACK bit
{
PORTC &= ~SCL;
return true;
}
else
{
PORTC &= ~SCL;
return false;
}
}
/*===================================================================
// 函数功能: 模拟I2C接口送出一个数据
// 形参: data 要送出的数据
// 返回: void
// 编写: 2004/6/27
// 备注: SCL的下降沿读数
===================================================================*/
unsigned char Lib_I2C_Send(unsigned char data)
{
unsigned char i;
DDRC |= SDA;
PORTC &= ~SCL;
for(i = 0; i < 8; i++)
{
if((data & 0x80) == 0x80)
PORTC |= SDA;
else
PORTC &= ~SDA;
Lib_I2C_CLK();
data <<= 1;
}
// if(!Lib_I2C_ACK()) // no ack
// return false;
// else
// return true;
while(Lib_I2C_ACK());
return true;
}
/*===================================================================
// 函数功能: 模拟I2C接口接收一个字节数据
// 形参: void
// 返回: unsigned char 接收到的数据
// 编写: 2004/6/27
===================================================================*/
unsigned char Lib_I2C_Rcv(void)
{
unsigned char i;
unsigned char indata;
DDRC &= ~SDA;
for(i = 0; i < 8; i++)
{
indata <<= 1;
if(SDA & PINC) // receive logic 1
indata ++;
Lib_I2C_CLK();
}
Lib_I2C_CLK();
return indata;
}
//***********************************初始化M16硬件**********8
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xff;
PORTB = 0x00;
DDRB = 0xff;
PORTC = 0x00; //m103 output only
DDRC = 0xff;
PORTD = 0x00;
DDRD = 0xff;
}
void Send_NByte(unsigned int Rom_add,unsigned int Adata)//********************************多字节数据的发送
{
Lib_I2C_Start();
Lib_I2C_Send(0xa0);//24C02的物理地址
Lib_I2C_Send(Rom_add);//数据存储地址
Lib_I2C_Send(Adata);//想要存储的数据
Lib_I2C_Stop();
delay_nms(10);
}
void Receive_NByte(unsigned int addr)//********************多字节接收
{
Lib_I2C_Start();
Lib_I2C_Send(0xa0);
Lib_I2C_Send(addr>>8);
Lib_I2C_Send(addr);
Lib_I2C_Start();
Lib_I2C_Send(0xa1);
mid=Lib_I2C_Rcv();
if (Lib_I2C_ACK())
delay_nus(10);
else
Lib_I2C_Stop();
}
void main(void)
{
unsigned char temp=0;
port_init();
uart_init();
Send_NByte(0x00aa,0x5a);
delay_nms(1000);
Receive_NByte(0x00aa);
while(1)
{
Receive_NByte(0x00aa);
PORTB=mid;
PORTB=0xff;
delay_nms(500);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -