标题:
基于单片机的汽车尾灯控制系统程序设计
[打印本页]
作者:
凌净清河
时间:
2019-6-5 18:43
标题:
基于单片机的汽车尾灯控制系统程序设计
不要问为什么这么简单。在最初的要求中一个尾灯控制电路还要求有掉电存储和检测功能,也不知道用作什么的。
【设计亮点】
淘宝上常见的LCD1602的并口转IIC转接小板原本为在Arduino上使用而设计的,但是既然遵守IIC协议,并且只是PCF8574芯片通信,那么51也可以才对。经过查找,在百度贴吧上找到了使用其驱动LCD1602的程序,并且重新排版编写使之清晰明了,工程中的LCD1602_IIC.c即为此转接板的驱动程序。
使用该转接板,在刷新速度不高的情况下,大大节省了IO口的占用数量。那个转接板的样子大概是这样的:
QQ截图20190605182904.png
(128.02 KB, 下载次数: 60)
下载附件
2019-6-5 18:41 上传
【设计说明】
设计包含4个独立按键,两个LED灯代表左右车尾灯。
使用PCF8574转接板以IIC方式连接LCD1602到单片机。
时钟电路选用DS1302,仅下载时设定时间。
功能设定:
待机状态下,显示当前系统时间;
按下左转按键,左转指示灯闪烁;
按下右转按键,右转指示灯闪烁;
按下紧急按键,两指示灯均闪烁;
任意状态下按住刹车按键,两灯保持常量,松开后还原为原来状态。
上述4种状态均有文字提示。
设计使用的单片机型号为STC12C5202AD,包含的头文件任意,因为没使用片内的特殊资源。没有安装外部晶振,片内的晶振并不十分准确,我测试的时候大约在13MHz左右。
单片机源程序如下:
#include<STC12C2052AD.h>
#include"LCD1602_IIC.c"
#include"DS1302.c"
sbit LED_Left=P3^0;
sbit LED_Right=P3^1;
sbit Key_Left=P3^7;
sbit Key_Right=P1^0;
sbit Key_Stop=P1^1;
sbit Key_Emergency=P1^2;
//运行标志位
bit flag_Key_Left=0;//按键执行标志位
bit flag_Key_Right=0;
bit flag_Key_Stop=0;
bit flag_Key_Emergency=0;
unsigned char Flash_Mode=0;//闪烁模式选择 0:正常待机模式 1:左转 2:右转 3:双闪 4:停止
//运行数组
char LCD1602_Buff[17];
unsigned int T0_Cnt=0;
/*
名称:基于单片机的汽车尾灯控制系统设计
平台:STC12C5202AD,使用内部RC振荡器,大约为13MHz
说明:
设计包含4个独立按键,两个LED灯代表左右车尾灯。
使用PCF8574转接板以IIC方式连接LCD1602到单片机。
时钟电路选用DS1302,仅下载时设定时间。
功能设定:
待机状态下,显示当前系统时间;
按下左转按键,左转指示灯闪烁;
按下右转按键,右转指示灯闪烁;
按下紧急按键,两指示灯均闪烁;
任意状态下按住刹车按键,两灯保持常量,松开后还原为原来状态。
上述4种状态均有文字提示。
硬件制作:凌净清河
程序/文档:凌净欣羽
新矿城学习基地#2019
*/
void delay(unsigned long i)
{
while(i--);
}
void Time_Display()
{
unsigned char i;
for(i=0;i<17;i++)
{
LCD1602_Buff[i]='\0';//先填充截断符
}
LCD1602_Buff[0]=' ';
LCD1602_Buff[1]=' ';
LCD1602_Buff[2]=' ';
LCD1602_Buff[3]=D_year[0]+'0';
LCD1602_Buff[4]=D_year[1]+'0';
LCD1602_Buff[5]=D_year[2]+'0';
LCD1602_Buff[6]=D_year[3]+'0';
LCD1602_Buff[7]='-';
LCD1602_Buff[8]=D_month[0]+'0';
LCD1602_Buff[9]=D_month[1]+'0';
LCD1602_Buff[10]='-';
LCD1602_Buff[11]=D_day[0]+'0';
LCD1602_Buff[12]=D_day[1]+'0';
LCD1602_Buff[13]=' ';
LCD1602_Buff[14]=' ';
LCD1602_Buff[15]=' ';
LCD1602_Print(0,0,LCD1602_Buff);
delay(10000);
for(i=0;i<17;i++)
{
LCD1602_Buff[i]='\0';//先填充截断符
}
LCD1602_Buff[0]=' ';
LCD1602_Buff[1]=' ';
LCD1602_Buff[2]=' ';
LCD1602_Buff[3]=' ';
LCD1602_Buff[4]=D_hour[0]+'0';
LCD1602_Buff[5]=D_hour[1]+'0';
LCD1602_Buff[6]=':';
LCD1602_Buff[7]=D_minute[0]+'0';
LCD1602_Buff[8]=D_minute[1]+'0';
LCD1602_Buff[9]=':';
LCD1602_Buff[10]=D_second[0]+'0';
LCD1602_Buff[11]=D_second[1]+'0';
LCD1602_Buff[12]=' ';
LCD1602_Buff[13]=' ';
LCD1602_Buff[14]=' ';
LCD1602_Buff[15]=' ';
LCD1602_Print(0,1,LCD1602_Buff);
}
void Timer0Init(void) //5毫秒@13MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0x02; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void sys_init()
{
//Ds1302Init();//首次上电写入时间
LED_Left=0;
LED_Right=0;
LCD1602_Light_Off();
LCD1602_Init();
LCD1602_Print(0,0," Ready! ");
LCD1602_Light_On();
Timer0Init();
EA=1;
ET0=1;
LED_Left=1;
LED_Right=1;
}
void main()
{
sys_init();
while(1)
{
GetTime();
if(Flash_Mode==0)Time_Display();
if(!Key_Left&&!flag_Key_Left)
{
delay(2000);
if(!Key_Left)
{
flag_Key_Left=1;
LCD1602_Print(0,0," Turn Left ");
LCD1602_Print(0,1," LED_Left Flash ");
if(Flash_Mode==1)
{
Flash_Mode=0;
LED_Left=1;
}
else
{
LED_Right=1;
Flash_Mode=1;
}
}
}
if(!Key_Right&&!flag_Key_Right)
{
delay(2000);
if(!Key_Right)
{
flag_Key_Right=1;
LCD1602_Print(0,0," Turn Right ");
LCD1602_Print(0,1," LED_Right Flash");
if(Flash_Mode==2)
{
Flash_Mode=0;
LED_Right=1;
}
else
{
LED_Left=1;
Flash_Mode=2;
}
}
}
if(!Key_Stop&&!flag_Key_Stop)
{
unsigned char Temp_Flash_Mode=0;
delay(2000);
if(!Key_Stop)
{
flag_Key_Stop=1;
LCD1602_Print(0,0," Stop ");
LCD1602_Print(0,1," LED_Stop Light ");
Temp_Flash_Mode=Flash_Mode;//模式备份
Flash_Mode=4;
LED_Left=0;
LED_Right=0;
}
while(!Key_Stop);
LED_Left=1;
LED_Right=1;
Flash_Mode=Temp_Flash_Mode;//模式还原
switch(Flash_Mode)//文字还原
{
case 1:
LCD1602_Print(0,0," Turn Left ");
LCD1602_Print(0,1," LED_Left Flash ");
break;
case 2:
LCD1602_Print(0,0," Turn Right ");
LCD1602_Print(0,1," LED_Right Flash");
break;
case 3:
LCD1602_Print(0,0," Emergency ");
LCD1602_Print(0,1," Double Flash ");
break;
default:break;
}
}
if(!Key_Emergency&&!flag_Key_Emergency)
{
delay(2000);
if(!Key_Emergency)
{
flag_Key_Emergency=1;
LCD1602_Print(0,0," Emergency ");
LCD1602_Print(0,1," Double Flash ");
if(Flash_Mode==3)
{
Flash_Mode=0;
LED_Left=1;
LED_Right=1;
}
else
{
LED_Left=0;
LED_Right=0;
Flash_Mode=3;
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
基于单片机的汽车尾灯控制系统设计 凌净清河.7z
(1.3 MB, 下载次数: 44)
2019-6-5 20:30 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1