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

📄 e_display.c

📁 基于AVR单片机的电池容量显示 开发环境 I
💻 C
📖 第 1 页 / 共 2 页
字号:
			    
			   //检测是否是充电
			   if (battery_adc_0s>(battery_adc_2s+5))//较一秒前电压高0.1V)
			     {
				   battery_stat=2;//充电开始
			     }
				 
//********************************检测是否放电**********************************			   
			   if (battery_adc_0s<(battery_adc_2s-5))//较一 秒前电压低0.1V)
			     {
				   battery_stat=5;//放电开始
			     }
			}
			 

//*************************进入充电状态***************************************
		if (battery_stat==2)
		  {
		    battery_dis_flag=2;//充电显示
			battery_stat=3; //进入充电检测完毕状态
			//battery_stat_3=0;//防充电死循环计时
			battery_charge_before=battery_adc_2s;
			charge_time=calculate_capacity(battery_charge_before); //计算剩余电量
			d1=charge_time;
			d1=d1*charge_settime;
			d1=d1/100;//计算需充电的秒数
			charge_time=(int)d1;
				 //d5=battery_charge_before-Vin_960;
				 //d5=d5*charge_settime/Vin_h_l; //需充电时间秒数
				 //charge_time=(int)d5;
			 
		  }  
		//进入充电检测完毕状态
		if (battery_stat==3)
		  {
		   	if ((battery_adc_0s+5)<battery_adc_2s)//较一秒钟前电压低0.1V)
			     {
				   battery_stat=4;//充电完毕
				   charge_end=0;
			     }
		    
		  }	
		if (battery_stat==4)
		  {
		    charge_end++;
			if (charge_end>=3) //延时3秒后进入停用状态
			  {
			    battery_stat=1;
				
			  }
			 
		  } 
			
//*********************进入放电状态************************
        if (battery_stat==5) //进入放电状态
		  {
		    battery_dis_flag=3;//放电显示
			battery_discharge_before=battery_adc_2s;
			capacity=calculate_capacity( battery_discharge_before );//计算电量
			battery_stat=6;
			V_down=0;
			battery_stat_6=0;
		  }  
		  
		if (battery_stat==6) //正在放电状态
		  {
		   // battery_stat_6++;
			//if (battery_stat_6>120) //死循环2分钟后进入状态8
			//    battery_stat=8;
			if (battery_adc_0s>(battery_adc_2s+5))
			  {
			    battery_stat=7;
				V_down=0;
			  }
			
		  }
		if (battery_stat==7)
		  {
			//battery_stat_6++;
			//if (battery_stat_6>120) //死循环2分钟后进入状态8
			//    battery_stat=8;
			if (battery_adc_0s>=battery_adc_2s)
			  {
			    V_down++;
			  }
			else
			  {
			    V_down=0;
			  }
		    if (V_down>=7) //持续5秒钟上升
			    battery_stat=8;
			if (battery_adc_0s<(battery_adc_2s-5))//较一秒种前电压小0.16V)
			     {
				   battery_stat=6;//正在放电
				   battery_stat_6=0;
			     }
		  } 
		  
		if (battery_stat==8)
		  {
		    battery_stat=1;
			for (battery_adc_ip=0;battery_adc_ip<battery_adc_count;battery_adc_ip++)
              {
                battery_adc[battery_adc_ip]=battery_rel;
              }
			battery_adc_ip=0;
		  }
	}
			 

   
//****************************测温度传感器***********************
 /*  
   if (clocks1s%200==198)
     {
        PRR = 0xae; //power controller 开ADC
	    ADMUX = 0xC0;  //select adc input 0 ;电池电压
	    ADCSRA|=(1<<ADSC); //启动AD转换
	 }
   if (clocks1s%200==1) //测电池温度
	    {
		  ADMUX = 0xC0;  //select adc input 0 ;电池电压
		  ADCSRA|=(1<<ADSC); //启动AD转换
		 
		}
	if (clocks1s%200==2) //读电池温度
	    {
		  battery_temperature=ADC&0x3ff; //读ADC
		  PRR = 0xaf; //power controller 关ADC
		  
		} 
	*/	
    
	//****************电动工具当前状态检测显示***********************
	  // if (battery_temperature>620) //电池温度超过45度
	     {
		  // battery_temperature_display();
		 }
	  //else
	     {
	       
 
           //***************************过载检测*********************
           /*
           if (battery_rel<V_discharge) //电压值下降过多,有过载
	          {
		         over_loading_display(); //过载显示
		      } 
	      else  */
	         led_display( ); //**********************发光管显示******************
		  }

}




void port_init(void)
{
 PORTB = 0x10;
 DDRB  = 0xC0;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0xFF;
}

//Watchdog initialize
// prescale: 64K

