/***************时钟采用定时中断方式,50MS一次******完整可用,硬件实验通过**********************/
#include
#include
#include
/***********************************************************************/
#define uchar unsigned char
#define uint unsigned int
//common part
#define HIGH 1
#define LOW 0
#define TRUE 1
#define FALSE 0
#define ZERO 0
#define MSB 0x80
#define LSB 0x01
//max7219 part
#define REG_NO_OP 0x00 // 定义空操作
#define DIG_1 0x01 // 定义数码管1
#define DIG_2 0x02 // 定义数码管2
#define DIG_3 0x03 // 定义数码管3
#define DIG_4 0x04 // 定义数码管4
#define DIG_5 0x05 // 定义数码管5
#define DIG_6 0x06 // 定义数码管6
#define DIG_7 0x07 // 定义数码管7
#define DIG_8 0x08 // 定义数码管8
#define DECODE_MODE 0x09
#define INTENSITY 0x0A
#define SCAN_LIMIT 0x0B
#define SHUT_DOWN 0x0C
#define DISPLAY_TEST 0x0F
uchar deda,sec,min,hour,week=1;
/***********************************************************************/
//change this part at different board
sbit LOAD=P2^7; //MAX7219 Load-Data Input: rising edge pin 12
sbit DIN=P2^5; //MAX7219 Serial-Data Input: rising edge pin 1
sbit CLK=P2^6; //MAX7219
sbit TT=P1^4;
sbit QQ=P3^6;
sbit RR=P3^7;
//function define
/***********************************************************************/
void Write_Max7219_byte(unsigned char temp);//write max7219 a byte
void Write_Max7219(unsigned char address,unsigned char dat);//write max7219 command and data
void Init_Max7219(void);//Initize max7219
//test program display from 1~8
/***********************************************************************/
/***********************************************************************/
void Write_Max7219_byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
CLK=LOW;
DIN=(bit)(temp&MSB);
temp<<=1;
CLK=HIGH;
}
}
/***********************************************************************/
void Write_Max7219(unsigned char address,unsigned char dat)
{
LOAD=LOW;
Write_Max7219_byte(address);
Write_Max7219_byte(dat);
LOAD=HIGH;
}
/***********************************************************************/
void Init_Max7219(void)
{
Write_Max7219(SHUT_DOWN, 0x01); //Normal Operation XXXXXXX1 Shutdown Mode XXXXXXXX0
Write_Max7219(DISPLAY_TEST, 0x00); //Normal Operation XXXXXXX0 Display Test Mode
Write_Max7219(DECODE_MODE, 0xff); //Decode Mode Select D7~D0 1 B decode 0 No decode
Write_Max7219(SCAN_LIMIT, 0x07); //SCAN LIMIT 0~7 0xX0~0xX7
Write_Max7219(INTENSITY, 0x04);
Write_Max7219(DIG_4,0x02); Write_Max7219(DIG_3,0x01);
Write_Max7219(DIG_6,0x07); Write_Max7219(DIG_5,0x05);
Write_Max7219(DIG_7,0x01);
}
/*****************************************************/
void init(void)
{
TMOD=0x01;
TH0=-(50000/256);
TL0=-(50000%256);
ET0=1;
TR0=1;
EA=1;
TT=0;
QQ=0;
RR=0;
Init_Max7219();
}
/******************************************/
void time0(void) interrupt 1
{
TH0=-(50000/256);
TL0=-(50000%256);
deda++;
}
/***************************************/
void conv(void)
{
if(deda>=20){deda=0;sec++;Write_Max7219(DIG_8,sec%10); Write_Max7219(DIG_7,sec/10); }
if(sec>=60){sec=0;min++; Write_Max7219(DIG_6,min%10); Write_Max7219(DIG_5,min/10);}
if(min>=60){min=0;hour++; Write_Max7219(DIG_4,hour%10); Write_Max7219(DIG_3,hour/10);}
if(hour>=24){hour=0;week++; Write_Max7219(DIG_2,week%10);}
if(week>7){week=1;}
if(hour==3) TT=1;
}
/*****************************************/
void delay(uint k)
{
uint data i,j;
for(i=0;i
{;}}
}
/**********************************************/
uchar scan_key(void)
{
uchar temp;
P3=0x3f;
temp=P3;
if(temp!=0x3f)
{delay(20);
temp=P3;
if(temp!=0x3f) return temp;
}
return 0x3f;
}
/********************************************/
void main(void)
{uchar key_flag;
init();
while(1)
{
conv();
key_flag=scan_key();
switch(key_flag)
{
case 0x3b:if(++min>59)min=0; delay(300); Write_Max7219(DIG_6,min%10); Write_Max7219(DIG_5,min/10);break;
case 0x37:if(++hour>23)hour=0; delay(300);Write_Max7219(DIG_4,hour%10); Write_Max7219(DIG_3,hour/10);break ;
case 0x2f:if(++week>7)week=1 ; delay(300); Write_Max7219(DIG_2,week%10);break;
default: break;
}
}
}
上一篇:可以改密码的单片机电子密码锁
下一篇:51单片机的几种精确延时
推荐阅读最新更新时间:2024-03-16 15:21