📄 sy_wt_tt.c
字号:
//-----------------------------------------------------------------------------
// SY_WT_TT.c
//-----------------------------------------------------------------------------
// Copyright (C) 2005.12.1
//
// This program is used to measure the temperature sensor on an 'F330
// 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: C8051F330
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#pragma debug code small
#include <c8051f330.h> // SFR declarations
#include <stdio.h>
#include <intrins.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <absacc.h>
#include <stdarg.h>
#include <setjmp.h>
#define _Nop() _nop_() /*定义空指令*/
#define UC unsigned char
#define UI unsigned int
#define UL unsigned long
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F33x
//-----------------------------------------------------------------------------
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 ADC0 = 0xbd; // ADC Data Word Register
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 3062500 //24500000 to 3062500 SYSCLK frequency in Hz
#define BAUDRATE 9600 // Baud rate of UART in bps
#define TIMER2_RATE 100 // Timer 2 overflow rate in Hz
sbit W_CLK = P1^2; /*MAX-7219 时钟线引脚 */
sbit W_LOAD = P1^1; /*MAX-7219 复位线引脚 */
sbit W_IO = P1^0; /*MAX-7219 数据线引脚 */
// 18B20
#define CONFIG 0x7f // 1f=9A/D; 3f=10A/D; 5f=11A/D; 7f=12A/D
sbit TEMPE1 = P1^5; // Temperature1
sbit TEMPE2 = P1^6; // Temperature2
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
bit TEM_FD;
UC data led[8];
UC code led_bcd[21]={0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b,0x01,0x08,0x77,0x4e,0x0d,0x4f,0x37,0x0e,0x47,0x67,0x00};
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -, _, A, C, c,
//-----------------------------------------------------------------------------
// Temperature Sensor Calibration PARAMETERS
//-----------------------------------------------------------------------------
#define AMB_TEMP 25 // Ambient Calibration Temperature
// (degC)
#define TEMP_SENSOR_GAIN 2860 // Temp Sensor Gain in (uV / degC)
#define VREF 2430 // 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 delay_us(UI use); // 1=4us
void delay_ms(UI yscnt); // 1=2ms
void delay_50us(UI count);
UC ow_reset1(void);
UC read_byte1(void);
void write_byte1(char val);
void ini_T1(void);
void start_temper1(void);
UI read_temper1(void);
void read_data(void);
void w_InputByte(UC ucDa);
void w_W7219(UC ucAddr, UC ucDa);
//*****************************************************************************
void dispiay_t0(void)
{
// MAX7219 disple D8,D7,D6,D5,D4,D3,D2,D1,
// 8..1 Addr, Data
w_W7219(0x05, 0x7f|0x80);
w_W7219(0x04, 0x7f|0x80);
w_W7219(0x03, 0x7f|0x80);
w_W7219(0x02, 0x7f|0x80);
w_W7219(0x01, 0x7f|0x80);
}
//-------------------------------------------------
void dispiay_t1(void)
{
// MAX7219 disple D8,D7,D6,D5,D4,D3,D2,D1,
// 8..1 Addr, Data
w_W7219(0x05, led[4]);
w_W7219(0x04, led[3]);
w_W7219(0x03, led[2]|0x80);// DP.
w_W7219(0x02, led[1]);
w_W7219(0x01, led[0]);
}
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
//float temp;
// 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 count=SYSCLK/TIMER2_RATE
//UART0_Init();
//AD0EN = 1; // Enable ADC0
//delay_50us(10);
// ini MAX7219
w_W7219(0x0b, 0x03); // dispie 7bit Ini MAX7219
w_W7219(0x09, 0xff); // disple NO_BCD
w_W7219(0x0a, 0x03); // LD
w_W7219(0x0c, 0x01); // on led
//w_W7219(0x0f, 0x01); // test all_LED
w_W7219(0x01, 0x01); // test x_LED; 1...8;
w_W7219(0x02, 0x02);
w_W7219(0x03, 0x83);
w_W7219(0x04, 0x0A);
w_W7219(0x0f, 0x00); // test all_LED
w_W7219(0x0c, 0x00); // off led
dispiay_t0();
while (1)
{
ini_T1();
start_temper1();
delay_ms(1000); // delay<=750ms, 1=1ms, ###############
read_data();
dispiay_t1();
}
}
//*****************************************************************************
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PORT_Init OK
//-----------------------------------------------------------------------------
//
// 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(TX) and P0.5(RX)
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P0SKIP = 0x01; // Skip P0.0 for external VREF
P0MDIN = 0xfe; // Configure P0.0 as analog input VREF=2.5V.
P0MDOUT = 0x00; // enable UTX as push-pull output
P1MDIN = 0xff; // P1.0...P1.7=I/O
P1MDOUT = 0x00;
P1SKIP = 0x00;
}
//-----------------------------------------------------------------------------
// SYSCLK_Init OK
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal oscillator
// at its maximum frequency 24.5MC.
// Also enables the Missing Clock Detector.
//
void SYSCLK_Init (void)
{
OSCICN |= 0x00; // Configure internal oscillator for
// its maximum frequencyb. 0x03|=24.5MC 0x00|=3.0625MC
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 = 0x10; // Temp sensor selected at + input
AMX0N = 0x11; // 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
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
void UART0_Init (void)
{
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
if (SYSCLK/BAUDRATE/2/256 < 1)
{
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
}
else if (SYSCLK/BAUDRATE/2/256 < 4)
{
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x09;
}
else if (SYSCLK/BAUDRATE/2/256 < 12)
{
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
}
else
{
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -