本帖最后由 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查询一次):
#include "mcc_generated_files/mcc.h"
#define SetAcquisitionChannel(X) do { ADPCH = X; } while (0)
//Sets sampling channel of ADCC without starting conversion
/*
Main application
*/
void main(void)
{
// Initialize the device
SYSTEM_Initialize();
// If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
// If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
// Use the following macros to:
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
int16_t gain;
int16_t offset;
gain = FLASH_ReadWord(DIA_TSHR1);
offset = FLASH_ReadWord(DIA_TSHR3);
INTERRUPT_GlobalInterruptEnable();
uint16_t ADC_MEAS = 0;
int24_t temp_c = 0;
SetAcquisitionChannel(channel_Temp);
while (1)
{
// Add your application code
//asm ("SLEEP");
//asm ("NOP");
__delay_ms(500);
ADC_MEAS = ADCC_GetConversionResult();
temp_c = (int24_t) (ADC_MEAS) * gain;
temp_c = temp_c / 256;
temp_c = temp_c + offset;
temp_c = temp_c / 10;
printf("Device Temperature: %dC \r\n", temp_c);
}
}
编译,下载,查看串口输出:
好了,内部温度传感器的测试就到这里~谢谢观看~
|