|
//说明:水位智能控制。20%-40%-60%-80%-99%继电器输出.
//时钟设定,一路时间智能控制继电器输出,
//水泵智能控制继电器输出。
//水泵工作时间智能控制继电器输出
#include
#include
#define Fosc (11059200) //Board cyrstal
/////////////////////////////////////////////////////////////////////////////////////////////////////////
sbit SecFlash = P3^3;
sbit SEG0 = P3^7;
sbit SEG1 = P3^6;
sbit SEG2 = P3^5;
sbit SEG3 = P3^4;
const unsigned char table[16] = { //0-F
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char RTC_BUFF[4];
unsigned char SPEED_BUFF[4];
unsigned char WATER_BUFF[4];
unsigned char SETUP_BUFF[4];
unsigned char ERROR_BUFF[4]; //显示"空BUG"
//数码管相关函数声明
void Date_Load(void);
void Led_Display(unsigned char disrow);
void Date_Update(void);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//DS1302引脚定义
sbit DS1302_RST = P1^5;
sbit DS1302_CLK = P1^6;
sbit DS1302_SDA = P1^7;
//相关数量定义
volatile unsigned char hour = 0;
volatile unsigned char minute = 0;
volatile unsigned char second = 0;
//DS1302相关函数声明
void DS1302_Init(void);
void DS1302_WriteByte(unsigned char Data);
unsigned char DS1302_ReadByte(void);
void DS1302_Write(unsigned char Addr, unsigned char Data);
unsigned char DS1302_Read(unsigned char Addr);
void DS1302_SecondSet(unsigned char Secnod);
void DS1302_MinuteSet(unsigned char Minute);
void DS1302_HourSet(unsigned char Hour);
void DS1302_DateSet(unsigned char Date);
void DS1302_WeekdaySet(unsigned char Weekday);
void DS1302_MonthSet(unsigned char Month);
void DS1302_YearSet(unsigned char Year);
unsigned char DS1302_SecondGet(void);
unsigned char DS1302_MinuteGet(void);
unsigned char DS1302_HourGet(void);
unsigned char BINToBCD(unsigned char val);
unsigned char BCDToBIN(unsigned char Val);
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//主循环相关变量的定义
unsigned int TCNT0 = 0;
unsigned char disrow = 0;
unsigned char dis_flag = 0; // 0=RTC ; 1=SPEED; 2=水位状态值,3=设置界面
//static unsigned char save_current_dis_flag = 0;
//基准定时器的初始化声明
void Timer0_Init(void);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned int OneSecondCounter = 0;
unsigned char pulse_counter = 0;
volatile unsigned int current_speed = 0;
//采用下降沿计脉冲,初始化函数声明
void INT0_Init(void);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
sbit Water20 = P1^0;
sbit Water40 = P1^1;
sbit Water60 = P1^2;
sbit Water80 = P1^3;
sbit WaterA0 = P1^4;
sbit Elec_Relay = P2^5; //Electromagnetic valve
volatile unsigned char current_water_status = 0;
//volatile unsigned char elec_relay_allow_on_flag = 0;
//volatile unsigned int elec_relay_delay2sec = 0;
void Water_Handler(void);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
sbit Heater_Relay = P2^6;
//相关变量定义
unsigned char heater_time_flag = 0; //0=设定开启时间<设定关闭时间,不跨天;1=设定开启时间>设定关闭时间,跨天
//相关函数声明
void Heater_Handler(void);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
sbit Pump_Relay = P2^4;
//相关变量定义
unsigned char pump_time_flag = 0;
unsigned char in_allow_work_time_flag = 0;
unsigned char pass_lower_limit_flag = 0;
unsigned char pass_upper_limit_flag = 0;
unsigned char lower_limit_continue_second = 0;
unsigned char upper_limit_continue_second = 0;
unsigned char continue_not_pass_second = 0;
unsigned char continue_second = 0;
//相关函数声明
void Pump_Handler(void);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Key相关引脚定义,要更换Button引脚接口,需在按键处理程序中进行修改
//sbit Set = P2^1;
//sbit Up = P2^0;
//sbit Down = P2^3;
//sbit Stratup = P2^2;
//Key相关变量定义
unsigned char pump_turnon_flag = 0;
unsigned int work_time = 0;
unsigned char setup_nemu = 0;
unsigned char key_value = 0;
unsigned char in_setup_flag = 0;
volatile unsigned int speed_lower_limit = 0;
volatile unsigned char speed_lower_time = 0;
volatile unsigned int speed_upper_limit = 0;
volatile unsigned char speed_upper_time = 0;
volatile unsigned char water_lower_limit = 0;
volatile unsigned char water_upper_limit = 0;
volatile unsigned char pump_start_hour = 0;
volatile unsigned char pump_start_minute = 0;
volatile unsigned char pump_end_hour = 0;
volatile unsigned char pump_end_minute = 0;
volatile unsigned char heater_start_hour = 0;
volatile unsigned char heater_start_minute = 0;
volatile unsigned char heater_end_hour = 0;
volatile unsigned char heater_end_minute = 0;
//Key相关函数声明
///unsigned char Get_Key(void);
unsigned char Read_Key(void);
void Key_Handle(void);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
sbit SCL = P3^0;
sbit SDA = P3^1;
//相关函数声明
void I2C_Delay(void);
void I2C_Init(void);
void I2C_Init(void);
void I2C_Startup(void);
void I2C_Stop(void);
void I2C_WriteByte(unsigned char dat);
unsigned char I2C_ReadByte(void);
void I2C_Write(unsigned char ds,unsigned char Address,unsigned char Dat);
unsigned char I2C_Read(unsigned char ds,unsigned char Address);
void I2C_Write_(unsigned char ds,unsigned char Address,unsigned char *buffer,unsigned char num);
void I2C_Read_(unsigned char ds,unsigned char Address,unsigned char *buffer,unsigned char num);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//unsigned char press_counter = 0;
//unsigned char fivemicrosecond = 0;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Main Function
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void main(void){
Timer0_Init();
INT0_Init();
I2C_Init();
DS1302_Init();
Date_Load();
while(1){
while(TCNT0<4608){ //5ms base time
TCNT0 = TH0*256 + TL0;
};
TCNT0 = 0;//clear
TH0 = 0x00;
TL0 = 0x00;
//显示部分处理
if(++disrow==4){ // 0,1,2,3
disrow = 0;
}
Led_Display(disrow);
//
//数据处理
Date_Update();
Water_Handler();
Heater_Handler();
Pump_Handler();
Key_Handle();
second = DS1302_SecondGet();
//秒闪烁,1S亮1S灭.....
//
}
}
void Led_Display(unsigned char disrow){
SEG0 = 1; SEG1 = 1; SEG2 =1; SEG3 = 1; //关所有数码管位显示
if(dis_flag==0){ //送数码管段代码到P0口
P0 = RTC_BUFF[disrow];
}
else if(dis_flag==1){
P0 = SPEED_BUFF[disrow];
}
else if(dis_flag==2){
P0 = WATER_BUFF[disrow];
}
else if(dis_flag==3){
P0 = SETUP_BUFF[disrow];
}
else{
P0 = ERROR_BUFF[disrow];
}
if(disrow==0){ //开相应数码管位显示
SEG0 = 1; SEG1 = 1; SEG2 = 1; SEG3 = 0;
}
else if(disrow==1){
SEG0 = 1; SEG1 = 1; SEG2 = 0; SEG3 = 1;
}
else if(disrow==2){
SEG0 = 1; SEG1 = 0; SEG2 = 1; SEG3 = 1;
}
else if(disrow==3){
SEG0 = 0; SEG1 = 1; SEG2 = 1; SEG3 = 1;
}
}
//数据初始化,读取存储数据加载到相应的缓冲数组中.
void Date_Load(void){
//读取DS1302的实时时间
/*
I2C_Write(0xa0,0x00,400/256);
I2C_Write(0xa0,0x01,400%256);
I2C_Write(0xa0,0x02,5);
I2C_Write(0xa0,0x03,800/256);
I2C_Write(0xa0,0x04,800%256);
I2C_Write(0xa0,0x05,7);
I2C_Write(0xa0,0x06,20);
I2C_Write(0xa0,0x07,80);
I2C_Write(0xa0,0x08,5); //5:00
I2C_Write(0xa0,0x09,0);
I2C_Write(0xa0,0x0a,21); //21:00
I2C_Write(0xa0,0x0b,0);
I2C_Write(0xa0,0x0c,9); //9:00
I2C_Write(0xa0,0x0d,30);
I2C_Write(0xa0,0x0e,22); //22:40
I2C_Write(0xa0,0x0f,40);
*/
speed_lower_limit = I2C_Read(0xa0,0x00)*256;
speed_lower_limit += I2C_Read(0xa0,0x01);
speed_lower_time = I2C_Read(0xa0,0x02);
speed_upper_limit = I2C_Read(0xa0,0x03)*256;
speed_upper_limit += I2C_Read(0xa0,0x04);
speed_upper_time = I2C_Read(0xa0,0x05);
water_lower_limit = I2C_Read(0xa0,0x06);
water_upper_limit = I2C_Read(0xa0,0x07);
pump_start_hour = I2C_Read(0xa0,0x08);
pump_start_minute = I2C_Read(0xa0,0x09);
pump_end_hour = I2C_Read(0xa0,0x0a);
pump_end_minute = I2C_Read(0xa0,0x0b);
heater_start_hour = I2C_Read(0xa0,0x0c);
heater_start_minute = I2C_Read(0xa0,0x0d);
heater_end_hour = I2C_Read(0xa0,0x0e);
heater_end_minute = I2C_Read(0xa0,0x0f);
//读取RTC
hour = DS1302_HourGet();
minute = DS1302_MinuteGet();
second = DS1302_SecondGet();
RTC_BUFF[0] = table[hour/10];
RTC_BUFF[1] = table[hour%10];
RTC_BUFF[2] = table[minute/10];
RTC_BUFF[3] = table[minute%10];
SPEED_BUFF[0] = table[0];
SPEED_BUFF[1] = table[current_speed/100];
SPEED_BUFF[2] = table[current_speed%100/10];
SPEED_BUFF[3] = table[current_speed%10];
WATER_BUFF[0] = table[0];
WATER_BUFF[1] = table[0];
if(current_water_status==100){
WATER_BUFF[2] = table[9];
WATER_BUFF[3] = table[9];
}
else{
WATER_BUFF[2] = table[current_water_status/10];
WATER_BUFF[3] = table[current_water_status%10];
}
SETUP_BUFF[0] = table[5];
SETUP_BUFF[1] = table[5];
SETUP_BUFF[2] = table[5];
SETUP_BUFF[3] = table[5];
}
//dis_flag 为当前显示的界面,当前显示哪个界面就更新哪个数据.
void Date_Update(void){
if(dis_flag==0){ //时间实时显示 XX时XX分
hour = DS1302_HourGet(); //读取RTC的时分秒
minute = DS1302_MinuteGet();
second = DS1302_SecondGet();
if(second%2==0){
SecFlash = 1;
}
else{
SecFlash = 0;
}
RTC_BUFF[0] = table[hour/10];
if(second%2==0){ //秒闪挂在时显示的个位数的小数点上
RTC_BUFF[1] = table[hour%10] & 0x7f;
}
else{
RTC_BUFF[1] = table[hour%10];
}
RTC_BUFF[2] = table[minute/10];
RTC_BUFF[3] = table[minute%10];
}
else if(dis_flag==1){
SPEED_BUFF[0] = 0xff;
SPEED_BUFF[1] = table[current_speed/100];
SPEED_BUFF[2] = table[current_speed%100/10];
SPEED_BUFF[3] = table[current_speed%10];
}
else if(dis_flag==2){
//WATER_BUFF[0] = 0xc6; //'C'
//WATER_BUFF[1] = 0x88; //'R'
WATER_BUFF[0] = 0xff; //'空'
WATER_BUFF[1] = 0xff; //'空'
if(current_water_status==100){
WATER_BUFF[2] = table[9];
WATER_BUFF[3] = table[9];
}
else{
WATER_BUFF[2] = table[current_water_status/10];
WATER_BUFF[3] = table[current_water_status%10];
}
}
else if(dis_flag==3){ //设置界面显示
if(setup_nemu==1){
SETUP_BUFF[0] = 0xc7; //LXXX
SETUP_BUFF[1] = table[speed_lower_limit/100];
SETUP_BUFF[2] = table[speed_lower_limit%100/10];
SETUP_BUFF[3] = table[speed_lower_limit%10];
}
else if(setup_nemu==2){
SETUP_BUFF[0] = table[10]; //AFXX
SETUP_BUFF[1] = table[15];
SETUP_BUFF[2] = table[speed_lower_time/10];
SETUP_BUFF[3] = table[speed_lower_time%10];
}
else if(setup_nemu==3){
SETUP_BUFF[0] = 0x89; //HXXX
SETUP_BUFF[1] = table[speed_upper_limit/100];
SETUP_BUFF[2] = table[speed_upper_limit%100/10];
SETUP_BUFF[3] = table[speed_upper_limit%10];
}
else if(setup_nemu==4){
SETUP_BUFF[0] = table[11]; //BFXX
SETUP_BUFF[1] = table[15];
SETUP_BUFF[2] = table[speed_upper_time/10];
SETUP_BUFF[3] = table[speed_upper_time%10];
}
else if(setup_nemu==5){
SETUP_BUFF[0] = 0xc6; //CLXX
SETUP_BUFF[1] = 0xc7;
if(water_lower_limit==100){
SETUP_BUFF[2] = table[9];
SETUP_BUFF[3] = table[9];
}
else{
SETUP_BUFF[2] = table[water_lower_limit/10];
SETUP_BUFF[3] = table[water_lower_limit%10];
}
}
else if(setup_nemu==6){
SETUP_BUFF[0] = 0xc6; //CHXX
SETUP_BUFF[1] = 0x89;
if(water_upper_limit==100){
SETUP_BUFF[2] = table[9];
SETUP_BUFF[3] = table[9];
}
else{
SETUP_BUFF[2] = table[water_upper_limit/10];
SETUP_BUFF[3] = table[water_upper_limit%10];
}
}
else if(setup_nemu==7){
SETUP_BUFF[0] = 0x89; //H-XX
SETUP_BUFF[1] = 0xbf;
SETUP_BUFF[2] = table[hour/10];
SETUP_BUFF[3] = table[hour%10];
}
else if(setup_nemu==8){
SETUP_BUFF[0] = 0x8c; //P-XX
SETUP_BUFF[1] = 0xbf;
SETUP_BUFF[2] = table[minute/10];
SETUP_BUFF[3] = table[minute%10];
}
else if(setup_nemu==9){
SETUP_BUFF[0] = 0xf9; //1HXX
SETUP_BUFF[1] = 0x89;
SETUP_BUFF[2] = table[pump_start_hour/10];
SETUP_BUFF[3] = table[pump_start_hour%10];
}
else if(setup_nemu==10){
SETUP_BUFF[0] = 0xf9; //1LXX
SETUP_BUFF[1] = 0xc7;
SETUP_BUFF[2] = table[pump_start_minute/10];
SETUP_BUFF[3] = table[pump_start_minute%10];
}
else if(setup_nemu==11){
SETUP_BUFF[0] = 0xf9; //1RXX
SETUP_BUFF[1] = 0x88;
SETUP_BUFF[2] = table[pump_end_hour/10];
SETUP_BUFF[3] = table[pump_end_hour%10];
}
else if(setup_nemu==12){
SETUP_BUFF[0] = 0xf9; //1PXX
SETUP_BUFF[1] = 0x8c;
SETUP_BUFF[2] = table[pump_end_minute/10];
SETUP_BUFF[3] = table[pump_end_minute%10];
}
else if(setup_nemu==13){
SETUP_BUFF[0] = 0xa4; //2HXX
SETUP_BUFF[1] = 0x89;
SETUP_BUFF[2] = table[heater_start_hour/10];
SETUP_BUFF[3] = table[heater_start_hour%10];
}
else if(setup_nemu==14){
SETUP_BUFF[0] = 0xa4; //2LXX
SETUP_BUFF[1] = 0xc7;
SETUP_BUFF[2] = table[heater_start_minute/10];
SETUP_BUFF[3] = table[heater_start_minute%10];
}
else if(setup_nemu==15){
SETUP_BUFF[0] = 0xa4; //2RXX
SETUP_BUFF[1] = 0x88;
SETUP_BUFF[2] = table[heater_end_hour/10];
SETUP_BUFF[3] = table[heater_end_hour%10];
}
else if(setup_nemu==16){
SETUP_BUFF[0] = 0xa4; //2PXX
SETUP_BUFF[1] = 0x8c;
SETUP_BUFF[2] = table[heater_end_minute/10];
SETUP_BUFF[3] = table[heater_end_minute%10];
}
}
}
void Timer0_Init(void){
TMOD = (TMOD&0xf0)| 0x01; //Timer0, Time mode, 16-bit.
TH0 = 0x00; //Load Initial value
TL0 = 0x00;
TR0 = 1; //Stratup Timer0
}
void INT0_Handler(void) interrupt IE0_VECTOR using 0{
++pulse_counter;
}
void INT0_Init(void){
EX0 = 1; //Enable External Interrupt 0
IT0 = 1; //下降沿有效
EA = 1; //Enable
}
void Water_Handler(void){
unsigned char temp = 0;
if(Water20==0){
temp &= ~(1<<0);
}
else{
temp |= (1<<0);
}
if(Water40==0){
temp &= ~(1<<1);
}
else{
temp |= (1<<1);
}
if(Water60==0){
temp &= ~(1<<2);
}
else{
temp |= (1<<2);
}
if(Water80==0){
temp &= ~(1<<3);
}
else{
temp |= (1<<3);
}
if(WaterA0==0){
temp &= ~(1<<4);
}
else{
temp |= (1<<4);
}
temp = temp &0x1f;
if(temp==0x1f){ //0b0001 1111
current_water_status = 0; //显示0
}
else if(temp==0x1e){ //0b0001 1110
current_water_status = 20; //显示20
}
else if(temp==0x1c){ //0b0001 1100
current_water_status = 40; //显示40
}
else if(temp==0x18){ //0b0001 1000
current_water_status = 60; //显示60
}
else if(temp==0x10){ //0b0001 0000
current_water_status = 80; //显示80
}
else if(temp==0x00){ //0b0000 0000
current_water_status = 100; //显示A0
}
else{
current_water_status = 255; //显示FF
}
if( (current_water_status==100) || (current_water_status==255) ){
//dis_flag = save_current_dis_flag;
//dis_flag = 0;
Elec_Relay = 1;
}
else if( (current_water_status==water_upper_limit) || (current_water_status>water_upper_limit) ){
//dis_flag = save_current_dis_flag;
//dis_flag = 0;
Elec_Relay = 1;
}
else if( ((current_water_status>water_lower_limit)||(current_water_status==water_lower_limit))&&(current_water_status
}
else if(current_water_status
//save_current_dis_flag = dis_flag;
//dis_flag = 2;
Elec_Relay = 0;
}
//end
}
void Heater_Handler(void){
if(heater_start_hour
heater_time_flag = 0;
}
else if(heater_start_hour==heater_end_hour){
if(heater_start_minute
heater_time_flag = 0;
}
else if(heater_start_minute==heater_end_minute){
}
else if(heater_start_minute>heater_end_minute){
heater_time_flag = 1;
}
}
else{//else if(heater_start_hour>heater_end_hour){
heater_time_flag = 1;
}
if(heater_time_flag==0){
if( ((hour*60+minute)>=(heater_start_hour*60+heater_start_minute))&&((hour*60+minute)<(heater_end_hour*60+heater_end_minute)) ){
Heater_Relay = 0;
}
else{
Heater_Relay = 1;
}
}
else if(heater_time_flag==1){
if( ((hour*60+minute)>=(heater_end_hour*60+heater_end_minute))&&((hour*60+minute)<(heater_start_hour*60+heater_start_minute)) ){
Heater_Relay = 1;
}
else{
Heater_Relay = 0;
}
}
}
void Pump_Handler(void){
if(pump_start_hour
pump_time_flag = 0;
}
else if(pump_start_hour==pump_end_hour){
if(pump_start_minute
pump_time_flag = 0;
}
else if(pump_start_minute==pump_end_minute){
}
else if(pump_start_minute>pump_end_minute){
pump_time_flag = 1;
}
}
else{//else if(heater_start_hour>heater_end_hour){
pump_time_flag = 1;
}
if(pump_time_flag==0){ //开启时间<关闭时间.for examle : 9:00开 15:00关
if( ((hour*60+minute)>=(pump_start_hour*60+pump_start_minute))&&((hour*60+minute)<(pump_end_hour*60+pump_end_minute)) ){
in_allow_work_time_flag = 1;
}
else{
in_allow_work_time_flag = 0;
}
}
else if(pump_time_flag==1){
if( ((hour*60+minute)>=(pump_end_hour*60+pump_end_minute))&&((hour*60+minute)<(pump_start_hour*60+pump_start_minute)) ){
in_allow_work_time_flag = 0;
}
else{
in_allow_work_time_flag = 1;
}
}
if(in_allow_work_time_flag==1){
//Pump_Relay = 0;
if(++OneSecondCounter==200){
OneSecondCounter = 0;
current_speed = pulse_counter;
pulse_counter = 0;
if( (current_speed>speed_lower_limit) ||(current_speed==speed_lower_limit) ){
++lower_limit_continue_second;
if(lower_limit_continue_second
Pump_Relay = 1;
pass_lower_limit_flag = 0;
}
else if(lower_limit_continue_second==speed_lower_time){
Pump_Relay = 0;
pass_lower_limit_flag = 1;
}
else{
if(lower_limit_continue_second>200){
lower_limit_continue_second = speed_lower_time;
}
Pump_Relay = 0;
pass_lower_limit_flag = 1;
}
}
else{//if(current_speed
Pump_Relay = 1;
lower_limit_continue_second = 0;
pass_lower_limit_flag = 0;
}
\
if( (pass_lower_limit_flag==1)&&(pass_upper_limit_flag==0) ){ \
if( (current_speed>speed_upper_limit) ||(current_speed==speed_upper_limit) ){
continue_not_pass_second = 0; //清零连续不能达到流速上限计秒器
++upper_limit_continue_second;
if(upper_limit_continue_second
Pump_Relay = 0;
pass_upper_limit_flag = 0;
}
else if(upper_limit_continue_second==speed_upper_time){
Pump_Relay = 0;
pass_upper_limit_flag = 1;
}
else{ //else if(upper_limit_continue_second>speed_upper_time){
if(upper_limit_continue_second>200){
upper_limit_continue_second = speed_upper_time;
}
Pump_Relay = 0;
pass_upper_limit_flag = 1;
}
}
else{//else if(current_speed
upper_limit_continue_second = 0;
++continue_not_pass_second;
if(continue_not_pass_second
Pump_Relay = 0;
pass_upper_limit_flag = 0;
}
else if(continue_not_pass_second==speed_upper_time){
Pump_Relay = 1;
pass_upper_limit_flag = 0;
}
else{ //else if(continue_not_pass_second>speed_upper_time){
Pump_Relay = 1;
pass_lower_limit_flag = 0;
pass_upper_limit_flag = 0;
continue_not_pass_second = 0 ;
upper_limit_continue_second = 0;
lower_limit_continue_second = 0;
}
}
}
if( (pass_lower_limit_flag==1)&&(pass_upper_limit_flag==1) ){
if( (current_speed>speed_upper_limit) ||(current_speed==speed_upper_limit) ){
Pump_Relay = 0;
pass_upper_limit_flag = 1;
continue_second = 0;
}
else{ //if(current_speed
continue_second++;
if(continue_second
Pump_Relay = 0;
pass_upper_limit_flag = 1;
}
else{//if(continue_second>=upper_limit_time)
Pump_Relay = 1;
pass_lower_limit_flag = 0;
pass_upper_limit_flag = 0;
continue_not_pass_second = 0 ;
upper_limit_continue_second = 0;
lower_limit_continue_second = 0;
continue_second = 0;
}
}
}
}
}
else{//if(in_allow_work_time_flag==0){
Pump_Relay = 1;
pass_lower_limit_flag = 0;
pass_lower_limit_flag = 0;
continue_not_pass_second = 0 ;
upper_limit_continue_second = 0;
lower_limit_continue_second = 0;
continue_second = 0;
}
unsigned char Read_key(void){
static unsigned char last_key = 0xff;
static unsigned int key_count = 0;
#define c_wobble_time (10)
#define c_keyover_time (200)
#define c_keyquick_time (20)
static unsigned int keyover_time = c_keyover_time;
unsigned char nc;
nc = P2&0x0f;
if(nc==0){
key_count = 0;
keyover_time = c_keyover_time;
return 0;
}
else{
if(nc==last_key){
if(++key_count==c_wobble_time){
return nc;
}
else{
if(key_count>keyover_time){
key_count = 0;
keyover_time = c_keyquick_time;
}
return 0;
}
}
else{
last_key = nc;
key_count = 0;
keyover_time = c_keyover_time;
return 0;
}
}
}
void Key_Handle(void){
key_value = Read_key();
if(key_value==0x0b){
pump_turnon_flag = 1;
Pump_Relay = 0;
work_time = 0;
}
if(pump_turnon_flag==1){
if(++work_time==1500){
Pump_Relay = 1;
work_time = 0;
pump_turnon_flag = 0;
}
else{
Pump_Relay = 0;
}
}
//end
if(dis_flag==0){
if(key_value==0x0d){
setup_nemu = 1;
dis_flag = 3;
}
if(key_value==0x0e){
dis_flag = 2;
}
if(key_value==0x07){
dis_flag = 1;
}
}
else if(dis_flag==1){
if(key_value==0x07){
dis_flag = 0;
}
}
else if(dis_flag==2){
if(key_value==0x0e){
dis_flag = 0;
}
}
else if(dis_flag==3){
if(setup_nemu==1){
if(key_value==0x0e){ //Up
if(++speed_lower_limit>999){
speed_lower_limit = 0;
}
}
if(key_value==0x07){ //Down
if(speed_lower_limit==0){
speed_lower_limit = 999;
}
speed_lower_limit--;
}
if(key_value==0x0d){ //Set
setup_nemu = 2;
I2C_Write(0xa0,0x00,speed_lower_limit/256);
I2C_Write(0xa0,0x01,speed_lower_limit%256);
}
}
else if(setup_nemu==2){
if(key_value==0x0e){ //Up
if(++speed_lower_time>10){
speed_lower_time = 1;
}
}
if(key_value==0x07){ //Down
if(--speed_lower_time==0){
speed_lower_time = 10;
}
}
if(key_value==0x0d){ //Set
setup_nemu = 3;
I2C_Write(0xa0,0x02,speed_lower_time);
}
}
else if(setup_nemu==3){
if(key_value==0x0e){ //Up
if(++speed_upper_limit>999){
speed_upper_limit = 0;
}
}
if(key_value==0x07){ //Down
if(speed_upper_limit==0){
speed_upper_limit = 999;
}
speed_upper_limit--;
}
if(key_value==0x0d){ //Set
setup_nemu = 4;
I2C_Write(0xa0,0x03,speed_upper_limit/256);
I2C_Write(0xa0,0x04,speed_upper_limit%256);
}
}
else if(setup_nemu==4){
if(key_value==0x0e){ //Up
if(++speed_upper_time>10){
speed_upper_time = 1;
}
}
if(key_value==0x07){ //Down
if(--speed_upper_time==0){
speed_upper_time = 10;
}
}
if(key_value==0x0d){ //Set
setup_nemu = 5;
I2C_Write(0xa0,0x05,speed_upper_time);
}
}
else if(setup_nemu==5){
if(key_value==0x0e){ //Up
water_lower_limit = (water_lower_limit/20)*20 + 20;
if(water_lower_limit>100){
water_lower_limit = 20;
}
}
if(key_value==0x07){ //Down
water_lower_limit = (water_lower_limit/20)*20 - 20;
if(water_lower_limit==0){
water_lower_limit = 100;
}
}
if(key_value==0x0d){ //Set
setup_nemu = 6;
I2C_Write(0xa0,0x06,water_lower_limit);
}
}
else if(setup_nemu==6){
if(key_value==0x0e){ //Up
water_upper_limit = (water_upper_limit/20)*20 + 20;
if(water_upper_limit>100){
water_upper_limit = 20;
}
}
if(key_value==0x07){ //Down
water_upper_limit = (water_upper_limit/20)*20 - 20;
if(water_upper_limit==0){
water_upper_limit = 100;
}
}
if(key_value==0x0d){ //Set
if( (water_upper_limit>water_lower_limit) || (water_upper_limit==water_lower_limit) ){
setup_nemu = 7;
I2C_Write(0xa0,0x07,water_upper_limit);//
}
else{ //if(water_upper_limit<=water_lower_limit){
setup_nemu = 5;
}
}
}
else if(setup_nemu==7){
if(key_value==0x0e){ //Up
if(++hour>23){
hour = 0;
}
}
if(key_value==0x07){ //Down
if(hour==0){
hour = 24;
}
hour--;
}
if(key_value==0x0d){ //Set
setup_nemu = 8;
DS1302_HourSet(hour);
}
}
else if(setup_nemu==8){
if(key_value==0x0e){ //Up
if(++minute>59){
minute = 0;
}
}
if(key_value==0x07){ //Down
if(minute==0){
minute = 60;
}
minute--;
}
if(key_value==0x0d){ //Set
setup_nemu = 9;
DS1302_MinuteSet(minute);
}
}
else if(setup_nemu==9){
if(key_value==0x0e){ //Up
if(++pump_start_hour>23){
pump_start_hour = 0;
}
}
if(key_value==0x07){ //Down
if(pump_start_hour==0){
pump_start_hour = 23;
}
pump_start_hour--;
}
if(key_value==0x0d){ //Set
setup_nemu = 10;
I2C_Write(0xa0,0x08,pump_start_hour);
}
}
else if(setup_nemu==10){
if(key_value==0x0e){ //Up
if(++pump_start_minute>59){
pump_start_minute = 0;
}
}
if(key_value==0x07){ //Down
if(pump_start_minute==0){
pump_start_minute = 59;
}
pump_start_minute--;
}
if(key_value==0x0d){ //Set
setup_nemu = 11;
I2C_Write(0xa0,0x09,pump_start_minute);
}
}
else if(setup_nemu==11){
if(key_value==0x0e){ //Up
if(++pump_end_hour>23){
pump_end_hour = 0;
}
}
if(key_value==0x07){ //Down
if(pump_end_hour==0){
pump_end_hour = 23;
}
pump_end_hour--;
}
if(key_value==0x0d){ //Set
setup_nemu = 12;
I2C_Write(0xa0,0x0a,pump_end_hour);
}
}
else if(setup_nemu==12){
if(key_value==0x0e){ //Up
if(++pump_end_minute>59){
pump_end_minute = 0;
}
}
if(key_value==0x07){ //Down
if(pump_end_minute==0){
pump_end_minute = 59;
}
pump_end_minute--;
}
if(key_value==0x0d){ //Set
if( (pump_end_hour==pump_start_hour)&& (pump_end_minute==pump_start_minute) ){
setup_nemu = 9;
}
else{
setup_nemu = 13;
I2C_Write(0xa0,0x0b,pump_end_minute);
}
}
}
else if(setup_nemu==13){
if(key_value==0x0e){ //Up
if(++heater_start_hour>23){
heater_start_hour = 0;
}
}
if(key_value==0x07){ //Down
if(heater_start_hour==0){
heater_start_hour = 23;
}
heater_start_hour--;
}
if(key_value==0x0d){ //Set
setup_nemu = 14;
I2C_Write(0xa0,0x0c,heater_start_hour);
}
}
else if(setup_nemu==14){
if(key_value==0x0e){ //Up
if(++heater_start_minute>59){
heater_start_minute = 0;
}
}
if(key_value==0x07){ //Down
if(heater_start_minute==0){
heater_start_minute = 59;
}
heater_start_minute--;
}
if(key_value==0x0d){ //Set
setup_nemu = 15;
I2C_Write(0xa0,0x0d,heater_start_minute);
}
}
else if(setup_nemu==15){
if(key_value==0x0e){ //Up
if(++heater_end_hour>23){
heater_end_hour = 0;
}
}
if(key_value==0x07){ //Down
if(heater_end_hour==0){
heater_end_hour = 23;
}
heater_end_hour--;
}
if(key_value==0x0d){ //Set
setup_nemu = 16;
I2C_Write(0xa0,0x0e,heater_end_hour);
}
}
else if(setup_nemu==16){
if(key_value==0x0e){ //Up
if(++heater_end_minute>59){
heater_end_minute = 0;
}
}
if(key_value==0x07){ //Down
if(heater_end_minute==0){
heater_end_minute = 59;
}
heater_end_minute--;
}
if(key_value==0x0d){ //Set
if( (heater_end_hour==heater_start_hour)&& (heater_end_minute==heater_start_minute) ){
setup_nemu = 13;
}
else{
setup_nemu = 0;
dis_flag = 0;
I2C_Write(0xa0,0x0f,heater_end_minute);
}
}
}
/*
else if(setup_nemu==17){
if(key_value==0x0d){
setup_nemu = 0;
dis_flag = 0;
}
}
*/
}
}
//****************************************24C16*******************************************
void I2C_Delay(void){
unsigned char i= 0;
for(i=8;i>0;i--)
;
}
void delay10(unsigned int time){
unsigned int i=0;
for(i=0;i
_nop_();
}
void I2C_Init(void){
SCL = 1 ;
SDA=1 ;
I2C_Delay();
}
void I2C_Startup(void){
SCL = 1 ;
I2C_Delay();
SDA=0;
I2C_Delay();
SCL=0;
I2C_Delay();
}
void I2C_Stop(void){
SDA=0;
I2C_Delay();
SCL = 1 ;
I2C_Delay();
SDA=1 ;
I2C_Delay();
SCL=0;
I2C_Delay();
}
void I2C_WriteByte(unsigned char dat){
unsigned char i = 0;
for(i=0;i<8;i++){
if(dat & 0x80)
SDA=1;
else
SDA=0;
dat=(dat<<1);
SCL = 1 ;
I2C_Delay();
SCL=0;
I2C_Delay();
}
SDA=1;
I2C_Delay();
SCL = 1 ;
I2C_Delay();
SCL=0;
I2C_Delay();
}
unsigned char I2C_ReadByte(void){
unsigned char temp = 0;
unsigned char i = 0;
for(i=0;i<8;i++){
temp = temp<<1; //temp = temp*2;
SCL = 1 ;
I2C_Delay();
if(SDA)
temp |= 1;
SCL=0;
I2C_Delay();
}
SDA=1;
I2C_Delay();
return(temp);
}
void I2C_Write(unsigned char ds,unsigned char Address,unsigned char Dat){
I2C_Startup();
I2C_WriteByte(ds);
I2C_WriteByte(Address);
I2C_WriteByte(Dat);
I2C_Stop();
delay10(200);
//delay10(0x500);
}
unsigned char I2C_Read(unsigned char ds,unsigned char Address){
unsigned char temp = 0;
I2C_Startup();
I2C_WriteByte(ds);
I2C_WriteByte(Address);
I2C_Startup();
I2C_WriteByte(ds+1);
temp = I2C_ReadByte();
I2C_Stop();
return(temp);
}
void I2C_Write_(unsigned char ds,unsigned char Address,unsigned char *buffer,unsigned char num){
unsigned char i = 0;
unsigned char temp = 0;
for(i=0;i
temp = *(buffer+i);
I2C_Write(ds,Address+i,temp);
}
}
void I2C_Read_(unsigned char ds,unsigned char Address,unsigned char *buffer,unsigned char num){
unsigned char i = 0;
for(i=0;i
*(buffer+i)=I2C_Read(ds,Address+i);
}
}
//**********************************************END*******************************************************
void DS1302_Init(void){
DS1302_Write(0x8e,0x00); //WP bit is 0,enable wirte
DS1302_Write(0x90,0xA5); //charge
DS1302_Write(0x8e,0x80);
}
void DS1302_WriteByte(unsigned char Data){
unsigned char i = 0;
for(i=8;i>0;i--){
if(Data&1){
DS1302_SDA = 1;
}
else{
DS1302_SDA = 0;
}
DS1302_CLK = 1;
;
;
DS1302_CLK = 0;
;
;
Data = Data >> 1;
}
}
unsigned char DS1302_ReadByte(void){
unsigned char i = 0;
unsigned char Data = 0;
;
for(i=0;i<8;i++){
if(DS1302_SDA){
Data |= (1<
}
DS1302_CLK = 1;
;
DS1302_CLK = 0;
;
}
return (Data);
}
void DS1302_Write(unsigned char Addr, unsigned char Data){
DS1302_RST=0;
;
;
DS1302_CLK=0;
;
;
DS1302_RST=1;
;
;
DS1302_WriteByte(Addr);
/* write a byte address */
DS1302_CLK = 0;
DS1302_WriteByte(Data);
/* Write 1 byte data*/
DS1302_CLK = 0;
;
DS1302_RST=0;
}
unsigned char DS1302_Read(unsigned char Addr){
unsigned char Data;
DS1302_RST=0;
;
;
DS1302_CLK=0;
;
;
DS1302_RST=1;
;
;
DS1302_WriteByte(Addr);
/* write a byte address */
Data = DS1302_ReadByte();
/* read 1Byte data */
DS1302_CLK=0;
;
;
DS1302_RST=0;
return(Data);
}
void DS1302_SecondSet(unsigned char Second){
DS1302_Write(0x8e,0x00);
/* command,WP=0,write mode*/
DS1302_Write(0x80,BINToBCD(Second));
DS1302_Write(0x8e,0x80);
/* command ,WP=1,write disable*/
}
void DS1302_MinuteSet(unsigned char Minute){
DS1302_Write(0x8e,0x00);
/* command,WP=0,write mode*/
DS1302_Write(0x82,BINToBCD(Minute));
DS1302_Write(0x8e,0x80);
/* command ,WP=1,write disable*/
}
void DS1302_HourSet(unsigned char Hour){
DS1302_Write(0x8e,0x00);
/* command,WP=0,write mode*/
DS1302_Write(0x84,BINToBCD(Hour));
DS1302_Write(0x8e,0x80);
/* command ,WP=1,write disable*/
}
void DS1302_DateSet(unsigned char Date){
DS1302_Write(0x8e,0x00);
/* command,WP=0,write mode*/
DS1302_Write(0x86,BINToBCD(Date));
DS1302_Write(0x8e,0x80);
/* command ,WP=1,write disable*/
}
void DS1302_WeekdaySet(unsigned char Weekday){
DS1302_Write(0x8e,0x00);
/* command,WP=0,write mode*/
DS1302_Write(0x8A,BINToBCD(Weekday));
DS1302_Write(0x8e,0x80);
/* command ,WP=1,write disable*/
}
void DS1302_MonthSet(unsigned char Month){
DS1302_Write(0x8e,0x00);
/* command,WP=0,write mode*/
DS1302_Write(0x88,BINToBCD(Month));
DS1302_Write(0x8e,0x80);
/* command ,WP=1,write disable*/
}
void DS1302_YearSet(unsigned char Year){
DS1302_Write(0x8e,0x00);
/* command,WP=0,write mode*/
DS1302_Write(0x8C,BINToBCD(Year));
DS1302_Write(0x8e,0x80);
/* command ,WP=1,write disable*/
}
unsigned char DS1302_SecondGet(void){
unsigned char temp_second = 0;
temp_second = DS1302_Read(0x81);
temp_second = BCDToBIN(temp_second);
return temp_second;
}
unsigned char DS1302_MinuteGet(void){
unsigned char temp_minute = 0;
temp_minute = DS1302_Read(0x83);
temp_minute = BCDToBIN(temp_minute);
return temp_minute;
}
unsigned char DS1302_HourGet(void){
unsigned char temp_hour = 0;
temp_hour = DS1302_Read(0x85);
temp_hour = BCDToBIN(temp_hour);
return temp_hour;
}
unsigned char BCDToBIN(unsigned char Val)
{
/*Translate BCD code to decimal code*/
Val = (Val >> 4) * 10 + (Val & 0x0f);
/*return demical decimal number*/
return Val;
}
unsigned char BINToBCD(unsigned char Val)
{
/*return demical decimal number*/
return ((Val/10)*16+Val%10);
}
[ 本帖最后由 用心思考 于 2012-8-31 11:38 编辑 ]
|
|