📄 iic.c
字号:
/********************************************************************************\
\*IIC.C V1.00
* Copyright 2003 by SEED Electronic Technology Ltd.
* All rights reserved. Property of SEED Electronic Technology Ltd.
* Designed by: Hongshuai.Li
*********************************************************************************/
#include <csl.h>
#include <csl_i2c.h>
#include <DEC643.h>
#include <IIC.h>
/********************************************************************************/
/* Set I2C registers. */
I2C_Config MyI2CCfgT = {
0, /* master mode, i2coar;采用主模式 */
0, /* no interrupt, i2cimr;只写,不读,采用无中断方式*/
(20-5), /* scl low time, i2cclkl; */
(20-5), /* scl high time,i2cclkh; */
2, /* configure later, i2ccnt;*/
0x1A, /* configure later, i2csar;*/
0x4620, /* master tx mode, */
/* i2c runs free, */
/* 8-bit data + NACK */
/* no repeat mode */
(75-1), /* 2MHz clock, i2cpsc */
};
I2C_Config MyI2CCfgR = {
0, /* master mode, i2coar;采用主模式 */
0, /* no interrupt, i2cimr;只写,不读,采用无中断方式*/
(20-5), /* scl low time, i2cclkl; */
(20-5), /* scl high time,i2cclkh; */
1, /* configure later, i2ccnt;*/
0x1A, /* configure later, i2csar;*/
0x4420, /* master tx mode, */
/* i2c runs free, */
/* 8-bit data + NACK */
/* no repeat mode */
(75-1), /* 2MHz clock, i2cpsc */
};
/********************************************************************************/
/********************************************************************************\
\*I2C_write16() -Write 16bits data to AIC23 registers. Configure AIC23 control
register.
\*Parameters: hI2C. I2C id.
regnum. The related register.
regval. Register value to be written.
\*Return: True or False.
\********************************************************************************/
Uint16 I2C_write16(I2C_Handle hI2C,Uint8 regnum,Uint8 regval)
{
Uint8 tempdata = 0;
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
/* Configure I2C. */
I2C_config(hI2C,&MyI2CCfgT);
//Write the low data into Data Transmit register
tempdata = regnum;
I2C_writeByte(hI2C, tempdata&0xFF);
//To invoke the start condition
I2C_start(hI2C);
/* Wait until MSB transmit is done */
while(!I2C_xrdy(hI2C));
//Write the high data into Data Transmit register.
tempdata = regval;
I2C_writeByte(hI2C, tempdata&0xFF);
//Generate Stop condition
I2C_sendStop(hI2C);
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
DEC643_waitusec(400);
return TRUE;
}
/********************************************************************************/
/********************************************************************************\
\*I2C_read16() -Write 16bits data to AIC23 registers. Configure AIC23 control
register.
\*Parameters: hI2C. I2C id.
regnum. The related register.
regval. Register value to be written.
\*Return: True or False.
\********************************************************************************/
Uint16 I2C_read16(I2C_Handle hI2C,Uint8 regnum,Uint8 regval)
{
Uint8 tempdata = 0;
/* Send address. */
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
I2C_config(hI2C,&MyI2CCfgT);
//Write the low data into Data Transmit register
tempdata = regnum;
I2C_writeByte(hI2C, tempdata&0xFF);
//To invoke the start condition
I2C_start(hI2C);
/* Wait until MSB transmit is done */
while(!I2C_xrdy(hI2C));
//Write the high data into Data Transmit register.
tempdata = regval;
I2C_writeByte(hI2C, tempdata&0xFF);
/* Generate Stop condition */
I2C_sendStop(hI2C);
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
/* Wait for the time from transmit to receive. */
DEC643_waitusec(10);
/* Receive data. */
I2C_config(hI2C,&MyI2CCfgR);
/* Generate start condition, starts transmission */
I2C_start(hI2C);
while(I2C_FGETH(hI2C,I2CSTR,ARDY));
/* Wait until MSB transmit is done */
while(!I2C_rrdy(hI2C));
/* Submit the MSB for transmit */
regval = I2C_RGETH(hI2C, I2CDRR);
/* Generate stop condition */
I2C_sendStop(hI2C);
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
waitForBusFree(hI2C);
return TRUE;
}
/********************************************************************************/
//This function waits until the I2C bus busy bit is reset
waitForBusFree(I2C_Handle hI2C)
{
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
while(I2C_bb(hI2C));
}
//This function waits till the I2C bus busy bit gets set
waitForBusBusy(I2C_Handle hI2C)
{
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to set
while(!I2C_bb(hI2C));
}
/********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -