?? clock.c
字號:
//時鐘實驗
//功能:第一,二位數碼管顯示時
//三,四位數碼管顯示分
//五,六位數碼管顯示秒
#include <reg52.h>
#define const_time 400; //100x5ms=500ms=0.5s
unsigned int buff_time; //buffer for storing the counts
unsigned char data time;
sbit light0 = P1^0; //the light0 bit
sbit DAT164 =P0^6; //the bit of HC164's data
sbit CLK164 =P0^7; //the bit of HC164's clock
sbit onebit=P0^0; //one of the "8bit led"
sbit twobit=P0^1;
sbit thrbit=P0^2; //one of the "8bit led"
sbit foubit=P0^3;
sbit fivbit=P0^4; //one of the "8bit led"
sbit sixbit=P0^5;
bit LED_Buffer;
unsigned char data wei;
unsigned char data hour;
unsigned char data min;
unsigned char data sec;
unsigned char data hour_hi;
unsigned char data min_hi;
unsigned char data sec_hi;
unsigned char data hour_low;
unsigned char data min_low;
unsigned char data sec_low;
unsigned char data LED;
code unsigned char LEDMAP[] = // define the table of the "8bit led"
{
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xA7,0xA1,0x86,0x8E
};
void send164() //the function of send the data to the "74HC164"
{
unsigned char data j;
for (j = 0; j < 8; j++)
{
if (LED & 0x80) DAT164 = 1;
else DAT164 = 0;
CLK164 = 1;
CLK164 = 0;
LED <<= 1;
}
}
void main()
{
TMOD=0x01; //set the timer's mode
TH0=(65536-2500)/256; //fill the timer0's higher register
TL0=(65536-2500)%256; //fill the timer0's lower register
IE=0x82; //EA=1,IT0=1
LED_Buffer=0;
light0=0;
buff_time=const_time;
TR0=1; //start the timer!
hour=0;
min=0;
sec=0;
while(1)
{
light0=LED_Buffer;
}
}
void T0Int() interrupt 1 using 3
{
TH0=(65536-2500)/256; //fill the timer0's higher register
TL0=(65536-2500)%256; //fill the timer0's lower register
buff_time--;
wei=buff_time%6;
hour_hi=hour/10;
hour_low=hour%10;
min_hi=min/10;
min_low=min%10;
sec_hi=sec/10;
sec_low=sec%10;
switch(wei)
{
case 5:
onebit=0;
twobit=1;
thrbit=1;
foubit=1;
fivbit=1;
sixbit=1;
LED=LEDMAP[hour_hi];
send164();
break;
case 4:
onebit=1;
twobit=0;
thrbit=1;
foubit=1;
fivbit=1;
sixbit=1;
LED=LEDMAP[hour_low];
LED=LED&0x7f;
send164();
break;
case 3:
onebit=1;
twobit=1;
thrbit=0;
foubit=1;
fivbit=1;
sixbit=1;
LED=LEDMAP[min_hi];
send164();
break;
//////////////
case 2:
onebit=1;
twobit=1;
thrbit=1;
foubit=0;
fivbit=1;
sixbit=1;
LED=LEDMAP[min_low];
LED=LED&0x7f;
send164();
break;
case 1:
onebit=1;
twobit=1;
thrbit=1;
foubit=1;
fivbit=0;
sixbit=1;
LED=LEDMAP[sec_hi];
send164();
break;
case 0:
onebit=1;
twobit=1;
thrbit=1;
foubit=1;
fivbit=1;
sixbit=0;
LED=LEDMAP[sec_low];
send164();
break;
}
if (buff_time==0)
{
buff_time=const_time; // refill the buffer
LED_Buffer=!LED_Buffer; // opposite the LED display
sec++;
if (sec==60)
{
min++;
sec=0;
if (min==60)
{
hour++;
min=0;
if (hour==60)
hour=0;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -