ADC采样——led显示

2019-03-24 14:16发布

#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 = 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亮     是为什么呢? 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
7条回答
starswings
2019-03-25 00:33
你看看DATA SHEET吧,应该是你的输入电压超出输入范围了!!使用片内参考电压的话,只有0~1.2V的有效范围,超了就被当成溢出了,所以只有前两个范围有效。

一周热门 更多>

相关问题

    相关文章