"); //-->
#include<c8051f040.h>
#define uint unsigned int
#define uchar unsigned char
uchar hour=11,min=8,sec=20;
uint ttt=0;
sbit cs=P4^0;
sbit clk=P4^2;
sbit dat=P4^4;
sbit key=P4^6;
void long_delay(void); //长延时
void short_delay(void); //短延时
void write7279(unsigned char, unsigned char);//写入HD7279
unsigned char read7279(unsigned char);//从HD7279 读出
void send_byte(unsigned char); //发送一个字节
unsigned char receive_byte(void); // 接收一个字节
unsigned char key_number; //定义全局变量,保存按键号(0 ~ 63)
void os_init();
void port_init();
void delay(uint d);
#define CMD_RESET 0xa4 //复位指令
#define DECODE1 0xc8 //方式1 译码指令,用来向HD7279 写数据以便显示
#define CMD_READ 0x15 //读键盘数据指令
#define CMD_TEST 0xbf //测试指令
#define DECODE0 0x80 //方式0译码指令,
#define UNDECODE 0x90 //下载数据但不译码
#define RTL_CYCLE 0xa3 //循环左移指令
#define RTR_CYCLE 0xa2 //循环右移指令
#define RTL_UNCYL 0xa1 //左移指令
#define RTR_UNCYL 0xa0 //右移指令
#define ACTCTL 0x98 //消隐控制指令
#define SEGON 0xe0 //段点亮指令
#define SEGOFF 0xc0 //段关闭指令
#define BLINKCTL 0x88 //闪烁控制指令
void main()
{
unsigned int k ;
WDTCN = 0xDE; // 关闭看门狗
WDTCN = 0xAD;
// for(k=0;k<200;k++);
send_byte(CMD_RESET);
for(k=0;k<20000;k++);
EA=1;
CKCON=0x01; //ckcon=0x01,sysclk*1/4; ckcon=0x00,sysclk*1/12;
// ckcon=0x02,sysclk*1/48; ckcon=0x03,extra clk *1/8;
//ckcon=0x08,sysclk;
TMOD=0x01; //16bit selection
TH0=0x3c;
TL0=0xb0;
ET0=1;
IT0=1;
TR0=1; //enable
// CKCON=0x08;
os_init();
port_init();
delay(1500); //24.0000MHZ
// delay(500); //11.0592MHZ
while(1)
{
write7279(DECODE1+2,hour%10);
write7279(DECODE1+3,hour/10);
// write7279(SEGON,0x08);
delay(30000);
write7279(DECODE1+5,min%10);
write7279(DECODE1+0,min/10);
// write7279(SEGON,0x30);
delay(30000);
write7279(DECODE1+7,sec/10);
write7279(DECODE1+4,sec%10);
delay(30000);
}
}
void os_init()
{
int i = 0;
SFRPAGE = CONFIG_PAGE;
OSCXCN = 0x67; //外部22.1184M晶振
for (i = 0; i < 3000; i++); //等待外部晶振稳定
while ((OSCXCN & 0x80) == 0);
CLKSEL = 0x01; //系统时钟不分频
}
void port_init()
{
SFRPAGE = CONFIG_PAGE;
P4MDOUT=0xff;
XBR2 = 0x40;
}
void write7279(unsigned char cmd, unsigned char dta)//写数据函数
{ //cmd 为指令,dta 为写入的数据
send_byte (cmd);
send_byte (dta);
}
unsigned char read7279(unsigned char command)//读数据指令,一般用来读按键号
{
send_byte(command);
return(receive_byte());
}
void send_byte( unsigned char out_byte) //发送一个字节
{
unsigned char i;
cs=0;
long_delay();
for (i=0;i<8;i++)
{
if (out_byte&0x80)
{
dat=1;
}
else
{
dat=0;
}
clk=1;
short_delay();
clk=0;
short_delay();
out_byte=out_byte*2;
}
dat=0;
}
unsigned char receive_byte(void) //接收一个字节
{
unsigned char i, in_byte;
dat=1;
long_delay();
for (i=0;i<8;i++)
{
clk=1;
short_delay();
in_byte=in_byte*2;
if (dat)
{
in_byte=in_byte|0x01;
}
clk=0;
short_delay();
}
dat=0;
return (in_byte);
}
void long_delay(void) //长延时
{
unsigned char i;
for (i=0;i<0x10;i++);
}
void short_delay(void) //短延时
{
unsigned char i;
for (i=0;i<30;i++); //24.0000MHZ
}
void delay(uint d)
{uint i,j;
for(i=0;i<d;i++);
{
for(j=10;j>0;j--);
}
}
void delay1us(void)
{
uchar xdata i;
for(i=10;i>0;i--);
}
void time()interrupt 1
{
TH0=0x3c;
TL0=0xb0;
ttt++;
if(ttt==120) //display 1 S, ckcon=0x01;
{
ttt=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。