基于51单片机公交车报站系统
(仿真+程序)
功能介绍
具体功能:
1.用LCD12864播报站台,显示当前站台和下一站,当前时间和温度;
2.站台名为一号到四号,下行从一号到四号,上行从四号到一号;
3.按下上行/下行准备播报,按下播报按键就会显示当前站台和下一站台;
4.K1~K4按键可以设置时间;
演示视频:
基于51单片机公交车报站系统—显示时间温度
添加图片注释,不超过 140 字(可选)
程序
#include <reg51.h>
/***公众号:木子单片机****/
#define uint unsigned int
#define uchar unsigned char
#include "value.h"
sbit x_key=P1^3;
sbit s_key=P1^4;
sbit bz_key=P1^5;
sbit gb_key=P1^6;
sbit x_LED=P2^6;
sbit s_LED=P2^7;
uchar x_mode,s_mode;
uchar x_num,s_num;
bit flag_200ms = 1;
uchar flag_c_en;
bit flag_100ms;
sbit dq=P3^2; //18b20 IO口的定义
uint temperature ; //温度变量
/************ 延时函数 *****************/
void delay_uint(uint z)
{
while(z--);
}
/********************************************************************
* 名称 : delay_1ms()
* 功能 : 延时1ms函数
* 输入 : q
* 输出 : 无
***********************************************************************/
void delay_1ms(uint q)
{
uint i,j;
for(i=0;i<q;i++)
for(j=0;j<115;j++);
}
uchar dis_lcd[10];
#include "lcd12864bu.h"
/******************显示时间函数***************************/
void dis_shijian() //显示时间
{
write_shu16(0,0*8,shi,0);
write_shuzi(1,0*2,2*8,fuhao[1],0);
write_shu16(0,3*8,fen,0);
write_shuzi(1,0*2,5*8,fuhao[1],0);
write_shu16(0,6*8,miao,0);
// write_yhanzi(2,0,5*16,qita[6]); //星期
// write_shu16(0,14*8,week,0);
// if(week==0)
// {
// write_yhanzi(1,0,7*16,xingqi[6]);
// }
// if(week==1)
// {
// write_yhanzi(1,0,7*16,xingqi[1]);
// }
// if(week==2)
// {
// write_yhanzi(1,0,7*16,xingqi[2]);
// }
// if(week==3)
// {
// write_yhanzi(1,0,7*16,xingqi[3]);
// }
// if(week==4)
// {
// write_yhanzi(1,0,7*16,xingqi[4]);
// }
// if(week==5)
// {
// write_yhanzi(1,0,7*16,xingqi[5]);
// }
// if(week==6)
// {
// write_yhanzi(1,0,7*16,xingqi[6]);
// }
write_shu16(1*2,0*8,0x20,0);
write_shu16(1*2,2*8,nian,0);
write_shuzi(1,1*2,4*8,fuhao[0],0);
write_shu16(1*2,5*8,yue,0);
write_shuzi(1,1*2,7*8,fuhao[0],0);
write_shu16(1*2,8*8,ri,0);
write_shu18b20(1*2,12*8,temperature,0);
write_shuzi(1,1*2,14*8,dian,0);
}
/************初始化显示*****************/
void init_12864_dis() //初始化显示
{
cs1=1; //左屏开显示
cs2=0;
dis_shijian(); //显示时间
}
/***********************18b20初始化函数*****************************/
void init_18b20()
{
bit q;
dq=1; //把总线拿高
delay_uint(1); //15us
dq=0; //给复位脉冲
delay_uint(80); //750us
dq=1; //把总线拿高 等待
delay_uint(10); //110us
q=dq; //读取18b20初始化信号
delay_uint(20); //200us
dq=1; //把总线拿高 释放总线
}
/*************写18b20内的数据***************/
void write_18b20(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{ //写数据是低位开始
dq=0; //把总线拿低写时间隙开始
dq=dat&0x01; //向18b20总线写数据了
delay_uint(5); // 60us
dq=1; //释放总线
dat>>=1;
}
}
/*************读取18b20内的数据***************/
uchar read_18b20()
{
uchar i,value;
for(i=0;i<8;i++)
{
dq=0; //把总线拿低读时间隙开始
value>>= 1; //读数据是低位开始
dq=1; //释放总线
if(dq==1) //开始读写数据
value|=0x80;
delay_uint(5); //60us 读一个时间隙最少要保持60us的时间
}
return value; //返回数据
}
/*************读取温度的值 读出来的是小数***************/
uint read_temp()
{
uint value;
uchar low; //在读取温度的时候如果中断的太频繁了,就应该把中断给关了,否则会影响到18b20的时序
init_18b20(); //初始化18b20
write_18b20(0xcc); //跳过64位ROM
write_18b20(0x44); //启动一次温度转换命令
delay_uint(50); //500us
init_18b20(); //初始化18b20
write_18b20(0xcc); //跳过64位ROM
write_18b20(0xbe); //发出读取暂存器命令
EA=0;
low=read_18b20(); //读温度低字节
value=read_18b20(); //读温度高字节
EA =1;
value<<=8; //把温度的高位左移8位
value|=low; //把读出的温度低位放到value的低八位中
value*=0.625; //转换到温度值 小数
return value; //返回读出的温度 带小数
}
/******************显示时间函数***************************/
void dis_shijian_set() //显示时间
{
write_shu16(0*2,0*8,0x20,0);
write_shu16(1*2,2*8,shi,0);
write_shu16(1*2,6*8,fen,0);
write_shu16(1*2,10*8,miao,0);
write_shu16(0*2,2*8,nian,0);
write_shu16(0*2,6*8,yue,0);
write_shu16(0*2,10*8,ri,0);
// write_yhanzi(1,4,5*16,xingqi[week]);
}
/************初始化设置显示*****************/
void init_12864_set1()
{
write_yhanzi(1,0,2*16,qita[0]); //年
write_yhanzi(1,0,4*16,qita[1]); //月
write_yhanzi(1,0,6*16,qita[2]); //日
write_yhanzi(1,2,2*16,qita[3]); //时
write_yhanzi(1,2,4*16,qita[4]); //分
write_yhanzi(1,2,6*16,qita[5]); //秒
// write_yhanzi(2,4,3*16,qita[6]); //星期
dis_shijian_set(); //显示时间
}
/******************时间芯片显示函数**********************/
void ds1302_dis()
{
uchar miao_1;
if(menu_1 == 0)
{
read_time();
if(miao_1 != miao)
{
miao_1 = miao;
dis_shijian(); //显示时间
}
}
}
/********************独立按键程序*****************/
uchar key_can; //按键值
void key() //独立按键程序
{
static uchar key_new;
key_can=20; //按键值还原
P3|=0x78; //对应的按键IO口输出为1
if((P3&0x78)!=0x78) //按键按下
{
delay_1ms(1); //按键消抖动
if(((P3&0x78)!=0x78)&&(key_new==1))
{ //确认是按键按下
key_new=0;
switch(P3&0x78)
{
case 0x70:key_can=1;break; //得到按键值
case 0x68:key_can=2;break; //得到按键值
case 0x58:key_can=3;break; //得到按键值
case 0x38:key_can=4;break; //得到按键值
}
}
}
else
key_new=1;
}
/**********************设置函数************************/
void key_with()
{
if(key_can==4) //退出键
{
qingling();
init_12864_dis(); //初始化显示
flag_200ms=1;
menu_1=0;
}
if(key_can==1) //设置键
{
menu_1 ++;
flag_200ms=1;
if(menu_1==1)
{
x_num=0;
s_num=0;
x_mode=0;
s_mode=0;
qingling();
init_12864_set1();
}
if(menu_1>7)
menu_1 = 1;
}
if(menu_1!=0)
{
if(menu_1==1) //设置年
{
if(key_can==2) //加
set_ds1302time(1,&nian,0x99);
if(key_can==3) //减
set_ds1302time(0,&nian,0x99);
}
if(menu_1==2) //设置月
{
if(key_can==2) //加
set_ds1302time(1,&yue,0x13);
if(key_can==3) //减
set_ds1302time(0,&yue,0x13);
}
if(menu_1==3) //设置日
{
if(key_can==2) //加
set_ds1302time(1,&ri,0x32);
if(key_can==3) //减
set_ds1302time(0,&ri,0x32);
}
if(menu_1==4) //设置时
{
if(key_can==4) //加
set_ds1302time(1,&shi,0x24);
if(key_can==3) //减
set_ds1302time(0,&shi,0x24);
}
if(menu_1==5) //设置分
{
if(key_can==2) //加
set_ds1302time(1,&fen,0x60);
if(key_can==3) //减
set_ds1302time(0,&fen,0x60);
}
if(menu_1==6) //设置秒
{
if(key_can==2) //加
set_ds1302time(1,&miao,0x60);
if(key_can==3) //减
set_ds1302time(0,&miao,0x60);
}
// if(menu_1==7) //设置星期
// {
// if(key_can==2) //加
// set_ds1302time(1,&week,0x8);
// if(key_can==3) //减
// set_ds1302time(0,&week,0x8);
// }
dis_shijian_set(); //显示时间
switch(menu_1) // 光标显示
{
case 1:write_shu16(0*2,2*8,nian,1);break;
case 2:write_shu16(0*2,6*8,yue,1);break;
case 3:write_shu16(0*2,10*8,ri,1);break;
case 4:write_shu16(1*2,2*8,shi,1);break;
case 5:write_shu16(1*2,6*8,fen,1);break;
case 6:write_shu16(1*2,10*8,miao,1);break;
// case 7:write_yhanzi(1,4,5*16,xingqi[week]);break;
}
write_time(); //把时间写进去
}
}
/*********************定时器0、定时器1初始化******************/
void time0_init()
{
EA=1; //开总中断
TMOD=0X11; //定时器0、定时器1工作方式1
ET0=1; //开定时器0中断
TR0=1; //允许定时器0定时
}
void baozhan()
{
if(x_key==0)
{
delay_1ms(20);
if(x_key==0)
{
qingling();
x_mode=1;
s_mode=0;
x_num=0;
s_num=0;
x_LED=0;
s_LED=1;
write_yhanzi(1,4,0*16,daoda[0]); //到
write_yhanzi(1,4,1*16,daoda[1]); //达
write_shuzi(1,2*2,4*8,fuhao[1],0);//:
write_yhanzi(1,6,0*16,xiayizhan[0]); //下
write_yhanzi(1,6,1*16,xiayizhan[1]); //站
write_shuzi(1,3*2,4*8,fuhao[1],0); //:
}
while(!x_key);
}
if(s_key==0)
{
delay_1ms(20);
if(s_key==0)
{
qingling();
x_mode=0;
s_mode=1;
x_num=0;
s_num=0;
x_LED=1;
s_LED=0;
write_yhanzi(1,4,0*16,daoda[0]); //到
write_yhanzi(1,4,1*16,daoda[1]); //达
write_shuzi(1,2*2,4*8,fuhao[1],0);//:
write_yhanzi(1,6,0*16,xiayizhan[0]); //下
write_yhanzi(1,6,1*16,xiayizhan[1]); //站
write_shuzi(1,3*2,4*8,fuhao[1],0); //:
}
while(!s_key);
}
if((bz_key==0)&&(x_mode==1))
{
delay_1ms(20);
if((bz_key==0)&&(x_mode==1))
{
x_num++;
if(x_num>=5)
{
x_num=5;
}
}
while(!bz_key);
}
if((bz_key==0)&&(s_mode==1))
{
delay_1ms(20);
{
s_num++;
if(s_num>=5)
{
s_num=5;
}
}
while(!bz_key);
}
if(gb_key==0)
{
delay_1ms(20);
if(gb_key==0)
{
x_mode=0;
s_mode=0;
x_num=0;
s_num=0;
x_LED=1;
s_LED=1;
qingling();
}
}
}
void yuyin()
{
/******************************下行报站显示***********/
if(x_num==1)
{
write_yhanzi(1,4,3*16,yihaozhan[0]); //一
write_yhanzi(1,4,4*16,yihaozhan[1]); //号
write_yhanzi(1,4,5*16,yihaozhan[2]); //站
write_yhanzi(1,6,3*16,erhaozhan[0]); //二
write_yhanzi(1,6,4*16,erhaozhan[1]); //号
write_yhanzi(1,6,5*16,erhaozhan[2]); //站
}
if(x_num==2)
{
write_yhanzi(1,4,3*16,erhaozhan[0]); //二
write_yhanzi(1,4,4*16,erhaozhan[1]); //号
write_yhanzi(1,4,5*16,erhaozhan[2]); //站
write_yhanzi(1,6,3*16,sanhaozhan[0]); //三
write_yhanzi(1,6,4*16,sanhaozhan[1]); //号
write_yhanzi(1,6,5*16,sanhaozhan[2]); //站
}
if(x_num==3)
{
write_yhanzi(1,4,3*16,sanhaozhan[0]); //三
write_yhanzi(1,4,4*16,sanhaozhan[1]); //号
write_yhanzi(1,4,5*16,sanhaozhan[2]); //站
write_yhanzi(1,6,3*16,sinhaozhan[0]); //四
write_yhanzi(1,6,4*16,sinhaozhan[1]); //号
write_yhanzi(1,6,5*16,sinhaozhan[2]); //站
}
if(x_num==4)
{
write_yhanzi(1,4,3*16,sinhaozhan[0]); //四
write_yhanzi(1,4,4*16,sinhaozhan[1]); //号
write_yhanzi(1,4,5*16,sinhaozhan[2]); //站
write_yhanzi(1,6,3*16,zhongdianzhan[0]); //终
write_yhanzi(1,6,4*16,zhongdianzhan[1]); //点
write_yhanzi(1,6,5*16,zhongdianzhan[2]); //站
}
if(x_num==5)
{
write_yhanzi(1,4,3*16,zhongdianzhan[0]); //终
write_yhanzi(1,4,4*16,zhongdianzhan[1]); //点
write_yhanzi(1,4,5*16,zhongdianzhan[2]); //站
write_yhanzi(1,6,0*16,ganxiechengzuo[0]); //感
write_yhanzi(1,6,1*16,ganxiechengzuo[1]); //谢
write_yhanzi(1,6,2*16,ganxiechengzuo[2]); //乘
write_yhanzi(1,6,3*16,ganxiechengzuo[3]); //坐
write_yhanzi(1,6,4*16,ganxiechengzuo[4]); //本
write_yhanzi(1,6,5*16,ganxiechengzuo[5]); //公
write_yhanzi(1,6,6*16,ganxiechengzuo[6]); //车
}
/******************************上行报站显示***********/
if(s_num==1)
{
write_yhanzi(1,4,3*16,sinhaozhan[0]); //四
write_yhanzi(1,4,4*16,sinhaozhan[1]); //号
write_yhanzi(1,4,5*16,sinhaozhan[2]); //站
write_yhanzi(1,6,3*16,sanhaozhan[0]); //三
write_yhanzi(1,6,4*16,sanhaozhan[1]); //号
write_yhanzi(1,6,5*16,sanhaozhan[2]); //站
}
if(s_num==2)
{
write_yhanzi(1,4,3*16,sanhaozhan[0]); //三
write_yhanzi(1,4,4*16,sanhaozhan[1]); //号
write_yhanzi(1,4,5*16,sanhaozhan[2]); //站
write_yhanzi(1,6,3*16,erhaozhan[0]); //二
write_yhanzi(1,6,4*16,erhaozhan[1]); //号
write_yhanzi(1,6,5*16,erhaozhan[2]); //站
}
if(s_num==3)
{
write_yhanzi(1,4,3*16,erhaozhan[0]); //二
write_yhanzi(1,4,4*16,erhaozhan[1]); //号
write_yhanzi(1,4,5*16,erhaozhan[2]); //站
write_yhanzi(1,6,3*16,yihaozhan[0]); //一
write_yhanzi(1,6,4*16,yihaozhan[1]); //号
write_yhanzi(1,6,5*16,yihaozhan[2]); //站
}
if(s_num==4)
{
write_yhanzi(1,4,3*16,yihaozhan[0]); //一
write_yhanzi(1,4,4*16,yihaozhan[1]); //号
write_yhanzi(1,4,5*16,yihaozhan[2]); //站
write_yhanzi(1,6,3*16,zhongdianzhan[0]); //终
write_yhanzi(1,6,4*16,zhongdianzhan[1]); //点
write_yhanzi(1,6,5*16,zhongdianzhan[2]); //站
}
if(s_num==5)
{
write_yhanzi(1,4,3*16,zhongdianzhan[0]); //终
write_yhanzi(1,4,4*16,zhongdianzhan[1]); //点
write_yhanzi(1,4,5*16,zhongdianzhan[2]); //站
write_yhanzi(1,6,0*16,ganxiechengzuo[0]); //感
write_yhanzi(1,6,1*16,ganxiechengzuo[1]); //谢
write_yhanzi(1,6,2*16,ganxiechengzuo[2]); //乘
write_yhanzi(1,6,3*16,ganxiechengzuo[3]); //坐
write_yhanzi(1,6,4*16,ganxiechengzuo[4]); //本
write_yhanzi(1,6,5*16,ganxiechengzuo[5]); //公
write_yhanzi(1,6,6*16,ganxiechengzuo[6]); //车
}
}
硬件设计
使用元器件:
单片机:AT89C51;
(注意:单片机是通用的,无论51还是52、无论stc还是at都一样,引脚功能都一样。程序也是一样的。)
添加图片注释,不超过 140 字(可选)
设计资料
01仿真图
本设计使用proteus7.8和proteus8.9两个版本设计!具体如图!
添加图片注释,不超过 140 字(可选)
02程序
本设计使用软件keil5版本编程设计!具体如图!
添加图片注释,不超过 140 字(可选)
03设计资料
资料获取请关注同名公众号,全部资料包括仿真源文件 、程序(含注释)等。具体内容如下,全网最全! !
可以关注下方公众号!
点赞分享一起学习成长。