/*
---------------------------------------------------------------------------------------------------------
*********************************************************************************************************
** 文件名称 : I2CINT.c
** 功能说明 : LPC2000硬件I2C中断方式软件包。
** 使用说明 : 主程序要配置好I2C总线接口(I2C引脚功能和I2C中断,并已使能I2C主模式)
*********************************************************************************************************
---------------------------------------------------------------------------------------------------------
*/
#include "config.h"
/* 定义用于和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--写操作
*/
/****************************************************************************
* 名称:Wait_I2c_End
* 功能:软件延时,提供给I2C总线等待超时使用
* 入口参数:dly 延时参数,值越大,延时越久
* 出口参数:无
****************************************************************************/
uint8 Wait_I2c_End(uint32 dly)
{ uint32 i;
if( I2C_end==1 ) return (1);
for(; dly>0; dly--)
for(i=0; i<5000; i++)
{
if( I2C_end==1 ) return (1);
}
return (0);
}
/*
**********************************************************************************************************
** 函数名称:ISendByte()
** 函数功能:向无子地址器件发送1字节数据。
** 入口参数:sla 器件地址
** dat 要发送的数据
** 出口参数:返回值为0时表示出错,为1时表示操作正确。
** 说明: 使用前要初始化好I2C引脚功能和I2C中断,并已使能I2C主模式
*********************************************************************************************************
*/
uint8 ISendByte(uint8 sla, uint8 dat)
{ /* 参数设置 */
I2C_sla = sla; // 写操作的器件地址
I2C_buf = &dat; // 待发送的数据
I2C_num = 1; // 发送1字节数据
I2C_suba_en = 0; // 无子地址
I2C_end = 0;
I2CONCLR = 0x2C;
I2CONSET = 0x60; // 设置为主机,并启动总线
return( Wait_I2c_End(20));
/*while(0==I2C_end);
if(1==I2C_end) return(1);
else return(0);*/
}
/*
*********************************************************************************************************
** 函数名称:IRcvByte()
** 函数功能:向无子地址器件读取1字节数据。
** 入口参数:sla 器件地址
** dat 接收数据的变量指针
** 出口参数:返回值为0时表示操作出错,为1时表示操作正确。
** 说明:使用前要初始化好I2C引脚功能和I2C中断,并已使能I2C主模式
*********************************************************************************************************
*/
uint8 IRcvByte(uint8 sla, uint8 *dat)
{ /* 参数设置 */
I2C_sla = sla+1; // 读操作的器件地址
I2C_buf = dat;
I2C_num = 1;
I2C_suba_en = 0; // 无子地址
I2C_end = 0;
I2CONCLR = 0x2C;
I2CONSET = 0x60; // 设置为主机,并启动总线
return( Wait_I2c_End(20));
/*while(0==I2C_end);
if(1==I2C_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)
{ /* 子地址为单字节 */
I2C_sla = sla + 1; /* 读器件的从地址,R=1 */
I2C_suba = suba; /* 器件子地址 */
I2C_suba_num = 1; /* 器件子地址为1字节 */
}
if (suba_type == 2)
{ /* 子地址为2字节 */
I2C_sla = sla + 1; /* 读器件的从地址,R=1 */
I2C_suba = suba; /* 器件子地址 */
I2C_suba_num = 2; /* 器件子地址为2字节 */
}
if (suba_type == 3)
{ /* 子地址结构为8+X*/
I2C_sla = sla + ((suba >> 7 )& 0x0e) + 1; /* 读器件的从地址,R=1 */
I2C_suba = suba & 0x0ff; /* 器件子地址 */
I2C_suba_num = 1; /* 器件子地址为8+x */
}
I2C_buf = s; /* 数据接收缓冲区指针 */
I2C_num = num; /* 要读取的个数 */
I2C_suba_en = 1; /* 有子地址读 */
I2C_end = 0;
/* 清除STA,SI,AA标志位 */
I2CONCLR = (1 << 2)| /* AA */
(1 << 3)| /* SI */
(1 << 5); /* STA */
/* 置位STA,启动I2C总线 */
I2CONSET = (1 << 5)| /* STA */
(1 << 6); /* I2CEN */
/* 等待I2C操作完成 */
return( Wait_I2c_End(20));
/*while (I2C_end == 0)
{
}
if (I2C_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)
{ /* 子地址为单字节 */
I2C_sla = sla; /* 读器件的从地址 */
I2C_suba = suba; /* 器件子地址 */
I2C_suba_num = 1; /* 器件子地址为1字节 */
}
if (suba_type == 2)
{ /* 子地址为2字节 */
I2C_sla = sla; /* 读器件的从地址 */
I2C_suba = suba; /* 器件子地址 */
I2C_suba_num = 2; /* 器件子地址为2字节 */
}
if (suba_type == 3)
{ /* 子地址结构为8+X */
I2C_sla = sla + ((suba >> 7 )& 0x0e); /* 读器件的从地址 */
I2C_suba = suba & 0x0ff; /* 器件子地址 */
I2C_suba_num = 1; /* 器件子地址为8+X */
}
I2C_buf = s; /* 数据 */
I2C_num = num; /* 数据个数 */
I2C_suba_en = 2; /* 有子地址,写操作 */
I2C_end = 0;
/* 清除STA,SI,AA标志位 */
I2CONCLR = (1 << 2)| /* AA */
(1 << 3)| /* SI */
(1 << 5); /* STA */
/* 置位STA,启动I2C总线 */
I2CONSET = (1 << 5)| /* STA */
(1 << 6); /* I2CEN*/
/* 等待I2C操作完成 */
return( Wait_I2c_End(20));
/*while (I2C_end == 0)
{
//
}
if (I2C_end == 1)
return (TRUE);
else
return (FALSE); */
}
return (FALSE);
}
/*
*********************************************************************************************************
** 函数名称 :__irq IRQ_I2C()
** 函数名次 :硬件I2C中断服务程序。
** 入口参数 :无
** 出口参数 :无
** 说明 :注意处理子地址为2字节的情况。
*********************************************************************************************************
*/
void __irq IRQ_I2C(void)
{ /* 读取I2C状态寄存器I2DAT */
/* 按照全局变量的设置进行操作及设置软件标志 */
/* 清除中断逻辑,中断返回 */
switch (I2STAT & 0xF8)
{ /* 根据状态码进行相应的处�
没有合适的资源?快使用搜索试试~ 我知道了~
LPC2131简单实验
共977个文件
o:198个
h:148个
tdt:147个
5星 · 超过95%的资源 需积分: 11 7 下载量 74 浏览量
2012-11-03
17:37:07
上传
评论 1
收藏 3.97MB ZIP 举报
温馨提示
周立功2131嵌入式开发板裸机实验代码,包括简单的gpio,中断,定时器,不带操作系统的实验代码
资源推荐
资源详情
资源评论
收起资源包目录
LPC2131简单实验 (977个子文件)
mat1.0chufaADzhuanhuan.axf 25KB
dantongdaoADzhuanhuan.axf 25KB
p0.16chufaADzhuanhuan.axf 25KB
IAPyanshishiyan.axf 20KB
wannianli.axf 19KB
uartzhongduan.axf 18KB
sspzuospicongji4-20.axf 18KB
uart0chaxun.axf 18KB
pwmyinyueshuchu.axf 18KB
zhongduanqiantao.axf 18KB
sspkongzhi74hc595.axf 17KB
zhongduanyouxianji.axf 17KB
kanmenDOGyichuzhongduan.axf 17KB
dingshiqi1puhuozhongduan26.axf 17KB
PLLchongxinshedingshiyan.axf 17KB
ruanzhongduan.axf 17KB
kanmenDOGyichu.axf 17KB
PLLzhongduanshiyan.axf 17KB
ruanzhongduan2.axf 17KB
kanmenDOGyichufuwei.axf 17KB
dingshiqi1shurupuhuo4.25.axf 17KB
cuowuweiDOGzhongduan.axf 17KB
anjianhefengmingqi.axf 17KB
feixiangliangzhongduan.axf 17KB
xiangliangzhongduan.axf 17KB
led4.6.axf 17KB
FIQzhongduanfuwu.axf 17KB
fuzhixiangliangbiaodaoRAM.axf 17KB
FIQzhongduan2.axf 17KB
waibuzhongduanhuanxingCPUchanshengzhongduan.axf 17KB
4.5danluLED.axf 17KB
chucunqiyingshe.axf 17KB
fengmingqi4.4.axf 16KB
fengmingqi4.4.axf 16KB
keycontrol.axf 16KB
pwmzuo32weidingshiqi.axf 16KB
RTCzuomiaozengliang.axf 16KB
rtczuomiaozenliang.axf 16KB
waizhongduanhuanxingdiaodianCPU.axf 16KB
dingshi0zhongduan.axf 16KB
rtczhongduanhuanxingcpu.axf 16KB
21dingshiqi0chaxun.axf 16KB
dingshiqi1jishu28.axf 16KB
I2CINT.c 12KB
I2CINT.c 12KB
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 8KB
main.c 7KB
main.c 4KB
main.c 4KB
main.c 4KB
main.c 3KB
main.c 3KB
共 977 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
- mi10712052422013-04-21有一定的指导作用
爬墙少年
- 粉丝: 9
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功