⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prj.c

📁 移动电源主程序
💻 C
📖 第 1 页 / 共 2 页
字号:

 #include "ht46r064.h"

///-----------CS:B8C0H  基本OK //-2012-2-23 17:04

#define			uint			unsigned int 
#define			ulong			unsigned  long

//#define		c_OUT_Current				0									// PA0 ---AN0
//#define		c_Bat_Voltage				1									// PA1 ---AN1

 /*  //--Test board
#define		_pin_Out_5V					_pa2 //b5   			//_pb1
#define		_pin_ChargeFull			_pa5				//_pa4
#define		_pin_CtrKey						_pa4			//_pa3
#define		_pin_PowerIN					_pa3			//_pa2

#define		_pin_LightKEY				_pa6

#define 		_pin_Light				_pb4
#define 		_pin_LLight				_pb5


#define 		_pin_LEDH				_pb0			//_pb3
#define 		_pin_LEDMH				_pb1			//_pb4
#define 		_pin_LEDML				_pb2			//_pb4
#define 		_pin_LEDL				_pb3			//_pb5

*/
#if 0
#define		_pin_ChargeFull				_pa6
#define		_pin_Out_5V					_pa5
#define		_pin_LightKEY				_pa4
#define		_pin_CtrKey					_pa3
#define		_pin_PowerIN				_pa2

#define 		_pin_LEDH				_pb0
#define 		_pin_LEDMH				_pb1
#define 		_pin_LEDML				_pb2
#define 		_pin_LEDL				_pb3

#define 		_pin_Light				_pb4
#define 		_pin_LLight				_pb5
#else

#define		c_OUT_Current				0									// PA0 ---AN0
#define		c_Bat_Voltage				1									// PA1 ---AN1

#define		_pin_PowerIN				_pa5		// 外部向电池充电检测
#define		_pin_Out_5V					_pa4		// 5v输出
#define		_pin_ChargeFull				_pa3		// 自已的电池充满状态
#define		_pin_CtrKey					_pa6		// 按键

#define 		_pin_LEDH				_pb0
#define 		_pin_LEDMH				_pb1
#define 		_pin_LEDML				_pb2
#define 		_pin_LEDL				_pb3

#define 		_pin_Light				_pb4
#define 		_pin_LLight				_pb5
#endif


//#define 		c_V35			540*4		// (1.8V/3.3V) *1024 =558 //(1.75V/3.3V) *1024 =543
#define 		c_V35			597*4		// (1.75V/3.0V) *1024 =597
#define 		c_V35_L			580*4		// (1.70V/3.0V) *1024 =597

//#define 		c_V38			605*4		// (1.95V/3.3V) *1024 =605 // (1.90V/3.3V) *1024 =589
#define 		c_V38			665*4		// (1.95V/3.0V) *1024 =665
#define 		c_V38_L			630*4		// (1.85V/3.0V) *1024 =630

//#define 		c_V40			620*4		// (2.0V/3.3V) *1024 =620
#define 		c_V40			682*4		// (2.0V/30V) *1024 =682
#define 		c_V40_L			648*4		// (1.9V/3.0V) *1024 =648

//#define 		c_V42			633*4		// (2.1V/3.3V) *1024 =651   // (2.04V/3.3V) *1024 =633   // (2.06V/3.3V) *1024 =639
#define 		c_V42			696*4		// (2.04V/3.0V) *1024 =696   
#define 		c_V42_L			682*4		// (2.0V/30V) *1024 =682   

#define 		c_C05A			24*4		// 24 : 0.8A ; 18 : 0.6A ; //12 : 0.4A 
#define 		c_C1A			40*4		// (0.1V/3.3V) *1024 =30
#define 		c_C2A			68*4		// (0.2V/3.0V) *1024 =68
#define 		c_C100mA		3*4			// (0.01V/3.3V) *1024 =3


#pragma  vector  EXTINT_ISR @ 0x04
#pragma  vector  TIMER_ISR @ 0x08

ulong		rl_adcValue,rl_adcValue_sum,rl_OUT_Current_sum,rl_VBat_sum;
ulong		rl_Out_Current,rl_VBat,rl_Out_Current_OVER_Time;

uint		r_count,r_250MS,r_3S,r_250MS_20S,r_ChargeFull_20MIN;
uint		r_MotoKey_Delay,b_ChargeFull_Delay,r_PowerInput_Delay,r_CtrKey_Delay;
uint		r_Low_Voltage_Time,r_LongKeyTime;

uint 		r_Close_5V_Delay,r_VBat_Delay,r_LightKey_Delay,r_LongKeyTime_Light;
uint		r_8Count,r_VBat_Step,r_blink_Time,r_LightNum;


bit  		b_HALL,b_PowerIN,b_Low_Voltage;
bit			b_2ms,b_250MS_1S,b_250MS_3S,b_250MS_20S,b_250MS_2S,b_EXT_PWR,b_pin_ChargeFull;
bit			b_Out_5V,b_Short_Error,b_ChargeFull;
bit			b_Charge,b_CtrKey_1,b_CtrKey_Action,b_LightKey_Action;
bit			b_250MS_M3S,b_250MS_5S;
bit			b_LongKey_Status,b_pin_CtrKey_Sleep,b_pin_PowerIN_Sleep,b_LightKey_1,b_pin_LightKey_Sleep;
bit			b_LongKey_Status_Light,b_250MS_5S_Light;

/* RAM 初始化, 功能初始化*/
void pro_function_initial()
{
	_pawk=0b11111111;
	_papu=0b11011100;						/// 
	_pa=0b11011111;							/// _pa6 Hall 检查脚 上拉 输入 
	_pac=0b11011111;						/// _pa7 外部电源检查脚 上拉 输入 //  pa4 主按键(带上拉) input;
	
	_pbpu=0b11110000;
	_pb=0b11110000;							///  pb5 内部充电控制线 无上拉 初始化为0
	_pbc=0b11000000;						/// _pb4	内部充电状态检查脚 上拉 输入      //pb3 5V 输出控制线
	

	_ctrl0=0;
	_ctrl1=0xca;							// 中断 双向触发...// WDT disable
	
	_adcr=0x10;							//   AN0-AN1; 

	_acsr=0x01;								// FSYS/16
	
	_tmr0=256-250;							// 250*8us = 2MS
	_tmr0c=0x85;								// FSYS/32,  4MHz= 8us  
	
	_intc0=0x05;
	_intc1=0;
	
	for(_mp0=0x40,_acc=96;_acc;_acc--)
	{
		_iar0=0;
		_mp0++;
	}
	if(_pin_PowerIN) b_PowerIN=1;
	if(_pin_CtrKey) b_CtrKey_1=1;
//	if(_pin_LightKEY) b_LightKey_1;
	if(_pin_ChargeFull) 	b_pin_ChargeFull=1;
}


/*==================
	AD conversion
===================*/
unsigned long AdcSub(unsigned char r_adChannel)
{
	unsigned long r_adcTemp;
	uint		i;
	r_adcTemp=0;
	rl_adcValue=0;
	_adcr&=0xf8;
	_adcr|=r_adChannel;
	
	for(i=0;i<4;i++)
	{
		_start=0;
		_start=1;
		_start=0;
		while(_eocb) ;
		r_adcTemp=_adrh;
		r_adcTemp<<=8;
		r_adcTemp+=_adrl;
		r_adcTemp>>=4;									// result for 12bit
		rl_adcValue=rl_adcValue+r_adcTemp;
	}
	rl_adcValue=rl_adcValue/4;			//8 times filter
	return rl_adcValue;
}

