高级会员
 
主题
帖子
积分2663
阅读权限30
注册时间2014-5-10
最后登录1970-1-1
在线时间 小时
|

楼主 |
发表于 2014-5-16 22:05
|
显示全部楼层
#include "stc15f2k60s2.h"
//包含画点、画直线、画矩形、画圆函数等,非常实用。
//#include <w78e052.h>
#include <intrins.h>
#include "ASCII.h"
#define uchar unsigned char
#define uint unsigned int
#define NOP _nop_()
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned char BYTE;
typedef unsigned char u32;
unsigned char PLAYBUF[64];
/*sbit OE = P3^2;
sbit A1 = P3^4;
sbit B1 = P3^6;
sbit SHCP=P4^0; //11脚SHCP 移位时钟
sbit STCP=P3^3; //12脚STCP 锁存时钟
sbit DATA=P3^5; //数据输入*/
sbit OE = P1^1;
sbit A1 = P1^0;
sbit B1 = P1^2;
sbit SHCP=P1^3; //11脚SHCP 移位时钟 SCL
sbit STCP=P1^4; //12脚STCP 锁存时钟 RCK
sbit DATA=P1^5; //数据输入 14 SI
sbit IR=P5^5;
sbit SW1=P3^3;
sbit SW2=P3^2;
sbit LED=P3^0;
sbit SCLK = P3^1; //DS1302时钟口7
sbit IO = P3^6; //DS1302数据口6
sbit RST = P3^7; //DS1302片选口5
unsigned char yw,zs;
unsigned char yw1,zs1;
unsigned char hh,mm;
unsigned char nian,yue,ri;
unsigned int sjjs,ss; //
u16 time;
unsigned char fy;
bit fp;
BYTE code init[] = {0x00, 0x00, 0x20, 0x01, 0x01, 0x05, 0x10};
BYTE data now[7];
void DS1302_Initial();
void DS1302_SetTime(BYTE *p);
void DS1302_GetTime(BYTE *p);
/*******************************************************************************************/
//数据串行输入
void serial_input(uchar dat)
{
uchar m;
for(m=0;m<8;m++)
{
if(dat & 0x80)
DATA=0;
else
DATA=1;
SHCP=0;
SHCP=1;
NOP;
NOP;
SHCP=0;
NOP;
NOP;
dat=dat<<1;
}
}
void delay()
{
unsigned int i = 200;
while(i--);
}
void delay2()
{
unsigned int i = 10000;
while(i--);
}
/*******************************************************************************************/
//并出
void serial_output()
{
STCP=0;
STCP=1;
}
/*******************************************************************************************/
/*******************************************************************************************/
//行显控制
void HC138_scan(uchar temp)
{
A1=0x01 & temp;
B1=0x01 &(temp>>1);
OE=1;
}
/*******************************************************************************************/
/*******************************************************************************************/
//整屏显示
void display()
{
uchar i,j,k;
unsigned char buf;
for(k=0;k<4;k++) //显示的四行
{
for(j=0;j<2;j++) //显示3、4列
{
for(i=0;i<2;i++) // 显示1、2列
{
if(fp)
{
buf = PLAYBUF[24+(k<<1)+i+(j<<5)];
serial_input(buf);
serial_input(0x00);
serial_input(0x00);
buf = PLAYBUF[(k<<1)+i+(j<<5)];
serial_input(buf);
}
else
{
serial_input(0x00);
buf = PLAYBUF[16+(k<<1)+i+(j<<5)];
serial_input(buf);
buf = PLAYBUF[8+(k<<1)+i+(j<<5)];
serial_input(buf);
serial_input(0x00);
}
}
}
serial_output();
HC138_scan(k);
delay();
OE=0;
}
}
/*void hzxs1()//字个数 起始位置
{
u8 i;
for (i=0;i<16;i++)
{
PLAYBUF[i*2] = (hz[zs*16+i] << yw) | (hz[(zs+1)*16+i] >> (8-yw));
PLAYBUF[(i*2)+1] = (hz[(zs+1)*16+i] << yw) | (hz[(zs+2)*16+i] >> (8-yw));
}
for (i=0;i<16;i++)
{
PLAYBUF[i*2+32] = (hz[(zs+2)*16+i] << yw) | (hz[(zs+3)*16+i] >> (8-yw));
PLAYBUF[(i*2)+1+32] = (hz[(zs+3)*16+i] << yw) | (hz[(zs+4)*16+i] >> (8-yw));
}
}*/
void timeload() //时间加载
{
u8 i;
for (i=0;i<10;i++)
{
PLAYBUF[i*2] = F10X6[(hh/10)*10+i]>>1;
PLAYBUF[(i*2)+1] = (F10X6[(hh%10)*10+i]) | (F10X6[10*10+i] >> 5);
PLAYBUF[i*2+32] = (F10X6[10*10+i] << 3) | (F10X6[(mm/10)*10+i] >> 2);
PLAYBUF[(i*2)+33] = F10X6[(mm%10)*10+i] >> 1;
}
}
/*void timeload2() //时间加载
{
u8 i;
for (i=0;i<7;i++)
{
PLAYBUF[i*2] = szzt[(hh/10)*7+i]>>1;
PLAYBUF[(i*2)+1] = (szzt[(hh%10)*7+i]) | (szzt[10*7+i] >> 5);
}
PLAYBUF[14]=0x00;
PLAYBUF[15]=0x00;
for (i=0;i<7;i++)
{
PLAYBUF[i*2+32] = (szzt[10*7+i] << 3) | (szzt[(mm/10)*7+i] >> 2);
PLAYBUF[(i*2)+33] = szzt[(mm%10)*7+i] >> 1;
}
PLAYBUF[46]=0x00;
PLAYBUF[47]=0x00;
}*/
void rqload() //日期加载
{
u8 i;
for (i=0;i<6;i++)
{
PLAYBUF[18+i*2] = F6X6[2*6+i]>>2;
PLAYBUF[19+i*2] = F6X6[i]>>1;
PLAYBUF[50+i*2] = F6X6[(nian/10)*6+i] | (F6X6[(nian%10)*6+i]>>7);
PLAYBUF[51+i*2] = F6X6[(nian%10)*6+i]<<1;
}
}
/*void rqload2() //日期加载
{
u8 i;
for (i=0;i<7;i++)
{
PLAYBUF[18+i*2] = szzt[2*7+i]>>2;
PLAYBUF[19+i*2] = szzt[i]>>1;
}
PLAYBUF[16]=0x00;
PLAYBUF[17]=0x00;
for (i=0;i<7;i++)
{
PLAYBUF[50+i*2] = szzt[(nian/10)*7+i] | (szzt[(nian%10)*7+i]>>7);
PLAYBUF[51+i*2] = szzt[(nian%10)*7+i]<<1;
}
PLAYBUF[48]=0x00;
PLAYBUF[49]=0x00;
}*/
/*void rqsy() //日期上移
{
u8 i;
if(yw1==1)
{
PLAYBUF[30]=0x00;
PLAYBUF[31]=0x00;
PLAYBUF[62]=0x00;
PLAYBUF[63]=0x00;
}
if(yw1==2)
{
PLAYBUF[30]=0x00;
PLAYBUF[31]=0x00;
PLAYBUF[62]=0x00;
PLAYBUF[63]=0x00;
}
for (i=0;i<6;i++)
{
PLAYBUF[18+i*2] = PLAYBUF[20+i*2];
PLAYBUF[19+i*2] = PLAYBUF[21+i*2];
}
for (i=0;i<6;i++)
{
PLAYBUF[50+i*2] = PLAYBUF[52+i*2];
PLAYBUF[51+i*2] = PLAYBUF[53+i*2];
}
if(yw1==0)
{
PLAYBUF[30]=0x00;
PLAYBUF[31]=0x00;
PLAYBUF[62]=0x00;
PLAYBUF[63]=0x00;
}
if(yw1>2)
{
PLAYBUF[30]=(szzt[(yue/10)*7+yw1-3]) | (szzt[(yue%10)*7+yw1-3]>>7);
PLAYBUF[31]=(szzt[(yue%10)*7+yw1-3]<<1) | (szzt[11*7+yw1-3]>>6);
PLAYBUF[62]=(szzt[11*7+yw1-3]<<2) | (szzt[(ri/10)*7+yw1-3]>>3);
PLAYBUF[63]=(szzt[(ri/10)*7+yw1-3]<<5) | (szzt[(ri%10)*7+yw1-3]>>2);
}
}*/
void rqsy2() //日期上移
{
u8 i;
for (i=0;i<6;i++)
{
PLAYBUF[20+i*2] = PLAYBUF[22+i*2];
PLAYBUF[21+i*2] = PLAYBUF[23+i*2];
PLAYBUF[52+i*2] = PLAYBUF[54+i*2];
PLAYBUF[53+i*2] = PLAYBUF[55+i*2];
}
if(yw1<2)
{
PLAYBUF[30]=0x00;
PLAYBUF[31]=0x00;
PLAYBUF[62]=0x00;
PLAYBUF[63]=0x00;
}
if(yw1>1)
{
PLAYBUF[30]=(F6X6[(yue/10)*7+yw1-3]) | (F6X6[(yue%10)*7+yw1-3]>>7);
PLAYBUF[31]=(F6X6[(yue%10)*7+yw1-3]<<1) | (F6X6[11*7+yw1-3]>>6);
PLAYBUF[62]=(F6X6[11*7+yw1-3]<<2) | (F6X6[(ri/10)*7+yw1-3]>>3);
PLAYBUF[63]=(F6X6[(ri/10)*7+yw1-3]<<5) | (F6X6[(ri%10)*7+yw1-3]>>2);
}
}
/*void ggxg1() //从左往右一条竖线
{
uchar s;
for(s=0;s<16;s++)
{
PLAYBUF[zs*16+i*2]
}
}*/
/*******************************************************************************************/
/*******************************************************************************************/
void main()
{
unsigned int js;
bit tl;//hzbz,hzqh;
P3=0x00;
P4=0x00;
OE=0;
delay2();
hh=13;
mm=56;
nian=14;
yue=3;
ri=26;
zs=0;
// hzbz=0;
time=0;
timeload();
rqload();
yw=0;
DS1302_Initial(); //初始化DS1302
DS1302_SetTime(init); //设置初始时间
DS1302_GetTime(now); //读取当前时间
while(1)
{
time++;
sjjs++;
ss++;
if(ss>1000)
{
ss=0;
mm++;
if(mm>59)
{
mm=0;
hh++;
if(hh>23)
hh=0;
}
//if(hzbz==0)timeload2();
}
display();
fp=~fp;
/*if(hzbz)
{
if(sjjs>40)
{
sjjs=0;
yw++;
if(yw>7)
{
yw=0;
zs++;
if(zs>100){zs=0;hzbz=0;yw=0;tl=0;yw1=0;tl=0;fy=0;time=0;hzqh=0;}
}
hzxs1();
}
}
else
{*/
if(time>2000)
{
time=0;
fy++;
if(fy>5){yw=0;}
}
if(fy==0)
{
timeload();
rqload();
}
if(fy==1)
{
timeload();
if(tl==0)
{
if(++js>20)
{
js=0;
tl=0;
rqsy2();
yw1++;
if(yw1>8)
{
yw1=0;
tl=1;
}
}
}
}
//}
}
}
/*延时X微秒(STC12C5A60S2@12M)
不同的工作环境,需要调整此函数
此延时函数是使用1T的指令周期进行计算,与传统的12T的MCU不同
**************************************/
void Delay1302()
{
_nop_();
_nop_();
}
/**************************************
从DS1302读1字节数据
**************************************/
BYTE DS1302_ReadByte()
{
BYTE i;
BYTE dat = 0;
for (i=0; i<8; i++) //8位计数器
{
SCLK = 0; //时钟线拉低
Delay1302(); //延时等待
dat >>= 1; //数据右移一位
if (IO) dat |= 0x80; //读取数据
SCLK = 1; //时钟线拉高
Delay1302(); //延时等待
}
return dat;
}
/**************************************
向DS1302写1字节数据
**************************************/
void DS1302_WriteByte(BYTE dat)
{
char i;
for (i=0; i<8; i++) //8位计数器
{
SCLK = 0; //时钟线拉低
Delay1302(); //延时等待
dat >>= 1; //移出数据
IO = CY; //送出到端口
SCLK = 1; //时钟线拉高
Delay1302(); //延时等待
}
}
/**************************************
读DS1302某地址的的数据
**************************************/
BYTE DS1302_ReadData(BYTE addr)
{
BYTE dat;
RST = 0;
Delay1302();
SCLK = 0;
Delay1302();
RST = 1;
Delay1302();
DS1302_WriteByte(addr); //写地址
dat = DS1302_ReadByte(); //读数据
SCLK = 1;
RST = 0;
return dat;
}
/**************************************
往DS1302的某个地址写入数据
**************************************/
void DS1302_WriteData(BYTE addr, BYTE dat)
{
RST = 0;
Delay1302();
SCLK = 0;
Delay1302();
RST = 1;
Delay1302();
DS1302_WriteByte(addr); //写地址
DS1302_WriteByte(dat); //写数据
SCLK = 1;
RST = 0;
}
/**************************************
写入初始时间
**************************************/
void DS1302_SetTime(BYTE *p)
{
BYTE addr = 0x80;
BYTE n = 7;
DS1302_WriteData(0x8e, 0x00); //允许写操作
while (n--)
{
DS1302_WriteData(addr, *p++);
addr += 2;
}
DS1302_WriteData(0x8e, 0x80); //写保护
}
/**************************************
读取当前时间
**************************************/
void DS1302_GetTime(BYTE *p)
{
BYTE addr = 0x81;
BYTE n = 7;
while (n--)
{
*p++ = DS1302_ReadData(addr);
addr += 2;
}
}
/**************************************
初始化DS1302
**************************************/
void DS1302_Initial()
{
RST = 0;
SCLK = 0;
DS1302_WriteData(0x8e, 0x00); //允许写操作
DS1302_WriteData(0x80, 0x00); //时钟启动
DS1302_WriteData(0x90, 0xa6); //一个二极管+4K电阻充电
DS1302_WriteData(0x8e, 0x80); //写保护
} |
|