51单片机问题求助(temprature == 0) 时6小时后LED关不了
时间:10-02
整理:3721RD
点击:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define sysclk 12000000
uint T0_ONE_MS = 0;
uint T0_ONE_S = 0;
uint T0_ONE_Miniute = 0;
uint T0_TEN_Miniute = 0;
uint T0_ADD_TEN_Miniute = 0;
uchar T0_Four_Hour = 0;
uchar T0_THREE_Hour = 0;
uchar LED_PWM = 0;
sbit SIGNAL = P3^2; //晚上:SIGNAL=1;白天:SIGNAL=0
sbit LED = P3^1;
sbit temprature =P3^0; //夏天:temprature=1;冬天:temprature=0
void Reset_Value(void)
{
T0_ONE_MS = 0;
T0_ONE_S = 0;
T0_TEN_Miniute = 0;
T0_Four_Hour = 0;
T0_THREE_Hour = 0;
TR0 = 0;
TR1 = 0;
}
void Time_Init(void)
{
TMOD = 0x11; //定时器0.1,模式1
EA = 1; //开总中断
ET0 = 1; //开定时器0中断
ET1 = 1; //开定时器1中断
TL0 = (65536-1000)%256; //1ms
TH0 = (65536-1000)/256;
TL1 = (65536-2200)%256;
TH1 = (65536-2200)/256;
}
void main()
{
Time_Init();
LED = 0;
SIGNAL = 0;
temprature = 0;
while(1)
{
if(SIGNAL == 0)
{
Reset_Value();
LED = 0;
}
else if(SIGNAL == 1) //天黑,SIGNAL=1
{
if(temprature == 1) //夏天
{TR0 = 1; //用定时器0延时10分钟
if(T0_TEN_Miniute == 1)
{
T0_TEN_Miniute = 0;
LED = 1; //点亮负载
T0_ADD_TEN_Miniute++;
if(T0_ADD_TEN_Miniute == 25) //4小时时间到
{
T0_Four_Hour = 1;
T0_ADD_TEN_Miniute = 0;
}
}
}
else if(temprature == 0) //冬天
{TR0 = 1;
if(T0_TEN_Miniute == 1)
{
T0_TEN_Miniute = 0;
LED = 1; //点亮负载
T0_ADD_TEN_Miniute++;
if(T0_ADD_TEN_Miniute == 19) //3小时时间到
{
T0_THREE_Hour = 1;
T0_ADD_TEN_Miniute++;
}
}
}
}
if(T0_ADD_TEN_Miniute > 37) //6小时时间到
{
LED = 0; //关闭负载
T0_ADD_TEN_Miniute = 0;
TR1 = 0;
}
else if(T0_Four_Hour == 1)
{
T0_Four_Hour = 0;
TR1 = 1; //开定时器1
LED = 0; //关闭负载
;
}
else if(T0_THREE_Hour == 1)
{
T0_THREE_Hour = 0;
TR1 = 1; //开定时器1
}
}
}
void Time0() interrupt 1
{
TL0 = (65536-1000)%256; //1ms
TH0 = (65536-1000)/256;
T0_ONE_MS++;
if(T0_ONE_MS>1000)
{
T0_ONE_MS = 0;
T0_ONE_S++;
if(T0_ONE_S == 600) //10分钟时间到
{
T0_ONE_S = 0;
T0_TEN_Miniute = 1;
}
}
}
void Time1() interrupt 3 //300Hz
{
if(LED_PWM == 1)
{
TL1 = (65536-1100)%256; //1100us
TH1 = (65536-1100)/256;
LED_PWM = 0;
LED = 1;
}
else if(LED_PWM == 0)
{
TL1 = (65536-2200)%256; //2200us
TH1 = (65536-2200)/256;
LED_PWM = 1;
LED = 0;
}
}
#define uchar unsigned char
#define uint unsigned int
#define sysclk 12000000
uint T0_ONE_MS = 0;
uint T0_ONE_S = 0;
uint T0_ONE_Miniute = 0;
uint T0_TEN_Miniute = 0;
uint T0_ADD_TEN_Miniute = 0;
uchar T0_Four_Hour = 0;
uchar T0_THREE_Hour = 0;
uchar LED_PWM = 0;
sbit SIGNAL = P3^2; //晚上:SIGNAL=1;白天:SIGNAL=0
sbit LED = P3^1;
sbit temprature =P3^0; //夏天:temprature=1;冬天:temprature=0
void Reset_Value(void)
{
T0_ONE_MS = 0;
T0_ONE_S = 0;
T0_TEN_Miniute = 0;
T0_Four_Hour = 0;
T0_THREE_Hour = 0;
TR0 = 0;
TR1 = 0;
}
void Time_Init(void)
{
TMOD = 0x11; //定时器0.1,模式1
EA = 1; //开总中断
ET0 = 1; //开定时器0中断
ET1 = 1; //开定时器1中断
TL0 = (65536-1000)%256; //1ms
TH0 = (65536-1000)/256;
TL1 = (65536-2200)%256;
TH1 = (65536-2200)/256;
}
void main()
{
Time_Init();
LED = 0;
SIGNAL = 0;
temprature = 0;
while(1)
{
if(SIGNAL == 0)
{
Reset_Value();
LED = 0;
}
else if(SIGNAL == 1) //天黑,SIGNAL=1
{
if(temprature == 1) //夏天
{TR0 = 1; //用定时器0延时10分钟
if(T0_TEN_Miniute == 1)
{
T0_TEN_Miniute = 0;
LED = 1; //点亮负载
T0_ADD_TEN_Miniute++;
if(T0_ADD_TEN_Miniute == 25) //4小时时间到
{
T0_Four_Hour = 1;
T0_ADD_TEN_Miniute = 0;
}
}
}
else if(temprature == 0) //冬天
{TR0 = 1;
if(T0_TEN_Miniute == 1)
{
T0_TEN_Miniute = 0;
LED = 1; //点亮负载
T0_ADD_TEN_Miniute++;
if(T0_ADD_TEN_Miniute == 19) //3小时时间到
{
T0_THREE_Hour = 1;
T0_ADD_TEN_Miniute++;
}
}
}
}
if(T0_ADD_TEN_Miniute > 37) //6小时时间到
{
LED = 0; //关闭负载
T0_ADD_TEN_Miniute = 0;
TR1 = 0;
}
else if(T0_Four_Hour == 1)
{
T0_Four_Hour = 0;
TR1 = 1; //开定时器1
LED = 0; //关闭负载
;
}
else if(T0_THREE_Hour == 1)
{
T0_THREE_Hour = 0;
TR1 = 1; //开定时器1
}
}
}
void Time0() interrupt 1
{
TL0 = (65536-1000)%256; //1ms
TH0 = (65536-1000)/256;
T0_ONE_MS++;
if(T0_ONE_MS>1000)
{
T0_ONE_MS = 0;
T0_ONE_S++;
if(T0_ONE_S == 600) //10分钟时间到
{
T0_ONE_S = 0;
T0_TEN_Miniute = 1;
}
}
}
void Time1() interrupt 3 //300Hz
{
if(LED_PWM == 1)
{
TL1 = (65536-1100)%256; //1100us
TH1 = (65536-1100)/256;
LED_PWM = 0;
LED = 1;
}
else if(LED_PWM == 0)
{
TL1 = (65536-2200)%256; //2200us
TH1 = (65536-2200)/256;
LED_PWM = 1;
LED = 0;
}
}
感觉你的逻辑好像冲突了,代码太多,没时间给你全部分析过去
if(T0_ADD_TEN_Miniute > 37) //6小时时间到
你的程序执行不到这句后面去啊!