我学习书籍上的程序,
可是我读不到0x41的值,程序执行到if(!iRetVal)之后 直接跳转到MAP那边 ,请问这是怎么回事呢?
此外,加速度传感器的值可以读取,是硬件问题吗?
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
看不出来啊,给你个官方的例程
//*****************************************************************************
//
// Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the
// distribution.
//
// Neither the name of Texas Instruments Incorporated nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//*****************************************************************************
//*****************************************************************************
//
// Application Name - I2C
// Application Overview - The objective of this application is act as an I2C
// diagnostic tool. The demo application is a generic
// implementation that allows the user to communicate
// with any I2C device over the lines.
// Application Details -
// http://processors.wiki.ti.com/index.php/CC32xx_I2C_Application
// or
// docs\examples\CC32xx_I2C_Application.pdf
//
//*****************************************************************************
//*****************************************************************************
//
//! \addtogroup i2c_demo
//! @{
//
//*****************************************************************************
// Standard includes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Driverlib includes
#include "hw_types.h"
#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_common_reg.h"
#include "rom.h"
#include "rom_map.h"
#include "interrupt.h"
#include "prcm.h"
#include "utils.h"
#include "uart.h"
// Common interface includes
#include "uart_if.h"
#include "i2c_if.h"
#include "pinmux.h"
//*****************************************************************************
// MACRO DEFINITIONS
//*****************************************************************************
#define APPLICATION_VERSION "1.1.1"
#define APP_NAME "I2C Demo"
#define UART_PRINT Report
#define FOREVER 1
#define CONSOLE UARTA0_BASE
#define FAILURE -1
#define SUCCESS 0
#define RETERR_IF_TRUE(condition) {if(condition) return FAILURE;}
#define RET_IF_ERR(Func) {int iRetVal = (Func); \
if (SUCCESS != iRetVal) \
return iRetVal;}
//*****************************************************************************
// GLOBAL VARIABLES -- Start
//*****************************************************************************
#if defined(ccs)
extern void (* const g_pfnVectors[])(void);
#endif
#if defined(ewarm)
extern uVectorEntry __vector_table;
#endif
//*****************************************************************************
// GLOBAL VARIABLES -- End
//*****************************************************************************
//****************************************************************************
// LOCAL FUNCTION DEFINITIONS
//****************************************************************************
//*****************************************************************************
//
//! Display a prompt for the user to enter command
//!
//! \param none
//!
//! \return none
//!
//*****************************************************************************
void
DisplayPrompt()
{
UART_PRINT("\n\rcmd#");
}
//*****************************************************************************
//
//! Display the usage of the I2C commands supported
//!
//! \param none
//!
//! \return none
//!
//*****************************************************************************
void
DisplayUsage()
{
UART_PRINT("Command Usage \n\r");
UART_PRINT("------------- \n\r");
UART_PRINT("write <dev_addr> <wrlen> <<byte0> [<byte1> ... ]> <stop>\n\r");
UART_PRINT("\t - Write data to the specified i2c device\n\r");
UART_PRINT("read <dev_addr> <rdlen> \n\r\t - Read data frpm the specified "
"i2c device\n\r");
UART_PRINT("writereg <dev_addr> <reg_offset> <wrlen> <<byte0> [<byte1> ... "
"]> \n\r");
UART_PRINT("\t - Write data to the specified register of the i2c device\n\r");
UART_PRINT("readreg <dev_addr> <reg_offset> <rdlen> \n\r");
UART_PRINT("\t - Read data from the specified register of the i2c device\n\r");
UART_PRINT("\n\r");
UART_PRINT("Parameters \n\r");
UART_PRINT("---------- \n\r");
UART_PRINT("dev_addr - slave address of the i2c device, a hex value "
"preceeded by '0x'\n\r");
UART_PRINT("reg_offset - register address in the i2c device, a hex value "
"preceeded by '0x'\n\r");
UART_PRINT("wrlen - number of bytes to be written, a decimal value \n\r");
UART_PRINT("rdlen - number of bytes to be read, a decimal value \n\r");
UART_PRINT("bytex - value of the data to be written, a hex value preceeded "
"by '0x'\n\r");
UART_PRINT("stop - number of stop bits, 0 or 1\n\r");
UART_PRINT("--------------------------------------------------------------"
"--------------- \n\r\n\r");
}
//*****************************************************************************
//
//! Display the buffer contents over I2C
//!
//! \param pucDataBuf is the pointer to the data store to be displayed
//! \param ucLen is the length of the data to be displayed
//!
//! \return none
//!
//*****************************************************************************
void
DisplayBuffer(unsigned char *pucDataBuf, unsigned char ucLen)
{
unsigned char ucBufIndx = 0;
UART_PRINT("Read contents");
UART_PRINT("\n\r");
while(ucBufIndx < ucLen)
{
UART_PRINT(" 0x%x, ", pucDataBuf[ucBufIndx]);
ucBufIndx++;
if((ucBufIndx % 8) == 0)
{
UART_PRINT("\n\r");
}
}
UART_PRINT("\n\r");
}
//*****************************************************************************
//
//! Application startup display on UART
//!
//! \param none
//!
//! \return none
//!
//*****************************************************************************
static void
DisplayBanner(char * AppName)
{
Report("\n\n\n\r");
Report("\t\t *************************************************\n\r");
Report("\t\t CC3200 %s Application \n\r", AppName);
Report("\t\t *************************************************\n\r");
Report("\n\n\n\r");
}
//****************************************************************************
//
//! Parses the read command parameters and invokes the I2C APIs
//!
//! \param pcInpString pointer to the user command parameters
//!
//! This function
//! 1. Parses the read command parameters.
//! 2. Invokes the corresponding I2C APIs
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
ProcessReadCommand(char *pcInpString)
{
unsigned char ucDevAddr, ucLen;
unsigned char aucDataBuf[256];
char *pcErrPtr;
int iRetVal;
//
// Get the device address
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucDevAddr = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
//
// Get the length of data to be read
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10);
//RETERR_IF_TRUE(ucLen > sizeof(aucDataBuf));
//
// Read the specified length of data
//
iRetVal = I2C_IF_Read(ucDevAddr, aucDataBuf, ucLen);
if(iRetVal == SUCCESS)
{
UART_PRINT("I2C Read complete\n\r");
//
// Display the buffer over UART on successful write
//
DisplayBuffer(aucDataBuf, ucLen);
}
else
{
UART_PRINT("I2C Read failed\n\r");
return FAILURE;
}
return SUCCESS;
}
//****************************************************************************
//
//! Parses the readreg command parameters and invokes the I2C APIs
//! i2c readreg 0x<dev_addr> 0x<reg_offset> <rdlen>
//!
//! \param pcInpString pointer to the readreg command parameters
//!
//! This function
//! 1. Parses the readreg command parameters.
//! 2. Invokes the corresponding I2C APIs
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
ProcessReadRegCommand(char *pcInpString)
{
unsigned char ucDevAddr, ucRegOffset, ucRdLen;
unsigned char aucRdDataBuf[256];
char *pcErrPtr;
//
// Get the device address
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucDevAddr = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
//
// Get the register offset address
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucRegOffset = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
//
// Get the length of data to be read
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucRdLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10);
//RETERR_IF_TRUE(ucLen > sizeof(aucDataBuf));
//
// Write the register address to be read from.
// Stop bit implicitly assumed to be 0.
//
RET_IF_ERR(I2C_IF_Write(ucDevAddr,&ucRegOffset,1,0));
//
// Read the specified length of data
//
RET_IF_ERR(I2C_IF_Read(ucDevAddr, &aucRdDataBuf[0], ucRdLen));
UART_PRINT("I2C Read From address complete\n\r");
//
// Display the buffer over UART on successful readreg
//
DisplayBuffer(aucRdDataBuf, ucRdLen);
return SUCCESS;
}
//****************************************************************************
//
//! Parses the writereg command parameters and invokes the I2C APIs
//! i2c writereg 0x<dev_addr> 0x<reg_offset> <wrlen> <0x<byte0> [0x<byte1> ...]>
//!
//! \param pcInpString pointer to the readreg command parameters
//!
//! This function
//! 1. Parses the writereg command parameters.
//! 2. Invokes the corresponding I2C APIs
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
ProcessWriteRegCommand(char *pcInpString)
{
unsigned char ucDevAddr, ucRegOffset, ucWrLen;
unsigned char aucDataBuf[256];
char *pcErrPtr;
int iLoopCnt = 0;
//
// Get the device address
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucDevAddr = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
//
// Get the register offset to be written
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucRegOffset = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
aucDataBuf[iLoopCnt] = ucRegOffset;
iLoopCnt++;
//
// Get the length of data to be written
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucWrLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10);
//RETERR_IF_TRUE(ucWrLen > sizeof(aucDataBuf));
//
// Get the bytes to be written
//
for(; iLoopCnt < ucWrLen + 1; iLoopCnt++)
{
//
// Store the data to be written
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
aucDataBuf[iLoopCnt] =
(unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
}
//
// Write the data values.
//
RET_IF_ERR(I2C_IF_Write(ucDevAddr,&aucDataBuf[0],ucWrLen+1,1));
UART_PRINT("I2C Write To address complete\n\r");
return SUCCESS;
}
//****************************************************************************
//
//! Parses the write command parameters and invokes the I2C APIs
//!
//! \param pcInpString pointer to the write command parameters
//!
//! This function
//! 1. Parses the write command parameters.
//! 2. Invokes the corresponding I2C APIs
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
ProcessWriteCommand(char *pcInpString)
{
unsigned char ucDevAddr, ucStopBit, ucLen;
unsigned char aucDataBuf[256];
char *pcErrPtr;
int iRetVal, iLoopCnt;
//
// Get the device address
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucDevAddr = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
//
// Get the length of data to be written
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10);
//RETERR_IF_TRUE(ucLen > sizeof(aucDataBuf));
for(iLoopCnt = 0; iLoopCnt < ucLen; iLoopCnt++)
{
//
// Store the data to be written
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
aucDataBuf[iLoopCnt] =
(unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
}
//
// Get the stop bit
//
pcInpString = strtok(NULL, " ");
RETERR_IF_TRUE(pcInpString == NULL);
ucStopBit = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10);
//
// Write the data to the specified address
//
iRetVal = I2C_IF_Write(ucDevAddr, aucDataBuf, ucLen, ucStopBit);
if(iRetVal == SUCCESS)
{
UART_PRINT("I2C Write complete\n\r");
}
else
{
UART_PRINT("I2C Write failed\n\r");
return FAILURE;
}
return SUCCESS;
}
//****************************************************************************
//
//! Parses the user input command and invokes the I2C APIs
//!
//! \param pcCmdBuffer pointer to the user command
//!
//! This function
//! 1. Parses the user command.
//! 2. Invokes the corresponding I2C APIs
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
ParseNProcessCmd(char *pcCmdBuffer)
{
char *pcInpString;
int iRetVal = FAILURE;
pcInpString = strtok(pcCmdBuffer, " \n\r");
if(pcInpString != NULL)
{
if(!strcmp(pcInpString, "read"))
{
//
// Invoke the read command handler
//
iRetVal = ProcessReadCommand(pcInpString);
}
else if(!strcmp(pcInpString, "readreg"))
{
//
// Invoke the readreg command handler
//
iRetVal = ProcessReadRegCommand(pcInpString);
}
else if(!strcmp(pcInpString, "writereg"))
{
//
// Invoke the writereg command handler
//
iRetVal = ProcessWriteRegCommand(pcInpString);
}
else if(!strcmp(pcInpString, "write"))
{
//
// Invoke the write command handler
//
iRetVal = ProcessWriteCommand(pcInpString);
}
else
{
UART_PRINT("Unsupported command\n\r");
return FAILURE;
}
}
return iRetVal;
}
//*****************************************************************************
//
//! Board Initialization & Configuration
//!
//! \param None
//!
//! \return None
//
//*****************************************************************************
static void
BoardInit(void)
{
/* In case of TI-RTOS vector table is initialize by OS itself */
#ifndef USE_TIRTOS
//
// Set vector table base
//
#if defined(ccs)
MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
#endif
#if defined(ewarm)
MAP_IntVTableBaseSet((unsigned long)&__vector_table);
#endif
#endif
//
// Enable Processor
//
MAP_IntMasterEnable();
MAP_IntEnable(FAULT_SYSTICK);
PRCMCC3200MCUInit();
}
//*****************************************************************************
//
//! Main function handling the I2C example
//!
//! \param None
//!
//! \return None
//!
//*****************************************************************************
void main()
{
int iRetVal;
char acCmdStore[512];
//
// Initialize board configurations
//
BoardInit();
//
// Configure the pinmux settings for the peripherals exercised
//
PinMuxConfig();
//
// Configuring UART
//
InitTerm();
//
// I2C Init
//
I2C_IF_Open(I2C_MASTER_MODE_FST);
//
// Display the banner followed by the usage description
//
DisplayBanner(APP_NAME);
DisplayUsage();
while(FOREVER)
{
//
// Provide a prompt for the user to enter a command
//
DisplayPrompt();
//
// Get the user command line
//
iRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
if(iRetVal < 0)
{
//
// Error in parsing the command as length is exceeded.
//
UART_PRINT("Command length exceeded 512 bytes \n\r");
DisplayUsage();
}
else if(iRetVal == 0)
{
//
// No input. Just an enter pressed probably. Display a prompt.
//
}
else
{
//
// Parse the user command and try to process it.
//
iRetVal = ParseNProcessCmd(acCmdStore);
if(iRetVal < 0)
{
UART_PRINT("Error in processing command\n\r");
DisplayUsage();
}
}
}
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @
//
//*****************************************************************************
这个官方例程我也有哦,应该不是I2C出问题了,毕竟加速度传感器可以工作, 可以想想也不可能就坏以个温度传感器啊