///================================================================
/*
void	pro_VBat()
{
	b_Low_Voltage=0;
	switch(r_VBat_Step)
	{
		case 0:
						b_Low_Voltage=1;
						if(rl_VBat<c_V35)	r_VBat_Delay=0;
							else
								{
									if(r_VBat_Delay>100)		
									{
										r_VBat_Step=1;
										r_VBat_Delay=0;
									}
								}
						break;
		case 1:
						if(rl_VBat<c_V35)	
							{
								if(r_VBat_Delay>100)
									{
										r_VBat_Step=0;
										r_VBat_Delay=0;
									}
							}
							else
								{
									if(rl_VBat<c_V38) r_VBat_Delay=0;
										else
											{
												if(r_VBat_Delay>100)	
													{
														r_VBat_Step=2;
														r_VBat_Delay=0;
													}
											}
								}
						break;
		case 2:
						if(rl_VBat<c_V38)	
							{
								if(r_VBat_Delay>100)		
									{
										r_VBat_Step=1;
										r_VBat_Delay=0;
									}
							}
							else
								{
									if(rl_VBat<c_V40) r_VBat_Delay=0;
										else
											{
												if(r_VBat_Delay>100)
													{
														r_VBat_Step=3;
														r_VBat_Delay=0;
													}
											}
								}
						break;
						
		case 3:
						if(rl_VBat<c_V40)	
							{
								if(r_VBat_Delay>100)		
									{
										r_VBat_Step=2;
										r_VBat_Delay=0;
									}
							}
							else
								{
									if(rl_VBat<c_V42) r_VBat_Delay=0;
										else
											{
												if(r_VBat_Delay>100)
													{
														r_VBat_Step=4;
														r_VBat_Delay=0;
													}
											}
								}
						break;
						
		case 4:
						if(rl_VBat>c_V42)	r_VBat_Delay=0;
							else
								{
									if(r_VBat_Delay>100)
										{
											r_VBat_Step=3;
											r_VBat_Delay=0;
										}
								}
						break;
	}
		
}
*/
/**/
///================================================================
/// 放电时 的电压 等级.
void pro_VBat_L()
{
	b_Low_Voltage=0;
	switch(r_VBat_Step)
	{
		case 0:
			b_Low_Voltage=1;
			if(rl_VBat<c_V35_L)	r_VBat_Delay=0;
			else
			{
				if(r_VBat_Delay>100)		
				{
					r_VBat_Step=1;
					r_VBat_Delay=0;
				}
			}
		break;
		case 1:
			if(rl_VBat<c_V35_L)	
			{
				if(r_VBat_Delay>100)
				{
					r_VBat_Step=0;
					r_VBat_Delay=0;
				}
			}
			else
			{
				if(rl_VBat<c_V38_L) r_VBat_Delay=0;
				else
				{
					if(r_VBat_Delay>100)	
					{
						r_VBat_Step=2;
						r_VBat_Delay=0;
					}
				}
			}
		break;
		case 2:
			if(rl_VBat<c_V38_L)	
			{
				if(r_VBat_Delay>100)		
				{
					r_VBat_Step=1;
					r_VBat_Delay=0;
				}
			}
			else
			{
				if(rl_VBat<c_V40_L) r_VBat_Delay=0;
				else
				{
					if(r_VBat_Delay>100)
					{
						r_VBat_Step=3;
						r_VBat_Delay=0;
					}
				}
			}
		break;

		case 3:
			if(rl_VBat<c_V40_L)	
			{
				if(r_VBat_Delay>100)		
				{
					r_VBat_Step=2;
					r_VBat_Delay=0;
				}
			}
			else
			{
				if(rl_VBat<c_V42_L) r_VBat_Delay=0;
				else
				{
					if(r_VBat_Delay>100)
					{
						r_VBat_Step=4;
						r_VBat_Delay=0;
					}
				}
			}
		break;

		case 4:
			if(rl_VBat>c_V42_L)	r_VBat_Delay=0;
			else
			{
				if(r_VBat_Delay>100)
				{
					r_VBat_Step=3;
					r_VBat_Delay=0;
				}
			}
		break;
	}
}

///================================================================

void pro_Check_ChargeFull()
{
	if(!b_Charge) 
	{
		return;
	}	
	if(_pin_ChargeFull)
	{
		if(!b_pin_ChargeFull)
		{
			b_ChargeFull_Delay++;
			if(b_ChargeFull_Delay>=50) 
			{
				b_pin_ChargeFull=1;
				b_ChargeFull=1;															///  pin_Full=0, 充饱.
			}
		}
		else	
		{
			b_ChargeFull_Delay=0;
		}
	}
	else
	{
		if(b_pin_ChargeFull)
		{
			b_ChargeFull_Delay++;
			if(b_ChargeFull_Delay>=50)
			{
				b_pin_ChargeFull=0;
			}
		}
		else
		{
			b_ChargeFull_Delay=0;
		}
	}
}
///================================================================
void pro_CtrKEY()
{
	if(_pin_CtrKey)
	{
		if(!b_CtrKey_1)
		{
			r_CtrKey_Delay++;
			if(r_CtrKey_Delay>100) 
			{
				b_CtrKey_1=1;
			}
			
		}
		else 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -