📄 temperature.c
字号:
#include "REG1210.H"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define LSB (1.25/8388608/375e-6)
#define WINDOWSIZE 16
extern void put_string(char code *string);
data char ROM_temp0 _at_ 0x000a; // For put_string function
data char ROM_temp1 _at_ 0x000b;
signed long summer(void);
extern void putspace4(void);
extern void autobaud(void);
extern void tx_byte(char);
sbit P14 = P1^4;
sbit P15 = P1^5;
// recursive digit display routine
void prt_digit(unsigned long int i, signed char d) reentrant
{ unsigned long int j; char c;
j=i/10; c=i-j*10; d--;
if (j!=0 || !(d &0x80)) prt_digit(j,d);
if (d==0) tx_byte('.');
tx_byte(c+48);
}
// Display long integer number with sign and decimal
void print(signed long int i, unsigned char d)
{
if (i<0) { tx_byte('-'); i*=-1;}
prt_digit(i,d); putspace4();
}
void main(void)
{
signed long int data sum;
float data window[16]={0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
float data adres=0, temperature;
unsigned char j,k,i, fill_ptr=1, mod_ptr=0;
// char WINDOWSIZE=32;
autobaud();
put_string("\x1b[1;33;46m\x1b[2J\x1b[12CTemperature Sensor\n");
PDCON &= 0x0f7; //turn on adc
ACLK = 1; // ACLK = 1.8432MHz/(1+1)= 0.9216MHz
DECIMATION = 0x7ff;
ODAC=0;
ADCON0 = 0x20; // Vref on 1.25V, Buff off, BOD off, PGA 1
ADMUX=0xff;
ADCON1 = 0x01; // bipolar, auto, self calibration, offset, gain
SSCON=0; // Clear SUMR0~3
SSCON=228; // Set SSCON for ADC 32 summations and div by 32
sum=summer();
SSCON=0; // Clear SUMR0~3
SSCON=228; // Set SSCON for ADC 32 summations and div by 32
while(1){
P15=!P15;
sum=summer();
P15=!P15;
SSCON=0; // Clear SUMR0~3
SSCON=228; // Set SSCON for ADC 32 summations and div by 32
adres=adres-window[mod_ptr]; // moving window of 10 results
window[mod_ptr]=(float)sum*LSB-281.667;
adres=adres+ window[mod_ptr];
temperature=adres/(float)fill_ptr;
if (fill_ptr==WINDOWSIZE) fill_ptr=WINDOWSIZE; else fill_ptr++;
if (mod_ptr==(WINDOWSIZE-1)) mod_ptr=0; else mod_ptr++;
print((signed long int)(temperature*1000),3);
put_string("degC ");
put_string("\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -