这个星期是实训周,很是无聊,回宿舍内卷,提前把代码写完了,上课打王者
我们实训用的是金沙滩开发板,我之前学的是普中开发板,但是看懂原理图基本是一样的。
我们要实现的现象是显示一个数字钟,可以通过按键停止,启动,调节时间
用到的有数码管,定时器,138译码器,矩阵按键.
这是我写的代码嘻嘻
#include"reg52.h"
typedef unsigned char uchar;
typedef unsigned int uint;
sbit LSA = P1^0; //38译码器
sbit LSB = P1^1;
sbit LSC = P1^2;
sbit ADDR3 = P1^3; //使能引脚
sbit ENLED =P1^4;
sbit KeyInt1 = P2^4; //按键
sbit KeyInt2 = P2^5;
sbit KeyInt3 = P2^6;
sbit KeyInt4 = P2^7;
uchar code smgduan[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; // 共阴极数码管段选
uint hour=0,min=0,second=0; //定义小时分钟秒
void delay(uint i) //延时函数i= 1;延时10us
{
while(i--);
}
void Timer0Int() //定时器1
{
TMOD = 0x01;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
void display() //38译码扫描显示
{
uchar i;
for(i=0;i<6;i++)
{
switch(i)
{
case 5: LSA = 0;LSB = 0;LSC = 0;P0 = smgduan[second%10];delay(10);P0 = 0xff; break;
case 4: LSA = 1;LSB = 0;LSC = 0;P0 = smgduan[second/10];delay(10);P0 = 0xff; break;
case 3: LSA = 0;LSB = 1;LSC = 0;P0 = smgduan[min%10]&0x7f;delay(10);P0 = 0xff; break;
case 2: LSA = 1;LSB = 1;LSC = 0;P0 = smgduan[min/10];delay(10);P0 = 0xff; break;
case 1: LSA = 0;LSB = 0;LSC = 1;P0 = smgduan[hour%10]&0x7f;delay(10);P0 = 0xff;break;
case 0: LSA = 1;LSB = 0;LSC = 1;P0 = smgduan[hour/10];delay(10);P0 = 0xff;break;
}
}
}
void keypress()
{
P2 = 0xf7; //1111/0111 先第一行全置一,第一列置0
if(KeyInt1==0) //KeyInt1被按下 0111/0111
{
delay(10); //消抖
if(KeyInt1==0) //再检测
{
while(!KeyInt1); //松手
EA = 0; //关中断
}
}
P2 = 0xf7;
if(KeyInt2==0)
{
delay(10);
if(KeyInt2==0)
{
while(!KeyInt2);
EA = 1;
}
}
P2 = 0xf7;
if(KeyInt3==0)
{
delay(10);
if(KeyInt3==0)
{
while(!KeyInt3);
EA = 1;
hour++;
if(hour==24)
{
hour=0;
}
}
}
if(KeyInt4==0)
{
delay(10);
if(KeyInt4==0)
{
while(!KeyInt4); //检测松手
EA = 1; //打开中断总开关
min++; //分钟累加
if(min==0)
{
min=0;
}
}
}
}
void main() //ִ主函数
{
ENLED=0;
ADDR3=1;
Timer0Int(); //定时器
while(1)
{
display(); //显示
keypress(); //按键
}
}
void Timer0()interrupt 1 //进入定时器1
{
uint num;
num++; //1秒
if(num==20)
{
second++; //秒累加
num = 0;
if(second==60) //如果秒等于60置0
{
second=0;
min++;
if(min==60) //如果分等于60置0
{
min=0;
hour++;
if(hour==24) //如果小时等于12置0
{
hour=0;
min =0;
second=0;
}
}
}
}
}
陌生的你支持一下,希望得到大佬的指点.