|
|
STC8G1K08A
ADC 多通道 進(jìn)行采樣時(shí),獲取到得數(shù)據(jù)與實(shí)際不一致,單通道時(shí)是對(duì)的
#include "config.h"
#include "STC8G_H_GPIO.h"
#include "STC8G_H_Timer.h"
#include "STC8G_H_ADC.h"
#include "STC8G_H_NVIC.h"
#include "STC8G_H_Delay.h"
#include "STC8G_H_UART.h"
#include "STC8G_H_Switch.h"
//==================== 硬件引腳定義 ====================
sbit CHONG = P3^3; // 充電控制(0=開啟充電,1=關(guān)閉充電)
sbit FANG = P3^2; // 放電控制(0=開啟放電,1=關(guān)閉放電)
//==================== ADC 通道定義 ====================
#define ADC_CH_BAT 4 // P5.4 電池電壓 → 通道4
#define ADC_CH_POWER 5 // P5.5 外部電源檢測 → 通道5
//==================== 閾值參數(shù) ====================
#define BAT_MAX 651 // 電池≥651 → 關(guān)充電 P3.2=1
#define BAT_MIN 512 // 電池≤512 → 關(guān)放電 P3.3=1
//==================== 全局變量 ====================
volatile u16 bat_adc = 0; // 電池電壓ADC
volatile u16 pwr_adc = 0; // 電源檢測ADC
volatile u16 TIME_COUNT = 0;
volatile u16 adc_flag = 0;
//==================== IO初始化 ====================
void GPIO_CONFIG(void)
{
P3_MODE_OUT_PP(GPIO_Pin_2); // P3.2 充電控制
P3_MODE_OUT_PP(GPIO_Pin_3); // P3.3 放電控制
P5_MODE_IN_HIZ(GPIO_Pin_4); // P5.4 電池ADC輸入
P5_MODE_IN_HIZ(GPIO_Pin_5); // P5.5 電源ADC輸入
}
//==================== ADC初始化 ====================
void ADC_CONFIG(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_SMPduty = 31;
ADC_InitStructure.ADC_CsSetup = 0;
ADC_InitStructure.ADC_CsHold =1;
ADC_InitStructure.ADC_Speed = ADC_SPEED_2X16T;
ADC_InitStructure.ADC_AdjResult = ADC_RIGHT_JUSTIFIED;
ADC_Inilize(&ADC_InitStructure);
ADC_PowerControl(ENABLE);
}
//==================== TIMER0 1MS 中斷 ====================
void TIM_CONFIG(void)
{
TIM_InitTypeDef TIM_InitStructure;
TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload;
TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T;
TIM_InitStructure.TIM_ClkOut = DISABLE;
TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 2400); // 1ms中斷
TIM_InitStructure.TIM_Run = ENABLE;
Timer_Inilize(Timer0,&TIM_InitStructure);
NVIC_Timer0_Init(ENABLE,Priority_1);
}
void UART_CONFIG(void)
{
COMx_InitDefine COMx_InitStructure;
COMx_InitStructure.UART_Mode = UART_8bit_BRTx;
COMx_InitStructure.UART_BRT_Use = BRT_Timer1;
COMx_InitStructure.UART_BaudRate = 115200ul;
COMx_InitStructure.UART_RxEnable = DISABLE;
UART_Configuration(UART1,&COMx_InitStructure);
NVIC_UART1_Init(ENABLE,Priority_2);
UART1_SW(UART1_SW_P30_P31);
}
//==================== 主函數(shù) ====================
void main(void)
{
EAXSFR();
GPIO_CONFIG();
ADC_CONFIG();
TIM_CONFIG();
UART_CONFIG();
EA = 1;
// // 上電默認(rèn)關(guān)閉充放電
// CHONG = 1;
// FANG = 1;
while(1)
{
// ================== 有外部電源:充電模式 ==================
if(pwr_adc > 100)
{
FANG = 1; // 關(guān)閉放電
if(bat_adc < BAT_MAX)
{
CHONG = 0; // 開啟充電
}
else
{
CHONG = 1; // 滿電,關(guān)閉充電
}
}
// ================== 無外部電源:放電模式 ==================
else
{
CHONG = 1; // 關(guān)閉充電
if(bat_adc > BAT_MIN)
{
FANG = 0; // 開啟放電
}
else
{
FANG = 1; // 欠壓,關(guān)閉放電
}
}
}
}
//==================== 1MS 中斷:ADC 單次采樣 ====================
// 全局區(qū)新增一個(gè)切換標(biāo)記
//==================== 1MS 中斷 ====================
//==================== 1MS 中斷:ADC 單次采樣 ====================
void Timer0_ISR_Handler (void) interrupt TMR0_VECTOR
{
TF0 = 0;
if(++TIME_COUNT >=20)
{
TIME_COUNT=0;
if(adc_flag == 0)
{
// //本次只采電源
pwr_adc = Get_ADCResult(ADC_CH_POWER);
adc_flag = 1;
}
else
{
//下次只采電池
bat_adc = Get_ADCResult(ADC_CH_BAT);
adc_flag = 0;
}
//異常值修正
if(pwr_adc>=4096) pwr_adc=0;
if(bat_adc>=4096) bat_adc=0;
//打印不變
TX1_write2buff('P');
TX1_write2buff(':');
TX1_write2buff((pwr_adc / 1000) % 10 + '0');
TX1_write2buff((pwr_adc / 100) % 10 + '0');
TX1_write2buff((pwr_adc / 10) % 10 + '0');
TX1_write2buff(pwr_adc % 10 + '0');
TX1_write2buff('\r');
TX1_write2buff('\n');
TX1_write2buff('B');
TX1_write2buff(':');
TX1_write2buff((bat_adc / 1000) % 10 + '0');
TX1_write2buff((bat_adc / 100) % 10 + '0');
TX1_write2buff((bat_adc / 10) % 10 + '0');
TX1_write2buff(bat_adc % 10 + '0');
TX1_write2buff('\r');
TX1_write2buff('\n');
}
}
|
-
捕獲.PNG
(14.47 KB, 下載次數(shù): 0)
下載附件
2026-6-2 10:57 上傳
讀取打印的值,但打印的值出來時(shí)反的
|