我想用单片机MSP430实现正弦信号采样,采集256个点。采集结果在串口中显示。怎么用程序实现?

2025-01-02 11:40:03
推荐回答(2个)
回答1:

#include

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
//串口初始化
FLL_CTL0 |= XCAP18PF; // Configure load caps
P2SEL |= 0x30; // P2.4,5 = USART0 TXD/RXD
ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD
UCTL0 |= CHAR; // 8-bit character
UTCTL0 |= SSEL1; // UCLK = SMCLK
UBR00 = 0x36; // 1MHz 19200
UBR10 = 0x00; // 1MHz 19200
UMCTL0 = 0x6B; // Modulation
UCTL0 &= ~SWRST; // Initialize USART state machine
unsigned int adtable[256];
unsigned int i;
unsigned int AD_Times;

AD12Init();
ADC12CTL0 |= ENC ; //enable conversion
ADC12CTL0 |= ADC12SC; // Sampling open
while(1)
{
If (AD_Times==256)
{
for(i=0;i<256;i++)
{
TXBUF0 = adtable[i]; //Send by 2 Bytes
adtable[i]>>8;
TXBUF0 = adtable[i];
}
}

}
void AD12Init(void)
{
P6SEL |= 0x01; // P6.0 ADC option select
ADC12CTL0 &= ~ENC;
ADC12CTL0 = SHT0_6 + ADC12ON+MSC+REFON+REF2_5V; // set the register
ADC12CTL1 = SHP+CONSEQ_2; // Use sampling timer
ADC12MCTL0 = SREF_1+INCH_0
ADC12CTL0 |= ENC; // Conversion enabled
ADC12IE = 0x01; //interrupt

}
#pragma vector=ADC_VECTOR
__interrupt void ADC12_ISR (void)
{
vu32 temp;
temp = ADC12MEM0;
adtable[AD_Times] = temp *2500/4095; // change to mv Value
AD_Times++;
if(AD_Times == 256)
{
ADC12CTL0 &= ~ENC;
ADC12CTL0 &= ~ADC12SC
}
应该是这个样子的,没调试过,有错误自己搞定吧

回答2:

TXBUF0 = adtable[i];
//这里应该添加等待发送完毕的代码

其实你的采集方法有问题,应该是先采集到所有256个点后再用串口发送出去,而不是采一个点发一个点的数据。