[51单片机] 利用E2PROM存储设备整体使用时间

[复制链接]
 楼主| zzjj44 发表于 2016-3-10 11:09 | 显示全部楼层 |阅读模式
用51单片机做个小东西,目的是当累积时间(hours)达到500小时时,红灯亮(LED_RED_POWER=1),平时绿灯亮(LED_GREEN_POWER =1),在仿真时,设定hours=161时,绿灯亮;hours=160时,红灯亮,请教大神是怎么回事??急等!
  1. #include"reg52.h"
  2. #include"MAIN.H"
  3. #include"i2c.c"

  4. void Times_Add();               
  5. void Times_Run();                       
  6. void Init_Port();
  7. void Init_Interrupt();
  8. void Delay_1ms();
  9. void Delay(uint);
  10. uint  counter;
  11. uchar times_min;
  12. uint  times_hours_low;
  13. uint  times_hours_high;
  14. uchar        times_min_ADD_FLAG;

  15. void main(void)
  16. {
  17.         Init_Port();
  18.         Init_Interrupt();
  19.         times_min=I2C_Data_Read(rom_p0, 0x00);
  20.         times_hours_low=I2C_Data_Read(rom_p0, 0x01);
  21.         times_hours_high=I2C_Data_Read(rom_p0, 0x02);
  22.         while (1)       
  23.         {               
  24.           Times_Run()        ;
  25.           if(RESET==0)       
  26.             {  
  27.        Delay(10);
  28.        if(RESET==0)                       
  29.                          {                                                        
  30.                                   TR0=0;
  31.                                   ET0=0;                                               
  32.                             LED_GREEN_POWER =1;
  33.                             LED_RED_POWER=1;
  34.                       Delay(5);
  35.                             I2C_Data_Write(rom_p0, 0x00, 0);
  36.                 I2C_Data_Write(rom_p0, 0x01, 0);
  37.                 I2C_Data_Write(rom_p0, 0x02, 0);
  38.                                         Delay(5);
  39.                                                          }
  40.       }
  41.   }
  42. }

  43. void Times_Add()                               
  44. {        
  45.   times_min=I2C_Data_Read(rom_p0, 0x00);
  46.         times_hours_low=I2C_Data_Read(rom_p0, 0x01);
  47.         times_hours_high=I2C_Data_Read(rom_p0, 0x02);
  48.         Delay(3);
  49.          if(counter==1200)
  50.               {
  51.                      counter=0;
  52.          times_min++;
  53.                      if(times_min<60)
  54.                        {
  55.                               times_min++;       
  56.                                          }
  57.          else
  58.                                   {       
  59.             times_min=0;                                               
  60.                               times_min_ADD_FLAG=1;                                               
  61.                         }
  62.                }
  63.         //--------------------         
  64.         if(times_min_ADD_FLAG)
  65.         {
  66.                 if(times_hours_low<255)
  67.                   {
  68.                         times_hours_low++;
  69.                   }
  70.                 else if(times_hours_high <244)
  71.                   {
  72.                         times_hours_high++;
  73.                   }
  74.                 else
  75.                   {
  76.                         times_hours_high=0;
  77.                   }
  78.                 times_min_ADD_FLAG=0;
  79.          }
  80.   Delay(3);
  81.         I2C_Data_Write(rom_p0, 0x00, times_min);
  82.         I2C_Data_Write(rom_p0, 0x01, times_hours_low);
  83.         I2C_Data_Write(rom_p0, 0x02, times_hours_high);
  84.         Delay(3);
  85. }


  86. void Times_Run()                               
  87. {
  88.   if(!COUNT_FLAG)
  89.   {
  90.     uint hours;
  91.     Times_Add();

  92.     Delay(10);
  93.     times_hours_low=I2C_Data_Read(rom_p0, 0x01);
  94.     times_hours_high=I2C_Data_Read(rom_p0, 0x02);
  95.     Delay(10);

  96.     hours=times_hours_low +times_hours_high;
  97.                
  98.                 if(hours<161)
  99.                   {
  100.             LED_GREEN_POWER =1;
  101.                         LED_RED_POWER=0;
  102.       }
  103.                 else
  104.                   {
  105.                         LED_GREEN_POWER =0;
  106.                         LED_RED_POWER=1;
  107.                         Delay(3);
  108.                         I2C_Data_Write(rom_p0, 0x00, 0);
  109.             I2C_Data_Write(rom_p0, 0x01, 0);
  110.             I2C_Data_Write(rom_p0, 0x02, 0);
  111.                         Delay(5);
  112.       }
  113.         }
  114. }


  115. void Delay_1ms()
  116. {
  117.         TMOD=0x11;

  118.         TH1=-(1000/256); //·Å³õÖµ
  119.         TL1=-(1000%256);

  120.         TR1=1;
  121.        
  122.         do{}while( !TF1 );

  123.         TR1=0;
  124.         TF1=0;
  125. }


  126. void Delay( uint delay_ms)
  127. {
  128.         uint i;
  129.        
  130.         for (i=0;i< delay_ms;i++)
  131.           {
  132.                 Delay_1ms();
  133.           }
  134. }


  135. void Init_Port()
  136. {

  137.         P1 = 0x22;
  138.         P3 = 0x70;
  139. }


  140. void Init_Interrupt()
  141. {
  142.         TMOD=0x11;
  143.         TH0=(65536L-50000)/256;
  144.   TL0=(65536L-50000)%256;
  145.         TR0=1;
  146.         IT0=1;
  147.         EX0=1;
  148.         ET0=1;
  149.         EA=1;
  150. }

  151. void time0(void) interrupt 1
  152. {
  153.         TH0=(65536L-50000)/256;
  154.   TL0=(65536L-50000)%256;
  155.         counter++;
  156. }
 楼主| zzjj44 发表于 2016-3-10 11:16 | 显示全部楼层
各位大神,指点一二啊
dwq175 发表于 2016-3-10 15:52 | 显示全部楼层
是不是你驱动LED的电平反了。
bear_li 发表于 2016-3-10 16:00 | 显示全部楼层
进入高字节时,低字节没清零
 楼主| zzjj44 发表于 2016-3-11 12:07 | 显示全部楼层
dwq175 发表于 2016-3-10 15:52
是不是你驱动LED的电平反了。

我用的共阴极led灯,不会反
 楼主| zzjj44 发表于 2016-3-11 12:07 | 显示全部楼层
bear_li 发表于 2016-3-10 16:00
进入高字节时,低字节没清零

怎么讲,还请明示啊
bear_li 发表于 2016-3-11 16:08 | 显示全部楼层
73行
                else if(times_hours_high <244)
                  {
                         times_hours_low=0;
                        times_hours_high++;
                  }
 楼主| zzjj44 发表于 2016-3-17 08:30 | 显示全部楼层
bear_li 发表于 2016-3-11 16:08
73行
                else if(times_hours_high

多谢回复!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

77

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

4

主题

77

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部