Exemple #1
0
unsigned char EEPROM_RandomRead(unsigned int Address)
// Description:
//   Random Read Operation. Data is read from the EEPROM. The EEPROM
//   address is defined with the parameter Address.
{
  unsigned char adr_hi;
  unsigned char adr_lo;

  while (I2CDCTL&I2CBB); //http://www.ti.com/lit/er/slaz018d/slaz018d.pdf

  adr_hi = Address >> 8;         // calculate high byte
  adr_lo = Address & 0xFF;       //     and low byte of address

  I2CBuffer[1] = adr_hi;         // store single bytes that have to be sent
  I2CBuffer[0] = adr_lo;         //   in the I2CBuffer.
  PtrTransmit = 1;               // set I2CBuffer Pointer

  I2CWriteInit();
  I2CNDAT = 2;         // 1 control byte + 2 bytes should be transmitted
  I2CIFG &= ~ARDYIFG;  // clear Access ready interrupt flag
  I2CTCTL |= I2CSTT;   // start condition generation
                       //      => I2C communication is started
  while ((~I2CIFG)&ARDYIFG);  // wait untill transmission is finished
  I2CReadInit();
  I2CNDAT = 1;         // 1 byte should be received
  PtrReceive = 0;

  I2CIFG &= ~ARDYIFG;         // clear Access ready interrupt flag
  I2CTCTL |= I2CSTT+I2CSTP;   // start receiving and finally generate
                              //                  re-start and stop condition
  while ((~I2CIFG)&ARDYIFG);  // wait untill transmission is finished
  return I2CBuffer[0];
}
Exemple #2
0
void readFromAddress(uint8_t address , unsigned char * Data , unsigned int Size)
{
    uint8_t addressWord = address;
    counterSize = Size;


    while(UCB0STAT & UCBUSY);
    I2CWriteInit();
    UCB0CTL1 |= UCTXSTT;
    while(UCB0CTL1 & UCTXSTT) {
        if(!(UCNACKIFG & UCB0STAT));
        break;

    }


    UCB0TXBUF = addressWord;// Load TX buffer
    if(UCB0STAT & UCNACKIFG)    // nack received this should not happen if address is correct
    {
        UCB0STAT &= ~UCNACKIFG;
        UCB0CTL1 |= UCTXSTP;       //stop...
        while(UCB0CTL1 & UCTXSTP);
        return;                  //... and exit
    }
    while(!(UCB0TXIFG & IFG2));


    // Read Data byte
    I2CReadInit();


    UCB0CTL1 |= UCTXSTT;                      // I2C start condition


    while(UCB0CTL1 & UCTXSTT)                // Start condition sent?
    {
        if(!(UCNACKIFG & UCB0STAT))           // Break out if ACK received
            break;
    }
    if(UCB0STAT & UCNACKIFG)    // nack received this should not happen if address is correct
    {
        UCB0STAT &= ~UCNACKIFG;
        UCB0CTL1 |= UCTXSTP;       //stop...
        while(UCB0CTL1 & UCTXSTP);
        return;
    }
}
Exemple #3
0
/*---------------------------------------------------------------------------*/
unsigned char EEPROM_CurrentAddressRead(void)
// Description:
//   Current Address Read Operation. Data is read from the EEPROM. The current
//   address from the EEPROM is used.
{
//  while (I2CDCTL&I2CBUSY); // wait until I2C module has finished all operations
  while (I2CDCTL&I2CBB); //http://www.ti.com/lit/er/slaz018d/slaz018d.pdf

  I2CReadInit();
  U0CTL |= MST;         // define Master Mode
  I2CNDAT = 1;          // 1 byte should be received
  I2CIFG &= ~ARDYIFG;         // clear Access ready interrupt flag
  I2CTCTL |= I2CSTT+I2CSTP;   // start receiving and finally generate
                              //              re-start and stop condition
  while ((~I2CIFG)&ARDYIFG);  // wait untill transmission is finished
  return I2CBuffer[0];
}
int main(void)
{
    unsigned int j;

    WDTCTL = WDTPW | WDTHOLD;               // Stop WDT
    if (CALBC1_8MHZ==0xFF)                  // If calibration constant erased
    {
        while(1);                           // do not load, trap CPU!!
    }
    DCOCTL = 0;                             // Select lowest DCOx and MODx settings
    BCSCTL1 = CALBC1_8MHZ;                  // Set DCO to 8MHz. This is needed for SCL = 400 KHz
    DCOCTL = CALDCO_8MHZ;

    P3SEL |= 0x06;                          // Assign I2C pins to USCI_B0

    UCB0CTL1 |= UCSWRST;                    // Enable SW reset
    UCB0CTL0 = UCMST | UCMODE_3 | UCSYNC;   // I2C Master, synchronous mode
    UCB0CTL1 = UCSSEL_2 | UCSWRST;          // Use SMCLK, keep SW reset
    UCB0BR0 = 18;                           // fSCL = SMCLK/18 = ~400kHz
    UCB0BR1 = 0;
    UCB0I2CSA = 0x48;                       // Slave Address is 048h
    UCB0CTL1 &= ~UCSWRST;                   // Clear SW reset, resume operation


    while(1)
    {
/*---------------------------------------------------------------------------*/
//  Start communication with write operation. Send a start with address and
//  send NUM_BYTES_TX bytes.
/*---------------------------------------------------------------------------*/
        while (UCB0CTL1 & UCTXSTP);         // Ensure stop condition got sent
        I2CWriteInit();                     // Init I2C module for a write operation
        UCB0CTL1 |= UCTXSTT;                // start condition generation

        __delay_cycles(400);                // Delay. This is needed when running at 400 KHz

        for(j = 0; j < NUM_BYTES_TX; j++)   //  send NUM_BYTES_TX bytes.
        {
            if(UCB0TXIFG & IFG2)
            {
                UCB0TXBUF = MSData++;       // Load TX buffer
                IFG2 &= ~UCB0TXIFG;         // Clear USCI_B0 TX interrupt flag
                while (UCB0STAT & UCBUSY);  // wait until I2C module serve for this transmit,arbitration is not lost

               // __delay_cycles(150);          // Delay between two bytes

            }

        }

/*---------------------------------------------------------------------------*/
//  Send a RESTART condition with a Read operation. Send start with address and
//  read NUM_BYTES_RX bytes.
/*---------------------------------------------------------------------------*/
        PRxData = (unsigned char *)RxBuffer;    // Start of RX buffer
        counter=0;
        I2CReadInit();                      // Init I2C module for a read operation
        UCB0CTL1 |= UCTXSTT;                // start condition generation
        while(UCB0CTL1 & UCTXSTT);          // Start condition sent?

        __bis_SR_register(LPM0_bits | GIE); // Enter LPM0 w/ interrupts. Stay in LPM until all expected bytes are read

        while(UCB0CTL1 & UCTXSTP);          // Stop condition sent?
        __delay_cycles(50);                 // Delay between STOP and new START condition
    }
}