1302吧 关注:50贴子:477
  • 1回复贴,共1

闹钟C程序

只看楼主收藏回复

这是我1302的程序,麻烦看下。谢谢!
#include<reg52.h>
sbit DS1302_CLK=P3^0;
sbit DS1302_IO=P3^1;
sbit DS1302_RST=P3^2;
unsigned char fy=0;
unsigned char tj=0;
sbit tjxz = P3^5;
sbit fanyel=P3^4;
sbit fanyer=P3^3;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit qian=P2^4;
sbit bai=P2^5;
sbit shi=P2^6;
sbit ge=P2^7;
bit flag;
sfr smdis = 0x80;
sfr p2m1 = 0x95;
sfr p2m0 = 0x96;
sfr p1m1 = 0x91;
sfr p1m0 = 0x92;
code unsigned char seg1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//???????
code unsigned char seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //????
unsigned char clk_time[8]={0x59,0x58,0x8,0x9,0x12,0x03,0x09,0x20}; //?,?,?,?,?,?,?
/******************DS1302:????(???)*********************/
/*void DS1302InputByte(unsigned char da)
{
unsigned char i;
ACC=da;
for(i=8;i>0;i--)
{
DS1302_IO=ACC0;
DS1302_CLK=0;
DS1302_CLK=1;
ACC=ACC>>1;
}
}
/******************DS1302:????(???)*****************/
/*unsigned char DS1302OutputByte(void)
{
unsigned char i;
for(i=0;i<8;i++)
{
ACC=ACC>>1;
DS1302_CLK = 1;
DS1302_CLK = 0;
ACC7 = DS1302_IO;
}
return(ACC);
}
*/
void DS1302InputByte(unsigned char d) //?????????(????)
{
unsigned char i,j;
EA=0;
for(i=8; i>0; i--)
{
DS1302_CLK = 0;
for(j=2;j>0;j--);
if(d&0x01)
DS1302_IO = 1;
else DS1302_IO = 0;
for(j=2;j>0;j--);
DS1302_CLK = 1;
d=d>>1;
}
EA=1;
}
unsigned char DS1302OutputByte(void) //?????????(????)
{
unsigned char i,j,d=0;
EA=0;
for(i=0; i<8; i++)
{
DS1302_IO = 1;
DS1302_CLK = 1;
for(j=2;j>0;j--);
DS1302_CLK = 0;
if( DS1302_IO)
d=d|(0x01<<i) ;
}
return(d);
EA=1;
}
void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302??, ucData: ?????
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); // ??,??
DS1302InputByte(ucDa); // ?1Byte??
DS1302_CLK = 1;
DS1302_RST = 0;
}
unsigned char Read1302(unsigned char ucAddr) //??DS1302??????
{
unsigned char ucData;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); // ??,??
ucData = DS1302OutputByte(); // ?1Byte??
DS1302_CLK = 1;
DS1302_RST = 0;
return(ucData);
}
void delayt(unsigned char t)//??
{
unsigned int i,j;
for(i=t;i>0;i--)
{



1楼2011-11-13 09:09回复
    for(j=0;j<500;j++);
    }
    }
    void delay(unsigned int a )//??
    {
    unsigned char i;
    while(a-- !=0)
    {
    for(i=0;i<125;i++);
    }
    }
    void led_disp1(unsigned char *poi)//????,?????????--??1
    {
    ge=0;
    P0=seg[*poi % 16];
    delay(2);
    ge=1;
    shi=0;
    P0=seg[*poi /16];
    delay(2);
    shi=1;
    poi++;
    bai=0;
    P0=0x0;
    delay(2);
    bai=1;
    qian=0;
    P0=0x0;
    delay(2);
    qian=1;
    }
    void led_disp2(unsigned char *poi)//????,?????????--??2
    {
    ge=0;
    P0=seg[*poi % 16];
    delay(2);
    ge=1;
    shi=0;
    P0=seg[*poi /16];
    delay(2);
    shi=1;
    poi++;
    bai=0;
    P0=seg1[*poi % 16];
    delay(2);
    bai=1;
    qian=0;
    P0=seg[*poi / 16];
    delay(2);
    qian=1;
    }
    void led_disp3(unsigned char *poi)//????,?????????--??2
    {
    ge=0;
    P0=seg[*poi % 16];
    delay(2);
    ge=1;
    shi=0;
    P0=seg[*poi /16];
    delay(2);
    shi=1;
    poi++;
    bai=0;
    P0=seg[*poi % 16];
    delay(2);
    bai=1;
    qian=0;
    P0=seg[*poi / 16];
    delay(2);
    qian=1;
    }
    void shixianfy()//?????
    {
    switch(fy)
    { case 4 : led_disp1(&clk_time[0]);
    break;
    case 3 : led_disp2(&clk_time[1]);
    break;
    case 2 : led_disp2(&clk_time[3]);
    break ;
    case 1 : led_disp1(&clk_time[5]);
    break;
    case 0 : led_disp3(&clk_time[6]);
    break;
    }
    }
    void kongzhify()//??????
    {
    if(fanyel==0)
    { delay(0);
    if(fanyel==0)
    {
    fy++;
    if(fy==5)
    fy=0;
    }
    }
    else
    {if(fanyer==0)
    {
    delay(10);
    if(fanyer==0)
    {
    fy--;
    if(fy==-1)
    fy=4;
    }
    }
    }
    }
    void main(void)
    {
    unsigned char temp=0x80;
    unsigned char i;
    //Write1302(0x80,0x80);Write1302(0x80,0x00); //WP=0 ???
    for(i=0;i<7;i++)
    {
    Write1302(temp,clk_time[i]);
    temp+=2;
    }
    //WP=1 ???
    while(1)
    {
    while(fanyer==0 );
    { kongzhify();}
    while(fanyel==0 );
    {kongzhify();}
    temp=0x81;
    for(i=0;i<7;i++)
    { //clk_time[i]=0x0;
    clk_time[i]=Read1302(temp);
    temp+=2;shixianfy();
    }
    }
    }
    


    2楼2011-11-13 09:09
    回复