#include
void IIC_Init(void)
{
SDA_OUT;
SCL_OUT;
SCL_1;
SDA_1;
}
void IIC_Start(void)
{
SDA_OUT;
SDA_1;
SCL_1;
delay_us(4);
SDA_0;
delay_us(4);
SCL_0;
}
void IIC_Stop(void)
{
SDA_OUT;
SCL_0;
SDA_0;
delay_us(4);
SCL_1;
SDA_1;
delay_us(4);
}
u8 IIC_Wait_Ack(void)
{
u8 ucErrTime = 0;
SDA_IN;
SDA_1;
delay_us(1);
SCL_1;
delay_us(1);
while(SDA_READ)
{
ucErrTime++;
if(ucErrTime > 250)
{
IIC_Stop();
}
}
SCL_0;
return 0;
}
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT;
SCL_0;
for(t=0;t<8;t++)
{
if((txd & 0x80)== 0x80)
{
SDA_1;
}
else
{
SDA_0;
}
txd <<= 1;
delay_us(2);
SCL_1;
delay_us(2);
SCL_0;
delay_us(2);
}
}
u8 write_dac7574(u16 dac ,u8 dacx)
{
u8 cmd;
IIC_Start();
IIC_Send_Byte(0x98);
IIC_Wait_Ack();
cmd = 0x10 | (dacx << 1);
IIC_send_Byte(cmd);
IIC_Wait_Ack();
cmd = dac >> 4 ;
IIC_Send_Byte(cmd);
IIC_Wait_Ack();
cmd = dac & 0x00f;
IIC_Send_Byte(cmd);
IIC_Wait_Ack();
IIC_Stop();
return 1;
}
u8 write_all_dac7574(u16 dac)
{
u8 cmd;
IIC_Start();
IIC_Send_Byte(0x98);
IIC_Wait_Ack();
IIC_Send_Byte(0x34);
IIC_Wait_Ack();
cmd = dac >> 4;
IIC_Send_Byte(cmd);
IIC_Wait_Ack();
cmd = dac & 0x00f;
IIC_Send_Byte(cmd);
IIC_Wait_Ack();
IIC_Stop();
return 1;
}
上面为DAC7574的驱动代码
全文件在
驱动代码
芯片的通信协议如上图
硬件部分就比较简单
SCL,SDA为IIC通信的引脚,外接;
A0,A1决定芯片IIC地址,接地情况下为0x98;
Vdd,GND为电源