⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds1302.c

📁 时钟:由于数码管是四位的
💻 C
字号:
//******************************************************************************
//  硬件连接:
//        MSP430 MCU                   DS1302
//    ------------------         ------------------
//    |      P15       | ------> |     SCLK       |
//    |      P17       | ------> |      RST       |
//    |      P16       | ------> |      SDI       |
//    ------------------         ------------------
//        MSP430 MCU                   KEYS
//     ------------------         ------------------
//  注意:由于数码管是四位的,所以只能显示小时和分钟,要显示年份星期等其它信息,
//        在convertion函数进行改动即可
//******************************************************************************
#include <msp430x14x.h>
//******************************************************************************
//宏定义
#define DS1302_DIR   P1DIR
#define DS1302_IN    P1IN
#define DS1302_OUT   P1OUT

#define DS1302_RST   BIT7
#define DS1302_SDI   BIT6
#define DS1302_SCLK  BIT5

#define DS1302_RST_LO   DS1302_OUT &= ~DS1302_RST
#define DS1302_RST_HI   DS1302_OUT |= DS1302_RST
#define DS1302_SCLK_LO  DS1302_OUT &= ~DS1302_SCLK
#define DS1302_SCLK_HI  DS1302_OUT |= DS1302_SCLK
#define DS1302_SDI_LO   DS1302_OUT &= ~DS1302_SDI
#define DS1302_SDI_HI   DS1302_OUT |= DS1302_SDI
//函数声明
void DS1302_Delay(unsigned int dtime);
void DS1302_Reset(void);
void DS1302_WriteOneByte(unsigned char w_dat);
void DS1302_WriteData(unsigned char addr,unsigned char w_dat);
void DS1302_SettingData(void);
void DS1302_GetData(unsigned char *str);
unsigned char DS1302_ReadOneByte(void);
unsigned char DS1302_ReadData(unsigned char addr);
//全局变量定义
char seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};     //共阴数码管0~9段码
unsigned char Setting_Time[7]={ //bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
                       0x08,    //--------十位-------|-------个位--------|年份(当前07年)
                       0x01,    //                        |-----个位-----|星期(当前周日)
                       0x04,    //              |十位|-------个位--------|月份(当前07月)
                       0x15,    //         |--十位---|-------个位--------|日期(当前01号)
                       0x10,    //-12H|    |--十位---|-------个位--------|小时(当前18点)
                       0x16,    //    |-----十位-----|-------个位--------|分钟(当前20分)
                       0x16     //    |-----十位-----|-------个位--------|秒钟(当前30秒)
};
unsigned char ReadingData[7];  //读出来的数据,同SettingData定义与格式
unsigned char gettime[7];
unsigned char convtime[4];
//******************************************************************************
//延时函数
void DS1302_Delay(unsigned int dtime) 
{
  unsigned int temp;
  for(temp = 0 ;temp < dtime; temp++);
}
//******************************************************************************
//DS1302复位函数
void DS1302_Reset(void) 
{
  DS1302_DIR |= (DS1302_RST + DS1302_SCLK);
  DS1302_SCLK_LO;
  DS1302_RST_LO;
  DS1302_Delay(10);
  DS1302_SCLK_HI;
}
//******************************************************************************
//向DS1302写入一个字节
void DS1302_WriteOneByte(unsigned char w_dat)
{
  unsigned char temp;
  DS1302_RST_HI;
  DS1302_DIR |= DS1302_SDI;
  for(temp=8;temp>0;temp--)
  {
    DS1302_SDI_LO;
    if(w_dat&BIT0)
      DS1302_SDI_HI;
    DS1302_SCLK_LO;
    DS1302_Delay(10);
    DS1302_SCLK_HI;
    DS1302_Delay(10);
    w_dat >>=1;
  } 
}
//******************************************************************************
//从DS1302中读取一个字节
unsigned char DS1302_ReadOneByte(void)
{ 
  unsigned char temp,rdata;
  rdata = 0x00;
  DS1302_RST_HI;
  DS1302_DIR &= ~DS1302_SDI;
  for(temp = 0; temp < 7; temp++)
  {
    DS1302_SCLK_HI;
    DS1302_Delay(10);
    DS1302_SCLK_LO;
    DS1302_Delay(10);
    if((DS1302_IN&DS1302_SDI)==DS1302_SDI)
      rdata |= BIT7;
    rdata >>= 1;
  }
  return(rdata); 
}
//******************************************************************************
//向DS1302中写入地址后写入数据
void DS1302_WriteData(unsigned char addr,unsigned char w_dat)
{
  DS1302_RST_LO;
  DS1302_SCLK_LO;
  DS1302_RST_HI;
  DS1302_WriteOneByte(addr);   //写入地址
  DS1302_WriteOneByte(w_dat);  //写入数据
  DS1302_SCLK_HI;
  DS1302_RST_LO;
} 
//******************************************************************************
//向DS1302写入地址后,从DS1302中读取数据
unsigned char DS1302_ReadData(unsigned char addr) 
{
  unsigned char r_dat;
  DS1302_RST_LO;
  DS1302_SCLK_LO;
  DS1302_RST_HI;
  DS1302_WriteOneByte(addr);     //写入地址
  r_dat = DS1302_ReadOneByte();  //读出数据
  DS1302_SCLK_LO;
  DS1302_RST_LO;
  return(r_dat);
}
//******************************************************************************
//按照SettingData的设置设置DS1302的时间
void DS1302_SettingData(void) 
{
  unsigned char temp;
  unsigned char addr = 0x8c;
  DS1302_WriteData(0x8e,0x00);  //写入控制命令,禁用写保护
  for(temp = 0 ;temp < 7; temp++)
  {
    DS1302_WriteData(addr,Setting_Time[temp]);
    addr -= 2;
  }
  DS1302_WriteData(0x8e,0x80);  //写入控制命令,启用写保护
}
//******************************************************************************
//读取DS1302时间到ReadingData中
void DS1302_GetData(unsigned char *str)
{
  unsigned char temp;
  unsigned char addr = 0x8d;
  for(temp = 0; temp < 7; temp++)
  {
    str[temp] = DS1302_ReadData(addr);//年份 星期 月份 日期 小时 分钟 秒钟
    addr -= 2;
 }
}
//******************************************************************************
//将BCD码转换成为十六进制码
void convertion(unsigned char time[])
{
  convtime[0] = (time[4] & 0xf0) >> 4;
  convtime[1] = time[4] & 0x0f;
  convtime[2] = (time[5] & 0xf0) >>4;
  convtime[3] = time[5] & 0x0f;
}
//******************************************************************************
//将数据在数码管上显示
void leddisplay(void)
{  
  unsigned int j,k;
  unsigned char i,npos=0xfe;
  P2DIR |= 0xff;
  P3DIR |= 0xff;
  for(k = 0; k < 10; k++)
  {
    for(i = 0; i < 4; i++)
    {
      P2OUT = npos;
      npos = (npos<<1) + 1;
      P3OUT = seg[convtime[i]];
      for(j = 0; j < 100; j++);    
    }
    npos=0xfe;
  }
}
//******************************************************************************
//主函数
void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;
  DS1302_Reset();
  DS1302_SettingData();
  while(1)
  {
    DS1302_GetData(gettime);
    convertion(gettime);
    leddisplay();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -