本帖最后由 yin_wu_qing 于 2024-3-2 23:39 编辑
根据宣传海报可知,LP-MSPM0L1306 LaunchPad开发套件是基于MSPM0L1306的易用型评估模块, 包含在MSPM0L1306 M0+ MCU 平台上开始开发所需要的全部资源,包括用于编程、调试和能量测量的板载调试探针。 该板包含三个按钮、两个LED(其中一个是RGB LED)、模拟温度传感器和光传感器。此次就板载的“TMP6131DECT”温度传感器进行检测。
通过上方的电路原理图可知,如果需要对温度传感器进出检测,则需要将J15通过跳线帽短接,形成电阻上拉部分电路。于此同时,还需要将J1的1、2脚短接在一起,这样才能将温度传感器连接到电路中。如果短接2、3脚,则是连接到光敏传感器。
关于这款温度传感器,TI官方提供的数据手册如下:
如何使用该温度传感器,需要借助ADC转换接口,TI专门提供了设计工具,使用该工具可设计多款传感器的基本配置代码。
这里使用“Thermistor Design Tool - Voltage Bias”文档来设置,如下图所示,设置好对应的参数,该工具可得出转换接口函数。
通过CCS从SDK中导出“adc_to_uart_LP_MSPM0L1306_nortos_ticlang”工程,然后双击工程中的“adc_to_uart.syscfg”文件,则可调出sysconfig GUI界面,将ADC的GPIO管脚设置成PA15。
部分的参考代码如下:
编译后自动生成的“ti_msp_dl_config.c”源码如下:
#include "ti_msp_dl_config.h"
/*
* ======== SYSCFG_DL_init ========
* Perform any initialization needed before using any board APIs
*/
SYSCONFIG_WEAK void SYSCFG_DL_init(void)
{
SYSCFG_DL_initPower();
SYSCFG_DL_GPIO_init();
/* Module-Specific Initializations*/
SYSCFG_DL_SYSCTL_init();
SYSCFG_DL_UART_0_init();
SYSCFG_DL_ADC12_0_init();
}
SYSCONFIG_WEAK void SYSCFG_DL_initPower(void)
{
DL_GPIO_reset(GPIOA);
DL_UART_Main_reset(UART_0_INST);
DL_ADC12_reset(ADC12_0_INST);
DL_GPIO_enablePower(GPIOA);
DL_UART_Main_enablePower(UART_0_INST);
DL_ADC12_enablePower(ADC12_0_INST);
delay_cycles(POWER_STARTUP_DELAY);
}
SYSCONFIG_WEAK void SYSCFG_DL_GPIO_init(void)
{
DL_GPIO_initPeripheralOutputFunction(GPIO_UART_0_IOMUX_TX, GPIO_UART_0_IOMUX_TX_FUNC);
}
SYSCONFIG_WEAK void SYSCFG_DL_SYSCTL_init(void)
{
//Low Power Mode is configured to be SLEEP0
DL_SYSCTL_setBORThreshold(DL_SYSCTL_BOR_THRESHOLD_LEVEL_0);
DL_SYSCTL_setSYSOSCFreq(DL_SYSCTL_SYSOSC_FREQ_BASE);
DL_SYSCTL_setMCLKDivider(DL_SYSCTL_MCLK_DIVIDER_DISABLE);
}
static const DL_UART_Main_ClockConfig gUART_0ClockConfig = {
.clockSel = DL_UART_MAIN_CLOCK_BUSCLK,
.divideRatio = DL_UART_MAIN_CLOCK_DIVIDE_RATIO_1
};
static const DL_UART_Main_Config gUART_0Config = {
.mode = DL_UART_MAIN_MODE_NORMAL,
.direction = DL_UART_MAIN_DIRECTION_TX,
.flowControl = DL_UART_MAIN_FLOW_CONTROL_NONE,
.parity = DL_UART_MAIN_PARITY_NONE,
.wordLength = DL_UART_MAIN_WORD_LENGTH_8_BITS,
.stopBits = DL_UART_MAIN_STOP_BITS_ONE
};
SYSCONFIG_WEAK void SYSCFG_DL_UART_0_init(void)
{
DL_UART_Main_setClockConfig(UART_0_INST, (DL_UART_Main_ClockConfig *) &gUART_0ClockConfig);
DL_UART_Main_init(UART_0_INST, (DL_UART_Main_Config *) &gUART_0Config);
/*
* Configure baud rate by setting oversampling and baud rate divisors.
* Target baud rate: 115200
* Actual baud rate: 115211.52
*/
DL_UART_Main_setOversampling(UART_0_INST, DL_UART_OVERSAMPLING_RATE_16X);
DL_UART_Main_setBaudRateDivisor(UART_0_INST,UART_0_IBRD_32_MHZ_115200_BAUD,
UART_0_FBRD_32_MHZ_115200_BAUD);
/* Configure FIFOs */
DL_UART_Main_enableFIFOs(UART_0_INST);
DL_UART_Main_setTXFIFOThreshold(UART_0_INST, DL_UART_TX_FIFO_LEVEL_1_2_EMPTY);
DL_UART_Main_enable(UART_0_INST);
}
/* ADC12_0 Initialization */
static const DL_ADC12_ClockConfig gADC12_0ClockConfig = {
.clockSel = DL_ADC12_CLOCK_ULPCLK,
.divideRatio = DL_ADC12_CLOCK_DIVIDE_8,
.freqRange = DL_ADC12_CLOCK_FREQ_RANGE_24_TO_32,
};
SYSCONFIG_WEAK void SYSCFG_DL_ADC12_0_init(void)
{
DL_ADC12_setClockConfig(ADC12_0_INST, (DL_ADC12_ClockConfig *) &gADC12_0ClockConfig);
DL_ADC12_configConversionMem(ADC12_0_INST, ADC12_0_ADCMEM_0,
DL_ADC12_INPUT_CHAN_9, DL_ADC12_REFERENCE_VOLTAGE_VDDA, DL_ADC12_SAMPLE_TIMER_SOURCE_SCOMP0, DL_ADC12_AVERAGING_MODE_DISABLED,
DL_ADC12_BURN_OUT_SOURCE_DISABLED, DL_ADC12_TRIGGER_MODE_AUTO_NEXT, DL_ADC12_WINDOWS_COMP_MODE_DISABLED);
DL_ADC12_setSampleTime0(ADC12_0_INST,500);
/* Enable ADC12 interrupt */
DL_ADC12_clearInterruptStatus(ADC12_0_INST,(DL_ADC12_INTERRUPT_MEM0_RESULT_LOADED));
DL_ADC12_enableInterrupt(ADC12_0_INST,(DL_ADC12_INTERRUPT_MEM0_RESULT_LOADED));
DL_ADC12_enableConversions(ADC12_0_INST);
}
“adc_to_uart.c”代码实现如下,这里将uart发送接口函数改成printf打印,这样可以在CCS中的Console窗口实时打印输出温度传感器采样的温度值。
#include "ti_msp_dl_config.h"
#include <stdio.h>
#include <math.h>
volatile bool gCheckADC;
volatile uint16_t gADCResult;
volatile float temperature;
float VBias = 3.30;
unsigned int ADC_BITS = 4096;
float VTEMP = 0;
float THRM_TEMP = 0;
float Thermistor(int raw_ADC)
{
// THRM calculations - 4th order polynomial regression
VTEMP = 0;
int THRM_ADC = raw_ADC;
float THRM_A0 = -4.232811E+02 ;
float THRM_A1 = 4.728797E+02 ;
float THRM_A2 = -1.988841E+02 ;
float THRM_A3 = 4.869521E+01 ;
float THRM_A4 = -1.158754E+00 ;
VTEMP = (VBias/ADC_BITS) * THRM_ADC;
THRM_TEMP =(THRM_A4 * powf( VTEMP,4)) + (THRM_A3 * powf( VTEMP,3)) + (THRM_A2 * powf( VTEMP,2)) + (THRM_A1 * VTEMP) + THRM_A0;
return THRM_TEMP;
}
int main(void)
{
SYSCFG_DL_init();
NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN);
gCheckADC = false;
while (1) {
DL_ADC12_startConversion(ADC12_0_INST);
while (false == gCheckADC) {
__WFE();
}
/*
* UART is 8 bits and the ADC result is 16 bits
* Split the ADC result into 2 then send via UART.
*/
gADCResult = DL_ADC12_getMemResult(ADC12_0_INST, DL_ADC12_MEM_IDX_0);
// uint8_t lowbyte = (uint8_t)(gADCResult & 0xFF);
// uint8_t highbyte = (uint8_t)((gADCResult >> 8) & 0xFF);
temperature = Thermistor(gADCResult);
printf("ADC=%d, temperature=%.2f\n", gADCResult, temperature);
// DL_UART_Main_transmitData(UART_0_INST, highbyte);
// DL_UART_Main_transmitData(UART_0_INST, lowbyte);
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;
}
}
开发板的硬件连接如下图所示,其中需注意J1处的跳线帽设置。
将编译后的程序下载到开发板后,即可观察到温度传感器实时采样的温度值,此时此刻南方的温度感觉有点冷。如果将手指靠近TMP6131,温度值会立马上升,足见其灵敏度比较高。此次分享内容告一段落,感谢来访阅读,欢迎留言交流。