开发版型号:SEED—MSP430F5529A
开发工具 :CCS 5.4v
以下内容是CCS中ADC12操作的一些简单的demo,现整理如下:
有关MSP430F5529芯片ADC12相关资料,可参考《MSP430x5xx and MSP430x6xx Family User's Guide (Rev. M)——Chapter 28 ADC12_A》
1:demo描述
Description: A single sample is made on A0 with reference to AVcc.
Software sets ADC12SC to start sample and conversion - ADC12SC
automatically cleared at EOC. ADC12 internal oscillator times sample (16x)
and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC12
conversion complete, ADC12_ISR will force exit from LPM0 in Mainloop on
reti. If A0 > 0.5*AVcc, P1.0 set, else reset.
参考AVcc电压、以A0定时器作为采样输入信号源的、单通道单次模式采样,当采样信号大于0.5*AVcc时,P1.0置位,否则为0。
代码如下:
#include <msp430.h>
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC12CTL0 = ADC12SHT02 + ADC12ON; // Sampling time, ADC12 on
ADC12CTL1 = ADC12SHP; // Use sampling timer
ADC12IE = 0x01; // Enable interrupt
ADC12CTL0 |= ADC12ENC;
P6SEL |= 0x01; // P6.0 ADC option select
P1DIR |= 0x01; // P1.0 output
while (1)
{
ADC12CTL0 |= ADC12SC; // Start sampling/conversion
__bis_SR_register(LPM0_bits + GIE); // LPM0, ADC12_ISR will force exit
__no_operation(); // For debugger
}
}
#pragma vector = ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
switch(__even_in_range(ADC12IV,34))
{
case 0: break; // Vector 0: No interrupt
case 2: break; // Vector 2: ADC overflow
case 4: break; // Vector 4: ADC timing overflow
case 6: // Vector 6: ADC12IFG0
if (ADC12MEM0 >= 0x7ff) // ADC12MEM = A0 > 0.5AVcc?
P1OUT |= BIT0; // P1.0 = 1
else
P1OUT &= ~BIT0; // P1.0 = 0
__bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
case 8: break; // Vector 8: ADC12IFG1
case 10: break; // Vector 10: ADC12IFG2
case 12: break; // Vector 12: ADC12IFG3
case 14: break; // Vector 14: ADC12IFG4
case 16: break; // Vector 16: ADC12IFG5
case 18: break; // Vector 18: ADC12IFG6
case 20: break; // Vector 20: ADC12IFG7
case 22: break; // Vector 22: ADC12IFG8
case 24: break; // Vector 24: ADC12IFG9
case 26: break; // Vector 26: ADC12IFG10
case 28: break; // Vector 28: ADC12IFG11
case 30: break; // Vector 30: ADC12IFG12
case 32: break; // Vector 32: ADC12IFG13
case 34: break; // Vector 34: ADC12IFG14
default: break;
}
}
复制代码
2:代码分析
1)主函数中代码:
ADC12CTL0 = ADC12SHT02 + ADC12ON; // Sampling time, ADC12 on
ADC12CTL1 = ADC12SHP; // Use sampling timer
ADC12IE = 0x01; // Enable interrupt
ADC12CTL0 |= ADC12ENC;
ADC12CTL0 |= ADC12SC; // Start sampling/conversion
复制代码
参考控制寄存器如下:
ADCSHT0x :采样保持定时器0,该四位定义了保存在转换存储寄存器中转换结果的采样周期;
ADC12ENC:转换允许
ADC12SHP:选择采样定时器A0输出
ADC12IE:中断允许 (来自中断允许寄存器)
2)中断部分代码:
中断寄存向量ADC12IV中的数字(0-36)用来判断中断标志寄存器中相关位是否置位,此时表明转换结果已经装入转换存储寄存器中,具体数字的含义参见下图:
3)
if (ADC12MEM0 >= 0x7ff) // ADC12MEM = A0 > 0.5AVcc?
P1OUT |= BIT0; // P1.0 = 1
else
P1OUT &= ~BIT0; // P1.0 = 0
复制代码
参考电压使用VCC,因为ADC精度为12位(4095),所以0x7FF(2047)就是0.5Vcc的AD值 |