找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5055|回复: 8
打印 上一主题 下一主题
收起左侧

海尔12位单片机写的移动电源程序

  [复制链接]
跳转到指定楼层
楼主
需要用海尔的单片机同学可以参考


完整代码下载: 移动电源Demo程序.rar (8.38 KB, 下载次数: 91)


  1. /*
  2.                  ---------------------
  3.    电源--|VDD                          VSS|--地
  4.    PinCharge--|PA5/OSC1/KIN5           PA0/AIN0|--NC
  5.    PinChargeFull--|PA4/OSC2/KIN4       PA1/AIN1|--PinCurrent
  6.    PinHalt--|PA3/MRST/KIN3             PA2/AIN2|--PinBoostEN
  7.    PinLed1--|PB3/AIN7                  PB0/AIN3|--PinSelMode
  8.    PinLed2--|PB2/AIN6                  PB1/AIN4|--NC
  9.    PinLed3--|PA7/AIN5/KIN7             PA6|--PinKey
  10.                  ---------------------
  11. */

  12. /*****************************************************************************
  13. ;*        Title        :                                                                                 
  14. ;*                                                                                               
  15. ;*                                                  
  16. ;*                                                                                                                                                       
  17. ;*        Device        :        HR7P155-SOP14                                                                                                   
  18. ;*        OSC                :        8Mhz( Internal 8Mhz )                                                                           
  19. ;*                                                                                                                                                       
  20. ;*        Compiler:        IDE                                                                                                                                           
  21. ;*        Hardware:                                                                                                
  22. ;* Version :   A00                                                         
  23. ;***************************************************************************/

  24. /*********************************************
  25. ;* Include headfiles
  26. ;********************************************/
  27. #include "Init.h"


  28. /*********************************************
  29. ;* Private variables
  30. ;********************************************/







  31. /**********************************************
  32. ;*
  33. ;* Function Name: main
  34. ;* Inputs:        None
  35. ;* Returns:       None
  36. ;* Description:   the main process
  37. ;*       
  38. ;**********************************************/
  39. void main(void)
  40. {
  41.     InitialMCU();
  42.    
  43.     while(1)
  44.     {
  45.                
  46.         CLRWDT();
  47.                 RCEN = 1;      
  48.         
  49.         if(FlagSysClk1ms)
  50.         {
  51.             FlagSysClk1ms = 0;
  52.             
  53.             KeyScan();           //按键扫描子程序           
  54.             
  55.             ADCurrentSample();   //电流采样处理子程序
  56.             
  57.         }
  58.                
  59.         if(FlagSysClk20ms)
  60.                   {
  61.                            FlagSysClk20ms = 0;                              
  62.               
  63.                            GetSysStatus();                //状态处理子程序                          
  64.            
  65.             LowLoadCheck();                //轻负载、过流保护处理子程序
  66.             
  67.             ControlCharge();     //充电控制子程序
  68.               
  69.                   }
  70.                                    
  71.                   if(FlagSysClk500ms)
  72.                   {
  73.                            FlagSysClk500ms = 0;
  74.                         
  75.             //for debug
  76.             //PowerBattaryQ = 2600; //blink
  77.             //PowerBattaryQ = 2700; //level1
  78.             //PowerBattaryQ = 2800; //level2
  79.             //PowerBattaryQ = 2850; //level3
  80.             //PowerBattaryQ = 3000; //level4
  81.          
  82.                            HandFlagProcess();  //功能模式处理子程序
  83.                
  84.             DealDispData();     //显示数据处理子程序  必须放在电量级别判断之后
  85.             
  86.             HaltProcess();      //休眠处理子程序
  87.            
  88.             AdjustToBalance();  //电池电量级别平衡处理子程序
  89.             
  90.            
  91.                      
  92.                   }
  93.          
  94.         
  95.     }
  96.    
  97.    
  98. }

  99. /**********************************************
  100. ;*
  101. ;* Function Name: interrupt service routine
  102. ;* Inputs:        None
  103. ;* Returns:       None
  104. ;* Description:   the isr process
  105. ;*       
  106. ;**********************************************/
  107. void isr(void) interrupt
  108. {
  109.     if (T8P2IE & T8P2IF)
  110.     {
  111.         T8P2IF = 0;
  112.         FlagSysClk1ms = 1;      
  113.         
  114.         CntSysClk20ms++;
  115.               
  116.         //每20ms执行一次
  117.         if(CntSysClk20ms >= 20)
  118.         {
  119.             FlagSysClk20ms = 1;
  120.             CntSysClk20ms = 0;

  121.             CntSysClk500ms++;
  122.             if(CntSysClk500ms >= 25)
  123.             {
  124.                 FlagSysClk500ms = 1;
  125.                 CntSysClk500ms = 0;               
  126.                
  127.             }
  128.             
  129.             
  130.         }  
  131.         
  132.     }
  133.    
  134.     if(KIE & KIF)
  135.     {
  136.         PABuf = PA;  //必须要读一次电平才能清除KIF
  137.         
  138.         KIF = 0;     
  139.       
  140.     }
  141.       
  142. }



  143. /**********************************************
  144. ;*
  145. ;* Function Name: GetSysStatus routine
  146. ;* Inputs:        None
  147. ;* Returns:       None
  148. ;* Description:   the GetSysStatus process
  149. ;*       
  150. ;**********************************************/
  151. #define CONST_NOCHARGE_CUR 6

  152. void InitChargePWM(void)
  153. {
  154.     T8P1P   = 131;    //周期132 * 1/8M * 2 = 33us
  155.     T8P1RL  = 20;     //初始占空比20 * 1/8M * 2 = 5us
  156.     T8P1C   = 0x84;   //PWM输出模式,使能T8P1
  157.     T8P1OC  = 0x01;   //选择PB0输出PWM
  158. }

  159. void GetSysStatus(void)
  160. {
  161.     if(PinCharge)
  162.     {   
  163.         CntDischargeShort = 0;
  164.         CntChargeShort++;
  165.         if(CntChargeShort >= COUNT_CHARGE_SHORT) //防止拔掉适配器后显示充满状态,保证PinCharge引脚变为低电平
  166.         {
  167.             CntChargeShort = 0;
  168.             if(!FlagChargeDisp)
  169.             {
  170.                 //充电处理程序
  171.                 FlagEnterBlink = 0;
  172.                
  173.                 FlagDispPower = 0;
  174.                 FlagChargeDisp = 1;
  175.                 FlagCharging = 1;               
  176.                
  177.                 FlagChargeFull = 0;   
  178.                
  179.                 InitChargePWM();
  180.                 CntDispCharge = 0;
  181.                               
  182.             }
  183.                  
  184.         }
  185.          
  186.     }   
  187.         
  188.     else
  189.     {
  190.         CntChargeShort = 0;
  191.         CntDischargeShort++;
  192.         if(CntDischargeShort >= COUNT_CHARGE_SHORT)
  193.         {
  194.             CntDischargeShort = 0;
  195.             
  196.             if(!FlagDispPower)
  197.             {
  198.                 FlagChargeDisp = 0;
  199.                 FlagDispPower = 1;     
  200.                
  201.                 FlagCharging = 0;
  202.                 FlagChargeFull = 0;
  203.                
  204.             }
  205.                         
  206.         }
  207.         
  208.         
  209.     }
  210.    
  211.    
  212.     //充电状态下,充电拔出判断
  213.     if(FlagCharging && (!FlagChargeFull))
  214.     {
  215.         if(ChargeCur < CONST_NOCHARGE_CUR)  
  216.         {
  217.             CntNoCharge++;
  218.             if(CntNoCharge >= 20)//150)
  219.             {
  220.                 CntNoCharge = 0;
  221.                     
  222.                 //充电拔出处理
  223.                 FlagCharging = 0;
  224.                               
  225.                 ClosePinChargePWM();
  226.                               
  227.                 CntNoLoad = 0;                           
  228.                     
  229.                 //边充边放时,拔掉充电不能进休眠
  230.                 if(!FlagDischarge)
  231.                 {
  232.                     Sleep();   
  233.                 }
  234.                           
  235.                     
  236.              }
  237.                   
  238.          }
  239.          
  240.          else
  241.          {
  242.              CntNoCharge = 0;  
  243.          }
  244.          
  245.             
  246.     }
  247.         
  248.          
  249.         

  250.    
  251. }

  252. /**********************************************
  253. ;*
  254. ;* Function Name: LowLoadCheck routine
  255. ;* Inputs:        None
  256. ;* Returns:       None
  257. ;* Description:   the LowLoadCheck process
  258. ;*       
  259. ;**********************************************/
  260. void LowLoadCheck(void)
  261. {
  262.       
  263.      //放电模式下过流、轻负载判断        
  264.    
  265.      if((LoadCurrentVol >= LOAD_OVER_CUR_VALUE))
  266.      {
  267.         CntShort++;
  268.         if(CntShort >= COUNT_SHORT)
  269.         {
  270.             CntShort = 0;
  271.               
  272.             //防止再次按键不能打开升压
  273.             CntADSample = 0;
  274.             ADCurSum = 0;
  275.             LoadCurrentVol = 0;
  276.             
  277.             PinBoostEN = 0;
  278.             FlagDischarge = 0;
  279.            
  280.         }      
  281.         
  282.      }
  283.         
  284.      else if(LoadCurrentVol >= NO_LOAD_VALUE)
  285.      {
  286.          CntShort = 0;
  287.          
  288.          CntNoLoad = 0;
  289.          CntHaveLoad++;
  290.          if(CntHaveLoad >= COUNT_HAVE_LOAD)
  291.          {
  292.              CntHaveLoad = 0;
  293.              CntDispDelay = 0;
  294.              FlagLoad = 1;
  295.             
  296.          }         
  297.             
  298.      }
  299.    
  300.      else
  301.      {
  302.          CntShort = 0;
  303.          
  304.          CntHaveLoad = 0;                       
  305.          CntNoLoad++;            
  306.          //轻负载时去抖
  307.          if(CntNoLoad >= COUNT_LIGHT_LOAD)
  308.          {
  309.              FlagLoad = 0;   
  310.          }
  311.             
  312.          //无负载10S关闭升压输出,并休眠
  313.          if(CntNoLoad >= COUNT_NO_LOAD)
  314.          {
  315.              CntNoLoad = 0;
  316.                                             
  317.              if(!FlagCharging)
  318.              {
  319.                 Sleep();  
  320.                
  321.              }
  322.                                 
  323.          }
  324.         
  325.      }   


  326. }


  327. /**********************************************
  328. ;*
  329. ;* Function Name: DisplaySub routine
  330. ;* Inputs:        None
  331. ;* Returns:       None
  332. ;* Description:   the DisplaySub process
  333. ;*
  334. ;*  0 //放电模式(有负载)
  335. ;*  1 //放电无负载、充电拔出显示处理
  336. ;*  2 //充电跑马灯显示处理
  337. ;*  3 //充电充满显示
  338. ;**********************************************/
  339. void DisplaySub(u8 uMode)
  340. {
  341.     //放电有负载、充电显示处理
  342.     if(uMode == 1)
  343.     {
  344.              //充电充满显示
  345.         if(FlagChargeFull)
  346.         {
  347.             SetPinLed1();
  348.             SetPinLed2();
  349.             SetPinLed3();
  350.             SetPinLed4();
  351.         }
  352.         
  353.         else
  354.         {
  355.             switch(PreLedLevel)  
  356.             {
  357.                 case 1:               
  358.                  CntDispCharge++;
  359.                  switch(CntDispCharge)
  360.                  {
  361.                      case 1:
  362.                       SetPinLed1();
  363.                       break;
  364.                      
  365.                      case 2:
  366.                       SetPinLed2();
  367.                       break;
  368.                      
  369.                      case 3:
  370.                       SetPinLed3();
  371.                       break;
  372.                      
  373.                      case 4:
  374.                       SetPinLed4();
  375.                       break;
  376.                      
  377.                      case 5:
  378.                        CntDispCharge = 0;
  379.                        ClrAllLed();
  380.                        break;
  381.                        
  382.                      default:
  383.                        break;
  384.                  }
  385.                                  
  386.                  break;
  387.                   
  388.                 case 2:                 
  389.                   SetPinLed1();
  390.                   CntDispCharge++;
  391.                   switch(CntDispCharge)
  392.                   {
  393.                      case 1:
  394.                       SetPinLed2();
  395.                       break;
  396.                      
  397.                      case 2:
  398.                       SetPinLed3();
  399.                       break;
  400.                      
  401.                      case 3:
  402.                       SetPinLed4();
  403.                       break;
  404.                      
  405.                      case 4:
  406.                       CntDispCharge = 0;
  407.                       ClrPinLed2();
  408.                       ClrPinLed3();
  409.                       ClrPinLed4();
  410.                       break;
  411.                        
  412.                      default:
  413.                        break;
  414.                   }
  415.                   
  416.                  break;
  417.                   
  418.                 case 3:               
  419.                  SetPinLed1();
  420.                  SetPinLed2();
  421.                  CntDispCharge++;
  422.                  switch(CntDispCharge)
  423.                  {
  424.                     case 1:
  425.                      SetPinLed3();
  426.                      break;
  427.                     
  428.                     case 2:
  429.                      SetPinLed4();
  430.                      break;
  431.                      
  432.                     case 3:
  433.                      CntDispCharge = 0;
  434.                      ClrPinLed3();
  435.                      ClrPinLed4();
  436.                      break;
  437.                      
  438.                     default:
  439.                      break;
  440.                      
  441.                  }
  442.                  
  443.                  break;
  444.                   
  445.                 case 4:
  446.                  SetPinLed1();
  447.                  SetPinLed2();
  448.                  SetPinLed3();
  449.                  CPLPinLed4();   
  450.                  break;
  451.                   
  452.                 default:
  453.                  PreLedLevel = NowLedLevel;
  454.                  break;
  455.             }                                                  
  456.         }
  457.                                                                                    
  458.     }               
  459.    
  460.     //放电模式(有负载或无负载)
  461.     else if(uMode == 0)
  462.     {      
  463.         
  464.         if((FlagDischarge || (!FlagLoad)) && FlagEnterBlink)
  465.         {
  466.              CPLPinLed1();  
  467.              ClrPinLed2();
  468.              ClrPinLed3();
  469.              ClrPinLed4();
  470.         }
  471.         
  472.         else
  473.         {
  474.              switch(PreLedLevel)  
  475.              {
  476.                 case 1:
  477.                  SetPinLed1();
  478.                  ClrPinLed2();
  479.                  ClrPinLed3();
  480.                  ClrPinLed4();                  
  481.                  break;
  482.                   
  483.                 case 2:                 
  484.                  SetPinLed1();
  485.                  SetPinLed2();
  486.                  ClrPinLed3();
  487.                  ClrPinLed4();  
  488.                  break;
  489.                   
  490.                 case 3:               
  491.                  SetPinLed1();
  492.                  SetPinLed2();
  493.                  SetPinLed3();
  494.                  ClrPinLed4();  
  495.                  break;
  496.                   
  497.                 case 4:
  498.                  SetPinLed1();
  499.                  SetPinLed2();
  500.                  SetPinLed3();
  501.                  SetPinLed4();   
  502.                  break;
  503.                   
  504.                 default:
  505.                  PreLedLevel = NowLedLevel;
  506.                  break;
  507.              }       
  508.             
  509.         }
  510.         
  511.      
  512.     }
  513.         

  514.    
  515. }

  516. /**********************************************
  517. ;*
  518. ;* Function Name: DealDispData routine
  519. ;* Inputs:        None
  520. ;* Returns:       None
  521. ;* Description:   the DealDispData process
  522. ;*
  523. ;*  0 //放电模式(有负载或无负载)
  524. ;*  1 //充电跑马灯显示处理

  525. ;**********************************************/
  526. void DealDispData(void)
  527. {         
  528.      
  529.     if(FlagShortLoad)
  530.     {
  531.         CntFlashErr++;
  532.         if(CntFlashErr >= 6)
  533.         {
  534.             CntFlashErr = 0;
  535.             FlagShortLoad = 0;
  536.             
  537.             //短路3S后进入休眠
  538.             Sleep();
  539.         }
  540.         
  541.         else
  542.         {
  543.             CPLPinLed1();
  544.             CPLPinLed2();
  545.             CPLPinLed3();
  546.             CPLPinLed4();
  547.         }   
  548.     }
  549.    
  550.     //边充边放优先显示充电
  551.     else if(FlagCharging)
  552.     {
  553.        DisplaySub(1);   
  554.     }   
  555.    
  556.     else if(FlagDischarge && (FlagLoad || FlagDispDelay))
  557.     {
  558.         DisplaySub(0);   //放电有负载或无负载显示处理
  559.     }
  560.    
  561.     else
  562.     {       
  563.         ClrAllLed();    //关闭显示输出  
  564.     }
  565.    
  566.    
  567. }


  568. /**********************************************
  569. ;*
  570. ;* Function Name: KeyScan routine
  571. ;* Inputs:        None
  572. ;* Returns:       None
  573. ;* Description:   the KeyScan process
  574. ;*       
  575. ;**********************************************/
  576. void FuncDischarge(void)
  577. {
  578.      FlagDischarge = 1;            
  579.      PinBoostEN = 1;
  580.      
  581.      FlagDispDelay = 1;
  582.      CntDispDelay = 0;
  583.             
  584.      CntNoLoad = 0;           
  585.      
  586. }

  587. /**********************************************
  588. ;*
  589. ;* Function Name: KeyScan routine
  590. ;* Inputs:        None
  591. ;* Returns:       None
  592. ;* Description:   the KeyScan process
  593. ;*       
  594. ;**********************************************/
  595. void KeyScan(void)
  596. {
  597.     if(!PinKey)
  598.     {
  599.         CntPressOn++;
  600.         if(CntPressOn >= KEY_SHORT)
  601.         {
  602.             CntPressOn = 0;
  603.             if(!FlagKeyPress)
  604.             {
  605.                 FlagKeyPress = 1;
  606.                
  607.                 //单击按键处理程序
  608.                 FuncDischarge();            
  609.             }
  610.             
  611.         }     
  612.         
  613.     }
  614.    
  615.     else
  616.     {
  617.        CntPressOn = 0;
  618.        FlagKeyPress = 0;
  619.     }
  620.            
  621. }
  622. /**********************************************
  623. ;*
  624. ;* Function Name: ControlCharge routine
  625. ;* Inputs:        None
  626. ;* Returns:       None
  627. ;* Description:   the ControlCharge process
  628. ;*       
  629. ;**********************************************/

  630. #define CONST_CHARGE_0A95    139//132
  631. #define CONST_CHARGE_1A05    154//132

  632. #define CONST_CHARGE_0A15   22//32

  633. #define MAX_CHARGE_PWM      100
  634. #define MIN_CHARGE_PWM      4


  635. //充电恒流控制
  636. void AdjustChargePWM(void)
  637. {
  638.     //电流增加
  639.     if(ChargeCur < CONST_CHARGE_0A95)
  640.     {
  641.         if(T8P1RL < MAX_CHARGE_PWM)
  642.         {
  643.               T8P1RL++;                     

  644.         }
  645.            
  646.     }
  647.    
  648.     //电流减小
  649.     else if(ChargeCur > CONST_CHARGE_1A05)
  650.     {
  651.         if(T8P1RL > MIN_CHARGE_PWM)
  652.         {
  653.            T8P1RL--;   
  654.         }   
  655.         
  656.     }   
  657.    
  658. }

  659. void ControlCharge(void)
  660. {
  661.     if(FlagCharging && (!FlagChargeFull))
  662.     {
  663.         if(PowerBattaryQ < CONST_BAT_4V15)
  664.         {
  665.             AdjustChargePWM();
  666.         }
  667.         
  668.         else if((PowerBattaryQ >= CONST_BAT_4V15))
  669.         {
  670.                      
  671.             //电压增加
  672.             if(PowerBattaryQ < CONST_BAT_4V10)
  673.             {
  674.                 if(T8P1RL < MAX_CHARGE_PWM)
  675.                 {
  676.                     T8P1RL++;                     

  677.                 }
  678.            
  679.             }
  680.    
  681.             //电压减小
  682.             else if(PowerBattaryQ > CONST_BAT_4V20)
  683.             {
  684.                 if(T8P1RL > MIN_CHARGE_PWM)
  685.                 {
  686.                     T8P1RL--;   
  687.                 }   
  688.         
  689.             }   
  690.             
  691.                
  692.             //连续5S延时满足条件则认为已充满
  693.             if(ChargeCur < CONST_CHARGE_0A15)
  694.             {
  695.                 CntFull++;
  696.                 if(CntFull >= 250)
  697.                 {
  698.                     CntFull = 0;
  699.                     FlagChargeFull = 1;
  700.                     ClosePinChargePWM();
  701.                
  702.                 }  
  703.             }
  704.             
  705.             else
  706.             {
  707.                 CntFull = 0;   
  708.             }
  709.         
  710.             
  711.             
  712.                      
  713.         }
  714.         
  715.       
  716.         
  717.         
  718.     }
  719.    
  720. }

  721. /**********************************************
  722. ;*
  723. ;* Function Name: BatLevelCheck routine
  724. ;* Inputs:        None
  725. ;* Returns:       None
  726. ;* Description:   the BatLevelCheck process
  727. ;*       
  728. ;**********************************************/
  729. void BatLevelCheck(void)
  730. {
  731.     if(!FlagCharging)
  732.     {
  733.         //放电级别判定           
  734.         
  735.         if(PowerBattaryQ > DISCHARGE_VOL_LEVEL3)
  736.         {
  737.              NowLedLevel = 4;        
  738.         }
  739.         
  740.         else if(PowerBattaryQ > DISCHARGE_VOL_LEVEL2)
  741.         {
  742.              NowLedLevel = 3;  
  743.         }
  744.         
  745.         else if(PowerBattaryQ > DISCHARGE_VOL_LEVEL1)
  746.         {
  747.              NowLedLevel = 2;   
  748.         }
  749.         
  750.         
  751.         else if(PowerBattaryQ > LED_BLINK_VOL)
  752.         {
  753.              NowLedLevel = 1;

  754.             
  755.         }
  756.         
  757.          
  758.     }
  759.    
  760.     //充电级别判定
  761.     else
  762.     {
  763.         if(PowerBattaryQ > CHARGE_VOL_LEVEL3)
  764.         {
  765.              NowLedLevel = 4;        
  766.         }
  767.         
  768.         else if(PowerBattaryQ > CHARGE_VOL_LEVEL2)
  769.         {
  770.              NowLedLevel = 3;  
  771.         }
  772.         
  773.         else if(PowerBattaryQ > CHARGE_VOL_LEVEL1)
  774.         {
  775.              NowLedLevel = 2;   
  776.         }
  777.         
  778.         
  779.         else if(PowerBattaryQ > LED_BLINK_VOL)
  780.         {
  781.              NowLedLevel = 1;
  782.             
  783.         }
  784.         
  785.          
  786.     }
  787.    
  788.     //初始上电时,将当前电量级别赋给上次电量级别
  789.     if(FlagNowToPre)
  790.     {
  791.         FlagNowToPre = 0;
  792.         PreLedLevel = NowLedLevel;
  793.     }
  794.    
  795. }


  796. /**********************************************
  797. ;*
  798. ;* Function Name: HandFlagProcess routine
  799. ;* Inputs:        None
  800. ;* Returns:       None
  801. ;* Description:   the HandFlagProcess process
  802. ;*       
  803. ;**********************************************/
  804. void HandFlagProcess(void)
  805. {           
  806.     if(FlagDispDelay && (!FlagLoad))
  807.     {
  808.         CntDispDelay++;
  809.         if(CntDispDelay >= 6)  //显示电量时间为3S
  810.         {
  811.              CntDispDelay = 0;
  812.              FlagDispDelay = 0;
  813.      
  814.         }
  815.         
  816.     }
  817.    
  818.     //电池电量检测
  819.     BatLevelCheck();
  820.    
  821.     // 电池电量盲区检测 放电才判断  
  822.     if((PowerBattaryQ < LED_BLINK_VOL) && (!FlagCharging))
  823.     {
  824.             FlagEnterBlink = 1;  
  825.             NowLedLevel = 1;   
  826.                   
  827.     }
  828.    
  829.    
  830. }

  831. /**********************************************
  832. ;*
  833. ;* Function Name: AdjustToBalance routine
  834. ;* Inputs:        None
  835. ;* Returns:       None
  836. ;* Description:   the AdjustToBalance process
  837. ;*       
  838. ;**********************************************/

  839. void AdjustToBalance(void)
  840. {
  841.     if(PreLedLevel > NowLedLevel)
  842.     {
  843.         if(FlagLoad)
  844.         {
  845.             CntDelayLevel1++;
  846.             if(CntDelayLevel1 >= COUNT_DELAY_LEVEL)
  847.             {
  848.                 CntDelayLevel1 = 0;
  849.                 PreLedLevel -= 1;
  850.             }
  851.             
  852.         }
  853.         
  854.     }
  855.    
  856.     else if(PreLedLevel < NowLedLevel)
  857.     {
  858.          if(FlagCharging)
  859.          {
  860.              CntDelayLevel2++;
  861.              if(CntDelayLevel2 >= COUNT_DELAY_LEVEL)
  862.              {
  863.                  CntDelayLevel2 = 0;
  864.                  PreLedLevel += 1;
  865.                  
  866.                  CntDispCharge = 0; //跑马灯显示计数清零
  867.                  
  868.              }   
  869.          }
  870.          
  871.          
  872.     }
  873.    
  874.     else
  875.     {
  876.         CntDelayLevel1 = 0;
  877.         CntDelayLevel2 = 0;
  878.     }   
  879.    
  880. }


  881. /**********************************************
  882. ;*
  883. ;* Function Name: HaltProcess routine
  884. ;* Inputs:        None
  885. ;* Returns:       None
  886. ;* Description:   the HaltProcess process
  887. ;*       
  888. ;**********************************************/
  889. void HaltProcess(void)
  890. {
  891.    
  892.     //当电池电量小于关机电量时休眠
  893.     if(!FlagCharging)
  894.     {
  895.         if(PowerBattaryQ < BAT_OFF_VOL)
  896.         {            
  897.             Sleep();         
  898.                        
  899.         }
  900.         
  901.         
  902.     }
  903.       
  904.    
  905. }


  906. /**********************************************
  907. ;*
  908. ;* Function Name: GetADC routine
  909. ;* Inputs:        u8 channel
  910. ;* Returns:       u16 m_ADCValue
  911. ;* Description:   the GetADC process
  912. ;*       
  913. ;**********************************************/

  914. u16 GetADC(u8 channel)
  915. {
  916.     //ADCCH = 0xB8;
  917.     ADCCL = channel;
  918.     ADTRG = 1;
  919.     while (ADTRG)
  920.     {
  921.         ;
  922.     }
  923.     m_ADCValue = (ADCRH << 8) + ADCRL;
  924.     return(m_ADCValue);
  925.    
  926. }



  927. /**********************************************
  928. ;*
  929. ;* Function Name: ADCurrentSample routine
  930. ;* Inputs:        None
  931. ;* Returns:       None
  932. ;* Description:   the ADCurrentSample process
  933. ;*       
  934. ;**********************************************/
  935. void ADCurrentSample(void)
  936. {
  937.     CntADSample++;
  938.     if(CntADSample != 17)
  939.     {
  940.         BakLoadCur = GetADC(AD_CHANNEL_1);
  941.         ADCurSum += BakLoadCur;
  942.         
  943.         ADChargeCurSum += GetADC(AD_CHANNEL_4);
  944.         
  945.         if(FlagCharging)
  946.         {
  947.             if(BakLoadCur > m_ADCValue)
  948.             {
  949.                 BakLoadCur =  (BakLoadCur -  m_ADCValue);
  950.             }
  951.             
  952.         }
  953.         
  954.         //短路1ms保护
  955.         if(BakLoadCur >= LOAD_SHORT_VALUE)
  956.         {
  957.             
  958.              PinBoostEN  = 0;
  959.                           
  960.              ClrAllLed();
  961.              FlagShortLoad = 1;                        
  962.         
  963.         }
  964.         
  965.         GetADC(AD_CHANNEL_0);
  966.         PABuf = (m_ADCValue >> 4);
  967.         ADBatterySum += PABuf;
  968.         
  969.         //ADBatterySum += GetADC(AD_CHANNEL_0);
  970.     }
  971.    
  972.     else
  973.     {
  974.         LoadCurrentVol = (ADCurSum >> 4);
  975.         ChargeCur = (ADChargeCurSum >> 4);
  976.         PowerBattaryQ = (ADBatterySum >> 4);
  977.         
  978.         //充电时放电电流处理
  979.         if(FlagCharging)
  980.         {
  981.             if(LoadCurrentVol >= ChargeCur)
  982.             {
  983.                 LoadCurrentVol = (LoadCurrentVol - ChargeCur);   
  984.             }
  985.                         
  986.         }      
  987.         
  988.         CntADSample = 0;
  989.         ADCurSum = 0;  
  990.         ADChargeCurSum = 0;
  991.         ADBatterySum = 0;
  992.         
  993.     }
  994.    
  995.    
  996. }

  997. /**********************************************
  998. ;*
  999. ;* Function Name: Sleep routine
  1000. ;* Inputs:        None
  1001. ;* Returns:       None
  1002. ;* Description:   the Sleep process
  1003. ;*       
  1004. ;**********************************************/
  1005. void Sleep(void)
  1006. {
  1007.    
  1008.     if(FlagChargeFull)  return;  
  1009.    
  1010.     ClrAllLed();
  1011.    
  1012.     PinBoostEN = 0;
  1013.     FlagDischarge = 0;
  1014.    
  1015.     CntNoLoad = 0;
  1016.    
  1017.     while(!SW_HS);
  1018.     RCEN = 0;
  1019.    
  1020.     ADEN = 0;//关闭ADC使能
  1021.    
  1022.     IDLE();
  1023.    
  1024.     while(!SW_HS); //等待高速时钟稳定
  1025.     RCEN = 1;      //打开看门狗
  1026.    
  1027.     CLRWDT();  



  1028. }

复制代码


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:60571 发表于 2018-5-10 10:40 | 只看该作者
感谢楼主分享,正需要呢,有原理图就更好了
回复

使用道具 举报

板凳
ID:286160 发表于 2018-7-7 23:42 | 只看该作者
大神,,逻辑非常清晰,
回复

使用道具 举报

地板
ID:324123 发表于 2018-7-13 14:55 | 只看该作者
大神学习了
回复

使用道具 举报

5#
ID:324123 发表于 2018-10-19 15:04 | 只看该作者
谢谢
回复

使用道具 举报

6#
ID:418005 发表于 2018-10-30 16:32 | 只看该作者
感谢!看看有没有参考价值!
回复

使用道具 举报

7#
ID:261034 发表于 2019-3-25 22:37 | 只看该作者

感谢!看看有没有参考价值!
回复

使用道具 举报

8#
ID:156220 发表于 2019-5-17 09:44 | 只看该作者
有没有参考原理图看看
回复

使用道具 举报

9#
ID:135093 发表于 2019-6-16 18:28 | 只看该作者
求原理图
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表