temp_3.c
来自「8051试验程序 基础教材」· C语言 代码 · 共 386 行 · 第 1/2 页
C
386 行
//-----------------------------------------------------------------------------
// Temp_3.c
//-----------------------------------------------------------------------------
// Copyright (C) 2005 Silicon Laboratories, Inc.
//
// AUTH: BW / FB
// DATE: 06 FEB 03
//
// This program prints the C8051F124 die temperature out the hardware
// UART at 115200bps. This example uses the internal 24.5MHz oscillator
// multiplied by 2 using the on-chip PLL (SYSCLK = 49MHz).
//
// 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: C8051F12x
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <ioC8051f124.h> // SFR declarations
#include "page_defs.h"
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F12x
//-----------------------------------------------------------------------------
__sfr __no_init volatile unsigned short DP @ 0x82; // data pointer
__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 RCAP @ 0xca;
__sfr __no_init volatile unsigned short TMR @ 0xcc;
__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 PCA0CP5 @ 0xe1; // PCA0 Module 5 capture
__sfr __no_init volatile unsigned short PCA0CP2 @ 0xe9; // PCA0 Module 2 capture
__sfr __no_init volatile unsigned short PCA0CP3 @ 0xeb; // PCA0 Module 3 capture
__sfr __no_init volatile unsigned short PCA0CP4 @ 0xed; // PCA0 Module 4 capture
__sfr __no_init volatile unsigned short PCA0 @ 0xf9; // PCA0 counter
__sfr __no_init volatile unsigned short PCA0CP0 @ 0xfb; // PCA0 Module 0 capture
__sfr __no_init volatile unsigned short PCA0CP1 @ 0xfd; // PCA0 Module 1 capture
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define BAUDRATE 9600 // Baud rate of UART in bps
#define INTCLK 24500000 // Internal oscillator frequency in Hz
#define SYSCLK 49000000 // Output of PLL derived from (INTCLK*2)
#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
// RCAP2 = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz
// TMR2 = RCAP2;
RCAP = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz
TMR = RCAP;
IE_bit.ET2 = 0; // Disable Timer 2 interrupts
TMR2CN_bit.TR2 = 1; // Start Timer 2
while(ms){
TMR2CN_bit.TF2 = 0;
while(!TMR2CN_bit.TF2); // wait until timer overflows
ms--; // decrement ms
}
TMR2CN_bit.TR2 = 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 multiplied by two using the PLL.
//
void SYSCLK_Init (void)
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?