|
ADC,32次转换后,数码管就不显示了,是什么问题呀?
[复制链接]
本帖最后由 dontium 于 2015-1-23 12:51 编辑
/*******************************************
ADC转换通道口为P1.1 本程模拟量大于0.5*AVCC时P1.6灯亮 小于0.5*AVCC时P1.0灯亮
数码管显示AD转换结果(非真实电压,未加算法处理)
********************************************/
/********************************************
本程序实现数码管四位动态显示如:8.29
#define AB P2^0
#define CLK P2^1
#define COM1 P2^2
#define COM2 P2^3
#define COM3 P2^4
#define COM4 P2^5
***********************************************/
#include "msp430g2553.h"
unsigned int results[32];
unsigned int average;
unsigned char led[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0~9数字代码
unsigned int x1,x2,x3,x4;//个十百千
/*******************74ls164*************************/
void sent(unsigned char nu)//74ls164是8位串入并出移位寄存器。发送数据到数码管
{
unsigned char a;
for(a=0;a<8;a++)
{
if((nu&0x01)==0)
{P2OUT&=~(BIT0);}
else
{P2OUT|=BIT0;}
P2OUT&=~(BIT1); //
P2OUT|=BIT1; //上升沿
nu=nu>>1;
}
}
/*******************取出要显示的数字的给位数字值*************************/
void geishu(int x)
{
x1=x%10;//个位
x=x/10;
x2=x%10;//十位
x=x/10;
x3=x%10;//百位
x=x/10;
x4=x%10;//千位
}
/*******************在数码管上一次显示各位*************************/
void display(void)
{
sent(led[x1]);//4
P2OUT&=~(BIT5);
P2OUT|=(BIT2+BIT3+BIT4);
__delay_cycles(5000);
sent(led[(x2)]);//3
P2OUT&=~(BIT4);
P2OUT|=(BIT2+BIT3+BIT5);
__delay_cycles(5000);
sent(led[x3]|0x80);//2 并且让该位小数点常亮
P2OUT&=~(BIT3);
P2OUT|=(BIT2+BIT4+BIT5);
__delay_cycles(5000);
sent(led[x4]);//1
P2OUT&=~(BIT2);
P2OUT|=(BIT3+BIT4+BIT5);
__delay_cycles(5000);
}
void main(void)
{
int xx;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL0|=ADC10ON+ADC10IE+REFON+REF2_5V+SREF_1+MSC+ADC10SHT_2;
ADC10CTL1 = INCH_1; // input A1
ADC10AE0 |= 0x02; // PA.1 ADC option select
P1DIR |= BIT6+BIT0; // Set P1.0 to output direction
P1OUT&=~(BIT6+BIT0);
P2DIR|=(BIT2+BIT3+BIT4+BIT5); // 打开1,并赋初值。
P2OUT|=(BIT2+BIT3+BIT4+BIT5);
P2DIR|=(BIT0+BIT1); // AB和CLOCK设置为输入。
P2OUT&=~(BIT0+BIT1);
for (;;)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
if (ADC10MEM < 0x1FF)
{ P1OUT |= BIT0; P1OUT &= ~BIT6; } // Clear P1.0 LED off
else
{ P1OUT |= BIT6; P1OUT &= ~BIT0; } // Set P1.0 LED on
// average=ADC10MEM;
xx=average;
geishu(xx);
display();
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
static unsigned int index = 0;
results[index++] = ADC10MEM; // Move results, IFG is cleared
if(index == 31)
{
unsigned int i;
average = 0;
for(i = 0; i < 32; i++)
average += results;
average >>= 5; //除以32求平均值
index = 0;
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
}
}
|
|