void init_ADC(void)
{
P6SEL = 0X01;
ADC12CTL9 &= ~(ENC);
ADC12MCTL0 = INCH_0 + EOS;
ADC12CTL1 = 0X00;
ADC12CTL1 += CSTARTADD_0;
ADC12CTL1 += SHP;
ADC12CTL1 += CONSEQ_1;
ADC12CTL1 += ADC12SSEL_0;
ADC12CTL1 += ADC12DIV_0;
ADC12CTL0 = ADC12ON + REFON + REF2_5V + SHT0_6;
ADC12IE = 0;
ADC12IE |= ENC;
return;
}
void init_TimerA(void)
{
TACTL = TASSEL1 + TACLR;
TACTL += ID1;
TACTL += ID0;
CCTL0 = CCIE;
CCR0 = 500;
TACTL |= MC0;
return;
}
#pragma vector = TIMERA1_VECTOR
__interrupt void TimerA(void)
{
int results;
ADC12CTL0 &= ~ENC;
result = ADC12MEM0;
ADC _BUF[nADC_Count] = results;
nADC_Count += 1;
//采集40个点
if(nADC_Count == 40)
{
nADC_Flag = 1;
nADC_Count = 0;
}
ADC12CTL0 |= ENC + ADC12SC;
}
//把数据编程要显示的格式。然后写到lcd_Buf中去
void format_Data()
{
int result;
unsigned char tmp;
if(nADC_Flag == 1)
{
nADC_Flag = 0;
for(i=0;i<40;i++)
{
result += ADC_BUF[i];
}
}
result = (int)(((REFVOL*result)/4096-0.986)/0.0000355);
for(tmp=0;tmp<7;tmp++)
{
lcd_Buf[tmp] = result%10;
result = result/10;
}
}
#pragma vector = ADC_VECTOR
__interrupt void ADC_Interrupt(void)
{
formal_Data();
adc_Flag = 1;
}
显示子程序
const unsigned char NUM_LCD[16]={
0x7b,0x12,0x4f,0x1f,0x36,
0x3d,0x7d,0x13,0x7f,0x3f,
0x73,0x7c,0x69,0x5e,0x6f,
0x65};
unsigned char lcd_Buf[LCD_IN_USE];
void init_LCD(void)
{
char tmpv;
LCDCTL = LCDP2 + LCD4MUX + LCDON;
P5SEL = 0xFC;
BTCTL = BTSSEL + BT_ADLY_1000 + BTFRFQ1;
BTCTL |= 0x10;
P3DIR = 0xff;
P5SEL = 0xfc;
LCDCTL = LCDON + LCD4MUX + LCDP1;
for(tmpv = 0;tmpv<7;tmpv++)
{
LCDMEM[tmpv] = 0x00;
LCDMEM[7] = 0x02;
}
}
void lcd_Display()
{
char tmpv;
for(tmpv=0;tmpv<LCD_IN_USE-1;tmpv++)
{
LCDMEM[tmpv] = NUM_LCD[lcd_buf[tmpv]];
}
}
//设置小数点
void lcd_SetRP()
{
LCDMEM[2] |= 0x80;
}
//lcd初始化
void Adcvolt(void) //进行电压转换时ADC12的初始化
{
ADC12CTL0&=~ENC; //ENC 为低电平,设置AD控制寄存器
ADC12CTL0|=ADC12ON+MSC; //打开ADC12,可以进行AD 转换,参考电压选3.3V
ADC12CTL1=CSTARTADD_0+CONSEQ_1+SHP; //单通道单次转换,采样频率源自采样定时器
ADC12MCTL0=INCH_0; //选择模拟输入通道2
ADC12IE|=BIT0; //AD 转换中断允许
ADC12CTL0|=ENC; //转换允许
ADC12CTL0|=ADC12SC; //开始A/D 转换
}
//A/D 转换中断子程序
void ADC(void) //A/D 转换中断子程序
{
ADresult0=ADC12MEM0; //转换结果寄存器给了变量ADresult0
Volttem0=((long)ADresult0*33)/4095; //计算实际电压值
ADresult1=ADC12MEM1; //转换结果寄存器给了变量ADresult1
Volttem1=((long)ADresult1*33)/4095; //计算实际电压值
Volt0=Volttem0;
Volt1=Volttem1;
}
//TIMERA中断子程序
void Timer_A(void) //TIMERA中断子程序
{
data0++;
if(data0>=30)
data1=1;
else
data1=0;
if (data0>=60)
data0=0;
P3OUT ^= BIT3;
ADC12CTL0|=ADC12SC; //每隔一定时间进行一次转换
}
|