Part Number: MSP430F5529
Tool/software: Code Composer Studio
Hi,
I try writing a program for DS2482-100 (specifically for reading DS18B20 through DS2482-100). I'm using MSP430F5529. According to Maxims "How to use the DS2482", there need to be:
I tried following code but it doesn't work, it hangs on reading operation (I marked the place in the code). Can you please look if I'm not missing anything?
unsigned char *PTxData; // Pointer to TX data
unsigned char TXByteCtr;
unsigned char RXData;
void initI2C()
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P4SEL |= 0x06; // Assign I2C pins to USCI_B1
UCB1CTL1 |= UCSWRST; // Enable SW reset
UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB1CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB1BR0 = 2; // fSCL = SMCLK/12 = ~100kHz
UCB1BR1 = 0;
UCB1I2CSA = 0x18; // shifted 0x30
UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB1IE |= UCTXIE; // Enable TX interrupt
}
void restart(int type)
{
UCB1CTL1 &= ~UCTR; // set receiver mode
UCB1CTL1 |= UCTXSTT; // send restart
while (UCB1CTL1 & UCTXSTT)
; // wait for start condition done
UCB1CTL1 |= UCTXSTP; // send STOP
}
void stopCommand()
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
}
unsigned char receiveData() //wywaliłam pętle, bo i tak odbieramy tylko 1 bajt
{
while (UCB1CTL1 & UCTXSTP)
; // Ensure stop condition got sent
UCB1CTL1 |= UCTXSTT; // I2C start condition
while (UCB1CTL1 & UCTXSTT)
; // Start condition sent?
UCB1CTL1 |= UCTXSTP; // I2C stop condition
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, enable interrupts
__no_operation(); // For debugger
return RXData; // Increment correct RX value
}
void startCommand()
{
UCB1CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
}
void sendData(unsigned char*TxData)
{
unsigned int i;
for (i = 0; i < 10; i++)
; // Delay required between transaction
PTxData = (unsigned char *) TxData; // TX array start address
// Place breakpoint here to see each
// transmit operation.
TXByteCtr = sizeof TxData; // Load TX byte counter
__bis_SR_register(LPM0_bits + GIE);
// Enter LPM0, enable interrupts
__no_operation(); // Remain in LPM0 until all data
// is TX'd
while (UCB1CTL1 & UCTXSTP)
; // Ensure stop condition got sent
}
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector = USCI_B1_VECTOR
__interrupt
void USCI_B1_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(USCI_B1_VECTOR))) USCI_B1_ISR (void)
#else
#error Compiler not supported!
#endif
{
switch (__even_in_range(UCB1IV, 12))
{
case 0:
break; // Vector 0: No interrupts
case 2:
break; // Vector 2: ALIFG
case 4:
break; // Vector 4: NACKIFG
case 6:
break; // Vector 6: STTIFG
case 8:
break; // Vector 8: STPIFG
case 10:
RXData = UCB0RXBUF; // Get RX data
__bic_SR_register_on_exit(LPM0_bits); // <- HERE IT STOPS
break; // Vector 10: RXIFG
case 12: // Vector 12: TXIFG
if (TXByteCtr) // Check TX byte counter
{
UCB1TXBUF = *PTxData++; // Load TX buffer
TXByteCtr--; // Decrement TX byte counter
}
else
{
UCB1CTL1 |= UCTXSTP; // I2C stop condition
UCB1IFG &= ~UCTXIFG; // Clear USCI_B1 TX int flag
__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
}
default:
break;
}
}