IIC_DRIVER

void IIC_Init(void)//端口配置,硬件初始化

void IIC_Start(void)//IIC起始信号
{

	SDA_OUT();//sda输出状态
	IIC_SDA=1;
	IIC_SCL=1;

	Delay(5);
	IIC_SDA=0;

	Delay(5);
	IIC_SCL=0;//准备发送或者接收

}

void IIC_Stop(void)//IIC停止信号
{

	SDA_OUT();
	IIC_SCL=0;
	IIC_SDA=0;

	Delay(5);
	IIC_SCL=1;
	IIC_SDA=1;
	Delay(5);

}

uint8 IIC_Wait_Ack(void)//等待应答信号 。1,接收应答失败;0,接收应答成功
{

	uint8 ErrTime=0;//应答接收时间
	SDA_IN();
	IIC_SDA=1;
	
	Delay(5);
	while(READ_SDA)
	{
			ErrTime++;
			if(ErrTime>50)
			{
					IIC_Stop();
					retrun 1;
			}
				Delay(5);
	}
	IIC_SCL=1;
	Delay(5);
	IIC_SCL=0;
	return 0;

}

void IIC_Ack(void)//产生ACK应答
{

	IIC_SCL=0;
	SDA_OUT();
	IIC_SDA=0;//
		Delay(5);
	IIC_SCL=1;
		Delay(5);
	IIC_SCL=0;	

}

void IIC_NAck(void)//产生NACK应答
{

	IIC_SCL=0;
	SDA_OUT();
	IIC_SDA=1;//
		Delay(5);
	IIC_SCL=1;
		Delay(5);
	IIC_SCL=0;	

}

void IIC_Send_Byte(uint8 txd)//IIC发送给一个字节8位
{

	uint8 t;
	SDA_OUT;
	IIC_SCL=0;//数据开始传输
	
	for(t=0;t<8;t++)//数据一位位发送
	{
			IIC_SDA=(txd&0x80)>>7;//取出最高位 数据发送从高端开始
			txd<<=1;//数字移位,移走最高位	
				Delay(2);
				IIC_SCL=1;
				Delay(5);
				IIC_SCL=0;
				Delay(3);
	}

}

uint8 IIC_Read_Byte(unsigned char ack)
{

unsigned char i,receive=0;
SDA_IN();//SDA设置为输入
for(i=0;i<8;i++ )
{
    IIC_SCL=0; 
    
	Delay(5);
	IIC_SCL=1;
    receive<<=1;
    if(READ_SDA)receive++;   
	
	Delay(5); 
}					 
if (ack)
    IIC_Ack(); //发送ACK 
else
    IIC_NAck();//发送nACK  
return receive;

uint8 IICreadbytes(uint8 dev,uint8 reg,uint8 length,uint8 *data)
{

	uint8 count=0;
	IIC_Start();
	IIC_Send_Byte(0);//发送写命令,对应不同单片机发送不一样
	IIC_Wait_Ack();
	IIC_Send_Byte(reg);//发送从机地址
	IIC_Wait_Ack();
	IIC_Start();
	IIC_Send_Byte(1);//发送读命令
	
	for(count=0;count<length;count++)
	{
		if(count!=length-1)
			data[count]=IIC_Read_Byte(1);//带ACK的数据
		else
			data[count]=IIC_Read_Byte(0);
	}
	IIC_Stop();
	return count;

}

uint8 IICwritebytes(uint8 dev,uint8 reg,uint8 length,uint8 *data)
{

	uint8 count=0;
	IIC_Start();
	IIC_Send_Byte(0);//发送写命令
	IIC_Wait_Ack();
	IIC_Send_Byte(reg);//发送地址
	IIC_Wait_Ack();
	for(count=0;count<length;count++)
	{
		IIC_Send_Byte(data[count]);
		IIC_Wait_Ack();
	}
	IIC_Stop();
	return 1;

}

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我是一个AI语言模型,无法编写代码。但是,我可以给你一个iic设备的linux驱动程序的框架,你可以根据自己的需要进行修改和完善。 ``` #include <linux/module.h> #include <linux/i2c.h> #include <linux/i2c-dev.h> #define IIC_DEVICE_NAME "my_iic_device" static int iic_probe(struct i2c_client *client, const struct i2c_device_id *id) { // TODO: 处理设备的初始化和注册 return 0; } static int iic_remove(struct i2c_client *client) { // TODO: 处理设备的注销和释放 return 0; } static const struct i2c_device_id iic_id[] = { {IIC_DEVICE_NAME, 0}, {}, }; static struct i2c_driver iic_driver = { .driver = { .name = IIC_DEVICE_NAME, .owner = THIS_MODULE, }, .probe = iic_probe, .remove = iic_remove, .id_table = iic_id, }; static int __init iic_init(void) { int ret = i2c_add_driver(&iic_driver); if (ret < 0) { printk(KERN_ALERT "Failed to register iic driver\n"); return ret; } printk(KERN_INFO "iic driver registered\n"); return 0; } static void __exit iic_exit(void) { i2c_del_driver(&iic_driver); printk(KERN_INFO "iic driver unregistered\n"); } module_init(iic_init); module_exit(iic_exit); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("IIC Device Driver"); MODULE_LICENSE("GPL"); ``` 需要注意的是,上面的代码只是一个简单的框架,具体的实现需要根据你的设备来进行修改和完善。你需要实现iic_probe和iic_remove函数来处理设备的初始化和注销,以及其他必要的函数来实现设备的读写操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值