#include "msp430g2553.h" #include "1602.h"
#define uchar unsigned char #define uint unsigned int #define Num_of_Results 32 static uint results[Num_of_Results]; //保存ADC转换结果的数组 // is not used for anything. void Trans_val(uint Hex_Val); /*************************************** 1602显示字符 和算法滤波的定义 *************************************/ uchar shuzi[] = {"0123456789."}; uchar tishi[] = {"The volt is:"};
/************************************** 主函数 ***************************************/ int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT LCD_init(); //复位1602液晶 LCD_write_string(2,0,tishi); //显示提示信息 LCD_write_char(11,1,'V'); ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled ADC10CTL1 = INCH_1; // input A1 ADC10AE0 |= 0x02; // PA.1 ADC option select ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start _EINT(); __bis_SR_register(CPUOFF + GIE); // Clear CPUOFF bit from 0(SR) }
/******************************************* 函数名称:ADC12ISR 功 能:ADC中断服务函数,在这里用多次平均的 计算P6.0口的模拟电压数值 参 数:无 返回值 :无 ********************************************/ #pragma vector=ADC10_VECTOR __interrupt void ADC10ISR (void) { static uint index = 0;
results[index++] = ADC10MEM; // 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 2的5次方=32 Trans_val(sum); } __bic_SR_register_on_exit(CPUOFF); }
/******************************************* 函数名称:Trans_val 功 能:将16进制ADC转换数据变换成三位10进制 真实的模拟电压数据,并在液晶上显示 参 数:Hex_Val--16进制数据 n--变换时的分母等于2的n次方 返回值 :无 ********************************************/ void Trans_val(uint Hex_Val) { unsigned long caltmp; uint Curr_Volt; uchar t1,i; uchar ptr[4]; 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 ptr[0] = Curr_Volt / 100; //Hex->Dec变换 t1 = Curr_Volt - (ptr[0] * 100); ptr[2] = t1 / 10; ptr[3] = t1 - (ptr[2] * 10); ptr[1] = 10; //shuzi表中第10位对应符号"." //在液晶上显示变换后的结果 for(i = 0;i < 4;i++) LCD_write_char((6 + i),1,shuzi[ptr]); }
我用的是四线驱动12864 测得P1.1口的电压为3.48v 但是显示屏上木有视数啊
|