📄 main.c
字号:
#include "main.h"
#use fast_io (b)
#type int=16
unsigned int AdcValue;
void GetAdc()
{// 1536
unsigned char TTL0,TTL1,NEWV0,NEWV1;
//SET_TRIS_B(0XEC);
//OutPut_Low(Pin_B4);
#asm
//*****************************
//IN TTL0 TTL1 NEWV0 NEWV1
//AD_OUT=RB6 AD_IN=RB7
//OUT NEWV0 NEWV1
//TOL TM = 48MS (OSC=4M)
//*****************************
//PORTA EQU 005H
#DEFINE PORTB 0X6
#DEFINE STATUS 0x3
#define C 0x0
#define Z 0x2
//PORTC EQU 007H
AD_C: CLRF NEWV0
CLRF NEWV1
CLRF TTL0
MOVLW 0XF1
MOVWF TTL1
BSF PORTB,4
AD_C3: BTFSC PORTB,3
GOTO AD_C1
INCFSZ TTL0,1
GOTO AD_C3
INCFSZ TTL1,1
GOTO AD_C3
MOVLW 0XFF
MOVWF NEWV0
MOVLW 0X05
MOVWF NEWV1
GOTO AD_CEND
AD_C1: CLRF TTL0
MOVLW 0XFA
MOVWF TTL1
GOTO AD_C10
AD_C13: BTFSC PORTB,3
GOTO AD_C10
AD_C14: BSF PORTB,4
INCF NEWV0,1
BTFSC STATUS,Z
INCF NEWV1,1
AD_C11: INCF TTL0,1
BTFSC STATUS,Z
INCF TTL1,1
BTFSS STATUS,Z
GOTO AD_C13
AD_CEND: BCF PORTB,4
goto AdcEnd
AD_C10: BCF PORTB,4
GOTO AD_C11
AD_C12: NOP
GOTO AD_C13
#endasm
AdcEnd:
//SET_TRIS_B(0XFC);
AdcValue=NewV1*256;
AdcValue|=NewV0;
}
#define bool int1
#define HaveBatPin Pin_B0
#define FullLed Pin_B1
#define ChargeLed Pin_B2
#define ChargePin Pin_B5
#define CHARGE_ON 1
#define CHARGE_OFF !CHARGE_ON
#define LED_ON 1
#define LED_OFF !LED_ON
#define WHOLE_CHG_TIME 27464 // 300 minutes
#define Minute2 228 //2.5 minutes
#define LOWER_VOL 614 //Not check -delV voltage limmit (or small charging)
#define FULL_VOL 1164 //1.60/cell
bool blBatteryIn,blFull;
unsigned char PreInCnt,MsCnt,SChgCnt;
unsigned int ChgTimer,RealVol,MaxVol;
bool CheckDelV()
{
if (RealVol>MaxVol) MaxVol=RealVol;
else
{
if ((MaxVol-RealVol)>4) return 1;
}
return 0;
}
void SetFull()
{
blFull=1;
Output_bit (ChargePin,CHARGE_OFF);
Output_Bit(CHARGELED,LED_OFF);
Output_Bit(FULLLED,LED_ON);
SChgCnt=0;
}
void SetBatIn()
{
blBatteryIn=1;
blFull=0;
Output_Bit(ChargePin,CHARGE_ON);
ChgTimer=WHOLE_CHG_TIME;
RealVol=MaxVol=0;
SChgCnt=0;
}
void SetBatRemove()
{
PreInCnt=32;
SChgCnt=0;
Output_bit (CHARGELED,LED_OFF);
OutPut_BIT (FullLed,LED_OFF);
Output_Bit (ChargePin,CHARGE_OFF);
if (blBatteryIn) Delay_ms(2000);
blFull=blBatteryIn=0;
}
void SmallChg()
{
SChgCnt++;
if (SChgCnt==1)
{
Output_Bit(ChargePin,CHARGE_ON);
}
else if(SChgCnt==2)
{
Output_Bit(ChargePin,CHARGE_OFF);
Delay_us(200);
GetAdc();
RealVol=AdcValue;
}
else if (SChgCnt==10) SChgCnt=0;
}
void InitVariable()
{
blBatteryIn=blFull=0;
PreInCnt=32;
MsCnt=0;
SChgCnt=0;
ChgTimer=WHOLE_CHG_TIME;
RealVol=MaxVol=AdcValue=0;
}
void main() {
//Output_A(0xff);
//OutPut_B(0xff);
//SET_TRIS_A(0);
SET_TRIS_B(0XC8);
Output_Bit (ChargePin,CHARGE_OFF);
Output_bit (CHARGELED,LED_OFF);
OutPut_BIT (FullLed,LED_OFF);
OUTPUT_BIT (HaveBatPin,1);
setup_counters(RTCC_INTERNAL,RTCC_DIV_256);
InitVariable();
Output_bit (CHARGELED,LED_ON);
Delay_ms(200);
Output_bit (CHARGELED,LED_OFF);
Delay_ms(200);
Output_bit (CHARGELED,LED_ON);
Delay_ms(200);
Output_bit (CHARGELED,LED_OFF);
Delay_ms(200);
for (;;)
{
if( MsCnt )
{
MsCnt--;
GetAdc();
}
else
{
MsCnt=9;
if(blBatteryIn && !blFull)
{
if( RealVol<LOWER_VOL)
{
SmallChg();
}
else
{
ChgTimer--;
if ( (ChgTimer&0xf)==0 )
{
Output_Bit(ChargePin,CHARGE_OFF);
Delay_us(200);
GetAdc();
//Output_Bit(ChargePin,CHARGE_ON);
RealVol=AdcValue;
//if(ChgTimer< (WHOLE_CHG_TIME-Minute2) )
{
//if(RealVol>LOWER_VOL)
{
if(CheckDelV()) SetFull();
}
}
if (RealVol>FULL_VOL) SetFull();
}
else
{
Output_Bit(ChargePin,CHARGE_ON);
GetAdc();
}
if (ChgTimer==0) SetFull();
}
}
else if(blBatteryIn && blFull)
{
SmallChg();
}
else GetAdc();
}
if (AdcValue>300 && AdcValue<1450)
{
if (!blBatteryIn)
{
Output_bit (CHARGELED,LED_ON);
if (PreInCnt) PreInCnt--;
else
{
SetBatIn();
OutPut_bit(HaveBatPin,0);
}
}
}
else
{
SetBatRemove();
OutPut_bit(HaveBatPin,1);
}
while (!(Get_Timer0()&0x80));
while (Get_Timer0()&0x80);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -