[开发工具] 【APM32F107VC-MINI开发板测评】串口打印热敏电阻值

[复制链接]
1980|20
 楼主| 比神乐 发表于 2023-3-6 12:22 | 显示全部楼层 |阅读模式
今天搞了一下AD采集热敏电阻值。
原理图:
3.jpg 4.jpg
代码:
  1. #include "main.h"

  2. /** @addtogroup Examples
  3.   @{
  4.   */

  5. /** @addtogroup ADC_ContinuousConversion
  6.   @{
  7.   */

  8. /** @defgroup ADC_ContinuousConversion_Macros Macros
  9.   @{
  10. */

  11. /* printf function configs to USART1 */
  12. #define DEBUG_USART  USART1

  13. /**@} end of group ADC_ContinuousConversion_Macros*/

  14. /** @defgroup ADC_ContinuousConversion_Functions Functions
  15.   @{
  16.   */

  17. void ADC_Init(void);
  18. void Delay(uint32_t count);

  19. /*!
  20. * [url=home.php?mod=space&uid=247401]@brief[/url]       Main program
  21. *
  22. * @param       None
  23. *
  24. * @retval      None
  25. *
  26. */
  27. int main(void)
  28. {
  29.     USART_Config_T USART_ConfigStruct;
  30.    
  31.     /* USART config */
  32.     USART_ConfigStruct.baudRate = 115200;
  33.     USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
  34.     USART_ConfigStruct.mode = USART_MODE_TX;
  35.     USART_ConfigStruct.parity = USART_PARITY_NONE;
  36.     USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
  37.     USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;

  38.     APM_MINI_COMInit(COM1, &USART_ConfigStruct);
  39.    
  40.     /* ADC1 initialization */
  41.     ADC_Init();
  42.    
  43.     while(1)
  44.     {
  45.                         printf("\r\n voltage : mV\r\n");
  46.     }
  47. }

  48. /*!
  49. * @brief     ADC Init
  50. *
  51. * @param     None
  52. *
  53. * @retval    None
  54. */
  55. void ADC_Init(void)
  56. {
  57.     GPIO_Config_T           gpioConfig;
  58.     ADC_Config_T            adcConfig;

  59.     /* Enable GPIOA clock */
  60.     RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);

  61.     /* ADC channel 0 configuration */
  62.     GPIO_ConfigStructInit(&gpioConfig);
  63.     gpioConfig.mode    = GPIO_MODE_ANALOG;
  64.     gpioConfig.pin     = GPIO_PIN_0;
  65.     GPIO_Config(GPIOA, &gpioConfig);

  66.     /* ADCCLK = PCLK2/4 */
  67.     RCM_ConfigADCCLK(RCM_PCLK2_DIV_4);
  68.    
  69.     /* Enable ADC clock */
  70.     RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_ADC1);

  71.     /* ADC configuration */
  72.     ADC_Reset(ADC1);
  73.     ADC_ConfigStructInit(&adcConfig);
  74.     adcConfig.mode                  = ADC_MODE_INDEPENDENT;
  75.     adcConfig.scanConvMode          = DISABLE;
  76.     adcConfig.continuosConvMode     = ENABLE;
  77.     adcConfig.externalTrigConv      = ADC_EXT_TRIG_CONV_None;
  78.     adcConfig.dataAlign             = ADC_DATA_ALIGN_RIGHT;
  79.     /* channel number */
  80.     adcConfig.nbrOfChannel          = 1;
  81.     ADC_Config(ADC1, &adcConfig);

  82.     /* ADC channel Convert configuration */
  83.     ADC_ConfigRegularChannel(ADC1, ADC_CHANNEL_0, 1, ADC_SAMPLETIME_13CYCLES5);
  84.    
  85.     /* Enable complete conversion interupt */
  86.     ADC_EnableInterrupt(ADC1, ADC_INT_EOC);

  87.     /* NVIC configuration */
  88.     NVIC_EnableIRQRequest(ADC1_2_IRQn, 1, 1);
  89.    
  90.     /* Enable ADC */
  91.     ADC_Enable(ADC1);

  92.     /* Enable ADC1 reset calibration register */
  93.     ADC_ResetCalibration(ADC1);
  94.     /* Check the end of ADC1 reset calibration register */
  95.     while(ADC_ReadResetCalibrationStatus(ADC1));

  96.     /* Start ADC1 calibration */
  97.     ADC_StartCalibration(ADC1);
  98.     /* Check the end of ADC1 calibration */
  99.     while(ADC_ReadCalibrationStartFlag(ADC1));

  100.     /* Start ADC1 Software Conversion */
  101.     ADC_EnableSoftwareStartConv(ADC1);
  102. }

  103. /*!
  104. * @brief       ADC1 Isr
  105. *
  106. * @param       None
  107. *
  108. * @retval      None
  109. *
  110. * [url=home.php?mod=space&uid=536309]@NOTE[/url]        This function need to put into ADC1_2_IRQHandler in apm32f10x_int.c
  111. */
  112. void ADC1_Isr(void)
  113. {
  114.     uint16_t adcData = 0;
  115.     uint16_t voltage = 0;
  116.    
  117.     if (ADC_ReadIntFlag(ADC1, ADC_INT_EOC) == SET)
  118.     {
  119.         ADC_ClearIntFlag(ADC1, ADC_INT_EOC);
  120.         /* Read ADC Conversion value */
  121.         adcData = ADC_ReadConversionValue(ADC1);
  122.         /* voltage(mV) = adcData * (3300mV / 4095) */
  123.         voltage = (adcData * 3300) / 4095;
  124.         printf("\r\n voltage : %d mV\r\n", voltage);
  125.     }
  126. }

  127. /*!
  128. * @brief     Delay
  129. *
  130. * @param     count:  delay count
  131. *
  132. * @retval    None
  133. */
  134. void Delay(uint32_t count)
  135. {
  136.     uint16_t i = 0;
  137.    
  138.     while (count--)
  139.     {
  140.         i = 7995;
  141.         while (i--);
  142.     }
  143. }

  144. #if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

  145. /*!
  146. * @brief       Redirect C Library function printf to serial port.
  147. *              After Redirection, you can use printf function.
  148. *
  149. * @param       ch:  The characters that need to be send.
  150. *
  151. * @param       *f:  pointer to a FILE that can recording all information
  152. *              needed to control a stream
  153. *
  154. * @retval      The characters that need to be send.
  155. *
  156. * @note
  157. */
  158. int fputc(int ch, FILE* f)
  159. {
  160.     /* send a byte of data to the serial port */
  161.     USART_TxData(DEBUG_USART, (uint8_t)ch);

  162.     /* wait for the data to be send */
  163.     while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

  164.     return (ch);
  165. }

  166. #elif defined (__GNUC__)

  167. /*!
  168. * @brief       Redirect C Library function printf to serial port.
  169. *              After Redirection, you can use printf function.
  170. *
  171. * @param       ch:  The characters that need to be send.
  172. *
  173. * @retval      The characters that need to be send.
  174. *
  175. * @note
  176. */
  177. int __io_putchar(int ch)
  178. {
  179.     /* send a byte of data to the serial port */
  180.     USART_TxData(DEBUG_USART, ch);

  181.     /* wait for the data to be send */
  182.     while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

  183.     return ch;
  184. }

  185. /*!
  186. * @brief       Redirect C Library function printf to serial port.
  187. *              After Redirection, you can use printf function.
  188. *
  189. * @param       file:  Meaningless in this function.
  190. *
  191. * @param       *ptr:  Buffer pointer for data to be sent.
  192. *
  193. * @param       len:  Length of data to be sent.
  194. *
  195. * @retval      The characters that need to be send.
  196. *
  197. * @note
  198. */
  199. int _write(int file, char* ptr, int len)
  200. {
  201.     int i;
  202.     for (i = 0; i < len; i++)
  203.     {
  204.         __io_putchar(*ptr++);
  205.     }

  206.     return len;
  207. }

  208. #else
  209. #warning Not supported compiler type
  210. #endif

  211. /**@} end of group ADC_ContinuousConversion_Functions */
  212. /**@} end of group ADC_ContinuousConversion */
  213. /**@} end of group Examples */
