Other Parts Discussed in Thread: BQ32000, MSP430G2553
#include <msp430.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
void I2CInit(unsigned char RTC_i2c_address);
void I2CWriteInit(void);
void I2CReadInit(void);
static unsigned char *PRxData; // Pointer to RX data
static unsigned char *PTxData; // Pointer to RX data
static unsigned char TxByteCtr;
static unsigned char RxByteCtr;
volatile unsigned char TxBuffer[]= {
0x00
};
volatile unsigned char RxBuffer[7];
static volatile unsigned char read_notwrite;
int main(void)
{
unsigned char Time[7];
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= 0x01; // set P1.0 to output direction
P1OUT &= ~0x01; // reset P1.0
I2CInit( 0x68 ); // init RTC
while (1)
{
while(UCB0STAT & UCBBUSY); // wait for bus to be free
read_notwrite=1;
PTxData = (unsigned char *)TxBuffer; // TX array start address
TxByteCtr = sizeof TxBuffer; // Load TX byte counter
RxByteCtr = 1;
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
// Remain in LPM0 until all data
// is RX'd
}
}
void I2CInit(unsigned char rtc_i2c_address)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2|= BIT6 + BIT7; // 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 = 20; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = rtc_i2c_address; // Slave Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE; // Enable TX interrupt
IE2 |= UCB0RXIE; // Enable RX interrupt
}
void I2CWriteInit(void)
{
}
void I2CReadInit(void)
{
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if( read_notwrite == 1 ) // if read
{
if ( TxByteCtr ) // Check TX byte counter
{
UCB0TXBUF = *PTxData++; // Load TX buffer
TxByteCtr--; // Decrement TX byte counter
}
else
{
UCB0CTL1 &= ~UCTR; // set read mode
UCB0CTL1 |= UCTXSTT; // I2C stop condition
IE2 &= ~UCB0TXIE; // Disable TX interrupt
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
}
}
}
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCIAB0RX_ISR(void)
{
if( read_notwrite == 1 ) // if read
{
if( RxByteCtr ) // Check RX byte counter
{
P1OUT |= 0x01; // set P1.0 (for testing)
RxBuffer[RxByteCtr] = UCB0RXBUF;
RxByteCtr--;
}
else
{
UCB0CTL1 |= UCTXSTP; // stop condition
IFG2 &= ~UCB0RXIFG; // Clear USCI_B0 RX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0 if data was
// received
}
}
}
Hey guys.
It´s the first time working with a microcontroller outside of university lessons.
I want to interface the RTC BQ32000 with the MSP430g2553.
I have an oscilloscope running on my SDA and CLK. What i want to do is read out the seconds register 0x00 of the RTC.
Its working fine until it comes to the point where it should read the RXBUFFER. The RTC sends much more data than expected because the Stop from RX-INTERRUPT is not going to happen.
Whats working:
-send start
-Startbit, RTC adress, subadress of register is send
-restart for read
-RTC adress with read is send, ACK, Data from RTC
Whats not working:
-read buffer from RX-INTERRUPT and stop communication
Pushing STOP-DEBUG holds the programm within the TX-INTERRUPT, it seems the programm is stuck within this interrupt and runs it all time again, even if the TX-FLAG and TX-INTERRUPT-ENABLE are 0.