MSP430 模数转换后串口发送

MSP430F149

#include <msp430.h>
/* 初始化ADC */
void setadc()
{
    P6SEL |= 0x01; /* 打开引脚特殊功能  P6.0引脚 */
    ADC12CTL0 = ADC12ON + SHT0_8 + MSC; /* 打开ADC转换核心 设置采样周期 */
    ADC12CTL1 = SHP + CONSEQ_2; /* 采样模式选择 */
    ADC12IE = 0x01; /* 采样通道 也就是P6.0 */
    ADC12CTL0 |= ENC; /* 使能功能 */
    ADC12CTL0 |= ADC12SC; /* 开始运行 */
}

/* 初始化串口 */
void setuart(void)
{
    P3SEL |= (BIT4 + BIT5); /* 初始化引脚特殊功能 P34 P35是串口0引脚 */
    ME1 |= UTXE0 + URXE0; /* 使能UART功能 */
    UCTL0 |= CHAR; /* 8N1数据传输模式 */
    UTCTL0 |= SSEL1; /*选择UART时钟=SMCLK=8MHZ */
    UBR00 = 0x45; /* 设定波特率 */
    UBR10 = 0x00; /* 设定波特率 */
    UMCTL0 = 0x00; /* 设定调制 */
    UCTL0 &= ~SWRST; /* 打开UART功能 */
    IE1 |= URXIE0; /* 打开UART接收中断 */
}

/* 发送一个字符 */
void send_byte(char t)
{
    while (!(IFG1 & UTXIFG0))
        ; /* 检测UART当前是否繁忙 */
    TXBUF0 = t; /* 发送字符 */
}

/* 发送一个字符串 */
void send_str(char *s)
{
    while (*s != '\0') /* 指针指向的不是0的时候 */
    {
        send_byte(*s++); /* 发送指针指向字符  指针移动 */
    }
}

/* 初始化单片机工作时钟*/
void setclock()
{
    unsigned int i;

    BCSCTL1 &= ~XT2OFF; /* 打开XT2时钟源头 */

    do
    {
        IFG1 &= ~OFIFG; /* 清除起振错误标志 */
        for (i = 0xFF; i > 0; i--)
            ; /* 等待起振完成 */
    }
    while ((IFG1 & OFIFG)); /* 检测起振是否成功 */

    BCSCTL2 |= SELM_2 + SELS; /* 设定MCLK=SMCLK=XT2外部时钟源8MHZ */
}

int temp; /* 最终电压数值 */
int main(void)
{
    char sed_data[20]; /*  */
    char sed_c = 0;
    WDTCTL = WDTPW | WDTHOLD; /* 关闭开门狗功能 */
    setclock(); /* 设置时钟 */
    setuart(); /* 设置串口 */
    setadc(); /* 设置ADC */
    __enable_interrupt(); /* 打开总中断 */
    LPM0;
    while (1)
    {
        /* 装填数组 */
        sed_c = 0;
        sed_data[sed_c++] = 0x30 + temp % 10000 / 1000;
        sed_data[sed_c++] = '.';
        sed_data[sed_c++] = 0x30 + temp % 1000 / 100;
        sed_data[sed_c++] = 0x30 + temp % 100 / 10;
        sed_data[sed_c++] = 0x30 + temp % 10;
        sed_data[sed_c++] = 'v';
        sed_data[sed_c++] = '\r';
        sed_data[sed_c++] = '\n';
        sed_data[sed_c++] = 0;
        send_str(sed_data); /* 发送字符串数组 */
        LPM0; /* 进入低功耗休眠  直到下次有新结果再次打开发送 */
    }
}

#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR(void)
{
    static int i = 0; /* 采样次数 */
    static int result = 0;

    i++; /* 采样次数 */
    if (i < 10)
    {
        result += (ADC12MEM0 / 10); /* 多算几次取平均 */
    }
    else
    {
        i = 0;
        temp = result * 0.805; /* 计算电压数值 */
        result = 0;
        LPM0_EXIT; /* 退出低功耗 回到主程序发送数值 */
    }
}


MSP430F5529

#include <msp430.h>

int lastadc; //电压值

//发送一个字符
void send_byte(char ff)
{
    while (!(UCA0IFG & UCTXIFG))
        ;             // USCI_A0 TX buffer ready?
    UCA0TXBUF = ff;                  // TX -> RXed character
}

