三个引脚,分别接3.3V,GND,ADC引脚。12位ADC有4096份,将360分成4096份,再乘以单片机返回的模拟量的值,即可得到角度。
M0代码
#include "ti_msp_dl_config.h"volatile bool gCheckADC;
volatile float gAdcResult;
float angle;int main(void)
{SYSCFG_DL_init();NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN);gCheckADC = false;while (1) {DL_ADC12_startConversion(ADC12_0_INST); //启动ADC转换while (false == gCheckADC){}gAdcResult = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_0); //返回所选内存索引的转换结果。//根据返回的gAdcResult计算出角度angleangle= (0.088*gAdcResult); //gAdcResult范围是0到4096,有4096格。求出每一格多少度,再乘以格数即可。gCheckADC = false; DL_ADC12_enableConversions(ADC12_0_INST); //允许转换}
}void ADC12_0_INST_IRQHandler(void)
{switch (DL_ADC12_getPendingInterrupt(ADC12_0_INST)) {case DL_ADC12_IIDX_MEM0_RESULT_LOADED:gCheckADC = true;break;default:break;}
}
相关配置