MSP430F149 ADC代码
时间: 2023-12-20 14:29:29 浏览: 93
msp430f149_code
以下是MSP430F149单片机ADC代码的示例:
```c
#include <msp430f149.h>
void ADC_Init(void)
{
ADC12CTL0 = ADC12ON + SHT0_2 + REFON + REF2_5V; //开启ADC12,设置采样保持时间,开启内部参考电压和2.5V参考电压
ADC12CTL1 = SHP + CONSEQ_0 + ADC12DIV_0 + ADC12SSEL_0; //设置采样方式为单通道单次转换,时钟源为ADC12OSC,时钟分频为1
ADC12MCTL0 = SREF_1 + INCH_0; //设置参考电压为AVCC和AVSS,选择A0通道作为输入
ADC12IE = 0x01; //开启ADC12中断
ADC12CTL0 |= ENC; //使能ADC12转换
}
#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) //如果采样值大于2047
P1OUT |= BIT0; //点亮LED
else
P1OUT &= ~BIT0; //熄灭LED
__bic_SR_register_on_exit(LPM0_bits); //退出低功耗模式
break;
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;
}
}
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; //停用看门狗定时器
P1DIR |= BIT0; //将P1.0设置为输出
P1OUT &= ~BIT0; //熄灭LED
ADC_Init(); //初始化ADC
while(1)
{
__delay_cycles(1000); //延时
ADC12CTL0 |= ADC12SC; //开始采样
__bis_SR_register(LPM0_bits + GIE); //进入低功耗模式并开启全局中断
}
}
```
阅读全文