📄 f300_highspeed_bc.c
字号:
//-----------------------------------------------------------------------------
//
// Copyright 2003 Cygnal Integrated Products, Inc.
//
// Filename: F300_HighSpeed_BC.c
// Target Device: 8051F300
// Created: 1 March 2003
// Created By: DKC
// Tool chain: KEIL Eval C51
//
// This is a stand alone battery charger for a Lithium ION battery.
// It utilizes a buck converter, controlled by the on-chip 8-bit PWM,
// to provide constant current followed by constant voltage battery charge.
// The High Frequency Output Mode is used to generate the switch rate.
// The default rate is 510 kHz.
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f300.h>
#include "F300_HighSpeed_BC.h" // Battery Hearder File
//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
void Config_F300(void)
{ RSTSRC = 0x02; // Enable VDD Monitor
XBR0 = 0x37; // Skip P0.0,1,2,4,5; they're analog In
XBR1 = 0x90; // Enable P0.6, P0.7, as CEX0 and CEX1
XBR2 = 0x40; // Make CEX0 an 8-Bit PWM
// and CEX1 Frequency Output Mode
// Also, Enable crossbar and weak pull-ups
CMPIN2 = 1; // Make Comparator Output Initially low
CMPIN1 = 0; // to minimize current spikes on start-up
P0MDOUT = 0xC8; // Set P0.3,6,7 output to push-pull
P0MDIN = 0xC8; // Configure P0.0,1,2,4,5 as Analog Inputs
OSCICN = 0x07; // Set SYSCLK to 24.5MHz, internal osc.
ADC0CN = 0xC0; // Turn on the ADC Module;
// enable low power mode for settling
REF0CN = 0x0C; // Configure ADC's to use VDD for
// Voltage Reference,
// Enable On-chip Temperature Sensor
//----------------------------------------------------------------
// Comparator Register Configuration
//----------------------------------------------------------------
CPT0MX = 0x22; // Comparator 0 MUX Selection Register
// P0.4,5 Input to Comparator
// P0.3 Output of Comparator
CPT0MD = 0x00; // Comparator 0 Mode Selection Register
CPT0CN = 0x80; // Comparator 0 Control Register, Turn on
//-----------------------------------------------------------------------------
// PCA Configuration
//-----------------------------------------------------------------------------
PCA0MD = 0x00; // Disable WDT
PCA0MD = 0x08; // Set PWM Time base = SYSCLK
PCA0L = 0x00; // Initialize PCA Counter to Zero
PCA0H = 0x00;
PCA0CN = 0x40; // Enable PCA Counter
// Clear PCA Counter Overflow flag
//Module 0
PCA0CPM0 = 0x00; // Configure CCM0 to Frequency Output Mode
PCA0CPL0 = 0x28; // Initialize PCA PWM to small duty cycle
PCA0CPH0 = 0x28; // 0x18 makes output frequency ~510kHz
// 0x28 makes output frequency ~306kHz
//Module 1
PCA0CPM1 = 0x42; // Configure CCM0 to 8-bit PWM mode
PCA0CPL1 = 0xE0; // Initialize PCA PWM to small duty cycle
PCA0CPH1 = 0xE0; // 0xB9 Ensures a Soft Initial Charge
//Module 2
PCA0CPM2 = 0x49; // Configure Module 1 as software timer
PCA0CPL2 = 0xFF; // Initialize to 255 so that Interrupt
// is generated when PCA ends
// 8-bit PWM Cycle
PCA0CPH2 = 0x00; // PCA0CPH is the high byte of the
// Output Compare Module
EIE1 = 0x08; // Enable PCA Overflow Interrupt
}
//-----------------------------------------------------------------------------
// Reset_Time_Base - Resets all Time Counting Values
//-----------------------------------------------------------------------------
void Reset_Time_Base()
{
TIME.sec = 0x00;
TIME.min = 0x00;
TIME.hour = 0x00;
TIME.t_count = PWM_CLOCK;
}
//-----------------------------------------------------------------------------
// Initialize CalibrateADCforVoltageMeasurement
//-----------------------------------------------------------------------------
// This function calibrates the voltage channel and stores the calibration
// coefficients in the parameters volt_slope and volt_offset.
//
void CalibrateADCforMeasurement()
// This calibration routine uses a 2 point cal.
{ unsigned char xdata *pwrite; // FLASH write pointer
long i=0;
EA = 0; // Disable All Interrupts
// Wait until 1st calibration voltage is ready for cal
//while (SW0 == 1); // Wait until SW0 pushed
for (i=0;i<100000;i++); // Wait for Switch Bounce
// Once ready, Get the first calibration voltage
AMX0SL = VBAT; // Select appropriate input for AMUX
ADC0CF = (SYSCLK/5000000) << 3; // ADC conversion clock = 5.0MHz
ADC0CF &=0xF8; // Clear any Previous Gain Settings
ADC0CF |= 0x01; // PGA gain = 1
temp_INT_1.i = Measure();
// Wait until 2nd calibration voltage is ready for cal
//while (SW0 == 1); // Wait until SW0 pushed
//for (i=0;i<100000;i++); // Wait for Switch Bounce
// Once ready, Get the 2nd calibration voltage
AMX0SL = VBAT; // Change Mux for second point
temp_INT_2.i = Measure();
// Calculate the SLOPE // V1 and V2 are in tenth of a degree
temp_LONG_1.l = (unsigned)(temp_INT_2.i-temp_INT_1.i);
temp_LONG_1.l *= (unsigned)100; // Account for Math Truncation Error
temp_LONG_1.l /= (unsigned)(V2_CAL - V1_CAL);
// Calculate the OFFSET
temp_LONG_2.l = (unsigned)temp_INT_1.i;
temp_LONG_2.l -= (signed)(temp_LONG_1.l * V1_CAL/100);
temp_LONG_1.l = 2050; // If no cal. use these
temp_LONG_2.l = 0; // as default values
// Erased memory at page 0x1A00
pwrite = (char xdata *)&(CHECK_BYTE.b[0]);
PSCTL = 0x03; // MOVX writes target FLASH memory;
// FLASH erase operations enabled
FLKEY = 0xA5; // FLASH key sequence #1
FLKEY = 0xF1; // FLASH key sequence #2
*pwrite = 0x00; // initiate PAGE erase
// Write the Volt SLOPE and OFFSET to Flash
PSCTL = 1; // MOVX writes to Flash
pwrite = (char xdata *)&(VOLT_SLOPE.b[0]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_1.b[0];
pwrite = (char xdata *)&(VOLT_SLOPE.b[1]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_1.b[1];
pwrite = (char xdata *)&(VOLT_SLOPE.b[2]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_1.b[2];
pwrite = (char xdata *)&(VOLT_SLOPE.b[3]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_1.b[3];
pwrite = (char xdata *)&(VOLT_OFFSET.b[0]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_2.b[0];
pwrite = (char xdata *)&(VOLT_OFFSET.b[1]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_2.b[1];
pwrite = (char xdata *)&(VOLT_OFFSET.b[2]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_2.b[2];
pwrite = (char xdata *)&(VOLT_OFFSET.b[3]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_2.b[3];
PSCTL = 0; // MOVX writes target XRAM
//-----------------------------------------------------------------------------
// Initialize CalibrateADCforCurrentMeasurement_NOAMP
//-----------------------------------------------------------------------------
// This function calibrates the current channel with no external amp
// and stores the calibration coefficients in the
// parameters i_noamp_slope and i_noamp__offset.
//
// This calibration routine uses a 2 point cal.
// Wait until calibration voltage is ready for cal
//while (SW0 == 1); // Wait until SW0 pushed
//for (i=0;i<100000;i++); // Wait for Switch Bounce
// Once ready, Get the first calibration voltage
AMX0SL = IBAT; // Select appropriate input for AMUX
ADC0CF = (SYSCLK/5000000) << 3; // ADC conversion clock = 5.0MHz
ADC0CF &=0xF8; // Clear any Previous Gain Settings
ADC0CF |= 0x03; // Set PGA gain = 4
temp_INT_1.i = Measure(); // Acquire 16-bit Conversion
temp_INT_1.i *= 2; // Account for Differential Mode
// Wait until 2nd calibration voltage is ready for cal
//while (SW0 == 1); // Wait until SW0 pushed
//for (i=0;i<100000;i++); // Wait for Switch Bounce
// Once ready, Get the 2nd calibration voltage
temp_INT_2.i = Measure(); // Acquire 16-bit Conversion
temp_INT_2.i *=2; // Account for Differential Mode
// Calculate the SLOPE
temp_LONG_1.l = (unsigned)(temp_INT_2.i - temp_INT_1.i);
temp_LONG_1.l *= (unsigned)100; // Account for Math Truncation Error
temp_LONG_1.l /= (unsigned)(I2_CAL - I1_CAL);
temp_LONG_1.l /= (unsigned)INT_CURRENT_GAIN;// Account for Gain
// Calculate the OFFSET
temp_LONG_2.l = (signed)(temp_INT_1.i/INT_CURRENT_GAIN);
temp_LONG_2.l -= (signed)(temp_LONG_1.l * V1_CAL/100);
temp_LONG_1.l = 2050; // If no cal. use these
temp_LONG_2.l = 0; // as default values
// Memory at 0x1A00 is already erased
// Write the Volt SLOPE and OFFSET to Flash
PSCTL = 1; // MOVX writes to Flash
pwrite = (char xdata *)&(I_NOAMP_SLOPE.b[0]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_1.b[0];
pwrite = (char xdata *)&(I_NOAMP_SLOPE.b[1]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_1.b[1];
pwrite = (char xdata *)&(I_NOAMP_SLOPE.b[2]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_1.b[2];
pwrite = (char xdata *)&(I_NOAMP_SLOPE.b[3]);
FLKEY = 0xA5;
FLKEY = 0xF1; // enable flash write
*pwrite = temp_LONG_1.b[3];
pwrite = (char xdata *)&(I_NOAMP_OFFSET.b[0]);
FLKEY = 0xA5;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -