用到 单片机三块 一个主控 两个副控 分为主路控制器和支路控制器
两个LED灯
一个LCD屏幕
两个红外感应
两个双端光敏电阻
一个三端光敏电阻
以及 时钟模块
支路控制器
#include "stm32f10x.h" // Device header
#include "MyRTC.h"
#include "OLED.h"
#include "button4_4.h"
#include <time.h>
time_t Time[] = {24,10,10,17,59,55};
time_t SetTimeA[] = {18,0,5};
time_t OffTimeA[] = {6,0,0};
time_t SetTimeB[] = {18,0,10};
time_t OffTimeB[] = {6,0,0};
///
//TIME
void OLED_TIME()
{
OLED_ShowNum(1,1,MyRTC_Time[3],2);
OLED_ShowChar(1,3,':');
OLED_ShowNum(1,4,MyRTC_Time[4],2);
OLED_ShowChar(1,6,':');
OLED_ShowNum(1,7,MyRTC_Time[5],2);
}
void TIME()
{
OLED_ShowNum(1,1,Time[3],2);
OLED_ShowChar(1,3,':');
OLED_ShowNum(1,4,Time[4],2);
OLED_ShowChar(1,6,':');
OLED_ShowNum(1,7,Time[5],2);
}
void OLED_ODATIME()
{
OLED_ShowNum(2,1,SetTimeA[0],2);
OLED_ShowChar(2,3,':');
OLED_ShowNum(2,4,SetTimeA[1],2);
OLED_ShowChar(2,6,':');
OLED_ShowNum(2,7,SetTimeA[2],2);
OLED_ShowNum(2,9,OffTimeA[0],2);
OLED_ShowChar(2,11,':');
OLED_ShowNum(2,12,OffTimeA[1],2);
OLED_ShowChar(2,14,':');
OLED_ShowNum(2,15,OffTimeA[2],2);
}
void OLED_ODBTIME()
{
OLED_ShowNum(3,1,SetTimeB[0],2);
OLED_ShowChar(3,3,':');
OLED_ShowNum(3,4,SetTimeB[1],2);
OLED_ShowChar(3,6,':');
OLED_ShowNum(3,7,SetTimeB[2],2);
OLED_ShowNum(3,9,OffTimeB[0],2);
OLED_ShowChar(3,11,':');
OLED_ShowNum(3,12,OffTimeB[1],2);
OLED_ShowChar(3,14,':');
OLED_ShowNum(3,15,OffTimeB[2],2);
}
//TIME_CHANGE
void TT()
{
int Key = 0;
int i = 0;
char p = 0;
for(i = 0;i<6;i++)
{
Time[i] = MyRTC_Time[i];
}
while(1)
{
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 2){p = 3;}
if(Key == 3){p = 4;}
if(Key == 4){p = 5;}
if(Key == 13){Key=0;break;};
if(p!=0)
{
OLED_ShowNum(1,16,p-2,1);
while(1)
{
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 6){Time[p]+=1;}
if(Key == 7){Time[p]+=10;}
if(Key == 10){Time[p]-=1;}
if(Key == 11){Time[p]-=10;}
TIME();
if(Key == 13){Key = 0;break;}
}
}
OLED_ShowString(1,16," ");
p = 0;
Key = 0;
}
for(i = 0;i<6;i++)
{
MyRTC_Time[i]=Time[i];
}
MyRTC_SetTime();
}
void TA()
{
int Key = 0;
char p = 0;
while(1)
{
p=0;
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 4)
{
OLED_ShowString(1,13,"AO");
while(1)
{
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 13){Key=0;break;}
if(Key == 1){p = 0;}
if(Key == 2){p = 1;}
if(Key == 3){p = 2;}
if(p==0||p==1||p==2)
{
OLED_ShowNum(1,16,p+1,1);
while(1)
{
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 6){SetTimeA[p]+=1;}
if(Key == 7){SetTimeA[p]+=10;}
if(Key == 10){SetTimeA[p]-=1;}
if(Key == 11){SetTimeA[p]-=10;}
OLED_ODATIME();
if(Key == 13){Key = 0;break;}
}
}
}
}
p=0;
if(Key == 8)
{
OLED_ShowString(1,13,"AD");
while(1)
{
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 13){Key=0;break;}
if(Key == 1){p = 0;}
if(Key == 2){p = 1;}
if(Key == 3){p = 2;}
if(p==0||p==1||p==2)
{
OLED_ShowNum(1,16,p+1,1);
while(1)
{
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 6){OffTimeA[p]+=1;}
if(Key == 7){OffTimeA[p]+=10;}
if(Key == 10){OffTimeA[p]-=1;}
if(Key == 11){OffTimeA[p]-=10;}
OLED_ODATIME();
if(Key == 13){Key = 0;break;}
}
}}
}
if(Key == 13){Key=0;break;}
}
}
void TB()
{
int Key = 0;
char p = 0;
while(1)
{
p=0;
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 4)
{
OLED_ShowString(1,13,"BO");
while(1)
{
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 13){Key=0;break;}
if(Key == 1){p = 0;}
if(Key == 2){p = 1;}
if(Key == 3){p = 2;}
if(p==0||p==1||p==2)
{
OLED_ShowNum(1,16,p+1,1);
while(1)
{
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 6){SetTimeB[p]+=1;}
if(Key == 7){SetTimeB[p]+=10;}
if(Key == 10){SetTimeB[p]-=1;}
if(Key == 11){SetTimeB[p]-=10;}
OLED_ODATIME();
if(Key == 13){Key = 0;break;}
}
}
}
}
p=0;
if(Key == 8)
{
OLED_ShowString(1,13,"BD");
while(1)
{
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 13){Key=0;break;}
if(Key == 1){p = 0;}
if(Key == 2){p = 1;}
if(Key == 3){p = 2;}
if(p==0||p==1||p==2)
{
OLED_ShowNum(1,16,p+1,1);
while(1)
{
Key = 0;
while(Key == 0)
Key = Button4_4_Scan();
if(Key == 6){OffTimeB[p]+=1;}
if(Key == 7){OffTimeB[p]+=10;}
if(Key == 10){OffTimeB[p]-=1;}
if(Key == 11){OffTimeB[p]-=10;}
OLED_ODBTIME();
if(Key == 13){Key = 0;break;}
}
}}
}
if(Key == 13){Key=0;break;}
}
}
void TIME_CHANFE()
{
int Key = 0;
if(Button4_4_Scan() == 16)
{
OLED_ShowString(1,10,"TC");
while(1)
{
//
while(Key == 0)
Key = Button4_4_Scan();
//
if(Key == 1)
{
OLED_ShowString(1,13,"TT");
TT();
OLED_ShowString(1,13," ");
}
if(Key == 5)
{
OLED_ShowString(1,13,"TA");
TA();
OLED_ShowString(1,13," ");
}
if(Key == 9)
{
OLED_ShowString(1,13,"TB");
TB();
OLED_ShowString(1,13," ");
}
if(Key == 13)
{
Key = 0;
break;
}
Key = 0;
}
OLED_Clear();
}
}
//OLED
void OLED()
{
MyRTC_ReadTime();
OLED_TIME();
OLED_ODATIME();
OLED_ODBTIME();
TIME_CHANFE();
//...
}
///
void OP_DOWN_Init()
{ GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure1;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_13|GPIO_Pin_0;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure1);
GPIO_SetBits(GPIOB,GPIO_Pin_14);
GPIO_SetBits(GPIOB,GPIO_Pin_13);
GPIO_SetBits(GPIOB,GPIO_Pin_0);
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_15|GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void PAON()
{
GPIO_ResetBits(GPIOB,GPIO_Pin_14);
}
void PAOFF()
{
GPIO_SetBits(GPIOB,GPIO_Pin_14);
}
void PBON()
{
GPIO_ResetBits(GPIOB,GPIO_Pin_13);
}
void PBOFF()
{
GPIO_SetBits(GPIOB,GPIO_Pin_13);
}
void OP_DOWN()
{
time_t TimeO,TimeA,TimeB,a,b;
MyRTC_ReadTime();
TimeO = MyRTC_Time[3]*60*60 + MyRTC_Time[4]*60 + MyRTC_Time[5];
TimeA = SetTimeA[0]*60*60 + SetTimeA[1]*60 + SetTimeA[2];
TimeB = OffTimeA[0]*60*60 + OffTimeA[1]*60 + OffTimeA[2];
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)
{
PAOFF();PBOFF();
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == 0)
{
OLED();
}
if(a == 1)
{
PAON();
}
if(b == 1)
{
PBON();
}
}
if(!(TimeO > TimeB && TimeO < TimeA))
{
PAON();a=1;
}
else
{
PAOFF();a=0;
}
TimeO = MyRTC_Time[3]*60*60 + MyRTC_Time[4]*60 + MyRTC_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))
{
PBON();b=1;
}
else
{
PBOFF();b=0;
}
}
//
void If_Break()
{
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == 0)
{
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10) == 0)
{
OLED();GPIO_ResetBits(GPIOB,GPIO_Pin_0);
OLED_ShowString(4,8,"10");
}
GPIO_SetBits(GPIOB,GPIO_Pin_0);
OLED_ShowString(4,8," ");
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == 0)
{
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) == 0)
{
OLED();GPIO_ResetBits(GPIOB,GPIO_Pin_0);
OLED_ShowString(4,10,"11");
}
GPIO_SetBits(GPIOB,GPIO_Pin_0);
OLED_ShowString(4,10," ");
}
}
//
int main()
{
OLED_Init();
MyRTC_Init();
MyRTC_SetTime();
Button4_4_Init();
OP_DOWN_Init();
while(1)
{
while(1){
OLED();
OP_DOWN();
If_Break();
}}
}
单元控制器
#include "stm32f10x.h" // Device header
#include "Delay.h"
/*
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_13);
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
*/
void LED_Init()
{
GPIO_InitTypeDef GPIO_InitStructure1;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_11|GPIO_Pin_6|GPIO_Pin_1;
GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure1);
GPIO_SetBits(GPIOA,GPIO_Pin_0);
GPIO_SetBits(GPIOA,GPIO_Pin_11);
GPIO_SetBits(GPIOA,GPIO_Pin_6);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
void Key_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure2;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure2);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;
GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void InitGPIO()
{
LED_Init();
Key_Init();
}
/
int main()
{
InitGPIO();
//GPIO_SetBits(GPIOA,GPIO_Pin_0);
//GPIO_ResetBits(GPIOA,GPIO_Pin_0);
while(1)
{
GPIO_SetBits(GPIOA,GPIO_Pin_6);
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14) == 0)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2) == 0)//我先看到车
{
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
Delay_s(1);
GPIO_SetBits(GPIOA,GPIO_Pin_6);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);//开灯
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3) != 0);
GPIO_SetBits(GPIOA,GPIO_Pin_0);//关灯
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7) != 0);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);//开灯
}
//if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3) == 0)
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) != 0)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_11);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) != 0);
GPIO_SetBits(GPIOA,GPIO_Pin_11);
}
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5) == 0)//对方先看到车
{
GPIO_SetBits(GPIOA,GPIO_Pin_0);//关灯
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_3) != 0);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);//开灯
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2) != 0);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
Delay_s(1);
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
}
else
{
GPIO_SetBits(GPIOA,GPIO_Pin_0);
}
}
}