访问手机版页面
你的位置:老古开发网 > MSP430单片机C语言编程 > 正文  
MSP430F249 IIC write and Uart send
内容导读:
//******************************************************************************/#include //注意:两次发送间隔必须要有延时,否则不能再次发送,串口发送格式:unsigned char PTxData[250];

 

//******************************************************************************/
#include


//注意:两次发送间隔必须要有延时,否则不能再次发送,串口发送格式:


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?,发送缓冲区空

}
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位地址模式,器件会发送一位读写位,正好8位。
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE; // Enable TX interrupt


}
void IICSend( void )
{
int i;

for(i=3000;i>0;i--); //两次发送间隔必须要有延时,否则不能再次发送


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

}
}

 

标签: iic,uart,write,send,
来源:互联网 作者:karen 时间:2018/5/24 10:38:24
相关阅读
推荐阅读
阅读排行
最近更新
商品推荐