这是我最近做的关于用单片机读取从串口调试小助手发送的TXT文件的程序,希望可以帮到大家的 啊。。这其中有什么问题的大家可以随时提的啊,谢谢!
#include <msp430f5310.h>
#define uint unsigned int
unsigned char adc_flag =0,temp =0,rx_flag =0,RX_buffer1 =0xff,RX_buffer2=0xff; //,deal_flag =0
unsigned int number =0,adc_number[2000];
unsigned int adc_temp=0xffff,a1=0xffff,a2=0xffff,a=0xffff,a0=0xffff,a00=0,RX_temp =0xffff;
void int_clk()
{
WDTCTL = WDTPW+WDTHOLD; //关闭看门狗
PMAPPWD = 0x02D52; // Enable Write-access to modify port mapping registers
P4MAP7 = PM_MCLK;
PMAPPWD = 0; // Disable Write-Access to modify port mapping registers
UCSCTL3 |= SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
// Increase Vcore setting to level1 to support fsystem=12MHz
// NOTE: Change core voltage one level at a time..
//SetVcoreUp (0x01);
// Initialize DCO to 12MHz
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_5; // Select DCO range 24MHz operation
UCSCTL2 = FLLD_1 + 374; // Set DCO Multiplier for 12MHz
// (N + 1) * FLLRef = Fdco
// (374 + 1) * 32768 = 12MHz
// Set FLL Div = fDCOCLK/2
__bic_SR_register(SCG0); // Enable the FLL control loop
// Worst-case settling time for the DCO when the DCO range bits have been
// changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
// UG for optimization.
// 32 x 32 x 12 MHz / 32,768 Hz = 375000 = MCLK cycles for DCO to settle
__delay_cycles(375000);
// Loop until XT1,XT2 & DCO fault flag is cleared
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
// Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
}
void UartConfig()
{
P4SEL = 0x30; // P4.0,4,5 USCI_A0 option select
P4DIR |=BIT4;
UCA1CTL1 |= UCSWRST; // **Put state machine in reset**
UCA1CTL1 |= UCSSEL_2; // SMCLK
UCA1BR0 = 78; // 12MHz 19200 (see User's Guide)
UCA1BR1 = 0; // 12MHz 19200
UCA1MCTL = UCBRS_0 + UCBRF_2 + UCOS16; // Modln UCBRSx=0, UCBRFx=0,
// over sampling
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA1IE |= UCRXIE; // Enable USCI_A0 RX interrupt
}
void main(void)
{
int_clk();
UartConfig();
__bis_SR_register(GIE);
while(1)
{
if(adc_flag ==0x01)
{
adc_flag =0;
adc_number[number++]=a00;
// UCA1TXBUF = temp;
}
if(adc_flag ==0x02)
{
adc_flag =0;
for(uint j=0;j<number;j++)
{
a00 =adc_number[j];
temp = a00>>8;
while (!(UCA1IFG&UCTXIFG));
UCA1TXBUF = temp;
while (!(UCA1IFG&UCTXIFG));
temp =a00;
UCA1TXBUF = temp;
}
}
__no_operation(); // For debugger
}
}
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
{
switch(__even_in_range(UCA1IV,4))
{
case 0:break; // Vector 0 - no interrupt
case 2: // Vector 2 - RXIFG
while (!(UCA1IFG&UCTXIFG)); // USCI_A0 TX buffer ready?
temp =UCA1RXBUF;
if(temp >=65)
{
temp =temp -55;
if(RX_buffer1 ==0xff)
RX_buffer1 =temp;
else if(RX_buffer2 ==0xff)
RX_buffer2 =temp;
}
else if(temp >=48)
{
temp =temp -48;
if(RX_buffer1 ==0xff)
RX_buffer1 =temp;
else if(RX_buffer2 ==0xff)
RX_buffer2 =temp;
}
if(temp ==32)
{
temp =RX_buffer1<<4;
temp +=RX_buffer2;
//deal_flag =0;
RX_buffer1 =0xff;
RX_buffer2 =0xff;
if(rx_flag ==0)
{
adc_temp = temp;
adc_temp =adc_temp <<8;
rx_flag =0x01;
}
else if(rx_flag ==0x01)
{
rx_flag =0;
adc_temp += temp;
if(adc_temp ==0xaaaa)
{
adc_flag =0x02;
}
else
{
if(a1 ==0xffff) //
a1=adc_temp;
else if(a2 ==0xffff)
a2 =adc_temp;
if((a1 ==0xffff)||(a2 ==0xffff))
{;}
else
adc_flag =0x01;
}
}
}
//if(rx_flag ==0x01)
//{
// rx_flag =0;
// adc_number[number++] =temp;
//}
break;
case 4:break; // Vector 4 - TXIFG
default: break;
}
} |