效果图:
不摸热敏电阻
0.jpg
用手摸热敏电阻
1.jpg
2.jpg
tpgf 发表于 2023-4-3 14:16 | 显示全部楼层
这种热敏电阻有没有一个合适的计算公式呀  目前只能查到一些点位的阻值
aoyi 发表于 2023-4-3 15:38 | 显示全部楼层
看采集的数据还是非常稳定的  楼主使用的哪种滤波啊
nawu 发表于 2023-4-3 15:52 | 显示全部楼层
这样飞线居然还能采集到如此稳定的数据
zljiu 发表于 2023-4-3 16:37 | 显示全部楼层
这种信号一般变化都是比较缓慢的 如果采集不准那才是有问题
gwsan 发表于 2023-4-3 16:58 | 显示全部楼层
NTC的阻值跨度还是很大的  仅仅只用一个电阻分压采集 在某些温度段会不会不准确呢
tfqi 发表于 2023-4-3 17:17 | 显示全部楼层
gwsan 发表于 2023-4-3 16:58
NTC的阻值跨度还是很大的  仅仅只用一个电阻分压采集 在某些温度段会不会不准确呢 ...

其实肯定存在这个情况 但是在我们正常使用的范围内 偏差不会有多大的
caigang13 发表于 2023-4-4 07:48 来自手机 | 显示全部楼层
ADC采样值有偏差很正常,只要在正常范围内。
 楼主| 比神乐 发表于 2023-4-4 09:59 | 显示全部楼层
tpgf 发表于 2023-4-3 14:16
这种热敏电阻有没有一个合适的计算公式呀  目前只能查到一些点位的阻值

借用新定义的AD滤波方法
albertaabbot 发表于 2023-4-8 21:53 | 显示全部楼层
可以实现虚拟串口的吗?              
gygp 发表于 2023-4-8 22:11 | 显示全部楼层
dma采样的吗?              
sesefadou 发表于 2023-4-8 22:17 | 显示全部楼层
自带ADC的精度怎么样              
 楼主| 比神乐 发表于 2023-4-9 10:39 | 显示全部楼层
 楼主| 比神乐 发表于 2023-4-9 10:40 | 显示全部楼层
sesefadou 发表于 2023-4-8 22:17
自带ADC的精度怎么样

还可以
yeates333 发表于 2023-4-9 14:02 | 显示全部楼层
APM32F107的性能怎么样?
hudi008 发表于 2023-4-9 14:15 | 显示全部楼层
APM32F107VC支持多大的波特率?
cashrwood 发表于 2023-4-9 14:38 | 显示全部楼层
这个ntc可以采样的吗?              
youtome 发表于 2023-4-9 14:44 | 显示全部楼层
热敏电阻是怎么实现转换为温度的              
alvpeg 发表于 2023-4-9 15:09 | 显示全部楼层
AD采集热敏电阻值的效率如何?              
 楼主| 比神乐 发表于 2023-4-10 10:09 | 显示全部楼层
cashrwood 发表于 2023-4-9 14:38
这个ntc可以采样的吗?

可以啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部