PIC18F16Q40内部有温度检测单元,可以用来检测芯片内部温度,等同测量了PCBA板上的温度。本应用需要用到ADC、FVR、UART模块,测量内部温度,通过串口打印出来。
1、FVR初始化
void FVR_Init(void)
{
FVRCON = (1 << 7) | (1 << 5) | (1 << 4) | (2 << 2) | (2 << 0);
}
2、ADC初始化
void ADC_Init(void)
{
ADCON1 = 0;
ADCON2 = 0x10;
ADCON3 = 0;
ADSTAT = 0x00;
ADCLK = 0x3f;
ADPRE = 0x03;
ADREF = 0x03;
ADCON0 = (1 << 7) | (1 << 4) | (1 << 2);
}
3、内部温度传感器初始化
void TIM_Init(void)
{
//读取温度增益和温度偏移值
EEPROM_Read(0x2c0024, (unsigned char *)(&TempGain), 2);
EEPROM_Read(0x2c0028, (unsigned char *)(&TempOffSet), 2);
//初始化FVR
FVR_Init();
//ADC初始化
ADC_Init();
//配置ADC转换通道为内部温度通道
ADC_Channel(ADC_TempChannel);
//ADC转换开始
ADC_Start();
}
4、内部温度读取,计算函数
void TIM_ReadValue(void)
{
long ADC_Meas;
ADC_Meas = (ADRESH << 8) | ADRESL;
ADC_Meas = ADC_Meas * TempGain;
ADC_Meas = ADC_Meas / 256;
ADC_Meas = ADC_Meas + TempOffSet;
TempMeas = ADC_Meas / 10;
ADC_Start();
}
5、串口初始化
void UART_Init(void)
{
//?????
TRISB = (5 << 0);
LATA = (1 << 7) | (1 << 5);
//????
PPS_Unlock();
U1RXPPS = (1 << 3) | (5 << 0); //RB5
RB7PPS = 0x10; //RB7
//PPS_Lock();
//??????
U1RXB = 0x0;
U1RXCHK = 0x0;
U1TXB = 0x0;
U1TXCHK = 0x0;
U1P1L = 0x0;
U1P1H = 0x0;
U1P2L = 0x0;
U1P2H = 0x0;
U1P3L = 0x0;
U1P3H = 0x0;
U1CON0 = 0xB0;
U1CON1 = 0x80;
U1CON2 = 0x00;
//64M时钟,4分频,波特率19200
U1BRG = (((64UL * 1000000 * 4) / 16) / 19200) - 1;
U1FIFO = 0x0;
U1UIR = 0x0;
U1ERRIR = 0x0;
U1ERRIE = 0x0;
}
6、EEPROM读取函数,
void EEPROM_Read(__uint24 addr, unsigned char *ptr, int len)
{
do{
TBLPTRU = (uint8_t)(addr >> 16);
TBLPTRH = (uint8_t)(addr >> 8);
TBLPTRL = (uint8_t)(addr >> 0);
asm("TBLRD");
*ptr = TABLAT;
addr++;
ptr++;
}while(len--);
}
7、串口打印结果如图:
|