学习MCU外设操作首先要知道相应外设的寄存器有哪些和什么作用:
ADC10 Registers
ADC10 input enable register 0 ADC10AE0 Read/write 04Ah Reset with POR
ADC10 input enable register 1 ADC10AE1 Read/write 04Bh Reset with POR
ADC10 control register 0 ADC10CTL0 Read/write 01B0h Reset with POR
ADC10 control register 1 ADC10CTL1 Read/write 01B2h Reset with POR
ADC10 memory ADC10MEM Read 01B4h Unchanged
ADC10 data transfer control register 0 ADC10DTC0 Read/write 048h Reset with POR
ADC10 data transfer control register 1 ADC10DTC1 Read/write 049h Reset with POR
ADC10 data transfer start address ADC10SA Read/write 01BCh 0200h with POR
至于这些外设的工具配置,我就不截图列出来了,上传一份网友整理的ADC外设说明见附件
分享一份ADC的源码:配置P1.1为ADC功能,主要功能是实现3.3V时LED1亮,否则灭。
#include "msp430g2231.h"
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // 转换时钟选择 ADC10ON(打开ADC10), interrupt enabled(使能中断)
ADC10CTL1 = INCH_1; // input A1(选择输入通道A1,对应P1.1口)
ADC10AE0 |= BIT1; // PA.1 ADC option select (使能P1.1模拟输入)
P1DIR |= BIT0; // Set P1.0 to output direction (P1.0为输出模式)
P1OUT&=~BIT0;
for (;;)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start (ADC10开始采样转换)
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit (开总中断,低功耗模式)
/*******翻转P1.0口状态******/
if (ADC10MEM < 0x3FF)
P1OUT &= ~BIT0; // Clear P1.0 LED off
else
P1OUT |= BIT0; // Set P1.0 LED on
}
}
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
} |