久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1613|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

lpc1114做i2c slave,不回復(fù)ack,也不進(jìn)入中斷

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:677816 發(fā)表于 2019-12-30 20:25 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
大家好,我目前項(xiàng)目需要使用lpc1114 做i2c slave ,移植了如下代碼。在進(jìn)行測試時(shí),lpc1114 不回復(fù)ack,也不進(jìn)入中斷,請大家?guī)兔纯锤郊鞘静ㄆ餍盘枅D。

  • /****************************************************************************
  • *   $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;
  • volatile uint32_t RdIndex = 0;
  • volatile uint32_t WrIndex = 0;
  • /*
  • 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 = 0;
  • 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 = 0;
  • LPC_I2C->CONSET = I2CONSET_AA;/* assert ACK after SLV_R is received */
  • LPC_I2C->CONCLR = I2CONCLR_SIC;
  • I2CSlaveState = I2C_RD_STARTED;
  • WrIndex = I2CRdBuffer[0];/* 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+1];/* 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 |= (0x1<<1);
  • #else
  •   LPC_SYSCON->PRESETCTRL |= (0x1<<0);
  • #endif
  •   LPC_SYSCON->SYSAHBCLKCTRL |= (1<<5);
  •   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 |= (0x1<<9);
  •   LPC_IOCON->PIO0_5 |= (0x1<<9);
  •   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();
  •   for ( i = 0; i < BUFSIZE; i++ )
  •   {
  • I2CRdBuffer = 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 );
  •   return 0;
  • }
  • /******************************************************************************
  • **                            End Of File
  • ******************************************************************************/

···

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:677816 發(fā)表于 2020-1-10 13:22 | 只看該作者
終于找到原因了,中斷問題
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 中文字幕亚洲视频 | 午夜爽爽爽男女免费观看影院 | 日本不卡在线观看 | 午夜视频在线视频 | 国产在线精品一区二区三区 | 欧美国产激情二区三区 | 久久久久久久av | 天堂亚洲网 | 日本中文字幕在线观看 | 成人在线视频网址 | 四虎最新| 国产色片在线 | 免费av观看 | 欧美一区二区在线观看视频 | 精品福利一区二区三区 | 欧日韩不卡在线视频 | 中文字幕精品一区 | 精品国产一区二区三区性色av | 国产情侣激情 | 91福利在线观看视频 | h在线免费观看 | 91久色 | 国产精品欧美精品 | 午夜影院 | 嫩草视频入口 | 国内精品久久久久久 | 99热热精品 | 噜久寡妇噜噜久久寡妇 | 亚洲午夜精品 | 国产精品一区二区免费 | 激情在线视频网站 | 国产日韩精品视频 | 久久51 | 欧美视频免费在线观看 | 91高清在线观看 | 天天操夜夜爽 | 日本又色又爽又黄又高潮 | 中文字幕亚洲视频 | 中文字幕欧美在线观看 | 一二三四在线视频观看社区 | 成人在线精品 |