标题:
提供一个自动窗帘的51单片机程序(蓝桥杯的题)
[打印本页]
作者:
fuyun1208
时间:
2019-3-29 16:03
标题:
提供一个自动窗帘的51单片机程序(蓝桥杯的题)
提供一个自动窗帘的51单片机程序(蓝桥杯的题)
单片机源程序如下:
#include<reg51.h>
#include<time1302.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P2^3; //独立键盘的各个位置
sbit key2=P2^4;
sbit key3=P2^5;
sbit key4=P2^6;
sbit clock=P3^6; //是单片机上IO的位置
sbit dout=P3^7; //是out对应的位置
uchar wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //位选
uchar duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00}; //段选 0到9
uchar cun[8]; //每位数据存放的数组
uchar a;
void mode_change();
void chushi();
void dis_clock_time();
uchar read();
void delay(uint x)
{
uint i,j;
for(i=x;i>0;i--)
for(j=110;j>0;j--);
}
void display() //显示函数的固定格式 其中i和wei的取值根据自己的需要增加和减少
{
uint i;
for(i=0;i<8;i++) //两位数的循环 两取决于i
{
P1=0xff; //消影
switch(i)
{
case 0:P0=wei[0];break; //选择第几位
case 1:P0=wei[1];break;
case 2:P0=wei[2];break;
case 3:P0=wei[3];break;
case 4:P0=wei[4];break;
case 5:P0=wei[5];break;
case 6:P0=wei[6];break;
case 7:P0=wei[7];break;
}
P1=cun[i]; //存放第几位
delay(1);
}
}
void main()
{
Ds1302Init();
chushi();
while(1)
{
a=read();
Ds1302ReadTime();
mode_change();
display();
}
}
void mode_change()
{
if(key1 == 0) //独立键盘的固定格式
{
while(!key1)
{
cun[0]=~duan[TIME[5]%16]; //年
cun[1]=~duan[TIME[5]/16];
cun[2]=~duan[TIME[6]/16];
cun[3]=~duan[TIME[6]%16]&0x7f;
cun[4]=~duan[TIME[4]/16]; //月
cun[5]=~duan[TIME[4]%16]&0x7f;
cun[6]=~duan[TIME[3]/16];
cun[7]=~duan[TIME[3]%16]; //日
display(); //这个可以根据自己的需要而定,即键盘的作用
}
}
}
void chushi()
{
cun[0]=~duan[10];
cun[1]=~duan[10];
cun[2]=~duan[10];
cun[3]=~duan[10];
cun[4]=~duan[10];
cun[5]=~duan[10];
cun[6]=~duan[10];
cun[7]=~duan[10];
}
void dis_clock_time() //时钟显示
{
Ds1302ReadTime();
cun[0]=~duan[TIME[2]/16]; //小时
cun[1]=~duan[TIME[2]%16];
//闪烁 在中断里面
cun[2]=~duan[TIME[1]/16]; //分钟
cun[3]=~duan[TIME[1]%16];
cun[4]=~duan[10]; //“-”
cun[5]=~duan[a/100]; //ad显示
cun[6]=~duan[a/10%10];
cun[7]=~duan[a%10];
display();
}
uchar read() //AD固定格式
{
uint i;
uchar ad=0x00;
dout=1;
for(i=0;i<8;i++)
{
ad=ad<<1; //左移的数码管的 方便显示各个位置的
if(dout)
{
ad++;
}
clock=1;
clock=0;
_nop_(); //相当于延迟函数delay
_nop_();
}
return ad;
}
复制代码
所有资料51hei提供下载:
自动窗帘.rar
(33.99 KB, 下载次数: 13)
2019-3-29 16:02 上传
点击文件名下载附件
自动窗帘
下载积分: 黑币 -5
作者:
admin
时间:
2019-3-31 02:36
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1