#include <string.h>
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\iic.h"
volatile int f_nGetACK;//获取中断响应标志
//初始化IIC接口
void Iic_init(void)
{
rIICCON=(1<<7)|(1<<6)|(1<<5)|(0x0A);// Enable ACK, interrupt, IICCLK=MCLK/512, Enable ACK//64Mhz/512/(15+1) = 8Khz
rIICADD=0x10; // S3C44B0X slave address
rIICSTAT=0x10;
}
/**************************************************************************
功能:IIC总线写函数
参数: unSlaveAddr-IIC设备地址
SubAddr-IIC设备子地址(0,8或16位)
SubMod-IIC设备子地址模式(0-无,1-8位,2-16位)
dat-待发送数据
Size-待发送数据字节个数
作者:王剑平(2008-5-23)
**************************************************************************/
void IIC_Write(unsigned char ucSlaveAddr,unsigned int SubAddr,
unsigned char SubMod, char *dat,unsigned int Size)
{
U8 address[2];
U32 ucCur;
f_nGetACK = 0;
// Send SlaveAddr byte
rIICDS = ucSlaveAddr; //Slave Address
rIICSTAT = 0xf0; // Master Tx,Start
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
// Send address
switch ( SubMod )//计算子地址
{
case 0://无子地址
break;
case 1://1位子地址
address[0] = (U8)(SubAddr);
break;
case 2://2位子地址
address[0] = (U8)(SubAddr >> 8);
address[1] = (U8)(SubAddr);
break;
default:
break;
}
for(ucCur=0;ucCur<SubMod;ucCur++)
{
rIICDS = address[ucCur];
rIICCON = 0xe5; // Resumes IIC operation.
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
}
// Send data
for(ucCur=0;ucCur<Size;ucCur++)
{
rIICDS = dat[ucCur];
rIICCON = 0xe5; // Resumes IIC operation.
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
}
// End send
rIICSTAT = 0xd0; // Stop Master Tx condition
rIICCON = 0xe5; // Resumes IIC operation.
while(rIICSTAT & 0x20 == 1); // Wait until stop condtion is in effect.
Delay(20);
}
/**************************************************************************
功能:IIC总线读函数
参数: unSlaveAddr-IIC设备地址
SubAddr-IIC设备子地址(0,8或16位)
SubMod-IIC设备子地址模式(0-无,1-8位,2-16位)
dat-读回数据指针
Size-待读数据字节个数
作者:王剑平(2008-5-23)
**************************************************************************/
void IIC_Read(unsigned char ucSlaveAddr,unsigned int SubAddr,
unsigned char SubMod,char *pData,unsigned int Size)
{
U8 address[2];
U32 ucCur;
f_nGetACK = 0;
// Send SlaveAddr byte 设备地址
rIICDS = ucSlaveAddr; //Slave Address
rIICSTAT = 0xf0; // Master Tx,Start
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
// Send address
switch ( SubMod )//计算子地址
{
case 0://无子地址
break;
case 1://1位子地址
address[0] = (U8)(SubAddr);
break;
case 2://2位子地址
address[0] = (U8)(SubAddr >> 8);
address[1] = (U8)(SubAddr);
break;
default:
break;
}
for(ucCur=0;ucCur<SubMod;ucCur++)
{
rIICDS = address[ucCur];
rIICCON = 0xe5; // Resumes IIC operation.
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
}
// Send SlaveAddr byte 设备地址
rIICDS = ucSlaveAddr; // 设备地址
rIICSTAT = 0xb0; // Master Rx,Start
rIICCON = 0xe5; // Resumes IIC operation.
while(f_nGetACK == 0); // Wait ACK
f_nGetACK = 0;
// Get data 数据
for(ucCur=0;ucCur<Size;ucCur++)
{
rIICCON = 0xe5; //zlb change
if(ucCur == Size-1) //zlb change
rIICCON = 0x65;//在这里为什么是6不是e?zlg change
while(f_nGetACK == 0); // Wait ACK but no ack just wait intrupt
f_nGetACK = 0;
pData[ucCur] = rIICDS;
}
/* End receive */
rIICSTAT = 0x90; // Stop Master Rx condition
rIICCON = 0xe5; // Resumes IIC operation.
while(rIICSTAT & 0x20 == 1); // Wait until stop condtion is in effect.
Delay(20);
}
/**************************************************************************
功能:IIC总线中断响应函数
参数: 无
作者:王剑平(2008-5-23)
**************************************************************************/
void __irq IicInt(void)
{
rI_ISPC=BIT_IIC;
f_nGetACK = 1;
return;
}