📄 i2c.c
字号:
/*****************************************************************************
* I2C.c: Fast I/O module file for NXP LPC24xx Family Microprocessors
*
* Copyright(C) 2006, NXP Semiconductor
* All rights reserved.
*
* History
* 2006.07.13 ver 1.00 Prelimnary version, first Release
*
******************************************************************************/
#include "LPC2468.h" /* LPC24xx definitions */
#include "type.h"
#include "irq.h"
#include "timer.h"
#include "I2C.h"
#include "target.h"
#define Fi2c 400000 // I2C总线速率
/*
I2C总线测试EEPROM器件函数:I2CInit,I2CWriteByte,I2CReadByte
*/
/*****************************************************************************
** Function name: I2CInit
**
** Descriptions: initialize I2C port
**
** parameters:
**
** Returned value: None
**
*****************************************************************************/
void I2CInit(void)
{
PINSEL1 = 0x1400000; // SDA0,SCL0 引脚配置
I20SCLH = (Fpclk/Fi2c + 1) / 2; // 速率设置
I20SCLL = (Fpclk/Fi2c) / 2;
I20CONCLR = (I2CONCLR_STAC|I2CONCLR_SIC|I2CONCLR_AAC);
I20CONSET = I2CONSET_I2EN; // enable I2C as master
//setup interrupt...
}
/*****************************************************************************
** Function name: I2CWriteByte
**
** Descriptions: 向I2C总线写1个字节到AT24C02
**
** parameters: data:数据
** address:写入地址
** Returned value: None
**
*****************************************************************************/
BYTE I2CWriteByte(BYTE data,BYTE address)
{
unsigned int i;
I20CONCLR = (I2CONCLR_STAC|I2CONCLR_SIC|I2CONCLR_AAC);
I20CONSET = I2CONSET_I2EN; //enable I2C as master
I20CONSET = I2CONSET_STA; //send start condition
while(I20STAT != 0x8);
I20DAT = 0xA0; //set SLA+W:1010000+0
I20CONCLR = (I2CONCLR_SIC|I2CONCLR_STAC); //clear SI bit to transmit SLA+W
while(I20STAT != 0x18);
I20DAT = address; //write address to eeprom
I20CONCLR = I2CONCLR_SIC; //clear SI bit to transmit address
while(I20STAT != 0x28); //stat must be 0x28
I20DAT = data; //write data to eeprom
I20CONCLR = I2CONCLR_SIC; //clear SI bit to transmit data
delayMs(0,1); //wait to send data to eeprom
I20CONCLR = I2CONCLR_SIC;
I20CONSET = I2CONSET_STO; //set STO bit to stop transmit
delayMs(0,2);
return 1;
}
/*****************************************************************************
** Function name: I2CReadByte
**
** Descriptions: 从AT24C02中读取一个字节数据
**
** parameters: address:读取地址
**
** Returned value: None
**
*****************************************************************************/
BYTE I2CReadByte(BYTE address)
{
char data;
I20CONCLR = (I2CONCLR_STAC|I2CONCLR_SIC|I2CONCLR_AAC);
I20CONSET = I2CONSET_I2EN; //enable I2C as master
I20CONSET = I2CONSET_STA; //send a start condition
I20CONSET = I2CONSET_STA; //test restart!!
while(I20STAT != 0x8);//state word must be 0x8
I20DAT = 0xA0;//set SLA+W
I20CONCLR = 0x8|0x20;//clear SI bit to transmit SLA+W
while(I20STAT != 0x18);//state word must be 0x18
I20DAT = address;//eeprom read address
I20CONCLR = I2CONCLR_SIC;//clear SI bit to transmit address
while(I20STAT != 0x28);//state word must be 0x28
I20CONSET = I2CONSET_STO;//stop dummy write
I20CONCLR = I2CONCLR_SIC;//clear SI bit
I20CONSET = I2CONSET_STA;//send another start condition
while(I20STAT != 0x8);
I20DAT = 0xA1;//send read order:1010000+1
I20CONCLR = (I2CONCLR_SIC|I2CONCLR_STAC);//clear SI bit to transmit read order
while(I20STAT != 0x40);//state word 0x40
I20CONCLR = I2CONCLR_SIC;//clear SI bit,read eeprom
while(I20STAT != 0x58);//received a data word,no ACK
data = I20DAT;//read data
I20CONCLR = (I2CONCLR_SIC|I2CONCLR_AAC);
I20CONSET = I2CONSET_STO;//set STO bit to stop transmit
return data;
}
/*********************************************************************************
** End Of File
*********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -