📄 i2cint.c
字号:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: i2cint.c
** Last modified Date: 2005-10-28
** Last Version: 1.0
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Created by: ChenXiBing
** Created date: 2005-10-20
** Version: 1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by: Ganda
** Modified date: 2005-10-28
** Version: 1.1
** Descriptions: Add error handle
**
********************************************************************************************************/
#include "config.h"
uint8 I2C_Buf[256]; // I2C Data Buffer
/* 定义用于和I2C中断传递信息的全局变量 */
volatile uint8 I2C_err; // I2C出错状态代码
volatile uint8 I2C_sla; // I2C器件从地址
volatile uint32 I2C_suba; // I2C器件内部子地址
volatile uint8 I2C_suba_num; // I2C子地址字节数
volatile uint8 *I2C_buf; // 数据缓冲区指针
volatile uint32 I2C_num; // 要读取/写入的数据个数
volatile uint8 I2C_end; // I2C总线结束标志:结束总线是置1
volatile uint8 I2C_suba_en; // 子地址控制
// 0--子地址已经处理或者不需要子地址
// 1--读取操作
// 2--写操作
/*********************************************************************************************************
** Function name: I2C_Init
**
** Descriptions: I2C Bus Initail routine
**
** input parameters: uint32 Fi2c I2C Bus working frequency (<400K)
** Returned value: None
**
** Used global variables: None
**
** Calling modules: None
**
** Created by: Ganda
** Created Date: 2005-10-28
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**
*********************************************************************************************************/
void I2C_Init(uint32 Fi2c)
{
if (Fi2c > 400000)
Fi2c = 400000;
PINSEL0 = (PINSEL0 & (~0xF0)) | 0x50; // 不影响其它管脚连接
I2SCLH = (Fpclk/Fi2c + 1) / 2; // 设定I2C时钟
I2SCLL = (Fpclk/Fi2c)/2;
I2CONCLR = 0x2C;
I2CONSET = 0x40; // 使能主I2C
I2C_Recover();
}
/*********************************************************************************************************
** Function name: I2C_IntInit
**
** Descriptions: I2C Interrupt Initail routine
**
** input parameters: None
** Returned value: None
**
** Used global variables: None
**
** Calling modules: None
**
** Created by: Ganda
** Created Date: 2005-10-28
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**
*********************************************************************************************************/
void I2C_IntInit(void)
{
VICIntSelect = 0x00000000; // 设置所有通道为IRQ中断
VICVectCntl0 = (0x20 | 0x09); // I2C通道分配到IRQ slot0,最高优先级
VICVectAddr0 = (int32)IRQ_I2C; // 设置I2C中断向量
VICIntEnable = (1 << 9); // 使能I2C中断
}
/*********************************************************************************************************
** Function name: I2C_Recover
**
** Descriptions: When I2C Bus had any errors(blocked), you can use it to recover.
** Change with I2C_IntInit() !!!
**
** input parameters: None
** Returned value: None
**
** Used global variables: None
**
** Calling modules: None
**
** Created by: Ganda
** Created Date: 2005-10-28
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**
*********************************************************************************************************/
void I2C_Recover(void)
{ uint32 i;
I2C_err = 0x00;
VICIntEnClr = (1 << 9); // Enable I2C Interrupt
I2CONCLR = 0x2C;
I2CONSET = 0x40;
I2CONCLR = 1 << STO;
for(i=0; i<0xfffff; i++);
I2CONCLR = 1 << I2EN // Clear some I2C flag, and enter master mode
| 1 << STA
| 1 << SI
| 1 << AA;
VICIntEnable = (1 << 9); // Disable I2C Interrupt
}
/*********************************************************************************************************
** Function name: I2C_WriteByte
**
** Descriptions: Write a byte to Chip dirtectly without suba
**
** input parameters: uint8 sla slave address
** uint8 dat data
** Returned value: TRUE / FALSE
** If FALSE, check error code in I2C_err
**
** Used global variables: I2C_sla, I2C_sla, I2C_num, I2C_suba_en, I2C_end, I2C_err
**
** Calling modules: None
**
** Created by: Ganda
** Created Date: 2005-10-28
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**
*********************************************************************************************************/
uint8 I2C_WriteByte(uint8 sla, uint8 dat)
{ uint32 i;
I2C_err = 0x00;
I2C_sla = sla; // Slave address of writing
I2C_buf = &dat; // Data
I2C_num = 1; // 1 byte
I2C_suba_en = 0; // No suba
I2C_end = 0; // Clear I2C operating flag
I2CONCLR = 1 << I2EN // Clear some I2C flag, and enter master mode
| 1 << STA
| 1 << SI
| 1 << AA;
I2CONSET = 1 << I2EN // Enable I2C and start I2C Bus
| 1 << STA;
for(i=0; i<0xfffffff; )
{
if(0 == I2C_end) // I2C Bus is busy now
{
i++;
}
else if(1 == I2C_end) // I2C Bus stop normally
{
i = 0x10000000;
return TRUE;
}
else if(0xff == I2C_end) // I2C Bus stop
{
i = 0x20000000;
return FALSE;
}
else // I2C return value is error
{
return FALSE;
}
}
return FALSE;
}
/*********************************************************************************************************
** Function name: I2C_ReadByte
**
** Descriptions: Read a byte from Chip dirtectly without suba
**
** input parameters: uint8 sla slave address
** uint8 *dat store point of data
** Returned value: TRUE / FALSE
** If FALSE, check error code in I2C_err
**
** Used global variables: I2C_sla, I2C_sla, I2C_num, I2C_suba_en, I2C_end, I2C_err
**
** Calling modules: None
**
** Created by: Ganda
** Created Date: 2005-10-28
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**
*********************************************************************************************************/
uint8 I2C_ReadByte(uint8 sla, uint8 *dat)
{ uint32 i;
I2C_err = 0x00;
I2C_sla = sla+1; // Slave address of writing
I2C_buf = dat; // Store point of data
I2C_num = 1; // 1 byte
I2C_suba_en = 0; // No suba
I2C_end = 0; // Clear I2C operating flag
I2CONCLR = 1 << I2EN // Clear some I2C flag, and enter master mode
| 1 << STA
| 1 << SI
| 1 << AA;
I2CONSET = 1 << I2EN // Enable I2C and start I2C Bus
| 1 << STA;
for(i=0; i<0xfffffff; )
{
if(0 == I2C_end) // I2C Bus is busy now
{
i++;
}
else if(1 == I2C_end) // I2C Bus stop normally
{
i = 0x10000000;
return TRUE;
}
else if(0xff == I2C_end) // I2C Bus stop
{
i = 0x20000000;
return FALSE;
}
else // I2C return value is error
{
return FALSE;
}
}
return FALSE;
}
/*********************************************************************************************************
** Function name: I2C_ReadNByte
**
** Descriptions: Read some bytes form Chip
**
** input parameters: uint8 sla Slave address
** uint32 suba_type Type of sub address
** uint32 suba Sub address
** uint8 *s Point of data buffer
** uint32 num Number of data
**
** Returned value: TRUE / FALSE
** If FALSE, check error code in I2C_err
**
** Used global variables: I2C_sla, I2C_sla, I2C_num, I2C_suba_en, I2C_end, I2C_err
**
** Calling modules: None
**
** Created by: ChenXiBing
** Created Date: 2005-10-20
**-------------------------------------------------------------------------------------------------------
** Modified by: Ganda
** Modified date: 2005-10-28
**
*********************************************************************************************************/
uint8 I2C_ReadNByte (uint8 sla, uint32 suba_type, uint32 suba, uint8 *s, uint32 num)
{ uint32 i;
I2C_err = 0x00;
if (num > 0)
{
if (suba_type == 1) // ONE_BYTE_SUBA
{
I2C_sla = sla + 1; // Slave address of reading
I2C_suba = suba; // Sub address
I2C_suba_num = 1; // Sub address length
}
if (suba_type == 2) // TWO_BYTE_SUBA
{
I2C_sla = sla + 1; // Slave address of reading
I2C_suba = suba; // Sub address
I2C_suba_num = 2; // Sub address length
}
if (suba_type == 3) // X_ADD_8_SUBA
{
I2C_sla = sla + ((suba >> 7 )& 0x0e) + 1; // Slave address of reading
I2C_suba = suba & 0x0ff; // Sub address
I2C_suba_num = 1; // Sub address length
}
I2C_buf = s; // Point of data buffer
I2C_num = num; // Number of data
I2C_suba_en = 1; // Enable sub address and Read mode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -