ds1302-MAX7219时钟

   
//DS1302 RTC drive program
//for 51 mcu with max7219 display
//designed by zhaoliang
//2005-6-16 18:27
#include "reg52.h"            /*头文件*/
#include "stdio.h"        
#include "intrins.h"
// common PreDefinition 
#define  HIGH     1
#define  LOW      0
#define  TRUE      1
#define  ZERO      0 
#define  MSB       0x80
#define  LSB       0x01
// max7219 PreDefinitiont
#define  DECODE_MODE   0x09 
#define  INTENSITY     0x0A 
#define  SCAN_LIMIT    0x0B 
#define  SHUT_DOWN     0x0C 
#define  DISPLAY_TEST  0x0F 
// ds1302 PreDefinition
#define  DS1302_WP    0x8E
#define  DS1302_RESET   SCL="LOW";RST="HIGH"
#define  DS1302_WP_ENABLE  Write_Ds1302(DS1302_WP,0X00)
#define  DS1302_WP_DISENABLE  Write_Ds1302(DS1302_WP,0x80)
//pin defined
/***********************************************************************/
//change this part at different board
sbit LOAD=P2^7; //MAX7219    Load-Data Input:    rising edge  pin 12 
sbit DIN=P2^5; //MAX7219    Serial-Data Input:   rising edge  pin 1
sbit CLK=P2^6; //MAX7219   Serial-Clock Input:  maximum 10MHz  pin 13
sbit SCL = P1^5;// DS1302 Serial-Clock Input  pin 7
sbit SDA = P1^6;// DS1302 Serial-Data  Input  pin 6
sbit RST = P1^7;// DS1302 Chip-Seclet  Input  pin 5
//function define
/***********************************************************************/
void Write_Max7219_byte(unsigned char temp);//write max7219 a byte
void Write_Max7219(unsigned char address,unsigned char dat);//write max7219 command and data
void Init_Max7219(void);//Initize max7219
void Write_Ds1302_byte(unsigned char temp); 
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void Read_RTC(void);//read RTC 
void Set_RTC(void);//set RTC 
void Display(void);
void Initial(void);
code unsigned char set_rtc_code[4]={0x00,0x23,0x07,0x17};
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; 
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned char read_rtc_code[4]={0};
/****************************************************************************/
/****************************************************************************/
void main (void)
{
 Initial ();
 while (1)
 {
  Read_RTC();
  Display ();  
 }
}
/****************************************************************************/
/****************************************************************************/
void Initial(void)      //MAX7219初始化

 Init_Max7219();
 DS1302_WP_ENABLE;
 Set_RTC();
 DS1302_WP_DISENABLE;
}
/****************************************************************************/
/***********************************************************************/
void Write_Max7219_byte(unsigned char temp)
{
 unsigned char i;
 for (i=0;i<8;i++)     
  { 
   CLK=LOW;
     DIN=temp&MSB;      
     temp<<=1;  
     CLK=HIGH;
   }
}
/***********************************************************************/
void Write_Max7219(unsigned char address,unsigned char dat)

 LOAD=LOW;
   Write_Max7219_byte(address); 
   Write_Max7219_byte(dat);
  LOAD=HIGH;                 
}
/***********************************************************************/
void Init_Max7219(void)      

 Write_Max7219(SHUT_DOWN, 0x01);     //Normal Operation XXXXXXX1 Shutdown Mode   XXXXXXXX0
 Write_Max7219(DISPLAY_TEST, 0x00);     //Normal Operation XXXXXXX0 Display Test Mode XXXXXXXX1
 Write_Max7219(DECODE_MODE, 0xff);     //Decode Mode Select D7~D0 1 B decode 0 No decode 
 Write_Max7219(SCAN_LIMIT, 0x07);     //SCAN LIMIT 0~7 0xX0~0xX7
 Write_Max7219(INTENSITY, 0x04);     //Set Intensity   0xX0~0xXf
}
/****************************************************************************/
/****************************************************************************/
void Write_Ds1302_Byte(unsigned  char temp) //写一字节数据 
{
 unsigned char i;
 for (i=0;i<8;i++)     
  { 
   SCL=LOW;
     SDA=temp&LSB;      
     temp>>=1;  
     SCL=HIGH;
   }
}   
/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )     
{
SCL=0;RST=1;
 Write_Ds1302_Byte(address);
 Write_Ds1302_Byte(dat);
 RST=LOW;  
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address ) //读DS1302数据函数
{
 unsigned char i,temp= 0x00,temp_temp;
 SCL=0;RST=1;
 Write_Ds1302_Byte(address);         //先写入被读寄存器地址 ,多字节方式 
 for (i=0;i<8;i++) 
 {
  if(SDA)
   temp|=0x80;
  SCL=LOW;
   temp>>=1;
  SCL=HIGH;
 } 
 RST=LOW;
 temp_temp=temp/16;
 temp=temp%16;
 temp=temp+temp_temp*10;
 return (temp);
}
/****************************************************************************/
void Read_RTC(void) //读DS1302某地址的数据 存入数组 
{
 unsigned char i,*p;
 p=read_rtc_address;    //指针指向读操作 寄存器的地址 
 for(i=0;i<4;i++)
 {
  read_rtc_code =Read_Ds1302(*p);      //依次读 秒,分,时 ,日期 ,存储到数组里 
  p++;
 }
}
/***********************************************************************/
void Set_RTC(void)         //设置初始时间 23号 7点23分00秒
{
 unsigned char i,*p;
 p=write_rtc_address;
 for(i=0;i<4;i++)
 {
  Write_Ds1302(*p,set_rtc_code);
  p++;  
 }
}  
/****************************************************************************/
void Display(void)      
{
 Write_Max7219(1,read_rtc_code[2]/10);  
 Write_Max7219(2,read_rtc_code[2]%10);   
 Write_Max7219(3,read_rtc_code[1]/10);
 Write_Max7219(4,read_rtc_code[1]%10);     
 Write_Max7219(5,read_rtc_code[0]/10);
 Write_Max7219(6,read_rtc_code[0]%10);    
 Write_Max7219(7,0x01);//no decode mode 
 Write_Max7219(8,read_rtc_code[3]%10);//display '-     
  
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值