#include "msp430g2553.h"
#include "1602.h"
#define uchar unsigned char
#define uint unsigned int
#define Num_of_Results 1
//unsigned long sum=0;
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:"};
void ClkInit()
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗定时器
if(CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); //挂起
}
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ; //设置DCO模式,MCLK默认时钟源为DCOCLK,即为1MHz
}//BCSCTL1 执行完后等于0x86,DCOCTL 执行完后等于0X60
/**************************************
主函数
***************************************/
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
//ClkInit();
LCD_init(); //复位1602液晶
LCD_write_string(2,0,tishi); //显示提示信息
LCD_write_char(11,1,'V');
//P1DIR&=~BIT1;
P1SEL|=BIT1;
ADC10DTC1=0;
ADC10CTL0 = ADC10SHT_0 + ADC10ON + ADC10IE + MSC; // 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( GIE); // Clear CPUOFF bit from 0(SR)
// while(1)
// {
// Trans_val(sum);
///}
}
/*******************************************
函数名称:ADC12ISR
功 能:ADC中断服务函数,在这里用多次平均的
计算P6.0口的模拟电压数值
参 数:无
返回值 :无
********************************************/
#pragma vector=ADC10_VECTOR
__interrupt void ADC10ISR (void)
{
static uint index = 1;
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 >>= 0; //除以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]);//在液晶上显示变换后的结果
}
大虾 帮我看下程序进中断没。。。