注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
yusiaochong的个人空间 https://home.eeworld.com.cn/space-uid-416682.html [收藏] [复制] [分享] [RSS]
日志

ADC采样——led显示

已有 503 次阅读2012-5-23 14:07

#include <msp430g2231.h>
#include <intrinsics.h>

long volCalibrated,volMeasured[8],volAverage;
unsigned char volMeasuredPosition = 0;
unsigned char i,j;
void main(void)
{
  //unsigned char i;
  //float average_AD;
  WDTCTL = WDTPW + WDTHOLD;
  ADC10CTL0 = SREF_1 + ADC10SHT_0 + ADC10ON + REFON;
  ADC10CTL0 |= REF2_5V;
  ADC10CTL1 = INCH_1+CONSEQ_0+ADC10DIV_0+ADC10SSEL_2;
  ADC10AE0 |= BIT1;
  P1DIR = 0x75;
  __delay_cycles(1000);
  ADC10CTL0 |= ENC + ADC10SC;
  volCalibrated = ADC10MEM;
  for(i = 0;i < 8;i++)
    volMeasured[i] = volCalibrated;
  volAverage = volCalibrated;
  while(1)
  {
    ADC10CTL0 |= ENC + ADC10SC;
    volMeasured[volMeasuredPosition++] = ADC10MEM;
    if(volMeasuredPosition == 8)
      volMeasuredPosition = 0;
    volAverage = 0;
    for(j = 0;j < 8;j++)
    {
      volAverage += volMeasured[j];
    }
    volAverage >>= 3;
    if(volAverage<0x019A)
      P1OUT = BIT0;
    else if(0x019A<=volAverage<0x0267)
      P1OUT = BIT2;
    else if(0x0267<=volAverage<0x0334)
      P1OUT = BIT4;
    else if(0x0334<=volAverage<0x0400)
      P1OUT = BIT5;
    else
      P1OUT = BIT6;
  }

}

 

想实现     当P1.1引脚的电压U<1.0V时,LED1亮

                当P1.1引脚的电压1.0V<=U<1.5V时,LED3亮

                当P1.1引脚的电压1.5V<=U<2.0V时,LED4亮

                当P1.1引脚的电压2.0V<=U<2.5V时,LED5亮

                当P1.1引脚的电压U>2.5V时,LED2亮

 

下载到DEMO板上后实际的情况是

                当P1.1引脚的电压U<1.0V时,LED1亮

                当P1.1引脚的电压U>1.0V时,LED3亮

 

 

是为什么呢?

本文含有来自论坛的附件或图片:[01.png],点击查看原帖附件。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章