?? c8051f31x
字號:
//-----------------------------------------------------------------------------
// T2Cal31x.c
//-----------------------------------------------------------------------------
// Copyright (C) 2004 Silicon Laboratories, Inc.
//
// AUTH: PD
// DATE: 24 OCT 02
//
// This program is used to measure the temperature sensor on an 'F31x
// device. It uses 1-point calibration and stores the offset value
// in FLASH memory. The program outputs temperature values in 100ths
// of a degree Celsius with UART.
// Target: C8051F31x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//
//-----------------------------------------------------------------------------
// 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);
int get_temp (void);
void calibrate(void);
unsigned int measure(void);
void wait_one_second (void);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
unsigned temperature;
// Disable Watchdog timer
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
PORT_Init(); // Initialize Port I/O
SYSCLK_Init (); // Initialize Oscillator
ADC0_Init (); // Init ADC0
Timer2_Init(SYSCLK/TIMER2_RATE); // Init Timer 2
UART0_Init();
AD0EN = 1; // Enable ADC0
if (TEMP_OFFSET == 0xFFFF) { // TRUE if first-time to execute
printf ("Calibrating...\n");
calibrate (); // execute calibration sequence
} else {
printf ("Not calibrating.\n");
}
while (1) {
// example of how to read the temperature
LED = 1;
temperature = get_temp ();
LED = 0;
printf ("Temperature = %+02d hundredths degrees C\n", temperature);
}
}
//-----------------------------------------------------------------------------
// 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)
{
XBR0 = 0x01; // Enable UART on P0.4(RX) and P0.5(TX)
XBR1 = 0x40; // Enable crossbar and enable
// weak pull-ups
P0MDOUT |= 0x10; // enable UTX as push-pull output
P3MDOUT |= 0x08; // enable LED as 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
}
2樓 admin 發表于:2006-8-11 17:31:00
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
void UART0_Init (void)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -