msp430g2553有8个外部通道可以输入,下面将完成3路输入控制3个LED亮灭的程序。
寄存器说明
msp430g2553的ADC10寄存器
代码说明
在熟悉adc10的几个寄存器后,来看代码
1、ADC10CTL1 = INCH_5+CONSEQ_1;控制寄存器设置了最高的通道和转换模式。
2、ADC10AE0|= BIT5+BIT4+BIT3;输入使能寄存器设置了要输入的引脚。
3、采样次数和存放位置:
ADC10DTC1 = 0x03;设置采样次数
unsigned int a[]={0};
ADC10SA = (unsigned int)a; 存放地址
若p1.5,p1.4,p1.3输入,采样3次,通道序列单次(转换模式)
则a[0]存放p1.5采样结果,则a[1]存放p1.4采样结果,则a[2]存放p1.3采样结果
若p1.5,p1.4,p1.3输入,采样6次,通道序重复(转换模式)
则a[0]存放p1.5采样结果,则a[1]存放p1.4采样结果,则a[2]存放p1.3采样结果
则a[3]存放p1.5采样结果,则a[4]存放p1.4采样结果,则a[5]存放p1.3采样结果
eg:4通道4次重复采样
unsigned int adc[4];
unsigned int adc_temp[16];
for(i=0;i<4;i++)
adc[i]=(adc_temp[i]+adc_temp[i+4]+adc_temp[i+8]+adc_temp[i+12])>>2;除4
#include <msp430.h>
unsigned int a[]={0};
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC10CTL1 = INCH_5+CONSEQ_1; // A5/A4, single sequence
ADC10CTL0 = ADC10SHT_1 + MSC + ADC10ON +ADC10IE;
ADC10DTC1 = 0x03; // 2 conversions
ADC10AE0|= BIT5+BIT4+BIT3; // P1.3,2,1 ADC10 option select
P1DIR |= BIT0+BIT1+BIT2; // Set P1.0 output
for(;;)
{
ADC10CTL0 &= ~ENC;
ADC10SA = (unsigned int)a; // Data buffer start
while (ADC10CTL1 & ADC10BUSY); // Wait if ADC10 core is active
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
if (a[0] < 0x1FF)
P1OUT &= ~0x01; // Clear P1.0 LED off
else
P1OUT |= 0x01; // Set P1.0 LED on
if (a[1] < 0x1FF)
P1OUT &= ~BIT1; // Clear P1.1 LED off
else
P1OUT |= BIT1; // Set P1.1 LED on
if (a[2] < 0x1FF)
P1OUT &= ~BIT2; // Clear P1.2 LED off
else
P1OUT |= BIT2; // Set P1.2 LED on
}
}
// ADC10 interrupt service routine
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(ADC10_VECTOR))) ADC10_ISR (void)
#else
#error Compiler not supported!
#endif
{
}