题目
硬件
用到 单片机三块 一个主控 两个副控 分为主路控制器和支路控制器
两个LED灯
一个LCD屏幕
两个红外感应
两个双端光敏电阻
一个三端光敏电阻
以及 时钟模块
代码
主要模块代码
主控
代码语言:javascript复制#include <REGX52.H>
#include "DS1302.h"
#include "LCD1602.h"
#include "XPT2046.h"
#include "MatrixKey.h"
#include "Buzzer.h"
unsigned char Key=0;
unsigned char Time[] = {24,10,10,17,59,55,1};//
unsigned char SetTimeA[] = {18,0,5};
unsigned char OffTimeA[] = {6,0,0};
unsigned char SetTimeB[] = {18,0,10};
unsigned char OffTimeB[] = {6,0,0};
unsigned char WT;
unsigned char ADValue;
unsigned char ifopen = 0;
unsigned int TimeO;
unsigned int TimeA;
unsigned int TimeB;
///
void LCD_time()
{
DS1302_ReadTime();
LCD_ShowNum(1,1,DS1302_Time[0],2);
LCD_ShowChar(1,3,'/');
LCD_ShowNum(1,4,DS1302_Time[1],2);
LCD_ShowChar(1,6,'/');
LCD_ShowNum(1,7,DS1302_Time[2],2);
LCD_ShowNum(2,1,DS1302_Time[3],2);
LCD_ShowChar(2,3,':');
LCD_ShowNum(2,4,DS1302_Time[4],2);
LCD_ShowChar(2,6,':');
LCD_ShowNum(2,7,DS1302_Time[5],2);
}
void LCD_ShowTimechange()
{
LCD_ShowNum(1,1,Time[0],2);
LCD_ShowChar(1,3,'/');
LCD_ShowNum(1,4,Time[1],2);
LCD_ShowChar(1,6,'/');
LCD_ShowNum(1,7,Time[2],2);
LCD_ShowNum(2,1,Time[3],2);
LCD_ShowChar(2,3,':');
LCD_ShowNum(2,4,Time[4],2);
LCD_ShowChar(2,6,':');
LCD_ShowNum(2,7,Time[5],2);
}
void LCD_ShowSetTimeA()
{
LCD_ShowNum(2,1 8,SetTimeA[0],2);
LCD_ShowChar(2,3 8,':');
LCD_ShowNum(2,4 8,SetTimeA[1],2);
LCD_ShowChar(2,6 8,':');
LCD_ShowNum(2,7 8,SetTimeA[2],2);
}
void LCD_ShowOffTimeA()
{
LCD_ShowNum(1,1 8,OffTimeA[0],2);
LCD_ShowChar(1,3 8,':');
LCD_ShowNum(1,4 8,OffTimeA[1],2);
LCD_ShowChar(1,6 8,':');
LCD_ShowNum(1,7 8,OffTimeA[2],2);
}
void LCD_ShowSetTimeB()
{
LCD_ShowNum(2,1 8,SetTimeB[0],2);
LCD_ShowChar(2,3 8,':');
LCD_ShowNum(2,4 8,SetTimeB[1],2);
LCD_ShowChar(2,6 8,':');
LCD_ShowNum(2,7 8,SetTimeB[2],2);
}
void LCD_ShowOffTimeB()
{
LCD_ShowNum(1,1 8,OffTimeB[0],2);
LCD_ShowChar(1,3 8,':');
LCD_ShowNum(1,4 8,OffTimeB[1],2);
LCD_ShowChar(1,6 8,':');
LCD_ShowNum(1,7 8,OffTimeB[2],2);
}
void InitDS1302WithTime()
{
DS1302_init();
DS1302_WriteByte(0x8E,0x00);
//Give time
DS1302_SetTime();
}
void Int0_Init(void)
{
IT0=1;
IE0=0;
EX0=1;
EA=1;
PX0=1;
}
//
void ReadTimeToDs1302()
{
int i = 0;
DS1302_ReadTime();
for(i = 0;i<=7;i )
{
Time[i] = DS1302_Time[i];
}
}
void WriteTimeToDs1302()
{
int i = 0;
for(i = 0;i<=7;i )
{
DS1302_Time[i] = Time[i];
}
DS1302_SetTime();
}
void ChangeTimePart()
{
ReadTimeToDs1302();
LCD_time();
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
WT = Key - 1;
if(Key == 1 || Key == 2 || Key == 3|| Key == 4||Key == 5||Key == 6)//year
{
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
if(Key == 1)
Time[WT] = 1;
if(Key == 2)
Time[WT] -= 1;
if(Key == 3)
Time[WT] = 10;
if(Key == 4)
Time[WT] -= 10;
LCD_ShowTimechange();
if(Key == 16)
{
Key = 0;
break;
}
}
if(Key == 16)
{
Key = 0;
break;
}
}
if(Key == 16)
{
Key = 0;
break;
}
}
WriteTimeToDs1302();
}
void ChangeTimePart_AA()
{
LCD_ShowSetTimeA();
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
WT = Key - 1;
if(Key == 1 || Key == 2 || Key == 3)
{
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
if(Key == 1)
SetTimeA[WT] = 1;
if(Key == 2)
SetTimeA[WT] -= 1;
if(Key == 3)
SetTimeA[WT] = 10;
if(Key == 4)
SetTimeA[WT] -= 10;
LCD_ShowSetTimeA();
if(Key == 16)
{
Key = 0;
break;
}
}
if(Key == 16)
{
Key = 0;
break;
}
LCD_ShowSetTimeA();
}
if(Key == 16)
{
Key = 0;
break;
}
}
}
void ChangeTimePart_AB()
{
LCD_ShowOffTimeA();
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
WT = Key - 1;
if(Key == 1 || Key == 2 || Key == 3)
{
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
if(Key == 1)
OffTimeA[WT] = 1;
if(Key == 2)
OffTimeA[WT] -= 1;
if(Key == 3)
OffTimeA[WT] = 10;
if(Key == 4)
OffTimeA[WT] -= 10;
LCD_ShowOffTimeA();
if(Key == 16)
{
Key = 0;
break;
}
}
if(Key == 16)
{
Key = 0;
break;
}
LCD_ShowOffTimeA();
}
if(Key == 16)
{
Key = 0;
break;
}
}
}
void ChangeTimePart_BA()
{
LCD_ShowSetTimeB();
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
WT = Key - 1;
if(Key == 1 || Key == 2 || Key == 3)
{
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
if(Key == 1)
SetTimeB[WT] = 1;
if(Key == 2)
SetTimeB[WT] -= 1;
if(Key == 3)
SetTimeB[WT] = 10;
if(Key == 4)
SetTimeB[WT] -= 10;
LCD_ShowSetTimeB();
if(Key == 16)
{
Key = 0;
break;
}
}
if(Key == 16)
{
Key = 0;
break;
}
LCD_ShowSetTimeB();
}
if(Key == 16)
{
Key = 0;
break;
}
}
}
void ChangeTimePart_BB()
{
LCD_ShowOffTimeB();
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
WT = Key - 1;
if(Key == 1 || Key == 2 || Key == 3)
{
while(1)
{
Key = 0;
while(Key == 0)
Key = MatrixKey();
if(Key == 1)
OffTimeB[WT] = 1;
if(Key == 2)
OffTimeB[WT] -= 1;
if(Key == 3)
OffTimeB[WT] = 10;
if(Key == 4)
OffTimeB[WT] -= 10;
LCD_ShowOffTimeB();
if(Key == 16)
{
Key = 0;
break;
}
}
if(Key == 16)
{
Key = 0;
break;
}
LCD_ShowOffTimeB();
}
if(Key == 16)
{
Key = 0;
break;
}
}
}
void Int0_Routine(void) interrupt 0
{
while(1)
{
while(Key == 0)
Key = MatrixKey();
if(Key == 13)
{
ChangeTimePart();
}
if(Key == 14)
{
ChangeTimePart_AA();
}
if(Key == 15)
{
ChangeTimePart_AB();
}
if(Key == 10)
{
ChangeTimePart_BA();
}
if(Key == 11)
{
ChangeTimePart_BB();
}
if(Key == 16)
{
Key = 0;
break;
}
}
}
//
//unsigned char Time[] = {24,10,10,17,59,55,1};
//unsigned char SetTime[] = {18,0,0};
//unsigned char OffTime[] = {6,0,0};
void LedWithTime()
{
DS1302_ReadTime();
TimeO = DS1302_Time[3]*60*60 DS1302_Time[4]*60 DS1302_Time[5];
TimeA = SetTimeA[0]*60*60 SetTimeA[1]*60 SetTimeA[2];
TimeB = OffTimeA[0]*60*60 OffTimeA[1]*60 OffTimeA[2];
if(TimeO > TimeB && TimeO < TimeA)
{
P2_0 = 1;
ifopen = 0;
}
else
{
P2_0 = 0;
ifopen = 1;
}
TimeO = DS1302_Time[3]*60*60 DS1302_Time[4]*60 DS1302_Time[5];
TimeA = SetTimeB[0]*60*60 SetTimeB[1]*60 SetTimeB[2];
TimeB = OffTimeB[0]*60*60 OffTimeB[1]*60 OffTimeB[2];
if(TimeO > TimeB && TimeO < TimeA)
{
P2_2 = 1;
ifopen = 0;
}
else
{
P2_2 = 0;
ifopen = 1;
}
ADValue = XPT2046_ReadAD(XPT2046_VBAT);
if(ADValue <= 16)
{
P2_1 = 0;
}
else
{
P2_1 = 1;
}
if(ADValue >= 28)
{
while(XPT2046_ReadAD(XPT2046_VBAT)>=28)
{
P2_0 = 1;
P2_2 = 1;
}
}
else
{
if(ifopen == 1)
{
P2_0 = 0;
P2_2 = 0;
}
}
}
void AD()
{
if(P3_0 == 0)
{P2_3 = 0;Buzzer_Time_500HZ(100);}
else
P2_3 = 1;
if(P3_1 == 0)
{P2_4 = 0;Buzzer_Time_500HZ(100);}
else
P2_4 = 1;
}
//
int main()
{
LCD_Init();
Int0_Init();
InitDS1302WithTime();
while(1)
{
LCD_time();
LedWithTime();
AD();
}
}
支路控制代码
代码语言:javascript复制#include <REGX52.H>
#define uchar unsigned char
sbit key = P3^3;
void Delay(unsigned int t)
{
while(t--);
}
void Adelay(unsigned int i)
{
unsigned int j;
for(;i>0;i--)
for(j=0;j<333;j ){}
}
void Delay1000ms(void) //@11.0592MHz
{
unsigned char data i, j, k;
i = 8;
j = 1;
k = 5;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Int0_Init(void)
{
IT0=1;
IE0=0;
EX0=1;
EA=1;
PX0=1;
}
//P3_0 == 0 hei
//P3_0 == 1 ming
//P3_1 == 0 qidong
//P3_1 == 1 guanji
//P0_0 == 0 qidong
//P0_0 == 1 guanji
void main()
{
Int0_Init();
P3_1 = 1;
P1_6 = 1;
P1_5 = 0;
P1_4 = 1;
P2_1 = 1;
while(1)
{
while(P3_1 == 0)
{
P1_7 = 1;
if(P1_6 == 0)
{
break;
}
if(P1_4 == 1)
{
P2_1 = 0;
P0_0 = 0;
while(P1_4 == 1);
P2_1 = 1;
P0_1 = 1;
}
if(P3_0 == 1)//PWM
{
P0_0 = 0;
Delay(2);
P0_0 = 1;
Delay(8);
}
if(P3_0 == 0)//PWM
{
P0_0 = 0;
Delay(7);
P0_0 = 1;
Delay(3);
}
}
P2_0 = 0;
}
}
void Int0_Routine(void) interrupt 0
{
if(P3_1 == 0)
{
P0_0= 0;
P1_7= 0;
while(P3_2 == 0);
}
}
头文件补充
LCD1602
代码语言:javascript复制#include <REGX52.H>
//引脚配置:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0
//函数定义:
/**
* @brief LCD1602延时函数,12MHz调用可延时1ms
* @param 无
* @retval 无
*/
void LCD_Delay()
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
/**
* @brief LCD1602写命令
* @param Command 要写入的命令
* @retval 无
*/
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602写数据
* @param Data 要写入的数据
* @retval 无
*/
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_EN=1;
LCD_Delay();
LCD_EN=0;
LCD_Delay();
}
/**
* @brief LCD1602设置光标位置
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @retval 无
*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1 0x40));
}
}
/**
* @brief LCD1602初始化函数
* @param 无
* @retval 无
*/
void LCD_Init()
{
LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
LCD_WriteCommand(0x01);//光标复位,清屏
}
/**
* @brief 在LCD1602指定位置上显示一个字符
* @param Line 行位置,范围:1~2
* @param Column 列位置,范围:1~16
* @param Char 要显示的字符
* @retval 无
*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
LCD_SetCursor(Line,Column);
LCD_WriteData(Char);
}
/**
* @brief 在LCD1602指定位置开始显示所给字符串
* @param Line 起始行位置,范围:1~2
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串
* @retval 无
*/
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='