|
突发奇想,用贴片三色LED设计了一台数字钟,不废话了,直接上菜。
- /*********************************************************************/
- /* DS1302 */
- /*********************************************************************/
- #include <reg52.h>
- #include <intrins.h>
- /*********************************************************************/
- /* 函数宏定义定义 */
- /*********************************************************************/
- #define INT8U unsigned char
- #define INT16U unsigned int
- /*********************************************************************/
- /* DS1302引脚定义 */
- /*********************************************************************/
- sbit SCLK = P1^5; //控制数据时钟
- sbit IO = P1^6; //数据口
- sbit RST = P1^7; //使能端、复位端
- /*********************************************************************/
- /* 按键引脚定义 */
- /*********************************************************************/
- sbit KEY_set = P2^4; //按键 选择
- sbit KEY_dec = P2^5; //按键 减
- sbit KEY_add = P2^6; //按键 加
- sbit KEY4 = P2^7; //备用
- sbit KEY_int0 = P3^2; //INT0
- /*********************************************************************/
- /* 红蓝绿三色引脚定义 */
- /*********************************************************************/
- sbit B_LED = P3^5; //蓝色使能
- sbit G_LED = P3^6; //绿色使能
- sbit R_LED = P3^7; //红色使能
- /*********************************************************************/
- /* 74HC164引脚定义 */
- /*********************************************************************/
- sbit DATA=P1^0;
- sbit CLK=P1^1;
- #define BLED_ON() B_LED=1;G_LED=0;R_LED=0
- #define GLED_ON() B_LED=0;G_LED=1;R_LED=0
- #define RLED_ON() B_LED=0;G_LED=0;R_LED=1
- /*********************************************************************/
- /* 蜂鸣器引脚定义 */
- /*********************************************************************/
- sbit BUZZER = P2^0;
- INT8U LED_array[] = //
- {
- 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //
- 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 //
- };
- INT8U BRGLED_array[] = //
- {
- 0xE0, 0xC0, 0xA0, 0x80, 0x60, 0x40, 0x20, 0xE0, 0xC0, 0xA0, 0x80, 0x60, 0x40, 0x20, 0x00, 0x00 //
- };
- /*********************************************************************/
- /* 写时分秒地址 */
- /*********************************************************************/
- #define write_miao 0x80
- #define write_fen 0x82
- #define write_shi 0x84
- /*********************************************************************/
- /* 读时分秒地址 */
- /*********************************************************************/
- #define read_miao 0x81
- #define read_fen 0x83
- #define read_shi 0x85
- //bit miao_flag;
- INT8U flag, NUM=15, n;//
- INT8U s, knum=0, snum, fnum, mnum, g;
- INT16U t;
- //INT8U miao_num;
- //INT8U Dmiao;
- char shi, fen, miao; //读出DS1302时分秒数据存储变量
- /*********************************************************************/
- /* 毫秒级延时函数 */
- /*********************************************************************/
- void delay_ms(INT16U ms)
- {
- INT16U x, y;
- for(x=0; x<ms; x++)
- for(y=0; y<110; y++);
- }
- /*********************************************************************/
- /* 74HC164函数 */
- /*********************************************************************/
- void HC164(INT8U dat)
- {
- INT8U i;
- for(i = 0; i < 8; i ++)
- {
- if(dat & 0x80)
- DATA = 1;
- else
- DATA = 0;
- dat <<= 1;
- CLK = 1;
- CLK = 0;
- }
- }
- /*********************************************************************/
- /* 无源蜂鸣器函数 */
- /*********************************************************************/
- void FMQ(INT8U g, INT16U t) //可以选择鸣响次数和时间
- {
- INT8U h,j;
- INT16U i ;
- for(h=0; h<g; h++)
- {
- for(i=0; i<t; i++)
- {
- BUZZER =~ BUZZER; //BUZZER取反
- for(j=0 ;j<90 ;j++); //需要产生方波,调节音频
- }
- delay_ms(100); //间隔时间
- }
- BUZZER = 1; //PNP-0;NPN-1关闭蜂鸣器
- }
- /*********************************************************************/
- /* 定时器0初始化函数 */
- /*********************************************************************/
- /*void TIMER0_init(void)
- {
- TMOD = 0x01;
- TH0 = (65536-50000)/256; //定时50ms一个中断
- TL0 = (65536-50000)%256;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- } */
- /*********************************************************************/
- /* DS1302读函数 */
- /*********************************************************************/
- INT8U read_1302(INT8U add) //
- {
- INT8U i, date;
- RST = 0; //禁止读写数据
- for(i=0; i<8; i++)
- {
- RST = 1;
- SCLK = 0;
- IO = add&0x01;
- add >>= 1;
- SCLK = 1;
- }
- for(i=0; i<8; i++)
- {
- if(IO)
- date = date|0x80;
- else
- date = date|0x00;
- SCLK = 1;
- date >>=1;
- SCLK = 0;
- }
- RST = 0;
- date = date/16*10 + date%16;//进制转换 16转10进制
- IO = 0; //数据读完后,IO口必须为0,不然小时一直显示85
- return date;
- }
- /*********************************************************************/
- /* DS1302写函数 */
- /*********************************************************************/
- void write_1302(INT8U add, INT8U dat) //
- {
- INT8U i;
- RST = 0; //禁止读写数据
- SCLK = 0;
- for(i=0; i<8; i++) //写地址
- {
- RST = 1; //允许读写数据
- SCLK = 0;
- IO = add&0x01;
- add >>= 1;
- SCLK = 1;
- }
- for(i=0; i<8; i++) //写数据
- {
- RST = 1; //允许读写数据
- SCLK = 0;
- IO = dat&0x01;
- dat >>= 1;
- SCLK = 1;
- }
- RST = 0;
- }
- /*********************************************************************/
- /* DS1302初始化函数(设置初始时间时使用) */
- /*********************************************************************/
- /*void DS1302_init(void)
- {
- flag = read_1302(0x81);
- if(flag&0x80)
- write_1302(0x8E, 0x00); //保护取消,可以进行读写操作
- write_1302(write_miao, 0x00); // 设置时间
- write_1302(write_fen, 0x03);
- write_1302(write_shi, 0x04);
- write_1302(0x90, 0xA5);
- write_1302(0xC0, 0xF0); //判断是否初始化一次标识写入
- write_1302(0x8E, 0x80); //保护启动,不能进行读写操作
- } */
- /*********************************************************************/
- /* 显示时间函数 */
- /*********************************************************************/
- void display(INT8U shi, INT8U fen, INT8U miao) //
- {
- for(n=0; n<30; n++)
- {
- if(miao/8==0)
- {
- GLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[miao]);
- delay_ms(NUM);
- }
- else if(miao/8==1)
- {
- GLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[miao-8]);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(miao/8==2)
- {
- GLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[miao-16]);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(miao/8==3)
- {
- GLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[miao-24]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(miao/8==4)
- {
- GLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[miao-32]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(miao/8==5)
- {
- GLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[miao-40]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(miao/8==6)
- {
- GLED_ON();
- HC164(0x00);
- HC164(LED_array[miao-48]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(miao/8==7)
- {
- GLED_ON();
- HC164(LED_array[miao-56]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- if(fen/8==0)
- {
- BLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[fen]);
- delay_ms(NUM);
- }
- else if(fen/8==1)
- {
- BLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[fen-8]);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(fen/8==2)
- {
- BLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[fen-16]);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(fen/8==3)
- {
- BLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[fen-24]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(fen/8==4)
- {
- BLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[fen-32]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(fen/8==5)
- {
- BLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[fen-40]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(fen/8==6)
- {
- BLED_ON();
- HC164(0x00);
- HC164(LED_array[fen-48]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if(fen/8==7)
- {
- BLED_ON();
- HC164(LED_array[fen-56]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- if((shi*5)/8==0)
- {
- RLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[(shi*5)]);
- delay_ms(NUM);
- }
- else if((shi*5)/8==1)
- {
- RLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[(shi*5)-8]);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if((shi*5)/8==2)
- {
- RLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[(shi*5)-16]);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if((shi*5)/8==3)
- {
- RLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[(shi*5)-24]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if((shi*5)/8==4)
- {
- RLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[(shi*5)-32]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if((shi*5)/8==5)
- {
- RLED_ON();
- HC164(0x00);
- HC164(0x00);
- HC164(LED_array[(shi*5)-40]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if((shi*5)/8==6)
- {
- RLED_ON();
- HC164(0x00);
- HC164(LED_array[(shi*5)-48]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- else if((shi*5)/8==7)
- {
- RLED_ON();
- HC164(LED_array[(shi*5)-56]);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- HC164(0x00);
- delay_ms(NUM);
- }
- }
- }
- /*********************************************************************/
- /* 读出DS1302时分秒且显示函数 */
- /*********************************************************************/
- void read_sfm(void)
- {
- miao = read_1302(read_miao);
- fen = read_1302(read_fen);
- shi = read_1302(read_shi);
- display(shi, fen, miao);
- }
- /*********************************************************************/
- /* 按键扫描函数 */
- /*********************************************************************/
- void keyscan(void) //
- {
- if(KEY_set==0) //选择按键按下
- {
- delay_ms(10);
- if(KEY_set==0)
- FMQ(1, 100);
- {
- while(!KEY_set)
- display(shi, fen, miao); //加上这句,按键按下就不会闪
- knum ++;
- if(knum==1) //分闪烁
- {
- write_1302(0x8E, 0x00); //保护取消
- write_1302(write_miao, 0x80);
- }
- if(knum==3) //时钟启动
- {
- knum=0;
- write_1302(write_miao, 0x00);
- write_1302(0x8E, 0x80); //保护启动
- }
- }
- }
- if(knum==1) //分钟调节
- {
- if(KEY_add==0) //加
- {
- delay_ms(10);
- if(KEY_add==0)
- FMQ(1, 100);
- {
- while(!KEY_add)
- display(shi, fen, miao); //加上这句,按键按下就不会闪
- fen ++;
- if(fen==60)
- fen = 0;
- write_1302(write_fen,fen/10*16 + fen%10); //写入1302
- read_sfm(); //读出时间,然后显示
- }
- }
- if(KEY_dec==0)
- {
- delay_ms(10);
- if(KEY_dec==0)
- FMQ(1, 100);
- {
- while(!KEY_dec)
- display(shi, fen, miao); //加上这句,按键按下就不会闪
- fen --;
- if(fen==-1)
- fen = 59;
- write_1302(write_fen,fen/10*16 + fen%10);
- read_sfm();
- }
- }
- }
- if(knum==2)
- {
- if(KEY_set==0)
- {
- delay_ms(10);
- if(KEY_set==0)
- FMQ(1, 100);
- {
- while(!KEY_set)
- display(shi, fen, miao); //加上这句,按键按下就不会闪
- shi ++;
- if(shi==12)
- shi = 0;
- write_1302(write_shi,shi/10*16 + shi%10);
- read_sfm();
- }
- }
- if(KEY_dec==0)
- {
- delay_ms(10);
- if(KEY_dec==0)
- FMQ(1, 100);
- {
- while(!KEY_dec)
- display(shi, fen, miao); //加上这句,按键按下就不会闪
- shi --;
- if(shi==-1)
- shi = 11;
- write_1302(write_shi,shi/10*16+shi%10);
- read_sfm();
- }
- }
- }
- }
- /*********************************************************************/
- /* 主程序 */
- /*********************************************************************/
- void main(void)
- {
- // DS1302_init();
- // TIMER0_init();
- FMQ(1, 120);
- while(1)
- {
- /* if(miao_num<10) //秒点闪烁
- led = 0;
- if(miao_num>=10)
- led = 1; */
- read_sfm();
- keyscan();
- }
- }
- /*********************************************************************/
- /* TIMER0(1秒)定时中断函数 */
- /*********************************************************************/
- /*
- void TIMER0() interrupt 1
- {
- TMOD = 0x01;
- TH0 = (65536-50000)/256;
- TL0 = (65536-50000)%256;
- miao_num ++;
- if(miao_num>=20)
- {
- miao_num = 0;
- miao_flag = 1;
- }
- }
- */
复制代码
|
评分
-
查看全部评分
|