找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6581|回复: 5
打印 上一主题 下一主题
收起左侧

MSP430F5529之ADC模数转换源程序

[复制链接]
跳转到指定楼层
楼主
ID:89763 发表于 2015-9-12 20:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:223163 发表于 2017-7-29 11:02 | 只看该作者
这个程序不对吧
回复

使用道具 举报

板凳
ID:343756 发表于 2018-7-19 13:35 | 只看该作者
能说说现象吗?
我是新手看不懂
回复

使用道具 举报

地板
ID:374899 发表于 2018-7-19 15:37 | 只看该作者
比赛应该是不可以用现成的开发板,我们得自己开发最小系统,这个程序不合适吧。
回复

使用道具 举报

5#
ID:314818 发表于 2018-7-21 12:17 | 只看该作者
多谢多谢多谢
回复

使用道具 举报

6#
ID:558624 发表于 2019-7-23 23:21 | 只看该作者
谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表