/*********************************************************
程序功能:MCU的片内ADC对P6.0端口的电压进行转换
将模拟电压值显示在1602液晶上。
*********************************************************/
#include <msp430x14x.h>
//typedef unsigned char uchar;
//typedef unsigned int uint;
#define Num_of_Results 32
#define uchar unsigned char
#define uint unsigned int
uint MAX=0;
uint index1=0,index2=0,A,flag=0x01;
static uint result1[Num_of_Results],result2[Num_of_Results]; //保存ADC转换结果的数组 // is not used for anything.
void Trans_val(uint Hex_Val);
/************************主函数****************************/
void main(void)
{
uint i;
WDTCTL = WDTPW+WDTHOLD; //关闭看门狗
BCSCTL1 &= ~XT2OFF; // XT2= HF XTAL
do
{
IFG1 &= ~OFIFG; // Clear OSCFault flag
for (i = 0xFF; i > 0; i--); // Time for flag to set
}
while ((IFG1 & OFIFG)); // OSCFault flag still set?
BCSCTL2 |= SELM_2+SELS;
P6DIR = 0XFF;
P6DIR |= BIT2;P6OUT |= BIT2; //关闭电平转换
P6SEL |= 0x01; // 使能ADC通道
P6SEL |= 0X01;
ADC12CTL0 = ADC12ON+SHT0_2+REFON+MSC+REF2_5V; // Turn on and set up ADC12
ADC12MCTL0 = SREF_1 + INCH_0; // Vr+=Vref+
ADC12MCTL1 = SREF_1 + INCH_1 + EOS;
ADC12CTL1 |= SHP+CONSEQ_3; // 使用采样定时器 /*
ADC12IE = 0x02; // 使能ADC中断
ADC12CTL0 |= ENC; // 使能转换
ADC12CTL0 |= ADC12SC; // 开始转换*/
CCTL0 |= CCIE;
TACTL = TASSEL_2 + MC_1+ TACLR + TAIE;
// CCR0 interrupt enabled
CCR0 = 1000-1;
// INCLK, upmode
_BIS_SR(LPM0_bits + GIE); // Enter LPM3 w/ interrupt
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
P1OUT^=0x01; // Toggle P1.0
}
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
uint temp;
if(flag==1)
{
result1[index1++] = ADC12MEM0; // Move results
if(index1 == Num_of_Results)
{
temp=MAX;
uchar i;
unsigned long temp= 0;
for(i=0;i<32;i++)
{
if(temp<result1
)
temp=result1;
}
MAX=temp;
index1=0;
}
}
else
{
result1[index2++]=ADC12MEM1;
if(index2 == Num_of_Results)
{
temp=MAX;
uchar i;
unsigned long temp= 0;
for(i=0;i<32;i++)
{
if(temp<result1)
temp=result1;
}
MAX=temp;
index2=0;
}
}
}
此帖出自小平头技术问答
一周热门 更多>