/* 发送一个字符串 */
void send_string(char *s)
{
    while (*s != '\0')
    {
        send_byte(*s++);
    }
}
int ifg = 0;
int main(void)
{
    char string_send[20]; /*  */
    char count = 0;

    WDTCTL = WDTPW + WDTHOLD;                   // Stop watchdog timer

    //     Vin -->|P6.0/CB0/A0      |
    P6SEL |= 0x01;                            // Enable A/D channel A0
    ADC12CTL0 = ADC12ON + ADC12SHT0_8 + ADC12MSC; // Turn on ADC12, set sampling time
                                                  // set multiple sample conversion
    ADC12CTL1 = ADC12SHP + ADC12CONSEQ_2;       // Use sampling timer, set mode
    ADC12IE = 0x01;                           // Enable ADC12IFG.0
    ADC12CTL0 |= ADC12ENC;                    // Enable conversions
    ADC12CTL0 |= ADC12SC;                     // Start conversion

    //            |     P3.3/UCA0TXD|------------>
    //            |                 | 115200 - 8N1
    //            |     P3.4/UCA0RXD|<------------
    P3SEL |= BIT3 + BIT4;                       // P3.3,4 = USCI_A0 TXD/RXD
    UCA0CTL1 |= UCSWRST;                      // **Put state machine in reset**
    UCA0CTL1 |= UCSSEL_2;                     // SMCLK
    UCA0BR0 = 9;                              // 1MHz 115200 (see User's Guide)
    UCA0BR1 = 0;                              // 1MHz 115200
    UCA0MCTL |= UCBRS_1 + UCBRF_0;            // Modulation UCBRSx=1, UCBRFx=0
    UCA0CTL1 &= ~UCSWRST;                   // **Initialize USCI state machine**
    //UCA0IE |= UCRXIE;                         // Enable USCI_A0 RX interrupt

    __enable_interrupt(); /* 打开总中断 */
    while (1)
    {
        if (ifg == 1)
        {
            ifg = 0;
            /* 装填数组 */
            count = 0;
            string_send[count++] = 0x30 + lastadc % 10000 / 1000;
            string_send[count++] = '.';
            string_send[count++] = 0x30 + lastadc % 1000 / 100;
            string_send[count++] = 0x30 + lastadc % 100 / 10;
            string_send[count++] = 0x30 + lastadc % 10;
            string_send[count++] = 'v';
            string_send[count++] = '\r';
            string_send[count++] = '\n';
            string_send[count++] = 0;
            send_string(string_send); /* 发送字符串数组 */

        }
    }
}

#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR(void)
{
    static int i = 0;
    static int value = 0;

    switch (__even_in_range(ADC12IV, 34))
    {
    case 0:
        break;                           // Vector  0:  No interrupt
    case 2:
        break;                           // Vector  2:  ADC overflow
    case 4:
        break;                           // Vector  4:  ADC timing overflow
    case 6:                                  // Vector  6:  ADC12IFG0
        i++;
        if (i < 3)
        {
            value += (ADC12MEM0 / 3);
        }
        else
        {
            i = 0;
            lastadc = value * 0.80566f;
            value = 0;
            ifg = 1;
        }
    case 8:
        break;                           // Vector  8:  ADC12IFG1
    case 10:
        break;                           // Vector 10:  ADC12IFG2
    case 12:
        break;                           // Vector 12:  ADC12IFG3
    case 14:
        break;                           // Vector 14:  ADC12IFG4
    case 16:
        break;                           // Vector 16:  ADC12IFG5
    case 18:
        break;                           // Vector 18:  ADC12IFG6
    case 20:
        break;                           // Vector 20:  ADC12IFG7
    case 22:
        break;                           // Vector 22:  ADC12IFG8
    case 24:
        break;                           // Vector 24:  ADC12IFG9
    case 26:
        break;                           // Vector 26:  ADC12IFG10
    case 28:
        break;                           // Vector 28:  ADC12IFG11
    case 30:
        break;                           // Vector 30:  ADC12IFG12
    case 32:
        break;                           // Vector 32:  ADC12IFG13
    case 34:
        break;                           // Vector 34:  ADC12IFG14
    default:
        break;
    }
}


猜你喜欢

转载自blog.csdn.net/x1131230123/article/details/107319685