📄 temptest.c
字号:
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f000.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F00x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define BAUDRATE 115200 // Baud rate of UART in bps
#define SYSCLK 11059200 // SYSCLK frequency in Hz
#define SAMPLE_RATE 50000 // Sample frequency in Hz
#define INT_DEC 4096 // integrate and decimate ratio
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer3_Init (int counts);
void ADC0_ISR (void);
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
long result; // ADC0 decimated value
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
long temperature; // temperature in hundredths of a
unsigned int j;
int temp_int, temp_frac; // integer and fractional portions of
// temperature
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
SYSCLK_Init (); // initialize oscillator
PORT_Init (); // initialize crossbar and GPIO
UART0_Init (); // initialize UART0
Timer3_Init (SYSCLK/SAMPLE_RATE); // initialize Timer3 to overflow at
// sample rate
ADC0_Init (); // init ADC
ADCEN = 1; // enable ADC
EA = 1; // Enable global interrupts
while (1) {
EA = 0; // disable interrupts
temperature = result;
EA = 1; // re-enable interrupts
// calculate temperature in hundredths of a degree
temperature = temperature - 42880;
temperature = (temperature * 100L) / 156;
temp_int = temperature / 100;
temp_frac = temperature - (temp_int * 100);
printf ("Temperature is %+02d.%02d\n", temp_int, temp_frac);
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 18.432MHz crystal
for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
XBR0 = 0x07; // Enable UART0
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
PRT0CF |= 0x40; // enable TX0 as a push-pull output
// PRT1CF |= 0x40; // enable P1.6 (LED) as push-pull output
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
SCON = 0x50; // SCON: mode 1, 8-bit UART, enable RX
TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
CKCON |= 0x10; // Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD = 1
TI = 1; // Indicate TX ready
}
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0x05; // ADC0 disabled; normal tracking
// mode; ADC0 conversions are initiated
// on overflow of Timer3; ADC0 data is
// left-justified
REF0CN = 0x07; // enable temp sensor, on-chip VREF,
// and VREF output buffer
AMX0SL = 0x0f; // Select TEMP sens as ADC mux output
ADC0CF = 0x80; // ADC conversion clock = SYSCLK/16
ADC0CF |= 0x01; // PGA gain = 2
EIE2 |= 0x02; // enable ADC interrupts
}
//-----------------------------------------------------------------------------
// Timer3_Init
//-----------------------------------------------------------------------------
void Timer3_Init (int counts)
{
TMR3CN = 0x02; // Stop Timer3; Clear TF3;
// use SYSCLK as timebase
TMR3RL = -counts; // Init reload values
TMR3 = 0xffff; // set to reload immediately
EIE2 &= ~0x01; // disable Timer3 interrupts
TMR3CN |= 0x04; // start Timer3
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// ADC0_ISR
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 15
{
static unsigned int_dec=INT_DEC; // integrate/decimate counter
// we post a new result when
// int_dec = 0
static long accumulator=0L; // here's where we integrate the
// ADC samples
ADCINT = 0; // clear ADC conversion complete
// indicator
accumulator += ADC0; // read ADC value and add to running
// total
int_dec--; // update decimation counter
if (int_dec == 0) { // if zero, then post result
int_dec = INT_DEC; // reset counter
result = accumulator >> 12;
accumulator = 0L; // reset accumulator
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -