打印
[51单片机]

利用E2PROM存储设备整体使用时间

[复制链接]
966|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
zzjj44|  楼主 | 2016-3-10 11:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用51单片机做个小东西,目的是当累积时间(hours)达到500小时时,红灯亮(LED_RED_POWER=1),平时绿灯亮(LED_GREEN_POWER =1),在仿真时,设定hours=161时,绿灯亮;hours=160时,红灯亮,请教大神是怎么回事??急等!
#include"reg52.h"
#include"MAIN.H"
#include"i2c.c"

void Times_Add();               
void Times_Run();                       
void Init_Port();
void Init_Interrupt();
void Delay_1ms();
void Delay(uint);
uint  counter;
uchar times_min;
uint  times_hours_low;
uint  times_hours_high;
uchar        times_min_ADD_FLAG;

void main(void)
{
        Init_Port();
        Init_Interrupt();
        times_min=I2C_Data_Read(rom_p0, 0x00);
        times_hours_low=I2C_Data_Read(rom_p0, 0x01);
        times_hours_high=I2C_Data_Read(rom_p0, 0x02);
        while (1)       
        {               
          Times_Run()        ;
          if(RESET==0)       
            {  
       Delay(10);
       if(RESET==0)                       
                         {                                                        
                                  TR0=0;
                                  ET0=0;                                               
                            LED_GREEN_POWER =1;
                            LED_RED_POWER=1;
                      Delay(5);
                            I2C_Data_Write(rom_p0, 0x00, 0);
                I2C_Data_Write(rom_p0, 0x01, 0);
                I2C_Data_Write(rom_p0, 0x02, 0);
                                        Delay(5);
                                                         }
      }
  }
}

void Times_Add()                               
{        
  times_min=I2C_Data_Read(rom_p0, 0x00);
        times_hours_low=I2C_Data_Read(rom_p0, 0x01);
        times_hours_high=I2C_Data_Read(rom_p0, 0x02);
        Delay(3);
         if(counter==1200)
              {
                     counter=0;
         times_min++;
                     if(times_min<60)
                       {
                              times_min++;       
                                         }
         else
                                  {       
            times_min=0;                                               
                              times_min_ADD_FLAG=1;                                               
                        }
               }
        //--------------------         
        if(times_min_ADD_FLAG)
        {
                if(times_hours_low<255)
                  {
                        times_hours_low++;
                  }
                else if(times_hours_high <244)
                  {
                        times_hours_high++;
                  }
                else
                  {
                        times_hours_high=0;
                  }
                times_min_ADD_FLAG=0;
         }
  Delay(3);
        I2C_Data_Write(rom_p0, 0x00, times_min);
        I2C_Data_Write(rom_p0, 0x01, times_hours_low);
        I2C_Data_Write(rom_p0, 0x02, times_hours_high);
        Delay(3);
}


void Times_Run()                               
{
  if(!COUNT_FLAG)
  {
    uint hours;
    Times_Add();

    Delay(10);
    times_hours_low=I2C_Data_Read(rom_p0, 0x01);
    times_hours_high=I2C_Data_Read(rom_p0, 0x02);
    Delay(10);

    hours=times_hours_low +times_hours_high;
               
                if(hours<161)
                  {
            LED_GREEN_POWER =1;
                        LED_RED_POWER=0;
      }
                else
                  {
                        LED_GREEN_POWER =0;
                        LED_RED_POWER=1;
                        Delay(3);
                        I2C_Data_Write(rom_p0, 0x00, 0);
            I2C_Data_Write(rom_p0, 0x01, 0);
            I2C_Data_Write(rom_p0, 0x02, 0);
                        Delay(5);
      }
        }
}


void Delay_1ms()
{
        TMOD=0x11;

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

        TR1=1;
       
        do{}while( !TF1 );

        TR1=0;
        TF1=0;
}


void Delay( uint delay_ms)
{
        uint i;
       
        for (i=0;i< delay_ms;i++)
          {
                Delay_1ms();
          }
}


void Init_Port()
{

        P1 = 0x22;
        P3 = 0x70;
}


void Init_Interrupt()
{
        TMOD=0x11;
        TH0=(65536L-50000)/256;
  TL0=(65536L-50000)%256;
        TR0=1;
        IT0=1;
        EX0=1;
        ET0=1;
        EA=1;
}

void time0(void) interrupt 1
{
        TH0=(65536L-50000)/256;
  TL0=(65536L-50000)%256;
        counter++;
}

相关帖子

沙发
zzjj44|  楼主 | 2016-3-10 11:16 | 只看该作者
各位大神,指点一二啊

使用特权

评论回复
板凳
dwq175| | 2016-3-10 15:52 | 只看该作者
是不是你驱动LED的电平反了。

使用特权

评论回复
地板
bear_li| | 2016-3-10 16:00 | 只看该作者
进入高字节时,低字节没清零

使用特权

评论回复
5
zzjj44|  楼主 | 2016-3-11 12:07 | 只看该作者
dwq175 发表于 2016-3-10 15:52
是不是你驱动LED的电平反了。

我用的共阴极led灯,不会反

使用特权

评论回复
6
zzjj44|  楼主 | 2016-3-11 12:07 | 只看该作者
bear_li 发表于 2016-3-10 16:00
进入高字节时,低字节没清零

怎么讲,还请明示啊

使用特权

评论回复
7
bear_li| | 2016-3-11 16:08 | 只看该作者
73行
                else if(times_hours_high <244)
                  {
                         times_hours_low=0;
                        times_hours_high++;
                  }

使用特权

评论回复
8
zzjj44|  楼主 | 2016-3-17 08:30 | 只看该作者
bear_li 发表于 2016-3-11 16:08
73行
                else if(times_hours_high

多谢回复!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

77

帖子

1

粉丝