老师的IIC 程序 拿来看看

longbiao831   2011-11-13 16:02 楼主
先把老师的程序写到这里,慢慢研读,
#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;
}

回复评论 (1)

你不但把老师程序看了,连他老人家的名字也公布了

http://shop34182318.taobao.com/ https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
点赞  2011-11-14 08:26
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复