TACTL |= TACLR; /* TAR may be cleared by setting the TACLR bit. Setting TACLR also clears the clock divider and count direction for up/down mode.*/
TACTL |= TASSEL_2 + MC_1; /* Timer A clock source select: 2 – SMCLK; Timer A mode control: 1 - Up to CCR0 */
TACCTL0 = CCIE; // CCR0 interrupt enabled
CCR0 = 4096;
void I2CSendByte(unsigned char bt) // Transfer from the Microcontroller a data or command byte to the ds1631
{
register unsigned char i;
for(i=0; i<8; i++)
{
if(bt & 0x80) // check if MSB is 1 or 0: 1: SDA = 1; 0: SDA = 0
{
SDA_high();
}
else
{
SDA_low();
}
I2CSCLHigh();
I2CBitDly();
SCL_low();
bt = bt << 1;
}
SDA_high();
I2CSCLHigh();
I2CBitDly();// check for ACK
if((P1OUT |= SDA)) // if SDA = 1, NACK
SCL_low();
SDA_high(); // end transmission
SCL_high();
}
unsigned char I2CGetByte(unsigned char lastone) // lastone == 1 for last byte; 0 for any other byte
{
register unsigned char i, res;
res = 0;
for(i=0; i<8; i++) // each bit at a time, MSB first
{
I2CSCLHigh();
I2CBitDly();
res *= 2; // shift current bit pattern once to the left
if((P1OUT |= SDA))
res++; // if bit read in is a 1, then set LSB to a 1, see above for shifting
SCL_low();
}
if(lastone == 1) // send ACK according to "lastone"
{
SDA_high();
}
else
{
SDA_low();
}
I2CSCLHigh();
I2CBitDly();
SCL_low();
SDA_high();
SCL_high();
return(res);
}