[讨论] msp430 AD转换程序

515164595   2012-7-29 17:10 楼主


#include #include"lcd12864.h" #include"BoardConfig.h" #define Num_of_Results 32 static uint results[Num_of_Results]; //保存ADC转换结果的数组 void int_adc() { P6SEL |= 0x01; //选择AD转换通道 ADC12CTL0 = ADC12ON+MSC+SHT0_8; // 采样保持时间为16个ADC12CLK //参考电压为2.5V ADC12CTL1 = SHP+CONSEQ_2; // 多通道单次转换模式 ADC12MCTL0 =0x10; // 使能转换 ADC12IE = 0x01; // 使能ADC中断 ADC12CTL0|=ENC; //使能AD转化器 ADC12CTL0|=ADC12SC; _EINT(); } /******************************************* 函数名称:Trans_val 功 能:将16进制ADC转换数据变换成三位10进制 真实的模拟电压数据,并在液晶上显示 参 数:Hex_Val--16进制数据 n--变换时的分母等于2的n次方 返回值 :无 ********************************************/ void Trans_val1(uint Hex_Val) { unsigned long caltmp; uint Curr_Volt; caltmp = Hex_Val; caltmp = (caltmp << 5) + Hex_Val; //caltmp = Hex_Val * 33 caltmp = (caltmp <<3) + (caltmp << 1); //caltmp = caltmp *10 Curr_Volt = caltmp >> 12; //Curr_Volt = caltmp / 2^n uchar t1,t2,t3; t1=Curr_Volt/100; t2=Curr_Volt%100/10; t3=Curr_Volt%10; Write_Cmd(0x90);Delay_1ms(); Write_Data(t1+0x30);Delay_1ms(); Write_Data('.');Delay_1ms(); Write_Data(t2+0x30);Delay_1ms(); Write_Data(t3+0x30);Delay_1ms(); Write_Data('V');Delay_1ms(); } void Trans_val2(uint Hex_Val) { unsigned long caltmp; unsigned long Curr_Volt; caltmp = Hex_Val; caltmp = (caltmp << 5) + Hex_Val; //caltmp = Hex_Val * 33 caltmp = (caltmp <<3) + (caltmp << 1); //caltmp = caltmp *10 Curr_Volt = caltmp >> 12; //Curr_Volt = caltmp / 2^n uchar v1,v2,v3; Curr_Volt=Curr_Volt*71; v1=Curr_Volt/10000; v2=Curr_Volt%10000/1000; v3=Curr_Volt/100%10; Write_Cmd(0x98);Delay_1ms(); Write_Data(v1+0x30);Delay_1ms(); Write_Data('.');Delay_1ms(); Write_Data(v2+0x30);Delay_1ms(); Write_Data(v3+0x30);Delay_1ms(); Write_Data('V');Delay_1ms(); } void init() { WDTCTL = WDTPW + WDTHOLD; //关狗 BoardConfig(0xb8); Ini_Lcd(); //初始化液晶 Clear_GDRAM(); display_string(0,0,"电压峰值:"); display_string(2,0,"有效值:"); } /******************************************* 函数名称:ADC12ISR 功 能:ADC中断服务函数,在这里用多次平均的 计算P6.0口的模拟电压数值 参 数:无 返回值 :无 ********************************************/ #pragma vector=ADC_VECTOR __interrupt void ADC12ISR (void) { static uint index = 0; results[index++] = ADC12MEM0; // Move results if(index == Num_of_Results) { uchar i; unsigned long sum = 0; index = 0; for(i = 0; i < Num_of_Results; i++) { sum += results; } sum >>= 5; //除以32 Trans_val1(sum); Trans_val2(sum); } } /***************************主函数*************************/ int main(void) { init(); int_adc(); while(1); } 这是我写的AD转化程序,可是只能转换单片机内部的电压,如果是转换外接信号(假如+1V的直流电压)时转换的结果是不对的,为什么呢

 

本帖最后由 paulhyde 于 2014-9-15 04:09 编辑

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复