基于MSP430G2553内部ADC10单通道详解
- //采用数码管显示转换结果
- #include "delay.h"
- #define Num_of_Results 32
- static uint results[Num_of_Results];
- uchar d[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
- uchar d1[10]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
- uchar a[5]={0xdf,0xef,0xf7,0xfb,0xff};
- void main()
- {
- // Stop watchdog timer to prevent time out reset
- WDTCTL = WDTPW + WDTHOLD;
- //主系统时钟切换为外部高速晶振
- if (CALBC1_8MHZ == 0xFF || CALDCO_8MHZ == 0xFF)
- {
- while(1); // If calibration constants erased, trap CPU!!
- }
- // Configure Basic Clock
- BCSCTL1 = CALBC1_8MHZ; // Set range
- DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation*/
- P1SEL |= BIT1+BIT0; // p1.0为A0
- P1DIR&=~(BIT1+BIT0);// p1.0为输入
- P2SEL=0X00;// p2为普通io口
-
- ADC10CTL0= ADC10ON+ADC10SHT_2+MSC+ADC10IE+SREF_2;
- // ADC12ON打开ADC;
- //ADC10SHT_3设置采样时间,合适即可
- //MSC设置多次采样
- // 中断允许,IFG置位后会进入中断服务程序
- ADC10CTL1= CONSEQ_2+INCH_0;
- //CONSEQ_2单通道多次转换
- // INCH_0选择通道A0,可以不写,默认为A0
- ADC10AE0|=0X03;
- ADC10SA=0X200;
- ADC10CTL0 |= ENC+ADC10SC; // 使能转换并且开始转换
- ADC10CTL0&=~ADC10IFG;//清除中断标志位
- _EINT();//打开中断
- while(1);
- }
- //ADC中断服务函数,在这里用多次平均的
- #pragma vector=ADC10_VECTOR
- __interrupt void ADC10ISR (void)
- {
- uchar i;
- uchar ptr[4];
- P1DIR=0XFC;//p4作为输出
- P2DIR=0XFF;
- static uint index = 0;
- results[index++] = ADC10MEM;
- if(index == Num_of_Results)
- {
- unsigned long sum = 0,real;
- index = 0;
- for(i = 0; i < Num_of_Results; i++)
- {
- sum += results;
- }
- sum >>= 5; //除以32求得平均值
- real=3300*sum/1023;//扩大1000倍
- ptr[3] = real / 1000;
- ptr[2] = (real - ptr[3]*1000)/100;
- ptr[1] = (real - ptr[3]*1000 - ptr[2]*100)/10;
- ptr[0] = (real - ptr[3]*1000 - ptr[2]*100 - ptr[1]*10);
- for(i=0;i<4;i++)
- {
- P1OUT=a;
- if(i==3)
- P2OUT=d1[ptr];
- else
- P2OUT=d[ptr];
- __delay_cycles(30000);
- P1OUT=a[4];
- P2OUT=d[10];
- }
- }
- }
[color=rgb(51, 102, 153) !important]复制代码
|
感谢分享