//同样的,使用lcd1602显示,这里不再多述其程序了,前面有
#include "lcd1602.h"
#define Num_of_Results 32
static uint results[Num_of_Results];
uchar a[10]="0123456789";
uchar b[16]="temperatu ";
unsigned int ad_value[3];
void main()
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
//主系统时钟切换为外部高速晶振
if (CALBC1_8MHZ == 0xFF || CALDCO_8MHZ == 0xFF)
{
while(1); // If calibration constants erased, trap CPU!!
}
// Configure Basic Clock
BCSCTL1 = CALBC1_8MHZ; // Set range
DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation*/
P1SEL |= BIT0+BIT1+BIT2; // p1.0为A0
P1DIR&=~(BIT0+BIT1+BIT2);// p1.0为输入
ADC10CTL0= ADC10ON+REFON+ADC10SHT_3+MSC+ADC10IE+SREF_1;
// ADC12ON打开ADC;
//ADC10SHT_3设置采样时间,合适即可
//MSC设置多次采样
// 中断允许,IFG置位后会进入中断服务程序
ADC10CTL1= CONSEQ_3+INCH_10;
//CONSEQ_2单通道多次转换
// INCH_10选择内部通道A10
ADC10CTL0&=~ADC10IFG;//清除中断标志位
_EINT();//打开中断
init_lcd();
delay(10);
while(1)
{
while (ADC10CTL1 & BUSY);// Wait if ADC10 core is active
ADC10CTL0 |= ENC+ADC10SC;//使能转换并且开始转换
_NOP();
}
}
//ADC中断服务函数,在这里用多次平均的
#pragma vector=ADC10_VECTOR
__interrupt void ADC10ISR (void)
{
uint i;
uchar ptr[4];
P2SEL=0X00;
static uint index = 0;
results[index++] =ADC10MEM;
if(index == Num_of_Results)
{
unsigned long sum = 0,real;
index = 0;
for(i = 0; i < Num_of_Results; i++)
{
sum += results;
}
sum >>= 5; //除以32求得平均值
real=((1.5*sum/1023)-0.986)/0.00355;//扩大1000倍
ptr[0] = real / 100;
ptr[1] = (real - ptr[0]*100)/10;
ptr[2] = (real - ptr[0]*100 - ptr[1]*10);
for(i=0;i<3;i++)
b[i+11]=a[ptr];
_NOP();
display_string(b,0xc0);
_NOP();
}
ADC10CTL0&=~ADC10IFG;//清除中断标志位
}
好久没有碰430了,过一段时间再拿出来好好玩一玩!