在烤箱拆出来的LED显示屏,和那个宝上TM1638d的驱动一样,加了DS12C887时钟芯片,废物利用一下,附上程序。
单片机源程序如下:
- #include "define.h"
- unsigned char code table[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
- 0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
- //unsigned char num[8]={0,1,2,3,4,5,6,7}; //各个数码管显示的值
- struct Timer{
- unsigned char sec;
- unsigned char min;
- unsigned char hour;
- };
- #define INBUF_LEN 4
- unsigned char ch;
- unsigned key_num=0;
- unsigned char cnt_num=0;
- uchar cnt_sec=0;
- bit read_flag=0;
- bit Set_flag =0;
- struct Timer Display;
- struct Timer Setting;
- void DS12C887_read_time()
- {
- Display.sec=DS12C887_read_ds(DC12C887_Sec_Address);
- Display.min=DS12C887_read_ds(DC12C887_Min_Address);
- Display.hour=DS12C887_read_ds(DC12C887_Hour_Address);
- }
- void init_serialcom( void )
- {
- SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
- TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
- PCON |= 0x80 ; //SMOD=1;
- TH1 = 0xFA ; //Baud:9600 fosc=11.0592MHz
- IE |= 0x90 ; //Enable Serial Interrupt
- TR1 = 1 ; // timer 1 run
- TI=1;
- }
- void Timer0Init(void) //50ms @11.0592MHz
- {
- // AUXR &= 0x7F; //?????12T??
- TMOD &= 0xF0; //???????
- TMOD |= 0x01; //???????
- TL0 = 0x00; //??????
- TH0 = 0x4C; //??????
- TF0 = 0; //??TF0??
- ET0 = 1;
- TR0 = 0; //???0????
- EA = 1;
- }
- void send_char_com( unsigned char ch)
- {
- SBUF=ch;
- while (TI== 0);
- TI= 0 ;
- }
- void Key_Scan()
- {
-
- if(KEY_SET==0)
- {
- delay_ms(1);
- if(KEY_SET==0)
- {
- while(!KEY_SET);
- key_num++;
- if(key_num>=3) //设置完成
- {
- key_num = 0;
- //DS12C887_write_time(Setting.hour,Setting.min,0);
- DS12C887_write_ds(0x0b,0x26);
- DS12C887_write_time(Display.hour,Display.min,0);
- Set_flag=0;
- TR0 = 0;
- //display_st = 1; //????
- //SET.hour=(DISPLAY.hour/10)*16+DISPLAY.hour%10;
- //SET.min=(DISPLAY.min/10)*16+DISPLAY.min%10;
- //iicWriteData(HOUR_ADDRESS,SET.hour);
- //iicWriteData(MIN_ADDRESS,SET.min);
- //iicWriteData(SEC_ADDRESS,0x00); //denable oscillator
- }
- }
- }
- if(key_num!=0)
- {
-
- TR0 = 1;
- //iicWriteData(SEC_ADDRESS,0x80); //disable oscillator
- Set_flag=1; //进入设置模式
- DS12C887_write_ds(0x0b,0xa6); //DS12C887禁止读取
- if(key_num == 1) //第一次按下按键,调小时
- {
- if(LED)
- {
- TM1638_Write_DATA(TM1638_Thous_Bit,0x00);
- TM1638_Write_DATA(TM1638_Hund_Bit,0x00);
- }
- //LED_DISPLAY(DISPLAY.hour,DISPLAY.min,1);
- //else LED_DISPLAY(DISPLAY.hour,DISPLAY.min,0);
- if(KEY_ADD==0)
- {
- delay_ms(1);
- if(KEY_ADD==0)
- {
- while(!KEY_ADD);
- Display.hour++;
- if(Display.hour>=24)
- Display.hour=0;
- }
- }
- if(KEY_DEC==0)
- {
- delay_ms(1);
- if(KEY_DEC==0)
- {
- while(!KEY_DEC);
- if(Display.hour>0)
- Display.hour--;
- }
- }
-
- }
-
- if(key_num == 2) //第二次按下设置键,调分钟
- {
- if(LED)
- {
- TM1638_Write_DATA(TM1638_Ten_Bit,0x00);
- TM1638_Write_DATA(TM1638_Bit,0x00);
- }
- if(KEY_ADD==0)
- {
- delay_ms(1);
- if(KEY_ADD==0)
- {
- while(!KEY_ADD);
- Display.min++;
- if(Display.min>=60)
- Display.min=0;
- }
- }
- if(KEY_DEC==0)
- {
- delay_ms(1);
- if(KEY_DEC==0)
- {
- while(!KEY_DEC);
- if(Display.min>0)
- Display.min--;
-
- }
- }
-
- }
- }
- // if(key_num==0)
- // {
- // if(KEY_ADD==0)
- // {
- // if(KEY_ADD==0)
- // {
- // while(!KEY_ADD);
- // show_set_value++;
- // if(show_set_value>=5)
- // show_set_value=0;
- //
- // }
- // }
- //
- // }
- // if(show_set_value!=0)
- // {
- // TR0=1;
- // if(sec_cnt>=15)
- // {
- // sec_cnt=0;
- // TR0=0;
- // show_set_value=0;
- // }
- // else
- // {
- //
- // if(show_set_value==1)
- // LED_DISPLAY(OPEN1.hour,OPEN1.min,4);
- // if(show_set_value==2)
- // LED_DISPLAY(CLOSE1.hour,CLOSE1.min,4);
- // if(show_set_value==3)
- // LED_DISPLAY(OPEN2.hour,OPEN2.min,4);
- // if(show_set_value==4)
- // LED_DISPLAY(CLOSE2.hour,CLOSE2.min,4);
- // }
- // }
- }
- void main(void)
- {
- unsigned char i;
- TM1638_init(); //初始化TM1638
- DS12C887_init();
- init_serialcom();
- Timer0Init();
- LED=0;
- for(i=0;i<8;i++)
- TM1638_Write_DATA(i<<1,0x00); //初始化寄存器
- //DS12C887_write_time(14,9,0);
- while(1)
- {
- /*i=Read_key(); //读按键值
- if(i<8)
- {
- num[i]++;
- while(Read_key()==i); //等待按键释放
- if(num[i]>15)
- num[i]=0;
- Write_DATA(i*2,tab[num[i]]);
- Write_allLED(1<<i);*/
- //Write_DATA(0,table[Display.hour/10]);
- Key_Scan();
- TM1638_Write_DATA(TM1638_Thous_Bit,table[Display.hour/10]);
- TM1638_Write_DATA(TM1638_Hund_Bit,table[Display.hour%10]);
- TM1638_Write_DATA(TM1638_Ten_Bit,table[Display.min/10]);
- TM1638_Write_DATA(TM1638_Bit,table[Display.min%10]);
- if(Set_flag==0) //不在设置模式
- {
- DS12C887_read_time();
- if(Display.sec%2==0)
- TM1638_Write_DATA(TM1638_Sec_Bit,0x01); //闪烁
-
- else TM1638_Write_DATA(TM1638_Sec_Bit,0x00);
- }
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
TM1628 DS12C887时钟.rar
(1006.43 KB, 下载次数: 163)
|