MSP430F149的ADC12模块
MSP430F149的ADC12模块
转自:http://hi.baidu.com/ms%C9%AD%C1%D6/blog/item/2fd0d355e0e71f59faf2c099.html
1 概述
MSP430F149的ADC12为SAR型12位AD,共有16路输入通道,其中8路独立的外部输入通道,2路接外部Vref+,Vref-,3路内部通道可分别测内部温度传感器、AVCC、和外部参考源。
P6口第二功能为AD输入端。MSP430F149的10、11分别接外部电压参考源正负极,7脚可将内部电压参考源输出。
ADC12共有18个中断源,公用一个中断向量ADC12_VECTOR。
AD的参考源可选择内部电压参考源或外部电压参考源。内部电压参考源有1.5V,2.5V可选,使用时向ADC12CTL0写入REFON+ REF2_5V就打开了2.5V。外部电压参考源由REF+接入。上电时若不设置参考源,则参考源为系统供电电压3.3V。
2 使用方法概述
2.1程序架构
中断方式
1、设置 ADC12工作模式,启动转换,开全局中断,等待中断
2、写中断处理函数
查询方式
设置ADC12工作模式,启动转换,查询中断标志ADC12IFG
while (!(0x01 & ADC12IFG));转换完毕读取采样值,系统自动清除中断标志
2.2 使用概述
主要参数配置
设置工作方式:sing\\sequence\\re-sing\\re-sequencd;
设置转换时间:SHTX
设置触发方式:ADC12SC\\MSC\\TimerA\\ TimerB
设置通道:外部通道\\内部Temperature sensor
设置参考源:系统电压\\内部参考源\\外部参考源
其他细节配置
一般要配置采样转换模式为脉冲(SHP),打开ADC12(ADC12ON),使能ADC12转换(ENC),使能中断(如果采取中断模式),触发转换(若采用ADC12SC触发)。
解释
ADC12模数转换是在SHI的上升沿初始化的。SHI信号有四个来源: The ADC12SC bit;The Timer_A Output Unit 1; The Timer_B Output Unit 0; The Timer_B Output Unit 1。故单次采样时只需要每次设置ADC12CTL0 |= ADC12SC就采样一次;重复采样时,如Rep-sing,设置ADC12CTL1 = SHS_1 +CONSEQ_2就选择了Rep-sing模式,每次采样通过定时器A触发。
3 相关寄存器
1、ADC12CTL0
SHT1x Bits
Sample-and-hold time. These bits define the number of ADC12CLK cycles in
the sampling period for registers ADC12MEM8 to ADC12MEM15.
SHT0x Bits
Sample-and-hold time. These bits define the number of ADC12CLK cycles in
the sampling period for registers ADC12MEM0 to ADC12MEM7.
ADC12ON
0 ADC12 off
1 ADC12 on
ENC
ENC Bit 1 Enable conversion
0 ADC12 disabled
1 ADC12 enabled
ADC12SC Bit 0 Start conversion. Software-controlled sample-and-conversion start.
ADC12SC and ENC may be set together with one instruction. ADC12SC is
reset automatically.
0 No sample-and-conversion-start
1 Start sample-and-conversion
MSC Bit 7 Multiple sample and conversion. Valid only for sequence or repeated modes.
0 The sampling timer requires a rising edge of the SHI signal to trigger
each sample-and-conversion.
1 The first rising edge of the SHI signal triggers the sampling timer, but
further sample-and-conversions are performed automatically as soon
as the prior conversion is completed.
2、ADC12CTL1
SHP
SHP Bit 9 Sample-and-hold pulse-mode select. This bit selects the source of the
sampling signal (SAMPCON) to be either the output of the sampling timer or
the sample-input signal directly.
0 SAMPCON signal is sourced from the sample-input signal.
1 SAMPCON signal is sourced from the sampling timer.
SHSx Bits
11-10
Sample-and-hold source select
00 ADC12SC bit
01 Timer_A.OUT1
10 Timer_B.OUT0
11 Timer_B.OUT1
CONSEQx Bits
2-1
Conversion sequence mode select
00 Single-channel, single-conversion
01 Sequence-of-channels
10 Repeat-single-channel
11 Repeat-sequence-of-channels
3、ADC12IE
ADC12IEx Bits
15-0
Interrupt enable. These bits enable or disable the interrupt request for the ADC12IFGx bits.
0 Interrupt disabled
1 Interrupt enabled
4、ADC12IFG
ADC12IFGx Bits
15-0
ADC12MEMx Interrupt flag. These bits are set when corresponding
ADC12MEMx is loaded with a conversion result. The ADC12IFGx bits are
reset if the corresponding ADC12MEMx is accessed, or may be reset with
software.
0 No interrupt pending
1 Interrupt pending
5、ADC12MCTLX
SREFx Bits
6-4
Select reference
000 VR+ = AVCC and VR− = AVSS
001 VR+ = VREF+ and VR− = AVSS
010 VR+ = VeREF+ and VR− = AVSS
011 VR+ = VeREF+ and VR− = AVSS
100 VR+ = AVCC and VR− = VREF−/ VeREF−
101 VR+ = VREF+ and VR− = VREF−/ VeREF−
110 VR+ = VeREF+ and VR− = VREF−/ VeREF−
111 VR+ = VeREF+ and VR− = VREF−/ VeREF−
INCHx Bits
3-0
Input channel select
0000 A0
0001 A1
0010 A2
0011 A3
0100 A4
0101 A5
0110 A6
0111 A7
1000 VeREF+
1001 VREF−/VeREF−
1010 Temperature sensor
1011 (AVCC – AVSS) / 2
1100 (AVCC – AVSS) / 2
1101 (AVCC – AVSS) / 2
1110 (AVCC – AVSS) / 2
1111 (AVCC – AVSS) / 2
EOS Bit 7 End of sequence. Indicates the last conversion in a sequence.
0 Not end of sequence
1 End of sequence
4实例
4.1 single采样,参考电源为系统电源
1.设置ADC12CTL0,使ADC12通道0采样保持时间为16 ADC12CLK(SHT0_2),开启ADC12模块(ADC12ON);
2.设置ADC12CTL1,选择采样保持脉冲模式即SAMPCON为采样定时器(SHP)
3.设置ADC12IE,是通道0中断使能(0x01);
4.设置ADC12CTL0,使能AD转换(ENC)
5.设置模拟信号输入IO口P60
7.设置ADC12CTL0,开启AD转换(ADC12SC),等待中断
8.中断中读取通道0转换值ADC12MEM0
ADC12CTL0 = SHT0_2 + ADC12ON; // Set sampling time, turn on ADC12
ADC12CTL1 = SHP; // Use sampling timer
ADC12IE = 0x01; // Enable interrupt
ADC12CTL0 |= ENC; // Conversion enabled
P6SEL |= 0x01; // P6.0 ADC option select
P2DIR |= 0x01;
ADC12CTL0 |= ADC12SC;
#pragma vector=ADC12_VECTOR
__interrupt void ADC12_ISR (void)
{
if (ADC12MEM0 < 0x7FF)
P2OUT = 0; // Clear P1.0 LED off
else
P2OUT = 0XFF; // Set P1.0 LED on
__low_power_mode_off_on_exit();
// 与上面等价_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bit from 0(SR)
}
4.2 single采样参考源为2.5V
在ADC12CTL0中设置参考源
在ADC12MCTL0中为通道0选择参考源
ADC12CTL0 = ADC12ON+SHT0_2+REFON+REF2_5V; // Turn on and set up ADC12
ADC12CTL1 = SHP; // Use sampling timer
ADC12MCTL0 = SREF_1; // Vr+=Vref+
for ( i=0; i<0x3600; i++); // Delay for reference start-up
ADC12CTL0 |= ENC;
while (1)
{
ADC12CTL0 |= ADC12SC; // Start conversion
while ((ADC12IFG & BIT0)==0);
_NOP(); // SET BREAKPOINT HERE
}
4.3 Repeat-single采样,模拟输入为内部Temperature sensor
设置ADC12CTL1,采样保持源为定时器A,脉冲保持模式,Repeat-single模式
ADC12CTL1 = SHS_1 + SHP + CONSEQ_2; // TA trig., rpt conv.
设置ADC12MCTL0,通道0参考源为内部REF,模拟输入通道0选择为Temperature sensor
ADC12MCTL0 = SREF_1 + INCH_10; // Channel A10, Vref+
ADC12IE = 0x01; // Enable ADC12IFG.0
ADC12CTL0 = SHT0_8 + REF2_5V + REFON + ADC12ON + ENC; // Config ADC12
TACCTL1 = OUTMOD_4; // Toggle on EQU1 (TAR = 0)
TACTL = TASSEL_2 + MC_2; // SMCLK, cont-mode
while (!(0x01 & ADC12IFG)); // First conversion?
FirstADCVal = ADC12MEM0; // Read out 1st ADC value
_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
if (ADC12MEM0 <= FirstADCVal + ADCDeltaOn)
P1OUT &= ~0x01; // LED off
else P1OUT |= 0x01; // LED on
}
摄氏温度和温度传感器电压转换关系:0摄氏度对应986mv,1摄氏度温差对应1.97mv温差
// oF = ((x/4096)*1500mV)-923mV)*1/1.97mV = x*761/4096 - 468
// IntDegF = (ADC12MEM0 - 2519)* 761/4096
IntDegF = (temp - 2519) * 761;
IntDegF = IntDegF / 4096;
// oC = ((x/4096)*1500mV)-986mV)*1/3.55mV = x*423/4096 - 278
// IntDegC = (ADC12MEM0 - 2692)* 423/4096
IntDegC = (temp - 2692) * 423;
IntDegC = IntDegC / 4096;
4.4 Repeat-sequence mode
Sequence模式时可以设置多个采样通道。在最后一个通道加上EOS就表明的采样通道结束位置。中断允许只需要设置最后一个通道。
为了采样速率尽可能快,可设置MSC,此时当SHI上升沿触发第一次采样后,后面的采样在上一次采样结束后自动进行。
ADC12CTL0 = ADC12ON+MSC+SHT0_8; // Turn on ADC12, extend sampling time
// to avoid overflow of results
ADC12CTL1 = SHP+CONSEQ_3; // Use sampling timer, repeated sequence
ADC12MCTL0 = INCH_0; // ref+=AVcc, channel = A0
ADC12MCTL1 = INCH_1; // ref+=AVcc, channel = A1
ADC12MCTL2 = INCH_2; // ref+=AVcc, channel = A2
ADC12MCTL3 = INCH_3+EOS; // ref+=AVcc, channel = A3, end seq.
ADC12IE = 0x08; // Enable ADC12IFG.3
ADC12CTL0 |= ENC; // Enable conversions
ADC12CTL0 |= ADC12SC; // Start conversion
_BIS_SR(LPM0_bits + GIE); // Enter LPM0, Enable interrupts
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
static unsigned int index = 0;
A0results[index] = ADC12MEM0; // Move A0 results, IFG is cleared
A1results[index] = ADC12MEM1; // Move A1 results, IFG is cleared
A2results[index] = ADC12MEM2; // Move A2 results, IFG is cleared
A3results[index] = ADC12MEM3; // Move A3 results, IFG is cleared
index = (index+1)%Num_of_Results; // Increment results index, modulo; Set Breakpoint here
}