下面是程序,将读到的值通过串口发出去。
现在的问题是发出去的和读过来的不符合。
#include <msp430.h>
unsigned char a;
int b,i;
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 |= XTS; // ACLK = LFXT1 = HF XTAL
BCSCTL3 |= LFXT1S1; // 3 ?16MHz crystal or resonator
do
{
IFG1 &= ~OFIFG; // Clear OSCFault flag
for (i = 0xFF; i > 0; i--); // Time for flag to set
}
while (IFG1 & OFIFG); // OSCFault flag still set?
BCSCTL2 |= SELM_3 + SELS+DIVS1; // MCLK = SMCLK = LFXT1 (safe)
P3SEL = BIT4+BIT5; // P3.4,5 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 8; // 4MHz/115200/4
UCA0BR1 = 0; //
UCA0MCTL = UCBRS2 + UCBRS1; // Modulation UCBRSx = 6
UCA0CTL1 &= ~UCSWRST;
UCB0CTL1 = UCSSEL_2;
P3SEL |= 0x06; // Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 |= UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE;
IE2 |=UCB0RXIE;
for(;;)
{
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0I2CSA = 0x15;
UCB0CTL1 |= UCTR ;
UCB0CTL1 |= UCTXSTT; // I2C TX, start condition
for(b=0;b<1000;b++);
for(b=0;b<1000;b++);
for(b=0;b<1000;b++);
UCB0TXBUF=0x08;
for(b=0;b<1000;b++);
for(b=0;b<1000;b++);
IFG2 &= ~UCB0TXIFG;
UCB0CTL1 &=~ UCTR ;
UCB0CTL1 |= UCTXSTT;
//dealy(1000);
UCB0CTL1 |= UCTXSTP;
UCB0CTL1 |= UCTXSTP;
a=UCB0RXBUF ; // TX -> RXed character
UCA0TXBUF=a;
for(b=0;b<1000;b++);
for(b=0;b<1000;b++);
}
}
此帖出自
小平头技术问答
一周热门 更多>