快捷搜索:  汽车  科技

怎么调整单片机的时钟频率(单片机入门-C51语言实现矩阵键盘和数码管调整时钟的时分秒)

怎么调整单片机的时钟频率(单片机入门-C51语言实现矩阵键盘和数码管调整时钟的时分秒)#include<reg52.h>代码如下:视频教程如下: 电路图如下:

单片机入门,这里介绍C51语言实现矩阵键盘和数码管调整时钟的时分秒,模拟电子钟的部分功能。一般步骤如下:

1.声明相应的变量去保存时分秒信号,保存用户的功能按键。

2.编写显示时分秒的函数

3.按键扫描,保存用户功能按键,根据用户选择调整时分秒。

视频教程如下:

电路图如下:

怎么调整单片机的时钟频率(单片机入门-C51语言实现矩阵键盘和数码管调整时钟的时分秒)(1)

代码如下:

#include<reg52.h>

#include<intrins.h>

#define uchar unsigned char

uchar code tab[]={ 0xc0 0xf9 0xa4 0xb0 0x99 0x92 0x82 0xf8

0x80 0x90 0x88 0x83 0xc6 0xa1 0x86 0x8e 0xff};

uchar jianhao=16 hang=4 lie=0 keydat=16;

uchar hour=15 minute=40 second=23;

uchar sel=0;

void keyscan(); //行列式键盘扫描函数

void display(uchar hour uchar minute uchar second);

void delayms(uchar t); //延时函数

void main(void)

{

while(1)

{

display(hour minute second);

keyscan();

}

}

void keyscan() //行列式键盘扫描函数,返回值为按下键的键值

{

P3=0x0f; //同时扫描4列

if(P3!=0x0f)

{ delayms(10);

if(P3!=0x0f)

{

switch(P3) //判断是哪一行有键按下

{

case 0x0e : hang=0;

break;

case 0x0d : hang=1;

break;

case 0x0b : hang=2;

break;

case 0x07 : hang=3;

break;

}

P3=0xef; // 扫描P3.4列

if(P3!=0xef) //此列的行值不全为0,说明此列有键按下

{delayms(10);

if(P3!=0xef)

lie=0; //取列码

}

P3=0xdf;

if(P3!=0xdf)

{delayms(10);

if(P3!=0xdf)

lie=1;

}

P3=0xbf;

if(P3!=0xbf)

{delayms(10);

if(P3!=0xbf)

lie=2;

}

P3=0x7f;

if(P3!=0x7f)

{delayms(10);

if(P3!=0x7f)

lie=3;

}

jianhao=4*hang lie; //键码=4 X行码 列码

P3=0x0f;

while(P3!=0x0f); //键释放了吗

}

keydat=jianhao;

switch(keydat)

{ case 1:sel=1;break;

case 2:sel=2;break;

case 3:sel=3;break;

}

if (keydat==10)//a键按下 1;

{

switch(sel)

{ case 1:hour ;break;

case 2:minute ;break;

case 3:second ;break;

}

}

}

}

void display(uchar hour uchar minute uchar second)

{

P2=0x01;

P0=tab[hour/10];

delayms(2);

P2=0x02;

P0=tab[hour];

delayms(2);

P2=0x04;

P0=0xbf;

delayms(2);

P2=0x08;

P0=tab[minute/10];

delayms(2);

P2=0x10;

P0=tab[minute];

delayms(2);

P2=0x20;

P0=0xbf;

delayms(2);

P2=0x40;

P0=tab[second/10];

delayms(2);

P2=0x80;

P0=tab[second];

delayms(2);

}

void delayms(uchar t)

{

uchar x y;

for(x=t;x>0;x--)

for(y=110;y>0;y--);

}

猜您喜欢: