temp_3.c
来自「8051试验程序 基础教材」· C语言 代码 · 共 352 行
C
352 行
//-----------------------------------------------------------------------------
// Temp_3.c
//-----------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// AUTH: BW / FB
// DATE: 06 FEB 03
//
// This program prints the C8051F040 die temperature out the hardware
// UART at 115200bps. This example uses the internal 24.5MHz oscillator.
//
// The ADC is configured to look at the on-chip temp sensor. The sampling
// rate of the ADC is determined by the constant <SAMPLE_RATE>, which is given
// in Hz.
//
// The ADC0 End of Conversion Interrupt Handler retrieves the sample
// from the ADC and adds it to a running accumulator. Every <INT_DEC>
// samples, the ADC updates and stores its result in the global variable
// <temperature>, which holds the current temperature in hundredths of a
// degree. The sampling technique of adding a set of values and
// decimating them (posting results every (n)th sample) is called 'integrate
// and dump.' It is easy to implement and requires very few resources.
//
// For each power of 4 of <INT_DEC>, you gain 1 bit of effective resolution.
// For example, <INT_DEC> = 256 gain you 4 bits of resolution: 4^4 = 256.
//
// Also note that the ADC0 is configured for 'LEFT' justified mode. In this
// mode, the MSB of the ADC word is located in the MSB position of the ADC0
// high byte. Using the data in this way makes the magnitude of the resulting
// code independent of the number of bits in the ADC (12- and 10-bits behave
// the same).
//
// Target: C8051F04x
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <ioC8051f040.h> // SFR declarations
#include "page_defs.h"
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F04x
//-----------------------------------------------------------------------------
__sfr __no_init volatile unsigned short DP @ 0x82; // data pointer
__sfr __no_init volatile unsigned short RCAP @ 0xCA; // Timer reload/capture value
__sfr __no_init volatile unsigned short TMR @ 0xCC; // Timer counter/timer
__sfr __no_init volatile unsigned short ADC0 @ 0xBE; // ADC0 data
__sfr __no_init volatile unsigned short ADC0GT @ 0xC4; // ADC0 greater than window
__sfr __no_init volatile unsigned short ADC0LT @ 0xC6; // ADC0 less than window
__sfr __no_init volatile unsigned short DAC0 @ 0xD2; // DAC0 data
__sfr __no_init volatile unsigned short DAC1 @ 0xD2; // DAC1 data
__sfr __no_init volatile unsigned short CAN0DAT @ 0xD8; // CAN data window
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
int BAUDRATE = 115200; // Baud rate of UART in bps
long SYSCLK = 24500000; // Internal oscillator frequency in Hz
#define SAMPLE_RATE 50000 // Sample frequency in Hz
#define INT_DEC 256 // integrate and decimate ratio
#define LED P1_bit.P16 // LED='1' means ON
#define SW1 P3_bit.P37 // SW1='0' means switch pressed
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void UART1_Init (void);
void ADC0_Init (void);
void Timer3_Init (int counts);
__interrupt void ADC0_ISR (void);
void wait_ms (int ms);
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
long result; // ADC0 decimated value
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
long temperature; // temperature in hundredths of a
// degree C
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
UART1_Init (); // initialize UART1
Timer3_Init (SYSCLK/SAMPLE_RATE); // initialize Timer3 to overflow at
// sample rate
ADC0_Init (); // init ADC
SFRPAGE = ADC0_PAGE;
ADC0CN_bit.AD0EN = 1; // enable ADC
IE_bit.EA = 1; // Enable global interrupts
while (1) {
IE_bit.EA = 0; // disable interrupts
temperature = result;
IE_bit.EA = 1; // re-enable interrupts
// calculate temperature in hundredths of a degree
temperature = temperature - 42380;
temperature = (temperature * 100L) / 156;
temp_int = temperature / 100;
temp_frac = temperature - (temp_int * 100);
SFRPAGE = UART1_PAGE;
printf ("Temperature is %+02d.%02d\n", temp_int, temp_frac);
SFRPAGE = CONFIG_PAGE;
LED = ~SW1; // LED reflects state of switch
wait_ms(50); // wait 50 milliseconds
}
}
//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// wait_ms
//-----------------------------------------------------------------------------
//
// This routine inserts a delay of <ms> milliseconds.
//
void wait_ms (int ms)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x00; // Stop Timer3; Clear TF3;
TMR2CF = 0x00; // use SYSCLK/12 as timebase
RCAP = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz
TMR = RCAP;
IE_bit.ET2 = 0; // Disable Timer 2 interrupts
TMR2CN_bit.TOG2 = 1; // Start Timer 2
while(ms){
TMR2CN &= 0x7F;
while(!(TMR2CN & 0x80)); // wait until timer overflows
ms--; // decrement ms
}
TMR2CN_bit.TOG2 = 0; // Stop Timer 2
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal oscillator
// at 24.5 MHz.
//
void SYSCLK_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
OSCICN = 0x83; // set internal oscillator to run
// at its maximum frequency
CLKSEL = 0x00; // Select the internal osc. as
// the SYSCLK source
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// This routine configures the crossbar and GPIO ports.
//
void PORT_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
XBR0 = 0x00;
XBR1 = 0x00;
XBR2 = 0x44; // Enable crossbar and weak pull-up
// Enable UART1
P0MDOUT |= 0x01; // Set TX1 pin to push-pull
P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// UART1_Init
//-----------------------------------------------------------------------------
//
// Configure the UART1 using Timer1, for <baudrate> and 8-N-1.
//
void UART1_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = UART1_PAGE;
SCON1 = 0x10; // SCON1: mode 0, 8-bit UART, enable RX
SFRPAGE = TIMER01_PAGE;
TMOD &= ~0xF0;
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x01; // T1M = 0; SCA1:0 = 01
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x02; // T1M = 0; SCA1:0 = 10
}
TL1 = TH1; // initialize Timer1
TH1_bit.TR1 = 1; // start Timer1
SFRPAGE = UART1_PAGE;
SCON1_bit.TI1 = 1; // Indicate TX1 ready
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
//
// Configure ADC0 to use Timer3 overflows as conversion source, to
// generate an interrupt on conversion complete, and to use left-justified
// output mode. Enables ADC end of conversion interrupt. Leaves ADC disabled.
//
void ADC0_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = ADC0_PAGE;
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 = (SYSCLK/2500000) << 3; // ADC conversion clock = 2.5MHz
ADC0CF |= 0x01; // PGA gain = 2
EIE2 |= 0x02; // enable ADC interrupts
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// Timer3_Init
//-----------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload at interval specified by <counts> (no
// interrupt generated) using SYSCLK as its time base.
//
void Timer3_Init (int counts)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR3_PAGE;
TMR3CN = 0x00; // Stop Timer3; Clear TF3;
TMR3CF = 0x08; // use SYSCLK as timebase
RCAP = -counts; // Init reload values
TMR = RCAP; // set to reload immediately
EIE2 &= ~0x01; // disable Timer3 interrupts
TMR3CN_bit.TOG3 = 1; // start Timer3
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// ADC0_ISR
//-----------------------------------------------------------------------------
//
// ADC0 end-of-conversion ISR
// Here we take the ADC0 sample, add it to a running total <accumulator>, and
// decrement our local decimation counter <int_dec>. When <int_dec> reaches
// zero, we post the decimated result in the global variable <result>.
//
#pragma vector=0x7B // interrupt 15
__interrupt void ADC0_ISR (void)
{
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
ADC0CN_bit.AD0INT = 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 >> 8;
accumulator = 0L; // reset accumulator
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?