找回密码
 立即注册

QQ登录

只需一步,快速开始

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

东软(海尔)哈弗架构单片机HR7P153SD例程

  [复制链接]
跳转到指定楼层
楼主
海尔单片HR7P153SD 三和一移动电源电源程序,海尔单片被上海东软收购,目前叫东软,单片机用的是哈弗架构和PIC的很像.

单片机源程序如下:

  1. /*
  2.                  ---------------------
  3.    电源--|VSS                          VDD|--地
  4.    PinKey--|PA5/OSC1/KIN5              PA0/AIN0|--BTI
  5.    PowerOn--|PA4/OSC2/KIN4             PA1/AIN1|--PinHalt
  6.    PinCharge--|PA3/MRST/KIN3           PA2/AIN2|--BT5V
  7.    PinLight--|PB5/AIN7                 PB0/AIN3|--BTV
  8.    PinLed1--|PC3/AIN6                  PC0/AIN4|--NTC
  9.    PinLed2--|PC2/AIN5/KIN7             PC1|--BTC
  10.    PinPWMP--|PA7/AIN5/KIN7             PA6|--PinPWMN
  11.    
  12.                  ---------------------
  13. */

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

  26. /*********************************************
  27. ;* Include headfiles
  28. ;********************************************/
  29. #include "Init.h"


  30. /*********************************************
  31. ;* Private variables
  32. ;********************************************/







  33. /**********************************************
  34. ;*
  35. ;* Function Name: main
  36. ;* Inputs:        None
  37. ;* Returns:       None
  38. ;* Description:   the main process
  39. ;*        
  40. ;**********************************************/
  41. #define VOL_BOOST_5V8 177   
  42. #define VOL_BOOST_5V6 171  //5.5V

  43. #define CONST_DEC_STEP 2

  44. void main(void)
  45. {
  46.     InitialMCU();

  47.         
  48.     while(1)
  49.     {
  50.         CLRWDT();
  51.         
  52.         //防止拔掉重负载时电压突然上冲  
  53.         if(FlagDischarge)
  54.         {
  55.             GetADC8Bit(AD_CHANNEL_6);
  56.             if(ADCRH >= VOL_BOOST_5V6)
  57.             {
  58.                 T8P2R = T8P2R - CONST_DEC_STEP;               
  59.                
  60.             }
  61.             
  62.         }      

  63.         //每1ms执行一次子程序
  64.         if(FlagSysClk250us)
  65.         {
  66.             FlagSysClk250us = 0;  
  67.             
  68.             Cnt1ms++;
  69.             
  70.             switch(Cnt1ms)
  71.             {
  72.                 case 1:
  73.                                 
  74.                     DisplayLED();        //显示处理子程序
  75.                      
  76.                     break;
  77.                     
  78.                 case 2:
  79.                     
  80.                     ADVoltageSample();   //电压温度检测子程序
  81.                     
  82.                     break;
  83.                     
  84.                 case 3:
  85.                     
  86.                     ADCurrentSample();   //电流采样处理子程序
  87.                     
  88.                     break;
  89.                      
  90.                 case 4:
  91.                     Cnt1ms = 0;
  92.                      
  93.                     
  94.                     CheckLoadInsert();  //负载插入检测子程序
  95.                     
  96.                     break;
  97.                      
  98.                 default:
  99.                     Cnt1ms = 0;
  100.                     break;                  
  101.                      
  102.             }               
  103.                        
  104.         }
  105.         
  106.         //每20ms执行一次子程序
  107.         if(FlagSysClk5ms)
  108.                   {
  109.                            FlagSysClk5ms = 0;
  110.                
  111.             Cnt20ms++;
  112.             switch(Cnt20ms)
  113.             {
  114.                 case 1:
  115.                                 
  116.                             GetSysStatus();                  //状态处理子程序   
  117.                
  118.                 break;
  119.                
  120.                 case 2:  
  121.                
  122.                 ControlCharge();      //充电控制子程序
  123.                
  124.                 break;
  125.                
  126.                 case 3:
  127.                
  128.                 LowLoadCheck();                  //轻负载、过流保护处理子程序  
  129.                
  130.                 break;
  131.                
  132.                 case 4:
  133.                 Cnt20ms = 0;
  134.                
  135.                 ControlBoostVol();    //升压控制子程序
  136.                  
  137.                 break;
  138.                
  139.              default:
  140.                 Cnt20ms = 0;
  141.                 break;
  142.                        
  143.             }            
  144.                
  145.                   }
  146.                
  147.         //每500ms执行一次子程序        
  148.                   if(FlagSysClk125ms)
  149.                   {
  150.                            FlagSysClk125ms = 0;
  151.                
  152.             Cnt500ms++;
  153.             switch(Cnt500ms)
  154.             {
  155.                  
  156.                 case 1:

  157.                
  158.                 break;
  159.                
  160.                 case 2:
  161.                
  162.                             HandFlagProcess();   //功能模式处理子程序
  163.                
  164.                 break;
  165.             
  166.                 case 3:
  167.                
  168.                 DealDispData();      //显示数据处理子程序  必须放在电量级别判断之后
  169.                
  170.                 break;
  171.                
  172.                 case 4:
  173.                 Cnt500ms = 0;
  174.                
  175.                 HaltProcess();        //休眠处理子程序         
  176.                
  177.                 AdjustToBalance();    //电池电量级别平衡处理子程序
  178.                                                          
  179.                 break;
  180.                
  181.              default:
  182.                 Cnt500ms = 0;
  183.                 break;
  184.             }
  185.                                 
  186.                   }
  187.                
  188.     }
  189.         
  190.       
  191. }

  192. /**********************************************
  193. ;*
  194. ;* Function Name: interrupt service routine
  195. ;* Inputs:        None
  196. ;* Returns:       None
  197. ;* Description:   the isr process
  198. ;*        
  199. ;**********************************************/

  200. void isr(void) interrupt
  201. {
  202.    
  203.     if(T8P1TIE && T8P1TIF)
  204.     {
  205.         T8P1TIF = 0;
  206.         
  207.         FlagSysClk250us = 1;
  208.         
  209.         CntSysClk5ms++;  
  210.         
  211.         if(CntSysClk5ms >= 20)
  212.         {
  213.             FlagSysClk5ms = 1;
  214.             CntSysClk5ms = 0;        
  215.             
  216.             CntSysClk250ms++;
  217.             if(CntSysClk250ms >= 50)
  218.             {
  219.                 CntSysClk250ms = 0;
  220.                 FlagSysClk250ms =  1;
  221.                
  222.             
  223.             }

  224.             CntSysClk125ms++;
  225.             if(CntSysClk125ms >= 25)
  226.             {
  227.                 FlagSysClk125ms = 1;
  228.                 CntSysClk125ms = 0;
  229.          
  230.               
  231.                
  232.             }
  233.             
  234.             
  235.         }  
  236.         
  237.     }
  238.    
  239.     if(KIE & KIF)
  240.     {
  241.         PABuf = PA;  //必须要读一次电平才能清除KIF     
  242.         KIF = 0;     
  243.         
  244.     }
  245.       
  246. }

  247. /**********************************************
  248. ;*
  249. ;* Function Name: CheckLoadInsert routine
  250. ;* Inputs:        None
  251. ;* Returns:       None
  252. ;* Description:   the CheckLoadInsert process
  253. ;*        
  254. ;**********************************************/
  255. void CheckLoadInsert(void)
  256. {
  257.     //放电负载插入检测
  258.     if(PinBoostEN) return;   
  259.       
  260.     if(!PinHalt)
  261.     {   
  262.         //判断是否释放负载
  263.         if(FlagLoadFree)
  264.         {
  265.             CntInsert++;
  266.             CntLoadFree = 0;
  267.         
  268.             //当输入电容为104时,可以识别到
  269.             if(CntInsert >= 10)
  270.             {
  271.                 CntInsert = 0;               
  272.                 FlagLoadFree = 0;
  273.                
  274.                 //第一次上电检测到负载默认不打开升压
  275.                 if(!FlagFirShort)
  276.                 {
  277.                     FuncDischarge();   
  278.                 }

  279.             }
  280.            
  281.         }
  282.         
  283.     }
  284.    
  285.     else
  286.     {      
  287.         CntInsert = 0;  
  288.         CntLoadFree++;
  289.         if(CntLoadFree >= 20)
  290.         {
  291.             CntLoadFree = 0;
  292.             FlagLoadFree = 1;
  293.             
  294.             FlagFirShort = 0;
  295.         }
  296.                
  297.     }
  298.    
  299.    
  300. }


  301. /**********************************************
  302. ;*
  303. ;* Function Name: GetSysStatus routine
  304. ;* Inputs:        None
  305. ;* Returns:       None
  306. ;* Description:   the GetSysStatus process
  307. ;*        
  308. ;**********************************************/
  309. #define CONST_NOCHARGE_CUR 6

  310. void GetSysStatus(void)
  311. {
  312.    
  313.     if(PinCharge)
  314.     {   

  315.         CntChargeShort++;
  316.         
  317.         //防止拔掉适配器后显示充满状态,保证PinCharge引脚变为低电平
  318.         if(CntChargeShort >= COUNT_CHARGE_SHORT)
  319.         {
  320.             CntChargeShort = 0;
  321.             if((!FlagChargeDisp) && (VinBoost <= VOL_BOOST_5V8))
  322.             {
  323.                 //充电处理程序               
  324.                
  325.                 FlagDispPower = 0;
  326.                 FlagChargeDisp = 1;
  327.                 FlagCharging = 1;
  328.                
  329.                 FlagChargeSmallCur = 0;
  330.                
  331.                 //必须清除放电标志位,然后关闭升压输出,再打开充电
  332.                 FlagDischarge = 0;
  333.                
  334.                 ClosePWMOut();
  335.                                 CloseBoostEN();
  336.                               
  337.                 InitChargePWM();

  338.                
  339.             }
  340.                  
  341.         }
  342.     }   
  343.         
  344.     else
  345.     {
  346.         CntChargeShort = 0;
  347.             
  348.         if(!FlagDispPower)
  349.         {

  350.              FlagChargeDisp = 0;
  351.              FlagDispPower = 1;              
  352.              FlagCharging = 0;
  353.                
  354.              FlagChargeFull = 0;
  355.                
  356.              FlagChargeSmallCur = 0;
  357.                
  358.                         
  359.                
  360.         }   
  361.   
  362.             
  363.     }  
  364.         
  365.         //充电状态下,充电拔出判断
  366.     if(FlagCharging && (!FlagChargeFull))
  367.     {
  368.         if(CurrentCharge < CONST_NOCHARGE_CUR)  
  369.         {
  370.             CntNoCharge++;
  371.             if(CntNoCharge >= 50)//150)
  372.             {
  373.                 CntNoCharge = 0;
  374.                     
  375.                 //充电拔出处理
  376.                 FlagCharging = 0;
  377.                               
  378.                                 ClosePinCharge();
  379.                                 ClosePWMOut();
  380.                                 CloseBoostEN();
  381.                               
  382.                 CntNoLoad = 0;                           
  383.                     
  384.                           
  385.                     
  386.              }
  387.                   
  388.          }
  389.          
  390.          else
  391.          {
  392.              CntNoCharge = 0;  
  393.          }     
  394.     }
  395. }



  396. /**********************************************
  397. ;*
  398. ;* Function Name: LowLoadCheck routine
  399. ;* Inputs:        None
  400. ;* Returns:       None
  401. ;* Description:   the LowLoadCheck process
  402. ;*        
  403. ;**********************************************/

  404. void LowLoadCheck(void)
  405. {      

  406.     //放电状态下才检测负载电流
  407.     if(!FlagDischarge) return;
  408.      
  409.      //放电模式下过流判断         
  410.      if((CurrentLoad >= LOAD_SHORT_VALUE))
  411.      {
  412.         CntShort++;
  413.         if(CntShort >= COUNT_SHORT)
  414.         {
  415.             CntShort = 0;
  416.             
  417.             //必须清除当前AD值,保证负载恢复正常后再次打开升压可以正常工作
  418.             CntADSample = 0;      
  419.             SumCurrentLoad = 0;
  420.             CurrentLoad = 0;
  421.             
  422.             ClosePWMOut();
  423.             CloseBoostEN();
  424.                     
  425.             FlagSleep = 1;
  426.                         Sleep();
  427.             
  428.         }
  429.    
  430.      }
  431.      
  432.      //放电模式下轻负载判断
  433.      else if(CurrentLoad >= NO_LOAD_VALUE)
  434.      {
  435.          CntShort = 0;
  436.          
  437.          CntNoLoad = 0;
  438.          CntHaveLoad++;
  439.          if(CntHaveLoad >= COUNT_HAVE_LOAD)
  440.          {
  441.              CntHaveLoad = 0;
  442.              FlagLoad = 1;  
  443.              CntDispDelay = 0;
  444.          }         
  445.             
  446.      }
  447.      
  448.      //轻负载小电流判断
  449.      else
  450.      {
  451.          CntShort = 0;
  452.          
  453.          CntHaveLoad = 0;                       
  454.          CntNoLoad++;  
  455.          
  456.          //轻负载时去抖
  457.          if(CntNoLoad >= COUNT_LIGHT_LOAD)
  458.          {
  459.              FlagLoad = 0;   
  460.          }
  461.             
  462.          //无负载15S关闭升压输出,并休眠
  463.          if(CntNoLoad >= COUNT_NO_LOAD)
  464.          {
  465.              CntNoLoad = 0;
  466.                
  467.              FlagDischarge = 0;
  468.             
  469.                          FlagSleep = 1;
  470.                          Sleep();
  471.             
  472.                                 
  473.          }
  474.         
  475.      }   
  476.      
  477. }


  478. /**********************************************
  479. ;*
  480. ;* Function Name: DisplaySub routine
  481. ;* Inputs:        None
  482. ;* Returns:       None
  483. ;* Description:   the DisplaySub process
  484. ;*        
  485. ;**********************************************/

  486. void DisplaySub(u8 uMode)
  487. {
  488.    
  489.     //放电和电量查询显示处理
  490.     if((uMode == 1))
  491.     {        
  492.        ClrAllFlagLed();
  493.             switch(PreLedLevel)                                                                                                                  
  494.                  {                                                                                                               
  495.                          case 1:                                                                                                                          
  496.                          SetFlagLed1();                                                                                                                     
  497.                                                                                                                                                                   
  498.                          break;                                                                                                               
  499.                                                                                                                                                                            
  500.                          case 2:                                                                                                               
  501.                          SetFlagLed1();                                                                                                               
  502.                          SetFlagLed2();                                                                                                               
  503.                                                                                                                                                                   
  504.                          break;                                                                                                               
  505.                                                                                                                                                                            
  506.                          case 3:                                                                                                               
  507.                          SetFlagLed1();                                                                                                               
  508.                          SetFlagLed2();                                                                                                               
  509.                          SetFlagLed3();                                                                                                               
  510.                                                                                                                                                                   
  511.                          break;                                                                                                               
  512.                                                                                                                                                                            
  513.                          case 4:                                                                                                               
  514.                          SetFlagLed1();                                                                                                               
  515.                     SetFlagLed2();                                                                                                               
  516.                          SetFlagLed3();                                                                                                               
  517.                          SetFlagLed4();                                                                                                               
  518.                          break;                                                                                                               
  519.                                                                                                                                                                            
  520.                          default:                                                                                                               
  521.                          PreLedLevel = NowLedLevel;                                                                                                               
  522.                          break;                                                                                                                                                               
  523.                     }   
  524.                     
  525.     }               
  526.    
  527.     //充电闪烁显示处理
  528.     else if(uMode == 2)
  529.     {

  530.             switch(PreLedLevel)                                                                                                                  
  531.                  {                                                                                                               
  532.                          case 1:                                                                                                                          
  533.                          CPLFlagLed1();                                                                                                                     
  534.                          ClrFlagLed2();
  535.                          ClrFlagLed3();
  536.                          ClrFlagLed4();
  537.                          break;                                                                                                               
  538.                                                                                                                                                                            
  539.                          case 2:                                                                                                               
  540.                          SetFlagLed1();                                                                                                               
  541.                          CPLFlagLed2();
  542.                          ClrFlagLed3();
  543.                          ClrFlagLed4();
  544.                                                                                                                                                                            
  545.                          break;                                                                                                               
  546.                                                                                                                                                                            
  547.                          case 3:                                                                                                               
  548.                          SetFlagLed1();                                                                                                               
  549.                          SetFlagLed2();                                                                                                               
  550.                          CPLFlagLed3();
  551.                          ClrFlagLed4();
  552.                                                                                                                                                                   
  553.                          break;                                                                                                               
  554.                                                                                                                                                                            
  555.                          case 4:                                                                                                               
  556.                          SetFlagLed1();                                                                                                               
  557.                      SetFlagLed2();                                                                                                               
  558.                          SetFlagLed3();                                                                                                               
  559.                          CPLFlagLed4();                                                                                                               
  560.                          break;                                                                                                               
  561.                                                                                                                                                                            
  562.                          default:                                                                                                               
  563.                          PreLedLevel = NowLedLevel;                                                                                                               
  564.                          break;        
  565.             
  566.              }      
  567.         
  568.     }
  569.         
  570.     //充电充满显示
  571.     else if(uMode == 3)
  572.     {
  573.        SetFlagLed1();
  574.        SetFlagLed2();
  575.        SetFlagLed3();
  576.        SetFlagLed4();
  577.     }
  578.    
  579. }

  580. /**********************************************
  581. ;*
  582. ;* Function Name: DealDispData routine
  583. ;* Inputs:        None
  584. ;* Returns:       None
  585. ;* Description:   the DealDispData process
  586. ;*
  587. ;*  0 //放电模式(有负载)
  588. ;*  1 //放电无负载、充电拔出显示处理
  589. ;*  2 //充电跑马灯显示处理
  590. ;*  3 //充电充满显示
  591. ;**********************************************/

  592. void DealDispData(void)
  593. {      
  594.    
  595.     if(FlagDispDelay)
  596.     {
  597.         DisplaySub(1);     //放电和按键查询显示处理
  598.     }
  599.       
  600.     else if(FlagChargeFull)
  601.     {
  602.               DisplaySub(3);     //充电充满显示
  603.     }
  604.    
  605.     else if(FlagCharging)
  606.     {
  607.               DisplaySub(2);     //充电跑马灯显示处理
  608.     }
  609.    
  610.     else
  611.     {        
  612.         ClrAllFlagLed();   //关闭显示输出
  613.    
  614.     }
  615.         
  616.         

  617.    

  618.    
  619. }

  620. /**********************************************
  621. ;*
  622. ;* Function Name: DisplayLED routine
  623. ;* Inputs:        None
  624. ;* Returns:       None
  625. ;* Description:   the DisplayLED process
  626. ;*        
  627. ;**********************************************/
  628. void DisplayLED(void)
  629. {
  630.     PinLed1Input();
  631.     PinLed2Input();
  632.     PinLed3Input();  
  633.    
  634.     DispCnt++;
  635.     switch(DispCnt)
  636.     {
  637.         case 1:
  638.             
  639.             if(FlagLed1)            
  640.             {
  641.                 SetPinLed1();   
  642.             }
  643.             else
  644.             {
  645.                 ClrPinLed1();   
  646.             }
  647.             
  648.             PinLed1Output();
  649.             
  650.             ClrPinLed2();
  651.             PinLed2Output();
  652.             
  653.             break;
  654.             
  655.         case 2:
  656.             
  657.             if(FlagLed2)
  658.             {
  659.                 SetPinLed2();   
  660.             }
  661.             else
  662.             {
  663.                 ClrPinLed2();   
  664.             }
  665.             
  666.             PinLed2Output();
  667.             
  668.             ClrPinLed1();
  669.             PinLed1Output();

  670.             break;   
  671.          
  672.         case 3:
  673.             
  674.             if(FlagLed3)
  675.             {
  676.                 SetPinLed3();   
  677.             }
  678.             else
  679.             {
  680.                 ClrPinLed3();   
  681.             }
  682.             
  683.             PinLed3Output();
  684.             
  685.             ClrPinLed2();
  686.             PinLed2Output();

  687.             break;
  688.             
  689.         case 4:
  690.                         
  691.             if(FlagLed4)
  692.             {
  693.                 SetPinLed2();   
  694.             }
  695.             else
  696.             {
  697.                 ClrPinLed2();   
  698.             }
  699.             
  700.             PinLed2Output();
  701.             
  702.             ClrPinLed3();
  703.             PinLed3Output();

  704.             break;  
  705.             
  706.         case 5:            
  707.             DispCnt = 0;                     
  708.             
  709.             KeyScan();          //按键扫描子程序
  710.             
  711.          default:
  712.             break;         
  713.         
  714.     }
  715.       
  716. }

  717. /**********************************************
  718. ;*
  719. ;* Function Name: KeyScan routine
  720. ;* Inputs:        None
  721. ;* Returns:       None
  722. ;* Description:   the KeyScan process
  723. ;*        
  724. ;**********************************************/
  725. void FuncDischarge(void)
  726. {
  727.      //边充边放时,输入电压大于5.8V时,不打开升压
  728.      if((VinBoost > VOL_BOOST_5V8)) return;
  729.       
  730.          SetBoostEN();     
  731.      InitComplePWM3x();
  732.                
  733.      CntNoLoad = 0;   
  734.      CntDispDelay = 0;
  735.      FlagDischarge = 1;   
  736.      FlagDispDelay = 1;   
  737.          
  738.          

  739. }


  740. void KeyScan(void)
  741. {
  742.    
  743.     PinLed1Input();
  744.     PinLed2Input();
  745.     PinLed3Input();  
  746.    
  747.     PinLed3Output();
  748.     SetPinLed3();
  749.     NOP();
  750.     NOP();
  751.     PinLed3Input();
  752.    
  753.     //延时保证按键电平恢复到原始电平   
  754.     NOP();
  755.     NOP();   
  756.     NOP();
  757.     NOP();
  758.    
  759.     NOP();
  760.     NOP();   
  761.     NOP();
  762.     NOP();
  763.    
  764.     NOP();
  765.     NOP();   
  766.     NOP();
  767.     NOP();
  768.    
  769.     if(!PinKey)
  770.     {
  771.         CntKeyPress++;
  772.         if(CntKeyPress > 400)
  773.         {
  774.             //长按键处理 手电筒开关   
  775.             CntKeyPress = 401;
  776.         }     
  777.         
  778.         
  779.     }
  780.    
  781.     else
  782.     {
  783.         if((CntKeyPress >= KEY_SHORT) && (CntKeyPress < 401))
  784.         {
  785.             //短按键处理
  786.             CntPressOn++;
  787.             if(CntPressOn == 1)
  788.             {
  789.                 FlagKeyPress = 1;
  790.                 DelayDoubleKey = 0;
  791.             }
  792.             
  793.             else if(CntPressOn >= 2)
  794.             {
  795.                 CntPressOn = 0;
  796.                 FlagKeyPress = 0;
  797.                         
  798.             }
  799.                        
  800.         }
  801.         
  802.         CntKeyPress = 0;
  803.                
  804.     }
  805.    
  806.     if(FlagKeyPress)
  807.     {
  808.         DelayDoubleKey++;

  809.         if(DelayDoubleKey > 200)
  810.         {
  811.             CntPressOn = 0;
  812.             FlagKeyPress = 0;

  813.             if(!FlagCharging)
  814.             {
  815.                 FlagFirShort = 0;
  816.                 FuncDischarge();

  817.             }

  818.         }
  819.         
  820.     }
  821.    
  822.    
  823. }

  824. /**********************************************
  825. ;*
  826. ;* Function Name: BatLevelCheck routine
  827. ;* Inputs:        None
  828. ;* Returns:       None
  829. ;* Description:   the BatLevelCheck process
  830. ;*        
  831. ;**********************************************/
  832. void BatLevelCheck(void)
  833. {
  834.     if(!FlagCharging)
  835.     {
  836.         //放电级别判定                  
  837.         if(VinBattary > DISCHARGE_VOL_LEVEL3)
  838.         {
  839.              NowLedLevel = 4;        
  840.         }
  841.         
  842.         else if(VinBattary > DISCHARGE_VOL_LEVEL2)
  843.         {
  844.              NowLedLevel = 3;  
  845.         }
  846.         
  847.         else if(VinBattary > DISCHARGE_VOL_LEVEL1)
  848.         {
  849.              NowLedLevel = 2;   
  850.         }
  851.         
  852.         else if(VinBattary > LED_BLINK_VOL)
  853.         {
  854.              NowLedLevel = 1;
  855.             
  856.         }
  857.          
  858.     }
  859.    
  860.    
  861.     else
  862.     {
  863.         //充电级别判定
  864.         if(VinBattary > CHARGE_VOL_LEVEL3)
  865.         {
  866.              NowLedLevel = 4;        
  867.         }
  868.         
  869.         else if(VinBattary > CHARGE_VOL_LEVEL2)
  870.         {
  871.              NowLedLevel = 3;  
  872.         }
  873.         
  874.         else if(VinBattary > CHARGE_VOL_LEVEL1)
  875.         {
  876.              NowLedLevel = 2;   
  877.         }
  878.         
  879.         else if(VinBattary > LED_BLINK_VOL)
  880.         {
  881.              NowLedLevel = 1;
  882.             
  883.         }
  884.          
  885.     }
  886.    
  887.     //初始上电时,将当前电量级别赋给上次电量级别
  888.     if(FlagNowToPre)
  889.     {
  890.         FlagNowToPre = 0;
  891.         PreLedLevel = NowLedLevel;
  892.     }
  893.    
  894. }

  895. /**********************************************
  896. ;*
  897. ;* Function Name: ControlBoostVol routine
  898. ;* Inputs:        None
  899. ;* Returns:       None
  900. ;* Description:   the ControlBoostVol process
  901. ;*        
  902. ;**********************************************/
  903. #define VOL_BOOST_4V5  137
  904. #define VOL_BOOST_4V8  146
  905. #define VOL_BOOST_4V95 151
  906. #define VOL_BOOST_4V9  149
  907. #define VOL_BOOST_5V0  152
  908. #define VOL_BOOST_5V1  155
  909. #define VOL_BOOST_5V1  155

  910. #define VOL_BOOST_5V25 160
  911. #define VOL_BOOST_5V5  168

  912. #define MIN_BOOST_PWM2 2
  913. #define MAX_BOOST_PWM2 28



  914. /**********************************************
  915. ;*
  916. ;* Function Name: InitComplePWM3x routine
  917. ;* Inputs:        void
  918. ;* Returns:       void
  919. ;* Description:   the InitComplePWM3x process
  920. ;*        
  921. ;**********************************************/
  922. #define VBAT_3V0  139
  923. #define VBAT_3V6  166
  924. #define VBAT_4V0  185
  925. #define VBAT_4V1  190

  926. #define VBAT_4V2  194
  927. #define VBAT_4V3  199
  928. #define VBAT_4V6  213
  929. #define VBAT_4V8  222

  930. void ClosePWMOut(void)
  931. {
  932.     T8P2OC = 0x83;
  933.         T8P2M = 0;        //定时器模式
  934.     CloseMosP();
  935.     NOP();
  936.     NOP();
  937.     CloseMosN();
  938.       
  939. }

  940. void InitComplePWM3x(void)
  941. {

  942.         ANS = 0xe3;                                //1110_0011B 使用AIN2/AIN3/AIN4通道  PWM20与PWM21输出同相
  943.         T8P2P = 39;                                //PWM周期为5us
  944.         T8P2R = 2;                                //PWM初值
  945.         T8P2C = 0x84;                        //预分频和后分频为1:1,使能T8P2模块,选择PWM模式
  946.         T8P2PEX = 0x00;
  947.         T8P2PMC = 0x01;                        //8位PWM精度,T8P2计数器与缓冲寄存器匹配
  948.         T8P2OC = 0x0e;                        //PWM输出使能,选择PB2输出PWM20,PB3输出PWM21
  949.         T8P2PDT = 0x01;                        //死区时间为125ns

  950.         PinChargeDisable();

  951. }


  952. void ControlBoostVol(void)
  953. {
  954.     if(FlagDischarge)
  955.     {

  956.                  if(VinBoost < VOL_BOOST_4V8)
  957.                  {
  958.                         if(T8P2R < MAX_BOOST_PWM2)
  959.                         {
  960.                                 if((VinBoost < VOL_BOOST_4V5) )
  961.                                 {
  962.                                         //如果输出电压小于4.5V,则快速增加升压
  963.                                         T8P2R++;                              
  964.                
  965.                                 }         
  966.                                 //增加升压         
  967.                                 T8P2R++;                     
  968.         
  969.                          }   
  970.                  
  971.                  }
  972.          
  973.                  else if(VinBoost > VOL_BOOST_5V0)
  974.                  {
  975.                          if(T8P2R > MIN_BOOST_PWM2)
  976.                          {
  977.                                  //降低升压
  978.                                  T8P2R--;   
  979.                           }
  980.          
  981.                  }
  982.          
  983.                  else if(VinBoost > VOL_BOOST_5V6)
  984.                  {
  985.                          //升压输出超过5.5V关闭PWM输出
  986.                          ClosePWMOut();

  987.                  }
  988.         
  989.     }
  990.    
  991. }

  992. /**********************************************
  993. ;*
  994. ;* Function Name: ControlCharge routine
  995. ;* Inputs:        None
  996. ;* Returns:       None
  997. ;* Description:   the ControlCharge process
  998. ;*        
  999. ;**********************************************/

  1000. #define CONST_CUR_DIF   3   //必须小于100mA时的电流值

  1001. #define CONST_VOL_L     2   //4.05V

  1002. #define CONST_VOL_H     4   //4.25V

  1003. void InitChargePWM(void)
  1004. {

  1005.         ANS = 0x73;                                //0110_0011B 使用AIN2/AIN3/AIN4通道  PWM20与PWM21输出反相
  1006.         T8P2P = 249;                        //PWM周期为31.25us
  1007.         T8P2R = 39;                                //PWM初值
  1008.         T8P2C = 0x84;                        //预分频和后分频为1:1,使能T8P2模块,选择PWM模式
  1009.         T8P2PEX = 0x00;
  1010.         T8P2PMC = 0x01;                        //8位PWM精度,T8P2计数器与缓冲寄存器匹配
  1011.         T8P2OC = 0x0f;                        //PWM输出使能,选择PB2输出PWM20,PWM21输出关闭
  1012.         T8P2PDT = 0x00;                        //死区时间为0
  1013.         PinChargeEnable();
  1014.    
  1015. }


  1016. void ControlCharge(void)
  1017. {
  1018.     if(FlagCharging)
  1019.     {
  1020.         //如果电池电压大于4.3V,则关闭充电模块
  1021.         if((VinBattary >= VBAT_4V3))
  1022.         {
  1023.             ClosePWMOut();
  1024.             ClosePinCharge();
  1025.         }
  1026.            
  1027.         //电池电压大于4.1V时,进行恒压充电
  1028.         else if(VinBattary >= VBAT_4V1)
  1029.         {
  1030.             //恒压涓流充电处理
  1031.             FlagChargeSmallCur = 1;
  1032.             //TargetChargeVol = VBAT_4V1;

  1033.                          //电压增加
  1034.                         if(VinBattary < (VBAT_4V1 - CONST_VOL_L))
  1035.                         {
  1036.                                 if(T8P2R < MAX_CHARGE_PWM)
  1037.                                 {
  1038.                                         T8P2R++;                     

  1039.                                 }
  1040.            
  1041.                         }
  1042.    
  1043.                         //电压减小
  1044.                         else if(VinBattary > (VBAT_4V1 + CONST_VOL_H))
  1045.                         {
  1046.                                 if(T8P2R > MIN_CHARGE_PWM)
  1047.                                 {
  1048.                                         T8P2R--;   
  1049.                                 }   
  1050.         
  1051.                         }

  1052.             if(CurrentCharge < CONST_CUR_100MA)
  1053.             {
  1054.                 //充电充满判定
  1055.                 CntFullDelay++;
  1056.                 if(CntFullDelay >= 250)
  1057.                 {
  1058.                      CntFullDelay = 0;
  1059.                      FlagChargeFull = 1;
  1060.                      ClosePWMOut();
  1061.                      ClosePinCharge();  
  1062.                 }
  1063.                         
  1064.             }
  1065.             
  1066.             else
  1067.             {
  1068.                  CntFullDelay = 0;  
  1069.             }
  1070.                      
  1071.             
  1072.         }            
  1073.         
  1074.         //如果电池电压小于3.0V时预充
  1075.         else if((VinBattary < VBAT_3V0))
  1076.         {

  1077.                         if(CurrentCharge < (CHARGE_CUR_0A1 - CONST_CUR_DIF))
  1078.                         {
  1079.                                 if(T8P2R < MAX_CHARGE_PWM)
  1080.                                 {
  1081.                                         T8P2R++;                     

  1082.                                 }
  1083.            
  1084.                         }
  1085.    
  1086.                         //电流减小
  1087.                         else if(CurrentCharge > (CHARGE_CUR_0A1 + CONST_CUR_DIF))
  1088.                         {
  1089.                                 if(T8P2R > MIN_CHARGE_PWM)
  1090.                                 {
  1091.                                         T8P2R--;   
  1092.                                 }   
  1093.         
  1094.                         }
  1095.             
  1096.         }
  1097.         
  1098.         //电池电压大于3.0V时恒流充电
  1099.         else
  1100.         {
  1101.                                        
  1102.                         if(CurrentCharge < (CHARGE_CUR_1A - CONST_CUR_DIF))
  1103.                         {
  1104.                                 if(T8P2R < MAX_CHARGE_PWM)
  1105.                                 {
  1106.                                         T8P2R++;                     

  1107.                                 }
  1108.            
  1109.                         }
  1110.    
  1111.                         //电流减小
  1112.                         else if(CurrentCharge > (CHARGE_CUR_1A + CONST_CUR_DIF))
  1113.                         {
  1114.                                 if(T8P2R > MIN_CHARGE_PWM)
  1115.                                 {
  1116.                                         T8P2R--;   
  1117.                                 }   
  1118.         
  1119.                         }
  1120.         }
  1121.          
  1122.     }

  1123. }

  1124. /**********************************************
  1125. ;*
  1126. ;* Function Name: HandFlagProcess routine
  1127. ;* Inputs:        None
  1128. ;* Returns:       None
  1129. ;* Description:   the HandFlagProcess process
  1130. ;*        
  1131. ;**********************************************/
  1132. void HandFlagProcess(void)
  1133. {   
  1134.     if(FlagDispDelay && (!FlagLoad))
  1135.     {
  1136.         CntDispDelay++;
  1137.         
  1138.         //放电显示电量时间为8S
  1139.         if(CntDispDelay >= COUNT_DELAY_DISP)   
  1140.         {
  1141.              CntDispDelay = 0;
  1142.              FlagDispDelay = 0;

  1143.         }
  1144.         
  1145.     }
  1146.         
  1147.     //电池电量检测
  1148.     BatLevelCheck();
  1149.    
  1150.    
  1151.    
  1152. }

  1153. /**********************************************
  1154. ;*
  1155. ;* Function Name: AdjustToBalance routine
  1156. ;* Inputs:        None
  1157. ;* Returns:       None
  1158. ;* Description:   the AdjustToBalance process
  1159. ;*        
  1160. ;**********************************************/
  1161. void AdjustToBalance(void)
  1162. {
  1163.     if(PreLedLevel > NowLedLevel)
  1164.     {
  1165.         if(FlagLoad)
  1166.         {
  1167.             CntDelayLevel++;
  1168.             if(CntDelayLevel >= COUNT_DELAY_LEVEL)
  1169.             {
  1170.                 CntDelayLevel = 0;
  1171.                 PreLedLevel -= 1;
  1172.             }
  1173.             
  1174.         }
  1175.         
  1176.     }
  1177.    
  1178.     else if(PreLedLevel < NowLedLevel)
  1179.     {
  1180.          if(FlagCharging)
  1181.          {
  1182.              CntDelayLevel++;
  1183.              if(CntDelayLevel >= COUNT_DELAY_LEVEL)
  1184.              {
  1185.                  CntDelayLevel = 0;
  1186.                  PreLedLevel += 1;        

  1187.              }   
  1188.          }            
  1189.     }
  1190.    
  1191.     else
  1192.     {
  1193.         CntDelayLevel = 0;

  1194.     }
  1195. }


  1196. /**********************************************
  1197. ;*
  1198. ;* Function Name: HaltProcess routine
  1199. ;* Inputs:        None
  1200. ;* Returns:       None
  1201. ;* Description:   the HaltProcess process
  1202. ;*        
  1203. ;**********************************************/
  1204. void HaltProcess(void)
  1205. {
  1206.    
  1207.     //当电池电量小于关机电量时休眠   
  1208.     if((!FlagCharging) && (!FlagLoad))
  1209.     {
  1210.         if(VinBattary < BAT_OFF_VOL)
  1211.         {         
  1212.             FlagSleep = 1;

  1213.                         Sleep();
  1214.             
  1215.         }
  1216.     }
  1217.      
  1218. }

  1219. /**********************************************
  1220. ;*
  1221. ;* Function Name: GetADC12Bit routine
  1222. ;* Inputs:        u8 channel
  1223. ;* Returns:       u16 m_ADCValue
  1224. ;* Description:   the GetADC12Bit process
  1225. ;*        
  1226. ;**********************************************/

  1227. u16 GetADC12Bit(u8 channel)
  1228. {
  1229.     ADCCL = channel;
  1230.         ADCCH = 0xb8;

  1231.     ADTRG = 1;
  1232.     while (ADTRG)
  1233.     {
  1234.         ;
  1235.     }
  1236.     m_ADCValue = (ADCRH << 8) + ADCRL;
  1237.     return(m_ADCValue);
  1238.    
  1239. }

  1240. /**********************************************
  1241. ;*
  1242. ;* Function Name: GetADC8Bit routine
  1243. ;* Inputs:        u8 channel
  1244. ;* Returns:       u8 m_ADCValue
  1245. ;* Description:   the GetADC8Bit process
  1246. ;*        
  1247. ;**********************************************/
  1248. u8 GetADC8Bit(u8 channel)
  1249. {
  1250.     ADCCL = channel;
  1251.         ADCCH = 0x38;

  1252.     ADTRG = 1;
  1253.     while (ADTRG)
  1254.     {
  1255.         ;
  1256.     }
  1257.     return(ADCRH);
  1258.    
  1259. }


  1260. /**********************************************
  1261. ;*
  1262. ;* Function Name: ADCurrentSample routine
  1263. ;* Inputs:        None
  1264. ;* Returns:       None
  1265. ;* Description:   the ADCurrentSample process
  1266. ;*        
  1267. ;**********************************************/

  1268. void ADCurrentSample(void)
  1269. {
  1270.     //升压使能打开才检测电流

  1271.     if(1)
  1272.     {
  1273.         CntADSample++;
  1274.         if(CntADSample != 17)
  1275.         {
  1276.             SumCurrentLoad += GetADC12Bit(AD_CHANNEL_2);
  1277.             
  1278.             SumCurrentCharge += GetADC12Bit(AD_CHANNEL_3);

  1279.         }
  1280.    
  1281.         else
  1282.         {      
  1283.             CurrentLoad = (SumCurrentLoad >> 4);
  1284.             
  1285.             CurrentCharge = (SumCurrentCharge >> 4);

  1286.                         //for debug
  1287.                         //CurrentCharge = CHARGE_CUR_0A5;
  1288. ……………………

  1289. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

代码下载(附件只包含代码1个文件):
153DEMO.rar (279.11 KB, 下载次数: 192)


评分

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

查看全部评分

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

使用道具 举报

沙发
ID:5564 发表于 2018-9-21 13:46 | 只看该作者
向老大学习来了
回复

使用道具 举报

板凳
ID:138119 发表于 2018-10-21 09:53 | 只看该作者
如果开发软件好用,仿真下载工具便宜,芯片抗干扰强,芯片性价比高,一定能火来!
支持!
回复

使用道具 举报

地板
ID:188446 发表于 2018-11-4 17:03 | 只看该作者
记得读书的时候  微芯和 海尔还打着官司    汇编指令都是一样的  
回复

使用道具 举报

5#
ID:93667 发表于 2019-3-30 10:56 | 只看该作者
谢谢分享!!!!!!!!!!!
回复

使用道具 举报

6#
ID:519577 发表于 2019-4-23 14:55 | 只看该作者
谢谢分享~~~~~~
回复

使用道具 举报

7#
ID:582276 发表于 2019-7-15 21:05 | 只看该作者
有电路原理图吗?
回复

使用道具 举报

8#
ID:347264 发表于 2019-7-29 16:47 | 只看该作者
谢谢分享
回复

使用道具 举报

9#
ID:140183 发表于 2019-12-22 14:36 | 只看该作者
下载学习学习.....
回复

使用道具 举报

10#
ID:672113 发表于 2019-12-26 18:47 | 只看该作者
楼主,出来看看你的眼里图啊。原理图呀?   变量太多,看不懂变量的意思啊?
回复

使用道具 举报

11#
ID:677678 发表于 2020-1-13 18:12 | 只看该作者
继续 分享多一些  东软载波单片机 的案例
回复

使用道具 举报

12#
ID:71535 发表于 2020-1-15 10:18 | 只看该作者
顶一下,看看参考程序
回复

使用道具 举报

13#
ID:506075 发表于 2020-7-6 19:18 | 只看该作者
好东西,大佬,请问现在还有在海尔单片机吗
回复

使用道具 举报

14#
ID:156760 发表于 2020-9-17 08:23 | 只看该作者
看不懂啊  大佬有原理图吗
回复

使用道具 举报

15#
ID:960995 发表于 2021-8-29 14:09 | 只看该作者
scottmaxwell 发表于 2018-11-4 17:03
记得读书的时候  微芯和 海尔还打着官司    汇编指令都是一样的

jianrong MICROCHIP?
回复

使用道具 举报

16#
ID:582276 发表于 2021-9-1 21:43 | 只看该作者
正在弄一款移动电源,现在的逆变输出是用H桥斩波输出,谐波很大。想改为SPWM输出。楼主有合适的方案吗?
回复

使用道具 举报

17#
ID:967600 发表于 2021-9-22 22:59 | 只看该作者
我也同问,现在还有海尔的单片机吗?
回复

使用道具 举报

18#
ID:805324 发表于 2021-10-22 11:37 | 只看该作者
软件三合一
回复

使用道具 举报

19#
ID:661775 发表于 2022-5-31 11:45 | 只看该作者
注释太少了,原理图谁能分享一下吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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