void watchdog_init(void)
{
 WDR(); //this prevents a timout on enabling
 WDTCSR = 0x0e; //WATCHDOG ENABLED - dont forget to issue WDRs
}


//ADC initialize
// Conversion time: 416uS
void adc_init(void)
{
 ADCSRA = 0x00; //disable adc
 ADMUX = 0xC0;  //select adc input 0
 ACSR  = 0x80;
 ADCSRB = 0x00;
 ADCSRA = 0xC5;
 
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer2_init();
 adc_init();

 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EIMSK = 0x00;
 
 TIMSK0 = 0x00; //timer 0 interrupt sources
 TIMSK1 = 0x00; //timer 1 interrupt sources
 TIMSK2 = 0x01; //timer 2 interrupt sources
 
 PCMSK0 = 0x00; //pin change mask 0 
 PCMSK1 = 0x00; //pin change mask 1 
 PCMSK2 = 0x00; //pin change mask 2
 PCICR = 0x00; //pin change enable 
 PRR = 0xae; //power controller
 //PRR = 0x00;
 SMCR=0x01;
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

//
void main(void)
{
 init_devices();

 //insert your functional code here...
 //*********************读基准电压10.00V***********************
 battery_stat=0; //进入到停止状态
 if ((PINB&0x10)==0)
   {
     CLI(); //disable all interrupts
	 PORTD=0x00;
	 V_eeprom=0;
	 for (j=1;j<=50;j++)
	   {
	      ADMUX = 0xC0;  //select adc input 1
	      ADCSRA|=(1<<ADSC); //启动AD转换
	      for (i=0;i<=1000;i++)
	        {
	           WDR(); //this prevents a timout on enabling
	        }
	     battery_rel=ADC&0x3ff; //读ADC
		 V_eeprom+=battery_rel; //读ADC
       }
	 V_eeprom=V_eeprom/50;
	 WDR(); //this prevents a timout on enabling
	 EEPROM_WRITE(0x10,V_eeprom);
	 WDR(); //this prevents a timout on enabling
	 EEPROM_WRITE(0xc8,V_eeprom);
	 PORTD=0x3ff;
	 while (1)
	 {
	   ;
	 }
   }
 
 //****************************测电池电压*****************************
 watchdog_init();
 PORTB = 0x00;
 clocks1s=0xff;
 clocks2s=0xffff;
 EEPROM_READ(0x10,V_eeprom);
 WDR(); //this prevents a timout on enabling
 d1=V_100;
 d1=d1*V_eeprom;
 V_1120=d1/V_1000;
 EEPROM_WRITE(0x20,V_1120);
 WDR(); //this prevents a timout on enabling
 //V_1120=(int)d1;
 d2=V_80;
 d2=d2*V_eeprom;
 V_1104=d2/V_1000;
 EEPROM_WRITE(0x30,V_1104);
 WDR(); //this prevents a timout on enabling
 //V_1104=(int)d2;
 d3=V_60;
 d3=d3*V_eeprom;
 V_1088=d3/V_1000;
 EEPROM_WRITE(0x40,V_1088);
 WDR(); //this prevents a timout on enabling
 //V_1088=(int)d3;
 d4=V_40;
 d4=d4*V_eeprom;
 V_1072=d4/V_1000;
 EEPROM_WRITE(0x50,V_1072);
 WDR(); //this prevents a timout on enabling
 //V_1072=(int)d4;
 d5=V_20;
 d5=d5*V_eeprom;
 V_1040=d5/V_1000;
 EEPROM_WRITE(0x60,V_1040);
 WDR(); //this prevents a timout on enabling
 //V_1040=(int)d5;
 d6=V_10;
 d6=d6*V_eeprom;
 V_1008=d6/V_1000;
 EEPROM_WRITE(0x70,V_1008);
 WDR(); //this prevents a timout on enabling
 //V_1008=(int)d6;
 
battery_adc_ip=0;
battery_stat=0; //进入到停止状态
//battery_adc_0s=battery_rel;
//battery_adc_1s=battery_rel;
//battery_adc_2s=battery_rel;
V_up=0;
V_down=0;
	
 while(1)
  {
     if (watchdog_flag==1)
	    {
	      WDR(); //this prevents a timout on enabling
		  watchdog_flag=0;
		}
		/*
	WDR(); //this prevents a timout on enabling
	EEPROM_WRITE(0xa3,battery_adc_0s);
	WDR(); //this prevents a timout on enabling
	WDR(); //this prevents a timout on enabling
	EEPROM_WRITE(0x83,battery_adc_0s);
	WDR(); //this prevents a timout on enabling
	EEPROM_WRITE(0x90,capacity);
	    */
		//WDR(); //this prevents a timout on enabling
	//EEPROM_WRITE(0x83,battery_adc_0s);
	  asm("SLEEP");
	
  }
}

⌨️ 快捷键说明

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