📄 iic.c
字号:
/************************************************************
Copyright (C), 2007 by SEED Electronic Technology LTD.
FileName: IIC.C
Author: Ya.X Version : V1.0 Date:2007-09-20
Description: IIC模块原文件,包括IIC读写操作
*************************************************************/
#include <csl.h>
#include <csl_i2c.h>
#include "IIC.h"
/*I2C发送寄存器配置结构体*/
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接收寄存器配置结构体*/
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 */
};
/*************************************************************
Function: I2C_write16
Description: 通过IIC写16bit的数据到AIC23寄存器,配置AIC23
Calls: No
Called By: main
Input: hI2C: I2C句柄
regnum:需要配置的寄存器
regval:待写入的寄存器值
Output: No
Return: True:成功 ; False:失败
Others: No
**************************************************************/
Uint16 I2C_write16(I2C_Handle hI2C, Uint8 regnum, Uint8 regval)
{
Uint8 tempdata = 0;
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
while(I2C_bb(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
while(I2C_bb(hI2C));
DEC643_waitusec(400);
return TRUE;
}
/*************************************************************
Function: I2C_read16
Description: 通过IIC读取寄存器的值
Calls: No
Called By: main
Input: hI2C: I2C句柄
regnum:需要配置的寄存器
regval:待写入的寄存器值
Output: No
Return: True:成功 ; False:失败
Others: No
**************************************************************/
Uint16 I2C_read16(I2C_Handle hI2C, Uint8 regnum, Uint8 regval)
{
Uint8 tempdata = 0;
//Waiting for Bit12 of ICSTR ie. BB (Bus Busy) to clear
while(I2C_bb(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
while(I2C_bb(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
while(I2C_bb(hI2C));
return TRUE;
}
/********************************************************************************/
/* End of IIC.C */
/********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -