*********************************************************/ #include #define num_of_results 32 static uint results[num_of_results]; /************************************ 功能:将数字量转为模拟量并显示 输入:寄存器ADC12MEM0中的数值 输出:无 ************************************/ void tran(uint ad1) { uint val; unsigned long ad; uchar aa,bb,cc; ad=ad1; val=ad*330/4096; //ad*330将会超过16位所以要把ad转为32的long型(如4096*330>65536) aa=val/100; bb=val/10%10; cc=val%10; gotoxy(5,1); write_data(0x30+aa); write_data('.'); write_data(0x30+bb); write_data(0x30+cc); } void main(void) { WDTCTL=WDTPW+WDTHOLD; init_clk(); init_lcd(); P6SEL|=0X01; ADC12CTL0=ADC12ON+SHT0_8+MSC; ADC12CTL1=SHP+CONSEQ_2; ADC12IE=0X01; ADC12CTL0|=ENC; ADC12CTL0|=ADC12SC; _EINT(); LPM0; } /************************************* 功能:读取ADC12MEM0中的值并求平均以提高稳定性和精确性 输入:无 输出:无 *************************************/ #pragma vector=ADC_VECTOR __interrupt void ADC12ISR(void) { static uchar index = 0; results[index++] = ADC12MEM0; //取转换存储寄存器值 if(index == num_of_results) { uchar i; unsigned long sum = 0; index = 0; for(i = 0; i < num_of_results; i++) { sum += results[i]; } sum = sum/num_of_results; //除以Num_of_Results tran(sum); } } 因篇幅问题不能全部显示,请点此查看更多更全内容