Other Parts Discussed in Thread: PCA9557
I am new to both TI devices(I am using MSP430F6779A) and I2C. I learned the basic but now I am facing a problem. I am unable to read input which is connected to an I/O expander (slave) and then to the I2C pins (SDA and SCL) of the microcontroller IC. There are in total 8 input pins connected to the I/O expander whose state (high or low) can be read by the I2C bus. My question is that how do I choose the I/O expander pin whose input I want to read. Suppose, I have 8 input pins at I/O expander from DI1 to DI8. Now, I want to read the input only at DI1. I have assigned correct slave address by reading the datasheet of my IO expander (PCA9557). Datasheet link - http://www.ti.com/lit/ds/symlink/pca9557.pdf. But, how do I choose the pin whose input I want to read ( for eg. DI1). I have made changes to the existing example code given by texas instruments.
/* --COPYRIGHT--,BSD * Copyright (c) 2016, Texas Instruments Incorporated * All rights reserved. * * 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. * --/COPYRIGHT--*/ #include "driverlib.h" //***************************************************************************** //! This example shows how to configure the I2C module as a master for //! single byte reception in interrupt driven mode. The address of the slave //! module that the master is communicating with also set in this example. //! //! Description: This demo connects two MSP430's via the I2C bus. The master //! reads from the slave. This is the MASTER CODE. The data from the slave //! transmitter begins at 0 and increments with each transfer. //! The USCI_B0 RX interrupt is used to know when new data has been received. //! ACLK = n/a, MCLK = SMCLK = BRCLK = DCO = 1MHz //! //! /|\ /|\ // //! MSP430F67791A 10k 10k MSP430F67791A //! slave | | master //! ----------------- | | ----------------- //! -|XIN P2.6/UCB0SDA|<-|----+->|P2.6/UCB0SDA XIN|- //! | | | | | 32kHz //! -|XOUT | | | XOUT|- //! | P2.5/UCB0SCL|<-+------>|P2.5/UCB0SCL | //! | | | P1.0|--> LED //! //! This example uses the following peripherals and I/O signals. You must //! review these and change as needed for your own board: //! - I2C peripheral //! - GPIO Port peripheral (for I2C pins) //! - SCL2 //! - SDA //! //! This example uses the following interrupt handlers. To use this example //! in your own application you must add these interrupt handlers to your //! vector table. //! - USCI_B0_VECTOR. //! // //***************************************************************************** //***************************************************************************** // //Set the address for slave module. This is a 7-bit address sent in the //following format: //[A6:A5:A4:A3:A2:A1:A0:RS] // //A zero in the "RS" position of the first byte means that the master //transmits (sends) data to the selected slave, and a one in this position //means that the master receives data from the slave. // //***************************************************************************** #define SLAVE_ADDRESS 0x33 uint8_t RXData; void main(void) { //Stop WDT WDT_A_hold(WDT_A_BASE); //Set the XT1 frequency to UCS UCS_setExternalClockSource(32768, 0); //Configure Pins for I2C (UCB0SCL, UCB0SDA) //Set P2.5 and P2.6 as Module Function Input GPIO_setAsPeripheralModuleFunctionInputPin( GPIO_PORT_P4, GPIO_PIN4 + GPIO_PIN5 ); GPIO_setAsOutputPin( GPIO_PORT_P5, GPIO_PIN0 + GPIO_PIN7 ); EUSCI_B_I2C_initMasterParam param = {0}; param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK; param.i2cClk = UCS_getSMCLK(); param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS; param.byteCounterThreshold = 1; param.autoSTOPGeneration = EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD; EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, ¶m); //Specify slave address EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, SLAVE_ADDRESS ); //Set Master in receive mode EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE ); //Enable I2C Module to start operations EUSCI_B_I2C_enable(EUSCI_B0_BASE); EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0 + EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT ); GPIO_setOutputLowOnPin( GPIO_PORT_P5, GPIO_PIN7 ); GPIO_setOutputHighOnPin( GPIO_PORT_P5, GPIO_PIN7 ); GPIO_setOutputLowOnPin( GPIO_PORT_P5, GPIO_PIN0 ); __delay_cycles(1000000); while(1) { __delay_cycles(2000); // I2C start condition RXData = EUSCI_B_I2C_masterReceiveSingleByte(EUSCI_B0_BASE); // When I run the code, it get stuck here. GPIO_setOutputLowOnPin( GPIO_PORT_P5, GPIO_PIN7 ); if( RXData) { GPIO_setOutputHighOnPin( GPIO_PORT_P5, GPIO_PIN0 + GPIO_PIN7 ); __delay_cycles(1000000); } else { GPIO_setOutputHighOnPin( GPIO_PORT_P5, GPIO_PIN7 ); GPIO_setOutputHighOnPin( GPIO_PORT_P5, GPIO_PIN0 ); __delay_cycles(1000000); } } }
The function code where I am stuck is here.
uint8_t EUSCI_B_I2C_masterReceiveSingleByte(uint16_t baseAddress) { //Set USCI in Receive mode HWREG16(baseAddress + OFS_UCBxCTLW0) &= ~UCTR; //Send start HWREG16(baseAddress + OFS_UCBxCTLW0) |= (UCTXSTT + UCTXSTP); //Poll for receive interrupt flag. while(!(HWREG16(baseAddress + OFS_UCBxIFG) & UCRXIFG)) { ; } //Send single byte data. return (HWREG16(baseAddress + OFS_UCBxRXBUF)); }
Please help me with this. Also, i want to ask is there any monitor where i could see the inputs at the microcontroller IC? Like the serial monitor at Arduino. I am using JTAG as programmer/debuggr and IAR as IDE. Thanks in advance.