MSP430F249 IIC write and Uart send
//******************************************************************************/
#include
//注意:兩次發(fā)送間隔必須要有延時,否則不能再次發(fā)送,串口發(fā)送格式:
unsigned char PTxData[250]; // Pointer to TX data
unsigned char pHead;
unsigned char pTail;
unsigned char TXByteCtr;
const unsigned char TxData[] = // Table of data to transmit
{
0x01,
0x02,
0x03,
0x04,
0x05
};
void UartInit(void)
{
if (CALBC1_1MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P3SEL |= 0x30; // P3.4,5 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600; (104)decimal = 0x068h
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
}
void UartSend( unsigned char Data )
{
UCA0TXBUF = Data; // TX -> RXed character
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?,發(fā)送緩沖區(qū)空
}
void IICInit(void)
{
UCB0CTL1 |= UCSWRST; // Enable SW reset
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 = 0xA0>>1; // Slave Address is ,注意地址需要右移一位,24C02地址為0XA0,故要寫入0X50
// 7位地址模式,器件會發(fā)送一位讀寫位,正好8位。
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE; // Enable TX interrupt
}
void IICSend( void )
{
int i;
for(i=3000;i>0;i--); //兩次發(fā)送間隔必須要有延時,否則不能再次發(fā)送
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P3SEL |= 0x06; // Assign I2C pins to USCI_B0
IICInit();
UartInit();
while (1)
{
UartSend('A');
PTxData[0] = 0;
PTxData[1] = 2;
PTxData[2] = 3;
PTxData[3] = 4;
PTxData[4] = 5;
pTail = 5;
IICSend();
PTxData[0] = 0X10;
PTxData[1] = 2;
PTxData[2] = 3;
PTxData[3] = 4;
PTxData[4] = 5;
pTail = 5;
IICSend();
PTxData[0] = 0x20;
PTxData[1] = 2;
PTxData[2] = 3;
PTxData[3] = 4;
PTxData[4] = 5;
pTail = 5;
IICSend();
PTxData[0] = 0X30;
PTxData[1] = 2;
PTxData[2] = 3;
PTxData[3] = 4;
PTxData[4] = 5;
pTail = 5;
IICSend();
while(1);
}
}
//------------------------------------------------------------------------------
// The USCIAB0TX_ISR is structured such that it can be used to transmit any
// number of bytes by pre-loading TXByteCtr with the byte count. Also, TXData
// points to the next byte to transmit.
//------------------------------------------------------------------------------
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if ( pHead < pTail ) // Check TX byte counter
{
UCB0TXBUF = PTxData [pHead++ ]; // Load TX buffer
}
else
{
pHead = 0;
UCB0CTL1 |= UCTXSTP; // I2C stop condition
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
}