找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 7391|回復(fù): 5
打印 上一主題 下一主題
收起左側(cè)

MSP430F5529之ADC模數(shù)轉(zhuǎn)換源程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:89763 發(fā)表于 2015-9-12 20:10 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include <stdint.h>
#include "msp430.h"

#define WHEEL_DIR      P8DIR
#define WHEEL_OUT      P8OUT
#define WHEEL_EN       BIT0
#define ADC_PORT_SEL   P6SEL
#define ADC_INPUT_A5   BIT5



unsigned int positionData;
unsigned int positionDataOld;
/******************************************************************************/

void Wheel_init(void)
{
    WHEEL_DIR |= WHEEL_EN;
    WHEEL_OUT |= WHEEL_EN;                    // Enable wheel

    ADC12CTL0 = ADC12SHT02 + ADC12ON;                  // Sampling time, ADC12 on
    ADC12CTL1 = ADC12SHP;                              // Use sampling timer
    ADC12MCTL0 = ADC12INCH_5;                          // Use A5 (wheel) as input
    ADC12CTL0 |= ADC12ENC;                             // Enable conversions
    ADC_PORT_SEL |= ADC_INPUT_A5;                      // P6.5 ADC option select (A5)
}

/***************************************************************************/
uint8_t Wheel_getPosition(void)
{
            uint8_t position = 0;

            Wheel_getValue();
                               //determine which position the wheel is in
            if (positionData > 0x0806)
                position = 7 - (positionData - 0x0806) / 128;  //scale the data for 8 different positions
            else
                position = positionData / 260;
     return position;
}
/**************************************************************************/
int Wheel_getValue(void)
{
    //measure ADC value
    ADC12IE = 0x01;                                    // Enable interrupt
    ADC12CTL0 |= ADC12SC;                              // Start sampling/conversion
    __bis_SR_register(LPM0_bits + GIE);                // LPM0, ADC12_ISR will force exit
    ADC12IE = 0x00;                                    // Disable interrupt

    //add hysteresis on wheel to remove fluctuations
    if (positionData > positionDataOld)
        if ((positionData - positionDataOld) > 10)
            positionDataOld = positionData;            //use new data if change is beyond
                                                       // fluctuation threshold
        else
            positionData = positionDataOld;            //use old data if change is not beyond
                                                       // fluctuation threshold
    else
    if ((positionDataOld - positionData) > 10)
        positionDataOld = positionData;                //use new data if change is beyond
                                                       // fluctuation threshold
    else
        positionData = positionDataOld;                //use old data if change is not beyond
                                                       // fluctuation threshold

    return positionData;
}

/***************************************************************************/
void Wheel_disable(void)
{
    WHEEL_OUT &= ~WHEEL_EN;                            //disable wheel
    ADC12CTL0 &= ~ADC12ENC;                            // Disable conversions
    ADC12CTL0 &= ~ADC12ON;                             // ADC12 off
}
/******************************************************************************/
void Wheel_enable(void)
{
    WHEEL_OUT |= WHEEL_EN;                       //enable wheel
    ADC12CTL0 |= ADC12ON;                              // ADC12 on
    ADC12CTL0 |= ADC12ENC;                             // Enable conversions
}
/******************************************************************************/

int main(void)
{
    P1DIR=0x3F;
    Wheel_enable();
    Wheel_init();
    while(1)
    {
            Wheel_getValue();
            switch(positionData)
                            {
                        case 0:P1OUT=0x01;break;
                        case 1:P1OUT=0x02;break;
                        case 2:P1OUT=0x04;break;
                        case 3:P1OUT=0x08;break;
                        case 4:P1OUT=0x10;break;
                        case 5:P1OUT=0x20;break;
                            }

    }
}
/******************************************************************************/
#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
    switch (__even_in_range(ADC12IV, ADC12IV_ADC12IFG15))
    {
        // Vector  ADC12IV_NONE:  No interrupt
        case  ADC12IV_NONE:
            break;

        // Vector  ADC12IV_ADC12OVIFG:  ADC overflow
        case  ADC12IV_ADC12OVIFG:
            break;

        // Vector  ADC12IV_ADC12TOVIFG:  ADC timing overflow
        case  ADC12IV_ADC12TOVIFG:
            break;

        // Vector  ADC12IV_ADC12IFG0: ADC12IFG0:
        case  ADC12IV_ADC12IFG0:
            positionData = ADC12MEM0;                  // ADC12MEM = A0 > 0.5AVcc?
            __bic_SR_register_on_exit(LPM0_bits);      // Exit active CPU
            break;

        // Vector  ADC12IV_ADC12IFG1:  ADC12IFG1
        case  ADC12IV_ADC12IFG1:
            break;

        // Vector ADC12IV_ADC12IFG2:  ADC12IFG2
        case ADC12IV_ADC12IFG2:
            break;

        // Vector ADC12IV_ADC12IFG3:  ADC12IFG3
        case ADC12IV_ADC12IFG3:
            break;

        // Vector ADC12IV_ADC12IFG4:  ADC12IFG4
        case ADC12IV_ADC12IFG4:
            break;

        // Vector ADC12IV_ADC12IFG5:  ADC12IFG5
        case ADC12IV_ADC12IFG5:
            break;

        // Vector ADC12IV_ADC12IFG6:  ADC12IFG6
        case ADC12IV_ADC12IFG6:
            break;

        // Vector ADC12IV_ADC12IFG7:  ADC12IFG7
        case ADC12IV_ADC12IFG7:
            break;

        // Vector ADC12IV_ADC12IFG8:  ADC12IFG8
        case ADC12IV_ADC12IFG8:
            break;

        // Vector ADC12IV_ADC12IFG9:  ADC12IFG9
        case ADC12IV_ADC12IFG9:
            break;

        // Vector ADC12IV_ADC12IFG10:  ADC12IFG10
        case ADC12IV_ADC12IFG10:
            break;

        // Vector ADC12IV_ADC12IFG11:  ADC12IFG11
        case ADC12IV_ADC12IFG11:
            break;

        // Vector ADC12IV_ADC12IFG12:  ADC12IFG12
        case ADC12IV_ADC12IFG12:
            break;

        // Vector ADC12IV_ADC12IFG13:  ADC12IFG13
        case ADC12IV_ADC12IFG13:
            break;

        // Vector ADC12IV_ADC12IFG14:  ADC12IFG14
        case ADC12IV_ADC12IFG14:
            break;

        // Vector ADC12IV_ADC12IFG15:  ADC12IFG15
        case ADC12IV_ADC12IFG15:
            break;

        default:
            break;
    }
}




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:223163 發(fā)表于 2017-7-29 11:02 | 只看該作者
這個(gè)程序不對(duì)吧
回復(fù)

使用道具 舉報(bào)

板凳
ID:343756 發(fā)表于 2018-7-19 13:35 | 只看該作者
能說(shuō)說(shuō)現(xiàn)象嗎?
我是新手看不懂
回復(fù)

使用道具 舉報(bào)

地板
ID:374899 發(fā)表于 2018-7-19 15:37 | 只看該作者
比賽應(yīng)該是不可以用現(xiàn)成的開(kāi)發(fā)板,我們得自己開(kāi)發(fā)最小系統(tǒng),這個(gè)程序不合適吧。
回復(fù)

使用道具 舉報(bào)

5#
ID:314818 發(fā)表于 2018-7-21 12:17 | 只看該作者
多謝多謝多謝
回復(fù)

使用道具 舉報(bào)

6#
ID:558624 發(fā)表于 2019-7-23 23:21 | 只看該作者
謝謝分享
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表