用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++;
}
|