本帖最后由 xiaoqi976633690 于 2023-10-8 19:30 编辑
《LP-MSPM0L1306开发板试用体验---板载光敏电阻+PTC热敏电阻实验》
1、开发环境
开发板:LP-MSPM0L1306 LaunchPad™ 开发套件
IDE:Code Composer Studio 12.4.0
仿真器:板载
例程参考自官方SDK\mspm0_sdk_1_10_01_05\examples\nortos\LP_MSPM0L1306\driverlib\adc12_sequence_conversion_LP_MSPM0L1306_nortos_gcc
2、硬件配置
注意:跳冒需要做一些变动
3、代码实现
PTC热敏电阻原理PTC (Positive Temperature Coefficient) 效应 , 即正温度系数效应,是指此材料的电阻会随温度的升高而增加。
我们通过ADC采集分压后的电压,电压变化有下面规律
热敏电阻对应表
TI.zip
(3.19 MB)
温度上升->阻值变大->分压电压升高
温度下降->阻值变下->分压电压下降
根据电阻的计算工具我们得到2个数组分别对应温度和不同温度对应的电压值(mv)
const int TMP6131DECT_PTC_TEMP_TABLE[166] = {
-40,-39, -38, -37, -36, -35, -34,-33, -32, -31,
-30, -29, -28,-27, -26, -25, -24, -23, -22,-21,
-20, -19, -18, -17, -16,-15, -14, -13, -12, -11,
-10,-9, -8, -7, -6, -5, -4, -3, -2, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125,
};
const int TMP6131DECT_PTC_VOTAGE_TABLE[166] = {
1305, 1310, 1316, 1321, 1326, 1331, 1336, 1342, 1347, 1352, /*-40~-31, */
1357, 1363, 1368, 1373, 1378, 1384, 1389, 1394, 1399, 1405, /*-30~-21, */
1410, 1415, 1420, 1426, 1431, 1436, 1442, 1447, 1452, 1458, /*-20~ -11, */
1463, 1468, 1473, 1479, 1484, 1489, 1495, 1500, 1505, 1510, /*-10~-1, */
1516, 1521, 1526, 1532, 1537, 1542, 1547, 1553, 1558, 1563, /*0~9, */
1568, 1574, 1579, 1584, 1589, 1595, 1600, 1605, 1610, 1615, /*10~19, */
1621, 1626, 1631, 1636, 1641, 1647, 1652, 1657, 1662, 1667, /*20~29, */
1672, 1678, 1683, 1688, 1693, 1698, 1703, 1708, 1713, 1718, /*30~39, */
1723, 1728, 1733, 1739, 1744, 1749, 1754, 1759, 1764, 1769, /*40~49, */
1774, 1778, 1783, 1788, 1793, 1798, 1803, 1808, 1813, 1818, /*50~59, */
1823, 1828, 1832, 1837, 1842, 1847, 1852, 1857, 1861, 1866, /*60~69, */
1871, 1876, 1881, 1885, 1890, 1895, 1899, 1904, 1909, 1913, /*70~79, */
1918, 1923, 1927, 1932, 1937, 1941, 1946, 1951, 1955, 1960, /*80~89*/
1964, 1969, 1973, 1978, 1982, 1987, 1991, 1996, 2000, 2005, /*90~99*/
2009, 2014, 2018, 2022, 2027, 2031, 2035, 2040, 2044, 2048, /*100~109*/
2053, 2057, 2061, 2066, 2070, 2074, 2078, 2083, 2087, 2091, /*110~119*/
2095, 2099, 2104, 2108, 2112, 2116, /*120~125*/
};
通过简单的函数就能实现将adc的电压值转换为温度
int get_temprature(int Votage)
{
int i = 0;
for (i = 0; i < 166; i++)
{
if (Votage<=TMP6131DECT_PTC_VOTAGE_TABLE[i])//当没有ADC值对应温度的时候,就取相近的,也就是大于的情况
{
return i;
}
}
return 0;
}
打印温度有2种方式
xprintf("get_temprature=%d℃\n",get_temprature(a)-40);//获取温度值方式1
xprintf("Tempreture=%d℃\n\n",TMP6131DECT_PTC_TEMP_TABLE[get_temprature(a)]);//获取温度值方式2
PTC光敏二极管原理
光敏二极管的特点是具有将光信号转换为电信号的功能,并且其光电流的大小与光照强度成正比,光照越强光电流越大
光敏二极管代码比较简单,只需要读取ADC的值就好了。
ADC实现
SYSCFG配置
获取ADC的值
uint16_t Get_ADC(DL_ADC12_MEM_IDX idx) //DL_ADC12_MEM_IDX_0
{
//uint16_t adcResult;
uint32_t adc_add=0;
uint8_t time=10;//采集次数
DL_ADC12_startConversion(ADC12_0_INST);
while (false == gCheckADC)
{
__WFE();
}
for (uint8_t i=0;i<time;i++)
{
delay_cycles((10));
adc_add+=DL_ADC12_getMemResult(ADC12_0_INST, idx);
gCheckADC = false;
DL_ADC12_enableConversions(ADC12_0_INST);
}
return adc_add/10;//返回10次平均值
}
ADC中断函数
void ADC12_0_INST_IRQHandler(void)
{
switch (DL_ADC12_getPendingInterrupt(ADC12_0_INST))
{
case DL_ADC12_IIDX_MEM1_RESULT_LOADED:
//case DL_ADC12_IIDX_MEM0_RESULT_LOADED:
gCheckADC = true;
break;
default:
break;
}
}
main函数
int main(void)
{
uint16_t adc_value = 0;
uint16_t adc_ch3=0;
uint16_t i=0;
int a = 0;
SYSCFG_DL_init();
NVIC_EnableIRQ(ADC12_0_INST_INT_IRQN);
NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);
NVIC_EnableIRQ(UART_0_INST_INT_IRQN);
DL_SYSCTL_disableSleepOnExit();
xdev_out(UART_0_write_Byte);//xprintf接口函数
gCheckADC = false;
while (1)
{
i++;
delay_cycles((32000));
adc_value = Get_ADC(DL_ADC12_MEM_IDX_0);
adc_ch3=Get_ADC(DL_ADC12_MEM_IDX_1);
//UART_0_write_str(str);
//UART_0_write_str("string send\n");
//xprintf("xprintf :%d\n\r", i);
if(adc_ch3>=3952) DL_GPIO_setPins(GPIOA, LED_GREEN_PIN_13_PIN);
else DL_GPIO_clearPins(GPIOA,LED_GREEN_PIN_13_PIN);
//if(adc_ch3>=3952) xprintf("adc_ch3=%d",adc_ch3);
DL_GPIO_clearPins(GPIOA,LED1_PIN_0_PIN );
if(i>500)
{
//delay_cycles((32000 * 1000));
i=0;
xprintf("ADC0_3 :%d,Voltge=%f\n", adc_ch3,adc_ch3 * (3.3 / 4095) * 1000);
xprintf("ADC0_2 :%d,Voltge=%f\n", adc_value,adc_value * (3.3 / 4095) * 1000);
a = adc_value * (3.3 / 4095) * 1000;
xprintf("get_temprature=%d℃\n",get_temprature(a)-40);//获取温度值方式1
//xprintf("Tempreture=%d℃\n\n",TMP6131DECT_PTC_TEMP_TABLE[get_temprature(a)]);//获取温度值方式2
}
DL_GPIO_setPins(GPIOA,LED1_PIN_0_PIN );
}
}
4、实验现象
串口实时打印2个ADC通道的数据(ADC值,电压,温度)
手遮住光敏传感器绿灯会亮起
哔哩哔哩演示视频
5、源码
ADC.zip
(202.57 KB)
|