Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17255
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2017-04-06 21:50
文章分类
文章存档

2017年(11)

我的朋友
最近访客

分类: 嵌入式

2017-04-06 21:54:23

#include "config.h" //相关头文件



#define C02MasterAddr 0xa0//C02的器件地址

#define C02SlaveAddr 0 //读写数据的起始地址

#define LED (1 << 19)//LED IO口号

#define LED2 (1 << 20)

#define AA (1 << 2)//I2CONSET的各位定义

#define SI (1 << 3)
#define STO (1 << 4)
#define STA (1 << 5)
#define I2EN (1 << 6)

#define WRMode 0 //写模式

#define RDMode 1 //读模式


#define lastbyte 1 //是否为最后一个字节




uint8 I2C_buf1 = 5;
uint8 I2C_buf2 = 0;
//延时函数

void Delay(uint32 dly)
{
    uint32 i;
    
    for(; dly > 0; dly--)
    {
     for(i = 0; i < 50000; i++);
    }
}

//I2C初始化函数

void I2C_Init(uint32 Fi2c)
{
    if(Fi2c > 400000)
    {
     Fi2c = 400000;
    }
    
    PINSEL0 = (PINSEL0 & (~0xf0)) | 0x50;//这里是让P02,P03选第二功能,即I2C总线

    
    I2SCLH = (Fpclk/Fi2c + 1) / 2;//设置I2C速度为400K

    I2SCLL = (Fpclk/Fi2c) / 2;
    
    I2CONCLR = 0x2c; //主发送模式对STA,AA,SI清零

    I2CONSET = 0x40;//使用I2C总线

    
//    I2CONCLR = STA|AA|SI;

//    I2CONSET = I2EN;

    
}
//I2C bus start function

void I2C_Start(void)
{
    I2CONSET = STA;//set start condition

    do{}while(0x08 != I2STAT);//A start condition has been transmitted

    I2CONCLR = STA;//clear start condition

}

//I2C bus stop function

void I2C_Stop(void)
{
    I2CONSET = STO; //set STO condition

    I2CONCLR = SI; //clear SI condition

}
//write byte function

void WriteByte(uint8 data)
{
    I2DAT = data;// data transmitted

    I2CONCLR = SI;//clear SI condition

}
//wirte I2C bus address mode is RDMode or WRMode

void WriteAddr(uint8 Mode)
{
    WriteByte(C02MasterAddr + Mode);//transmite the I2C address and R/W mode

    
    if(Mode)
    {
        do{}while(0x40 != I2STAT);//SLA+R has been transmitted;ACK has been recevied

    }
    else
    {
        do{}while(0x18 != I2STAT);//SLA+W has been transmitted;ACK has been received

    }
}
//write data to I2C

void WriteData(uint8 data)
{
    WriteByte(data);// data is will transmited data

    do{}while(0x28 != I2STAT);//Data byte in I2DAT has been transmitted;ACK has been received

}
//read i2c byte

uint8 ReadByte(uint8 last)
{
    if(last)
    {
        I2CONCLR = AA;
        I2CONCLR = SI;
        
        do{} while(0x58 != I2STAT);//data byte has been received;NOT ACK has been returned

    }
    else
    {
        I2CONSET = AA;//transmited ACK

        I2CONCLR = SI;
        do{}while(0x50 != I2STAT);//data byte has been received;ACK has been returned.

    }    
    
    return I2DAT;//return read data

}
//write data into 24c02

void WriteC02(void)
{
//    uint8 i;//define unsigned char i

    I2C_Start();
    WriteAddr(WRMode);
    WriteData(C02SlaveAddr);//the data stor address

    
    WriteData(I2C_buf1);
    I2C_Stop();
}

//read I2C data

void ReadC02(void)
{
//    uint8 i;

    I2C_Start();
    WriteAddr(WRMode);
    WriteData(C02SlaveAddr);
    I2C_Stop();
    
    I2C_Start();
    WriteAddr(RDMode);
    
    I2C_buf2 = ReadByte(lastbyte);
    
    I2C_Stop();
}

int main(void)
{
    
    PINSEL0 = 0x00000000;
    PINSEL1 = 0x00000000;
    
    IODIR = LED|LED2;
    IOSET = LED|LED2;

    
    I2C_Init(400000);
    
    WriteC02();
    Delay(1000);
    ReadC02();
    
    if(I2C_buf2 == I2C_buf1)
    {
    while(1){
        IOCLR = LED;//correct LED1 light

        Delay(50);
        IOSET = LED;
        Delay(50);
        }
    }
    else
    {
        while(1)
        {
            IOCLR = LED2;//error LED2 light

            Delay(50);
         IOSET = LED2;
            Delay(50);
        }
    }
    
    


return 0;    
}


文件:2_I2C.zip
大小:78KB
下载:下载
阅读(618) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~