/*
---------------------------------------------------------------------------------------------------------
*********************************************************************************************************
** 文件名称 : I2C1INT.c
** 功能说明 : LPC2000硬件I2C1中断方式软件包。
** 使用说明 : 主程序要配置好I2C1总线接口(I2C1引脚功能和I2C1中断,并已使能I2C1主模式)
*********************************************************************************************************
---------------------------------------------------------------------------------------------------------
*/
#include "config.h"
/* 定义用于和I2C中断传递信息的全局变量 */
volatile uint8 I2C1_sla; /* I2C1器件从地址 */
volatile uint32 I2C1_suba; /* I2C1器件内部子地址 */
volatile uint8 I2C1_suba_num; /* I2C1子地址字节数 */
volatile uint8 *I2C1_buf; /* 数据缓冲区指针 */
volatile uint32 I2C1_num; /* 要读取/写入的数据个数 */
volatile uint8 I2C1_end; /* I2C1总线结束标志:结束总线是置1 */
volatile uint8 I2C1_suba_en; /* 子地址控制。
0--子地址已经处理或者不需要子地址
1--读取操作
2--写操作
*/
/*
**********************************************************************************************************
** 函数名称:ISendByte()
** 函数功能:向无子地址器件发送1字节数据。
** 入口参数:sla 器件地址
** dat 要发送的数据
** 出口参数:返回值为0时表示出错,为1时表示操作正确。
** 说明: 使用前要初始化好I2C1引脚功能和I2C1中断,并已使能I2C1主模式
*********************************************************************************************************
*/
uint8 ISendByte(uint8 sla, uint8 dat)
{ /* 参数设置 */
I2C1_sla = sla; // 写操作的器件地址
I2C1_buf = &dat; // 待发送的数据
I2C1_num = 1; // 发送1字节数据
I2C1_suba_en = 0; // 无子地址
I2C1_end = 0;
I2C1CONCLR = 0x2C;
I2C1CONSET = 0x60; // 设置为主机,并启动总线
while(0==I2C1_end);
if(1==I2C1_end) return(1);
else return(0);
}
/*
*********************************************************************************************************
** 函数名称:IRcvByte()
** 函数功能:向无子地址器件读取1字节数据。
** 入口参数:sla 器件地址
** dat 接收数据的变量指针
** 出口参数:返回值为0时表示操作出错,为1时表示操作正确。
** 说明:使用前要初始化好I2C1引脚功能和I2C1中断,并已使能I2C1主模式
*********************************************************************************************************
*/
uint8 IRcvByte(uint8 sla, uint8 *dat)
{ /* 参数设置 */
I2C1_sla = sla+1; // 读操作的器件地址
I2C1_buf = dat;
I2C1_num = 1;
I2C1_suba_en = 0; // 无子地址
I2C1_end = 0;
I2C1CONCLR = 0x2C;
I2C1CONSET = 0x60; // 设置为主机,并启动总线
while(0==I2C1_end);
if(1==I2C1_end) return(1);
else return(0);
}
/*
*********************************************************************************************************
** 函数名称 :I2C_ReadNByte()
** 函数功能 :从有子地址器件任意地址开始读取N字节数据
** 入口参数 : sla 器件从地址
** suba_type 子地址结构 1-单字节地址 2-8+X结构 2-双字节地址
** suba 器件子地址
** s 数据接收缓冲区指针
** num 读取的个数
** 出口参数 : TRUE 操作成功
** FALSE 操作失败
*********************************************************************************************************
*/
uint8 I2C_ReadNByte (uint8 sla, uint32 suba_type, uint32 suba, uint8 *s, uint32 num)
{
if (num > 0) /* 判断num个数的合法性 */
{ /* 参数设置 */
if (suba_type == 1)
{ /* 子地址为单字节 */
I2C1_sla = sla + 1; /* 读器件的从地址,R=1 */
I2C1_suba = suba; /* 器件子地址 */
I2C1_suba_num = 1; /* 器件子地址为1字节 */
}
if (suba_type == 2)
{ /* 子地址为2字节 */
I2C1_sla = sla + 1; /* 读器件的从地址,R=1 */
I2C1_suba = suba; /* 器件子地址 */
I2C1_suba_num = 2; /* 器件子地址为2字节 */
}
if (suba_type == 3)
{ /* 子地址结构为8+X*/
I2C1_sla = sla + ((suba >> 7 )& 0x0e) + 1;/* 读器件的从地址,R=1 */
I2C1_suba = suba & 0x0ff; /* 器件子地址 */
I2C1_suba_num = 1; /* 器件子地址为8+x */
}
I2C1_buf = s; /* 数据接收缓冲区指针 */
I2C1_num = num; /* 要读取的个数 */
I2C1_suba_en = 1; /* 有子地址读 */
I2C1_end = 0;
/* 清除STA,SI,AA标志位 */
I2C1CONCLR = (1 << 2)| /* AA */
(1 << 3)| /* SI */
(1 << 5); /* STA */
/* 置位STA,启动I2C1总线 */
I2C1CONSET = (1 << 5)| /* STA */
(1 << 6); /* I2CEN */
/* 等待I2C操作完成 */
while (I2C1_end == 0)
{ }
if (I2C1_end == 1)
return (TRUE);
else
return (FALSE);
}
return (FALSE);
}
/*
*********************************************************************************************************
** 函数名称 :I2C_WriteNByte()
** 函数功能 :向有子地址器件写入N字节数据
** 入口参数 : sla 器件从地址
** suba_type 子地址结构 1-单字节地址 3-8+X结构 2-双字节地址
** suba 器件内部物理地址
** *s 将要写入的数据的指针
** num 将要写入的数据的个数
** 出口参数 : TRUE 操作成功
** FALSE 操作失败
*********************************************************************************************************
*/
uint8 I2C_WriteNByte(uint8 sla, uint8 suba_type, uint32 suba, uint8 *s, uint32 num)
{
if (num > 0)/* 如果读取的个数为0,则返回错误 */
{ /* 设置参数 */
if (suba_type == 1)
{ /* 子地址为单字节 */
I2C1_sla = sla; /* 读器件的从地址 */
I2C1_suba = suba; /* 器件子地址 */
I2C1_suba_num = 1; /* 器件子地址为1字节 */
}
if (suba_type == 2)
{ /* 子地址为2字节 */
I2C1_sla = sla; /* 读器件的从地址 */
I2C1_suba = suba; /* 器件子地址 */
I2C1_suba_num = 2; /* 器件子地址为2字节 */
}
if (suba_type == 3)
{ /* 子地址结构为8+X */
I2C1_sla = sla + ((suba >> 7 )& 0x0e); /* 读器件的从地址 */
I2C1_suba = suba & 0x0ff; /* 器件子地址 */
I2C1_suba_num = 1; /* 器件子地址为8+X */
}
I2C1_buf = s; /* 数据 */
I2C1_num = num; /* 数据个数 */
I2C1_suba_en = 2; /* 有子地址,写操作 */
I2C1_end = 0;
/* 清除STA,SI,AA标志位 */
I2C1CONCLR = (1 << 2)| /* AA */
(1 << 3)| /* SI */
(1 << 5); /* STA */
/* 置位STA,启动I2C总线 */
I2C1CONSET = (1 << 5)| /* STA */
(1 << 6); /* I2CEN*/
/* 等待I2C操作完成 */
while (I2C1_end == 0)
{ }
if (I2C1_end == 1)
return (TRUE);
else
return (FALSE);
}
return (FALSE);
}
/*
*********************************************************************************************************
** 函数名称 :__irq IRQ_I2C1()
** 函数名次 :硬件I2C1中断服务程序。
** 入口参数 :无
** 出口参数 :无
** 说明 :注意处理子地址为2字节的情况。
*********************************************************************************************************
*/
void __irq IRQ_I2C1(void)
{ /* 读取I2C状态寄存器I2C1DAT */
/* 按照全局变量的设置进行操作及设置软件标志 */
/* 清除中断逻辑,中断返回 */
switch (I2C1STAT & 0xF8)
{ /* 根据状态码进行相应的处理 */
case 0x08: /* 已发送起始条件 */ /* 主发送和主接收都有 */
/* 装入SLA+W或者SLA+R */
if(I2C1_suba_en == 1)/* SLA+R */ /* 指定子地址读 */
{ I2C1DAT = I2C1_sla & 0xFE; /* 先写入地址 */
}
else /* SLA+W */
{ I2C1DAT = I2C1_sla; /* 否则直接发送从机地址 */
}
/* 清零SI位 */
I2C1CONCLR = (1 << 3)| /* SI */
(1 << 5); /* STA */
break;
case 0x10: /*已发送重复起始条件 */ /* 主发送和主接收都有 */
/*
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
LPC2131功能部件例程 (639个子文件)
UART0_int.axf 20KB
SSP_SPI_Master.axf 19KB
SSP_SPI_Master.axf 19KB
SSP_SPI_Master.axf 18KB
I2C1INT.c 11KB
I2C1INT.c 11KB
I2CINT.c 11KB
I2CINT.c 11KB
I2CINT.c 11KB
target.c 9KB
target.c 9KB
main.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
target.c 9KB
main.c 7KB
main.c 7KB
main.c 7KB
main.c 6KB
main.c 6KB
main.c 6KB
main.c 6KB
main.c 5KB
main.c 5KB
main.c 5KB
main.c 5KB
main.c 5KB
main.c 5KB
main.c 5KB
main.c 5KB
main.c 4KB
main.c 4KB
main.c 4KB
main.c 4KB
main.c 4KB
main.c 4KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 3KB
main.c 2KB
共 639 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
wangzi8732415
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功