Other Parts Discussed in Thread: MSP430F2132, MSP430F5529
Hi,
I am using MSP430f2132 to interface RTC(ISL1208-->slave address 1101111x) I2C interface. My problem is when i am using I2C in master mode it is not giving ACK after START genertion sometimes i observed that UCTXSTT is not setted, i tried with example codes also but i didnt get the solution.Please tell me where i am doing wrong?.
thank u.
---> Code :
#include "msp430x21x2.h"
typedef unsigned char u8;
typedef unsigned int u16;
/* RTC Slave addresses */
#define RTC_I2C_WRITE_DE 0xDE
#define RTC_I2C_READ_DF 0xDF
/* RTC register addresses */
#define RTC_SEC_REG_ADDR 0x00
#define RTC_MIN_REG_ADDR 0x01
#define RTC_HR_REG_ADDR 0x02
#define RTC_DAY_REG_ADDR 0x06
#define RTC_DATE_REG_ADDR 0x03
#define RTC_MON_REG_ADDR 0x04
#define RTC_YR_REG_ADDR 0x05
#define RTC_CTL_REG_ADDR 0x07
#define RTC_SEC_BIT_CH 0x80 /* Clock Halt (in Register 0) */
#define RTC_CTL_BIT_RTCF 0x01 /* Rate select 0 */
#define RTC_CTL_BIT_BAT 0x02 /* Rate select 1 */
#define RTC_CTL_BIT_WRTC 0x10 /* Square Wave Enable */
#define RTC_CTL_BIT_ARST 0x80 /* Output Control */
u8 sec_gc;
u8 min_gc;
u8 hour_gc;
u8 mday_gc;
u8 mon_gc;
u16 year_gi;
u8 wday_gc;
volatile u8 tx_index = 0;
volatile u8 rx_index = 0;
volatile u8 TxByte_gc = 0;
volatile u16 delaytime_gi = 0;
volatile u8 rx_gf = 0, tx_gf = 0;
volatile u8 TxByte2PC = 0;
u8 temp_lc;
u16 index = 0;
u8 RTCSetBuff[] = {0x03, 0x05, 0x07, 0x09, 0x02, 0x09, 0x05};
u8 RTCRxBuff[10];
void tx_uart0_byte(u8 );
void delay(u16 );
void RTC_Write(void);
void RTC_Read(void);
u8 bcd2bin (u8 );
u8 bin2bcd (u16 );
void main (void)
{
//__bic_SR_register(GIE); // interrupt disable
tx_index = 0;
rx_index = 0;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= 0x01;
// CLK Init
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
// Timer Init
TA0CCTL0 = CCIE; // TA0CCR0 interrupt enabled
TA0CCR0 = 1000;
TA0CTL = TASSEL_2 + MC_1; // SMCLK, upmode
// UART Tx/Rx Init
P3SEL |= 0x30; // P3.4,5 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
// UART in I2C mode Init
P3SEL |= 0x06; // P3.4,5 = USCI_A0 TXD/RXD
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = (RTC_I2C_WRITE_DE >> 1);
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
UCB0I2CIE|= UCSTPIE + UCSTTIE;
__bis_SR_register(GIE); // interrupt enable
while(1)
{
P1OUT ^= 0x01;
RTC_Write();
P1OUT ^= 0x01;
}
}
// Timer0_A0 interrupt service routine // Timer0_A0 ISR
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
//P1OUT ^= 0x01; // Toggle P1.0
delaytime_gi++;
}
#pragma vector = USCIAB0TX_VECTOR // UART_Tx_ISR
__interrupt void uart0_tx (void)
{
if(rx_gf == 1)
{
UCB0TXBUF = TxByte_gc;
IFG2 &= ~UCB0TXIFG;
rx_gf = 0;
}
if(tx_gf == 1)
{
tx_gf = 0;
if((IFG2 & UCB0TXIFG) == UCB0TXIFG)
{
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
UCB0TXBUF = TxByte_gc;
tx_index += 1;
}
if(tx_index == 7)
{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
while (UCB0CTL1 & UCTXSTP);
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
}
}
}
#pragma vector = USCIAB0RX_VECTOR // UART_Rx_ISR
__interrupt void uart0_rx (void)
{
// UCB0STAT &= ~(UCSTPIFG + UCSTTIFG);
if(rx_index > 6)
{
UCB0CTL1 |= UCTXSTP; // Generate I2C stop condition
while (UCB0CTL1 & UCTXSTP);
}
IFG2 &= ~UCA0RXIFG;
RTCRxBuff[rx_index] = UCA0RXBUF;
rx_index = rx_index + 1;
}
void RTC_Write(void)
{
if ((UCB0STAT & (~UCBBUSY)) == 0)
{
UCB0CTL1 |= (UCTXSTT | UCTR); // I2C start condition
while (UCB0CTL1 & UCTXSTT); // Loop until I2C STT is sent
if(UCB0STAT & UCNACKIFG)
{ // waiting until data sent or NACK
UCB0CTL1 |= UCTXSTP; // Generate STOP
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
}
tx_index = 0;
while(tx_index < 7)
{
tx_gf = 1;
TxByte_gc = bin2bcd(RTCSetBuff[tx_index]);
IE2 |= UCB0TXIE;
__bis_SR_register(GIE); // interrupt enable
delay(20);
}
}
}
void RTC_Read(void)
{
UCB0CTL1 |= UCTR + UCTXSTT; // I2C start condition
while (UCB0CTL1 & UCTXSTT);
UCB0I2CSA = (RTC_I2C_WRITE_DE >> 1); // Slave Address is 0xDEh(right justified 0xBC)
TxByte_gc = bin2bcd(RTC_SEC_REG_ADDR);
rx_gf = 1;
IE2 |= UCB0TXIE;
__bis_SR_register(GIE); // interrupt enable
rx_gf = 1;
UCB0CTL1 &= ~UCTR;
UCB0CTL1 |= UCTXSTT;
UCB0I2CSA = (RTC_I2C_READ_DF >> 1);
while (UCB0CTL1 & UCTXSTT);
rx_index = 0;
while (rx_index < 7)
{
IE2 |= UCB0RXIE;
__bis_SR_register(GIE); // interrupt enable
}
}
void tx_uart0_byte(u8 data_lc)
{
TxByte2PC = data_lc;
IE2 |= UCA0TXIE;
}
void delay(u16 delaytime_li)
{
delaytime_gi = 0;
while(delaytime_gi < delaytime_li);
}
u8 bcd2bin (u8 n)
{
return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
}
u8 bin2bcd (u16 n)
{
return (((n / 10) << 4) | (n % 10));
}