📄 adc_temp.c
字号:
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f310.h> // SFR declarations
#include <intrins.h>
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F31x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 PCA0CP0 = 0xfb; // PCA0 Module 0 Capture/Compare
sfr16 PCA0CP1 = 0xe9; // PCA0 Module 1 Capture/Compare
sfr16 PCA0CP2 = 0xeb; // PCA0 Module 2 Capture/Compare
sfr16 PCA0CP3 = 0xed; // PCA0 Module 3 Capture/Compare
sfr16 PCA0CP4 = 0xfd; // PCA0 Module 4 Capture/Compare
sfr16 PCA0 = 0xf9; // PCA0 counter
sfr16 ADC0 = 0xbd; // ADC Data Word Register
sfr16 ADC0GT = 0xc3; // ADC0 Greater-Than
sfr16 ADC0LT = 0xc5; // ADC0 Less-Than
//-----------------------------------------------------------------------------
// Temperature Sensor Calibration PARAMETERS
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 24500000 // SYSCLK frequency in Hz
#define BAUDRATE 9600 // Baud rate of UART in bps
#define TIMER2_RATE 1000 // Timer 2 overflow rate in Hz
sbit LED = P3^3; // LED='1' means ON
//-----------------------------------------------------------------------------
// Temperature Sensor Calibration PARAMETERS
//-----------------------------------------------------------------------------
#define AMB_TEMP 25 // Ambient Calibration Temperature
// (degC)
#define TEMP_SENSOR_GAIN 3300 // Temp Sensor Gain in (uV / degC)
#define VREF 3300 // ADC Voltage Reference (mV)
#define SOAK_TIME 15 // Soak Time in Seconds
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
// TEMP_OFFSET allocates two bytes of code memory space in FLASH
// that will be used to store the calibrated temperature value
unsigned int code TEMP_OFFSET = 0xFFFF;
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void ADC0_Init (void);
void UART0_Init (void);
void PORT_Init (void);
void Timer2_Init (int);
void Write_COM(unsigned yjcom );
void Write_CHAR(unsigned char yjchar);
void YJ_Init(void);
void calibrate(void);
void wait_one_second (void);
int get_temp (void);
void SPI0_Init (void);
unsigned int measure(void);
unsigned char xdata NCDdata[14]={0x43,0x61,0x7c,0x69,0x62,0x72,0x61,0x74,0x69,0x6e,0x67,0x2e,0x2e,0x2e};
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
unsigned temperature;
int i;
int temp_int, temp_frac;
// Disable Watchdog timer
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
PORT_Init(); // Initialize Port I/O
SPI0_Init ();
SYSCLK_Init (); // Initialize Oscillator
ADC0_Init (); // Init ADC0
Timer2_Init(SYSCLK/TIMER2_RATE); // Init Timer 2
UART0_Init();
YJ_Init();
AD0EN = 1; // Enable ADC0
if (TEMP_OFFSET == 0xFFFF) { // TRUE if first-time to execute
printf ("Calibrating...\n");
Write_COM(0x02);
for(i=0;i<14;i++){
Write_CHAR(NCDdata[i]);}
calibrate (); // execute calibration sequence
}
else
{
printf ("Not calibrating.\n");
}
while (1) {
// example of how to read the temperature
PORT_Init();
LED = 1;
temperature = get_temp ();
LED = 0;
temp_int = temperature / 100;
temp_frac = temperature - (temp_int * 100);
printf ("Temperature = %+02d hundredths degrees C\n", temperature);
NCDdata[0]=temp_int/100+0x30;NCDdata[1]=(temp_int%100)/10+0x30;NCDdata[2]=(temp_int%100)%10+0x30;NCDdata[3]=0x2e;
NCDdata[4]=temp_frac/10+0x30;NCDdata[5]=temp_frac%10+0x30;NCDdata[6]=0x20;NCDdata[7]=0x20;NCDdata[8]=0x20;
NCDdata[9]=0x20;NCDdata[10]=0x20;NCDdata[11]=0x20;NCDdata[12]=0x20;NCDdata[13]=0x20;NCDdata[14]=0x20;
YJ_Init();
for(i=0;i<14;i++)
{
Write_CHAR(NCDdata[i]);}
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
//
// P0.4 - UART TX
// P0.5 - UART RX
// P3.3 - LED
void PORT_Init (void)
{
P0SKIP = 0xCE;
P1SKIP = 0x7F;
XBR0 = 0x03; // Enable SPI pins
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT = 0xFF; // All P0 pins open-drain output
P2MDOUT = 0xFF;
P3MDOUT |= 0x04; // P3.3 push-pull output
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal oscillator
// at its maximum frequency.
// Also enables the Missing Clock Detector.
//
void SYSCLK_Init (void)
{
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency
RSTSRC = 0x04; // Enable missing clock detector
}
//-----------------------------------------------------------------------------
// ADC0_Init ADBUSY, LP tracking, no Interrupt, ADC disabled
//-----------------------------------------------------------------------------
//
// Configure ADC0 to use ADBUSY as conversion source, and to sense the output
// of the temp sensor. Disables ADC end of conversion interrupt. Leaves ADC
// disabled.
//
void ADC0_Init (void)
{
ADC0CN = 0x40; // ADC0 disabled; LP tracking
// mode; ADC0 conversions are initiated
// on a write to ADBusy
AMX0P = 0x1E; // Temp sensor selected at + input
AMX0N = 0x1F; // Single-ended mode
ADC0CF = (SYSCLK/3000000) << 3; // ADC conversion clock <= 3MHz
ADC0CF &= ~0x04; // Make ADC0 right-justified
REF0CN = 0x0e; // enable temp sensor, VREF = VDD, bias
// generator is on.
EIE1 &= ~0x08; // Disable ADC0 EOC interrupt
}
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -