C51 COMPILER V9.56.0.0   BSP_TIMER                                                         02/14/2025 12:21:42 PAGE 1   


C51 COMPILER V9.56.0.0, COMPILATION OF MODULE BSP_TIMER
OBJECT MODULE PLACED IN .\Objects\bsp_timer.obj
COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE ..\bsp\bsp_timer.c LARGE OPTIMIZE(8,SPEED) BROWSE INCDIR(..\bsp;..\app) 
                    -DEBUG OBJECTEXTEND PRINT(.\Listings\bsp_timer.lst) TABS(2) OBJECT(.\Objects\bsp_timer.obj)

line level    source

   1          #include "bsp.h"
   2          
   3          /*************************************************************
   4           * 定时器功能
   5           *************************************************************/
   6           
   7           
   8          /* 计时器的计数值 */
   9          volatile u32    g_clock_ms = 0;
  10          bit s1_flag=0;
  11          bit ms100_flag = 0;
  12          bit rs485_recv_timeout_flag = 0;
  13          bit rs485_recv_timeout_check_flag = 0;
  14          u8 rs485_timeout_cnt = 0;
  15          bit timeout_flag=0;
  16          bit timeout_enable=0;
  17          static u16 timeout_countdown=0;
  18          void reset_timeout_countdown(u16 count)
  19          {
  20   1        timeout_enable = 1;
  21   1        timeout_flag=0;
  22   1        timeout_countdown=count;
  23   1      }
  24          void stop_timeout_countdown(void)
  25          {
  26   1        timeout_enable = 0;
  27   1      }
  28          void reset_rs485_recv_timeout(void)
  29          {
  30   1        rs485_recv_timeout_check_flag=1;
  31   1        rs485_recv_timeout_flag = 0;
  32   1        rs485_timeout_cnt = 0;
  33   1      }
  34          
  35          bit rs485_has_recv_timeout(void)
  36          {
  37   1        if(rs485_recv_timeout_check_flag && rs485_recv_timeout_flag)
  38   1        {
  39   2          rs485_recv_timeout_check_flag = 0;
  40   2          rs485_timeout_cnt = 0;
  41   2          return 1;
  42   2        }
  43   1        else
  44   1        {
  45   2          return 0;
  46   2        }
  47   1      }
  48          bit rs485_recv_timeout_flag2 = 0;
  49          bit rs485_recv_timeout_check_flag2 = 0;
  50          u8 rs485_timeout_cnt2 = 0;
  51          void reset_rs485_recv_timeout2(void)
  52          {
  53   1        rs485_recv_timeout_check_flag2=1;
  54   1        rs485_recv_timeout_flag2 = 0;
C51 COMPILER V9.56.0.0   BSP_TIMER                                                         02/14/2025 12:21:42 PAGE 2   

  55   1        rs485_timeout_cnt2 = 0;
  56   1      }
  57          
  58          bit rs485_has_recv_timeout2(void)
  59          {
  60   1        if(rs485_recv_timeout_check_flag2 && rs485_recv_timeout_flag2)
  61   1        {
  62   2          rs485_recv_timeout_check_flag2 = 0;
  63   2          rs485_timeout_cnt2 = 0;
  64   2          return 1;
  65   2        }
  66   1        else
  67   1        {
  68   2          return 0;
  69   2        }
  70   1      }
  71          void hal_timer_init(void)
  72          {
  73   1        TCON1 = 0x00;           //Tx0定时器时钟为Fosc
  74   1        TMOD = 0x00;            //16位重装载定时器/计数器
  75   1      
  76   1        //Tim0计算时间  = (65536 - 0xCBEB) * (1 / (Fosc /Timer分频系数))
  77   1        //        = 13333 / (16000000 / 12)
  78   1        //        = 10 ms
  79   1      
  80   1        //定时1ms
  81   1        //反推初值  = 65536 - ((1/1000) / (1/(Fosc / Timer分频系数)))
  82   1        //        = 65536 - ((1/1000) / (1/(16000000 / 12)))
  83   1        //      = 65536 - 1333
  84   1        //      = 0xFACB
  85   1        TH0 = 0xFA;
  86   1        TL0 = 0xCB;             //T0定时时间1ms
  87   1        IE |= 0x02;             //打开T0中断
  88   1        TCON |= 0x10;           //使能T0
  89   1      
  90   1      //  /* T1 40 ms */
  91   1      //  TH1 = 0x2F;
  92   1      //  TL1 = 0xAB;
  93   1      //  IE |= 0x08;             //
  94   1      //  TCON |= 0x40;           //
  95   1        
  96   1        EA = 1;
  97   1        
  98   1        
  99   1      }
 100          
 101          void TIMER0_Rpt(void) interrupt TIMER0_VECTOR
 102          {
 103   1        g_clock_ms ++;
 104   1        if(rs485_recv_timeout_check_flag)
 105   1        {
 106   2          rs485_timeout_cnt ++;
 107   2          if(rs485_timeout_cnt>5)
 108   2          {
 109   3            rs485_recv_timeout_flag = 1;
 110   3          }
 111   2        }
 112   1        if(timeout_enable == 1){
 113   2          if(timeout_countdown >0) timeout_countdown --;
 114   2          else timeout_flag = 1;
 115   2        }else{
 116   2          timeout_flag = 0;
C51 COMPILER V9.56.0.0   BSP_TIMER                                                         02/14/2025 12:21:42 PAGE 3   

 117   2        }
 118   1        if(rs485_recv_timeout_check_flag2)
 119   1        {
 120   2          rs485_timeout_cnt2 ++;
 121   2          if(rs485_timeout_cnt2>2)
 122   2          {
 123   3            rs485_recv_timeout_flag2 = 1;
 124   3          }
 125   2        }
 126   1        if((g_clock_ms % 80) == 0){
 127   2          ms100_flag = 1;
 128   2        }
 129   1      
 130   1        if((g_clock_ms % 1000) == 0){
 131   2          s1_flag = 1;
 132   2        }
 133   1        TCON &=~0x20;
 134   1      }
 135          
 136          //volatile u32 tmp = 0;
 137          //void TIMER1_Rpt(void) interrupt TIMER1_VECTOR
 138          //{
 139          //  tmp ++;
 140          //  if(tmp == 100) {
 141          //    if(g_adc_cb != NULL) {
 142          //      g_adc_cb();/* 每隔40ms调用一次此函数，采样电池电压 */
 143          //    }
 144          //    tmp = 0;
 145          //  }
 146          //  TCON &=~0x80;
 147          //}


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    343    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =      8    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      8    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
