[PIC®/AVR®/dsPIC®产品] 【CuriosityNano测评报告】+PIC18F16Q41 ADC测试内部温度传感器

[复制链接]
10558|1
 楼主| qjp1988113 发表于 2021-8-16 10:17 | 显示全部楼层 |阅读模式
本帖最后由 pzsh 于 2021-9-13 13:23 编辑

今天我们测试下PIC18F16Q41的内部传感器~这个看起来简单,但是完全使用了ADC的各种功能及内部FLASH的读写及FVR的设置~
我们先看下DATASHEET里面温度传感器测试的描述:

我们看到,这里我们选择High Range的模式进行测量~
那么测量的条件是:
Vref=2.048,我们这里选择FVR来配置我们的参考电压:

温度传感器的计算:

我们发现需要gain 和 offset 两个值,我们可以从FLASH中读出它们的数值:



ADC我们决定配置成TMR外部触发:


其中对应ADC采样的计算模式,我们选择Burst Average:
这是一种多次采样后,取平均值的模式:

好了,我们下面通过MCC对上面的设置,进行配置:
选配置system module:

配置FVR module:

配置TMR module:

配置MEMORY module:

配置ADC module:

配置UART module:

配置GPIO module:

生成代码,然后对main.c进行修改(我们设置成每500ms查询一次):
  1. #include "mcc_generated_files/mcc.h"

  2. #define SetAcquisitionChannel(X) do { ADPCH = X; } while (0)
  3. //Sets sampling channel of ADCC without starting conversion
  4. /*
  5.                          Main application
  6. */
  7. void main(void)
  8. {
  9.     // Initialize the device
  10.     SYSTEM_Initialize();

  11.     // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
  12.     // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
  13.     // Use the following macros to:

  14.     // Enable the Global Interrupts
  15.     //INTERRUPT_GlobalInterruptEnable();

  16.     // Disable the Global Interrupts
  17.     //INTERRUPT_GlobalInterruptDisable();

  18.     int16_t gain;
  19.     int16_t offset;

  20.     gain = FLASH_ReadWord(DIA_TSHR1);  
  21.     offset = FLASH_ReadWord(DIA_TSHR3);

  22.     INTERRUPT_GlobalInterruptEnable();
  23.     uint16_t ADC_MEAS = 0;
  24.     int24_t temp_c = 0;
  25.     SetAcquisitionChannel(channel_Temp);
  26.    
  27.     while (1)
  28.     {
  29.         // Add your application code
  30.         //asm ("SLEEP");
  31.         //asm ("NOP");
  32.         __delay_ms(500);
  33.         ADC_MEAS = ADCC_GetConversionResult();
  34.         temp_c = (int24_t) (ADC_MEAS) * gain;
  35.         temp_c = temp_c / 256;
  36.         temp_c = temp_c + offset;
  37.         temp_c = temp_c / 10;
  38.         printf("Device Temperature: %dC \r\n", temp_c);
  39.     }
  40. }


编译,下载,查看串口输出:

好了,内部温度传感器的测试就到这里~谢谢观看~




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
gaoyang9992006 发表于 2021-8-16 11:20 | 显示全部楼层
内部温度计非常好用的,可以检测工作环境。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

111

主题

627

帖子

2

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