NXP LPC11xx I2C Slave 从机程序
- /****************************************************************************
- * $Id:: i2cslave.c 3635 2010-06-02 00:31:46Z usb00423 $
- * Project: NXP LPC11xx I2C Slave example
- *
- * Description:
- * This file contains I2C slave code example which include I2C slave
- * initialization, I2C slave interrupt handler, and APIs for I2C slave
- * access.
- *
- ****************************************************************************
- * Software that is described herein is for illustrative purposes only
- * which provides customers with programming information regarding the
- * products. This software is supplied "AS IS" without any warranties.
- * NXP Semiconductors assumes no responsibility or liability for the
- * use of the software, conveys no license or title under any patent,
- * copyright, or mask work right to the product. NXP Semiconductors
- * reserves the right to make changes in the software without
- * notification. NXP Semiconductors also make no representation or
- * warranty that such application will be suitable for the specified
- * use without further testing or modification.
- ****************************************************************************/
- #include "LPC11xx.h"/* LPC11xx Peripheral Registers */
- #include "type.h"
- #include "i2cslave.h"
- volatile uint32_t I2CMasterState = I2C_IDLE;
- volatile uint32_t I2CSlaveState = I2C_IDLE;
- volatile uint32_t I2CMode;
- volatile uint8_t I2CWrBuffer[BUFSIZE];
- volatile uint8_t I2CRdBuffer[BUFSIZE];
- volatile uint32_t I2CReadLength;
- volatile uint32_t I2CWriteLength;
- ;
- ;
- /*
- From device to device, the I2C communication protocol may vary,
- in the example below, the protocol uses repeated start to read data from or
- write to the device:
- For master read: the sequence is: STA,Addr(W),offset,RE-STA,Addr(r),data...STO
- for master write: the sequence is: STA,Addr(W),offset,RE-STA,Addr(w),data...STO
- Thus, in state 8, the address is always WRITE. in state 10, the address could
- be READ or WRITE depending on the I2C command.
- */
- /*****************************************************************************
- ** Function name:I2C_IRQHandler
- **
- ** Descriptions:I2C interrupt handler, deal with master mode only.
- **
- ** parameters:None
- ** Returned value:None
- **
- *****************************************************************************/
- void I2C_IRQHandler(void)
- {
- uint8_t StatValue;
- /* this handler deals with master read and master write only */
- StatValue = LPC_I2C->STAT;
- switch ( StatValue )
- {
- case 0x60:/* An own SLA_W has been received. */
- case 0x68:
- RdIndex = ;
- LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after SLV_W is received */
- LPC_I2C->CONCLR = I2CONCLR_SIC;
- I2CSlaveState = I2C_WR_STARTED;
- break;
- case 0x80:/* data receive */
- case 0x90:
- if ( I2CSlaveState == I2C_WR_STARTED )
- {
- I2CRdBuffer[RdIndex++] = LPC_I2C->DAT;
- LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after data is received */
- }
- else
- {
- LPC_I2C->CONCLR = I2CONCLR_AAC;/* assert NACK */
- }
- LPC_I2C->CONCLR = I2CONCLR_SIC;
- break;
- case 0xA8:/* An own SLA_R has been received. */
- case 0xB0:
- RdIndex = ;
- LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after SLV_R is received */
- LPC_I2C->CONCLR = I2CONCLR_SIC;
- I2CSlaveState = I2C_RD_STARTED;
- WrIndex = I2CRdBuffer[];/* The 1st byte is the index. */
- break;
- case 0xB8:/* Data byte has been transmitted */
- case 0xC8:
- if ( I2CSlaveState == I2C_RD_STARTED )
- {
- LPC_I2C->DAT = I2CRdBuffer[WrIndex+];/* write the same data back to master */
- WrIndex++;/* Need to skip the index byte in RdBuffer */
- LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK */
- }
- else
- {
- LPC_I2C->CONCLR = I2CONCLR_AAC;/* assert NACK */
- }
- LPC_I2C->CONCLR = I2CONCLR_SIC;
- break;
- case 0xC0:/* Data byte has been transmitted, NACK */
- LPC_I2C->CONCLR = I2CONCLR_AAC;/* assert NACK */
- LPC_I2C->CONCLR = I2CONCLR_SIC;
- I2CSlaveState = DATA_NACK;
- break;
- case 0xA0:/* Stop condition or repeated start has */
- LPC_I2C->CONSET = I2CONSET_AA;/* been received, assert ACK. */
- LPC_I2C->CONCLR = I2CONCLR_SIC;
- I2CSlaveState = I2C_IDLE;
- break;
- default:
- LPC_I2C->CONCLR = I2CONCLR_SIC;
- LPC_I2C->CONSET = I2CONSET_I2EN | I2CONSET_SI;
- break;
- }
- return;
- }
- /*****************************************************************************
- ** Function name:I2CSlaveInit
- **
- ** Descriptions:Initialize I2C controller
- **
- ** parameters:I2c mode is either MASTER or SLAVE
- ** Returned value:true or false, return false if the I2C
- **interrupt handler was not installed correctly
- **
- *****************************************************************************/
- void I2CSlaveInit( void )
- {
- /* SSP and I2C reset are overlapped, a known bug,
- for now, both SSP and I2C use bit 0 for reset enable.
- Once the problem is fixed, change to "#if 1". */
- #if 1
- LPC_SYSCON->PRESETCTRL |= ();
- #else
- LPC_SYSCON->PRESETCTRL |= ();
- #endif
- LPC_SYSCON->SYSAHBCLKCTRL |= (<<);
- LPC_IOCON->PIO0_4 &= ~0x3F;/* I2C I/O config */
- LPC_IOCON->PIO0_4 |= 0x01;/* I2C SCL */
- LPC_IOCON->PIO0_5 &= ~0x3F;
- LPC_IOCON->PIO0_5 |= 0x01;/* I2C SDA */
- /*--- Clear flags ---*/
- LPC_I2C->CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;
- /*--- Reset registers ---*/
- #if FAST_MODE_PLUS
- LPC_IOCON->PIO0_4 |= ();
- LPC_IOCON->PIO0_5 |= ();
- LPC_I2C->SCLL = I2SCLL_HS_SCLL;
- LPC_I2C->SCLH = I2SCLH_HS_SCLH;
- #else
- LPC_I2C->SCLL = I2SCLL_SCLL;
- LPC_I2C->SCLH = I2SCLH_SCLH;
- #endif
- LPC_I2C->ADR0 = PCF8594_ADDR;
- I2CSlaveState = I2C_IDLE;
- /* Enable the I2C Interrupt */
- NVIC_EnableIRQ(I2C_IRQn);
- LPC_I2C->CONSET = I2CONSET_I2EN | I2CONSET_SI;
- return;
- }
- /******************************************************************************
- ** End Of File
- ******************************************************************************/
- /****************************************************************************
- * $Id:: i2cslave.h 3635 2010-06-02 00:31:46Z usb00423 $
- * Project: NXP LPC11xx I2C Slave example
- *
- * Description:
- * This file contains I2C slave code header definition.
- *
- ****************************************************************************
- * Software that is described herein is for illustrative purposes only
- * which provides customers with programming information regarding the
- * products. This software is supplied "AS IS" without any warranties.
- * NXP Semiconductors assumes no responsibility or liability for the
- * use of the software, conveys no license or title under any patent,
- * copyright, or mask work right to the product. NXP Semiconductors
- * reserves the right to make changes in the software without
- * notification. NXP Semiconductors also make no representation or
- * warranty that such application will be suitable for the specified
- * use without further testing or modification.
- ****************************************************************************/
- #ifndef __I2CSLAVE_H
- #define __I2CSLAVE_H
- #define FAST_MODE_PLUS 1
- #define BUFSIZE 6
- #define MAX_TIMEOUT 0x00FFFFFF
- #define PCF8594_ADDR 0xA0
- #define READ_WRITE 0x01
- #define RD_BIT 0x01
- #define I2C_IDLE 0
- #define I2C_STARTED 1
- #define I2C_RESTARTED 2
- #define I2C_REPEATED_START 3
- #define DATA_ACK 4
- #define DATA_NACK 5
- #define I2C_WR_STARTED 6
- #define I2C_RD_STARTED 7
- #define I2CONSET_I2EN (0x1<<6) /* I2C Control Set Register */
- #define I2CONSET_AA (0x1<<2)
- #define I2CONSET_SI (0x1<<3)
- #define I2CONSET_STO (0x1<<4)
- #define I2CONSET_STA (0x1<<5)
- #define I2CONCLR_AAC (0x1<<2) /* I2C Control clear Register */
- #define I2CONCLR_SIC (0x1<<3)
- #define I2CONCLR_STAC (0x1<<5)
- #define I2CONCLR_I2ENC (0x1<<6)
- #define I2DAT_I2C 0x00000000 /* I2C Data Reg */
- #define I2ADR_I2C 0x00000000 /* I2C Slave Address Reg */
- #define I2SCLH_SCLH 0x00000180 /* I2C SCL Duty Cycle High Reg */
- #define I2SCLL_SCLL 0x00000180 /* I2C SCL Duty Cycle Low Reg */
- #define I2SCLH_HS_SCLH 0x00000020 /* Fast Plus I2C SCL Duty Cycle High Reg */
- #define I2SCLL_HS_SCLL 0x00000020 /* Fast Plus I2C SCL Duty Cycle Low Reg */
- extern void I2C_IRQHandler( void );
- extern void I2CSlaveInit( void );
- #endif /* end __I2CSLAVE_H */
- /****************************************************************************
- ** End Of File
- *****************************************************************************/
- /****************************************************************************
- * $Id:: i2cslvtst.c 3635 2010-06-02 00:31:46Z usb00423 $
- * Project: NXP LPC11xx I2C example
- *
- * Description:
- * This file contains I2C slave test modules, main entry, to test I2C
- * slave APIs.
- *
- ****************************************************************************
- * Software that is described herein is for illustrative purposes only
- * which provides customers with programming information regarding the
- * products. This software is supplied "AS IS" without any warranties.
- * NXP Semiconductors assumes no responsibility or liability for the
- * use of the software, conveys no license or title under any patent,
- * copyright, or mask work right to the product. NXP Semiconductors
- * reserves the right to make changes in the software without
- * notification. NXP Semiconductors also make no representation or
- * warranty that such application will be suitable for the specified
- * use without further testing or modification.
- ****************************************************************************/
- #include "LPC11xx.h"/* LPC11xx Peripheral Registers */
- #include "type.h"
- #include "i2cslave.h"
- extern volatile uint8_t I2CWrBuffer[BUFSIZE];
- extern volatile uint8_t I2CRdBuffer[BUFSIZE];
- extern volatile uint32_t I2CSlaveState;
- extern volatile uint32_t I2CReadLength, I2CWriteLength;
- /*******************************************************************************
- ** Main Function main()
- *******************************************************************************/
- int main (void)
- {
- uint32_t i;
- SystemInit();
- ; i < BUFSIZE; i++ )
- {
- I2CRdBuffer[i] = 0x00;
- }
- I2CSlaveInit();/* initialize I2c */
- /* When the NACK occurs, the master has stopped the
- communication. Just check the content of I2CRd/WrBuffer. */
- while ( I2CSlaveState != DATA_NACK );
- ;
- }
- /******************************************************************************
- ** End Of File
- ******************************************************************************/
NXP LPC11xx I2C Slave 从机程序的更多相关文章
- java: Runtime和Process调用本机程序
java: Runtime和Process调用本机程序 调用纸牌程序,Process用来销毁程序 import java.io.IOException; public class RunTimeDem ...
- JAVA - ATM机程序
ATM机程序 UnionPayTest.java package oo.day06.work; public class UnionPayTest { } interface UnionPay{ // ...
- vc++MFC开发上位机程序
用vc++MFC开发过不少跟单片机通讯的上位机程序了.搞懂了MFC架构,开发还是很快的,与底层单片机程序通讯,可以用串口.usb.网络.短信形式.串口现在用的越来越少了,一般电脑跟单片机在一块,使用串 ...
- VC++编写简单串口上位机程序
VC++编写简单串口上位机程序 转载: http://blog.sina.com.cn/s/articlelist_1809084904_0_1.html VC++编写简单串口上位机程序 串口通信 ...
- 如何在linux中测试i2c slave模式驱动的功能?
1. 硬件要求 1.1 需要两台机器,一台作为i2c master(记为M),另一台作为i2c slave(记为S) 1.2 使用杜邦线连接两台机器的i2c信号线 2. 使能内核选项CONFIG_I2 ...
- 模拟I2C从机程序
;Slave.asm SCL BIT P1. SDA BIT P1. ;---------------------------- ORG RESET: SETB SCL SETB SDA CALL I ...
- QT编写上位机程序一定要初始化变量以及谨慎操作指针
背景: 在编写QT上位机界面时,界面在运行的时候经常出现卡死或者直接挂掉的怪现象. 正文: 上位机有个函数为check_receive():该函数的作用为定时调用循环检测USB是否有数据.若有,则将信 ...
- 第一次尝试使用JAVA编写的ATM机程序
package study; import java.util.Scanner; public class ATM { private static int[] users = { 111111, 2 ...
- tomcat如何按站点调试本机程序
1.配置host host地址:c:\windows\system32\drivers\etc 配置本机域名: # localhost name resolution is handled withi ...
随机推荐
- flume学习安装
近期项目组有需求点击流日志须要自己收集,学习了一下flume而且成功安装了.相关信息记录一下. 1)下载flume1.5版本号 wget http://www.apache.org/dyn/clos ...
- Java语言基础(八)
Java语言基础(八) 一.数学运算 + - * / % (1)凡是byte short char类型都按int类型的计算 看看上面的代码,为什么出错! 我已经将100转成byte类型,( ...
- [转] 再叙TIME_WAIT
http://huoding.com/2013/12/31/316 之所以起这样一个题目是因为很久以前我曾经写过一篇介绍TIME_WAIT的文章,不过当时基本属于浅尝辄止,并没深入说明问题的来龙去脉, ...
- xslt语法之---If Else
大家都知道,XSL中是没有if else的,那么要想实现if else该怎么办呢? 其实很简单 <xsl:choose> <xsl:when test="position( ...
- HttpContext.Current
HttpContext. Response 直接这样写会报错 是因为 httpcontext没有提供response 这个静态的方法. 通过这样写就可以 ASP.NET还为它提供了一个静态属性Http ...
- WisDom.Net 框架设计(三) 数据缓存
WisDom.Net --数据缓存 1.几种缓存方式 1.静态全局变量 C#静态变量使用 static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明 ...
- Uri、UriMatcher、ContentUris详解
http://blog.csdn.net/feng88724/article/details/6331396 1.Uri 通用资源标志符(Universal Resource Identifier, ...
- ILMerge合并程序
在DOS窗口中,进入到ILMerge的安装目录 中 如图所示,之后写合并代码, 使用命令进行捆绑,以如图为例,将CSkin.dll和MyTool.exe捆绑成一个新的newtool.exe文件./ou ...
- Java反射学习(java reflect)(二)
ok之前说了Java的反射和反射分析类,那这些东西有神马作用呢,下面就来说应用: 三.运行时使用反射分析对象 简单写一个Employee类,然后利用JAVA反射去取name域,getDeclareFi ...
- 关于JavaScript的类的继承
其实最一开始学JS的时候就看过继承的实现.当时只是去试着理解从书上看来的代码段而已.今天又重新思考了一下,感觉这是一个思维探索演进的结果. 继承,即复用. 如果抛开继承的固有思想,让b复用a的成员,最 ...