基于51单片机的8位数码管显示的可调时电子时钟
本人大二本科生,第一次发东西,功能比较简单,代码有点复杂,希望能有大神指正。
- 基于51单片机的不可调时间的时钟显示,晶振为11.0592MHZ,60HZ左右刷新一次,用了两个定时器,定时器0负责LED循环显示,定时器1负责定时,大概200秒左右落后1秒。
#include <REGX51.H>
#include <intrins.h>
void begin();
void ledshow();
unsigned char number[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40};
unsigned char wei[]={0xe3,0xe7,0xeb,0xef,0xf3,0xf7,0xfb,0xff};
unsigned char num[8];
unsigned char hour,min,sec;
int main(){
begin();
hour=8;
min=56;
sec=00;
while(1){
ledshow();
}
}
void begin(){
TMOD &= 0xF0; //???????
TL0 = 0x18; //??????
TH0 = 0xC4; //??????
TF0 = 0; //??TF0??
TR0 = 1; //???0????
TMOD &= 0x0F; //???????
TMOD |= 0x10; //???????
TL1 = 0x00; //??????
TH1 = 0xF7; //??????
TF1 = 0; //??TF1??
TR1 = 1; //???1????
EA=1;
ET0=1;
ET1=1;
}
void ledshow(){
num[0]=number[hour/10];
num[1]=number[hour%10];
num[2]=~number[10];
num[3]=number[min/10];
num[4]=number[min%10];
num[5]=~number[10];
num[6]=number[sec/10];
num[7]=number[sec%10];
}
void BBBBBBBB() interrupt 1{
static char i=0;
TL0 = 0x18; //??????
TH0 = 0xC4; //??????
P0=0;
P2=~wei[i];
P0=~num[i];
i++;
if(i>7){
i=0;
}
}
void AAAAAAAA() interrupt 3{
static unsigned int n=0;
TL1 = 0x00; //??????
TH1 = 0xF7; //??????
n++;
if(n==400){
n=0;
sec++;
if(sec==60){
sec=0;
min++;
if(min==60){
min=0;
hour++;
if(hour==24){
hour=0;
}
}
}
}
}
2.第二段代码为可调电子时钟,K1负责退出选位操作,K2负责选位,K3负责加1,K4负责减1.看
#include <REGX52.H>
#include <intrins.h>
void begin();
void ledshow();
char keymode();
void Delay10ms();
void Delay2080us();
void K2(char i);
void K3();
void K4();
unsigned char number[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40};
unsigned char wei[]={0xe3,0xe7,0xeb,0xef,0xf3,0xf7,0xfb,0xff};
unsigned char num[8];
char hour,min,sec;
char x=2;
sbit key_1=P3^1;
sbit key_2=P3^0;
sbit key_3=P3^2;
sbit key_4=P3^3;
int main(){
unsigned char key,i=0;
begin();
hour=11;
min=11;
sec=11;
while(1){
ledshow();
if(keymode()==1){
key=1;
}
if(keymode()==2){
key=2;
switch(x){
case 0:i=1;break;
case 1:i=4;break;
case 2:i=7;break;
}
}
switch(key){
case 1: ET0=1;break;
case 2: ET0=0;K3();K4();K2(i); break;
default:ET0=1;
}
}
}
void begin(){
TMOD &= 0xF0; //???????
TL0 = 0x18; //??????
TH0 = 0xC4; //??????
TF0 = 0; //??TF0??
TR0 = 1; //???0
TMOD &= 0x0F; //???????
TMOD |= 0x10; //???????
TL1 = 0x00; //??????
TH1 = 0xF7; //??????
TF1 = 0; //??TF1??
TR1 = 1; //???1????
EA=1;
ET0=1;
ET1=1;
}
char keymode(){
char key;
if(key_1==0){
Delay10ms();
if(key_1==0){
while(key_1==0);
key=1;
}
}
else if(key_2==0){
Delay10ms();
if(key_2==0){
while(key_2==0);
key=2;
x++;
if(x>2){
x=0;
}
}
}
return key;
}
void K2(char i){
static char j=0;
static char t;
t++;
if(t/100==1){
if(j==i||j+1==i){
P0=0;
P2=~wei[j];
}
else{
P0=0;
P2=~wei[j];
P0=~num[j];
}
}
else{
P0=0;
P2=~wei[j];
P0=~num[j];
}
if(t>200){
t=0;
}
j++;
if(j>7){
j=0;
}
Delay2080us();
}
void K3(){
if(key_3==0){
Delay10ms();
if(key_3==0){
while(key_3==0);
if(x==0){
hour++;
if(hour>23){
hour=0;
}
}
if(x==1){
min++;
if(min>59){
min=0;
}
}
if(x==2){
sec++;
if(sec>59){
sec=0;
}
}
}
}
}
void K4(){
if(key_4==0){
Delay10ms();
if(key_4==0){
while(key_4==0);
if(x==0){
hour--;
if(hour<0){
hour=23;
}
}
if(x==1){
min--;
if(min<0){
min=59;
}
}
if(x==2){
sec--;
if(sec<0){
sec=59;
}
}
}
}
}
void ledshow(){
num[0]=number[hour/10];
num[1]=number[hour%10];
num[2]=~number[10];
num[3]=number[min/10];
num[4]=number[min%10];
num[5]=~number[10];
num[6]=number[sec/10];
num[7]=number[sec%10];
}
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 18;
j = 235;
do
{
while (--j);
} while (--i);
}
void Delay2080us() //@11.0592MHz
{
unsigned char i, j;
i = 4;
j = 183;
do
{
while (--j);
} while (--i);
}
void BBBBBBBB() interrupt 1{
static char i=0;
TL0 = 0x18; //??????
TH0 = 0xC4; //??????
P0=0;
P2=~wei[i];
P0=~num[i];
i++;
if(i>7){
i=0;
}
}
void AAAAAAAA() interrupt 3{
static unsigned int n=0;
TL1 = 0x00; //??????
TH1 = 0xF7; //??????
n++;
if(n==398){
n=0;
sec++;
if(sec>=60){
sec=0;
min++;
if(min>=60){
min=0;
hour++;
if(hour>=24){
hour=0;
}
}
}
}
}