#include"12864.h"
#include"12864.c"
#define uchar unsigned char
#define uint unsigned int
uint result[64];
uint bai,shi,ge;
unsigned long dianya,average=0;
uchar a=0,i;
uchar shuzi[] = {"0123456789."};
uint ptr[5];
void turn_value();
void main()
{
WDTCTL = WDTPW + WDTHOLD;
LCD_Init();
LCD_WriteString(0,0,"msp430f149-ADC12");
LCD_WriteString(1,0,"XXX");
LCD_WriteString(2,0,"电压值: ");
P6SEL |= BIT0; //通道A0
ADC12CTL0 = ADC12ON + MSC + SHT0_7;
ADC12CTL1 = SHP + CONSEQ_2;
ADC12IE = 0x01;
ADC12CTL0 |= ENC;
ADC12CTL0 |= ADC12SC;
_EINT();
while(1)
{
LPM0;
}
}
#pragma vector = ADC_VECTOR
__interrupt void ADC12ISR(void)
{
LPM0_EXIT;
result[a++]= ADC12MEM0;
if(a == 63)
{
for(i=0;i<63;i++)
{
average += result[i];
}
average >>= 6; (除以64求平均值)
a = 0;
turn_value();
}
}
void turn_value()
{
dianya = average;
dianya = (dianya << 6) + (dianya<<5) +(dianya<<2); //乘以100
dianya = (dianya << 5) + average; //dianya = a * 33
dianya = (dianya << 3) + (dianya << 1); //dianya = dianya * 10(再次扩大了100倍,三位数)
dianya = dianya >> 12; //dianya = dianya / 4095 (2的十二次方)
ptr[0]=dianya/10000;
ptr[1]=dianya/1000%10;
ptr[2]=dianya/100%10;
ptr[3]=dianya%100/10;
ptr[4]=dianya%100%10;
//在液晶上显示变换后的结果
write_com(0x8C);
write_dat(ptr[0]+0x30);
write_dat(0x2e);
write_dat(ptr[1]+0x30);
write_dat(ptr[2]+0x30);
write_dat(ptr[3]+0x30);
write_dat(ptr[4]+0x30);
write_word("V");
}