找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3878|回复: 1
打印 上一主题 下一主题
收起左侧

msp430g2331 ds1302时钟模块

[复制链接]
跳转到指定楼层
楼主
     手上有块ds1302时钟模块,也有些数码管,于是试着做了个时钟,当然因为是手工,所以没有淘宝上买的好看,当然自己做的成本也高,所以无法相比,只是作为一种学习方式。做着送人了,所以照片只有一张,也没有显示。
        这是我用手机拍的,只有一张,没有多拍几张,样子太丑了,没有淘宝上做的美观,被同学批了一顿。
  程序很简单,不是很占用空间,io资源,所以只用了msp430g2231,我后来想用xin,xout做io口时,发现不行,所以就只用了8个io口。
数码管直接显示年月日小时,分钟,秒,星期,每30秒刷新一次。用hc164做片选数码管断码显示芯片。

部分程序预览:
  1. #include"io430.h"
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. #define ulong unsigned long
  5. #define CLK0  P1OUT&=~BIT6
  6. #define CLK1   P1OUT|=BIT6
  7. #define CPU_F ((double)1000000)   //外部高频晶振16MHZ
  8. //#define CPU_F ((double)32768)   //外部低频晶振32.768KHZ
  9. #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
  10. #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))


  11. unsigned char const code_hex[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x39,0x79,0x0e,0x3e,0x00};//0,1,2,3,4,5,6,7,8,9,c,e,j,u, ,
  12. uchar t,t1,t2,t3;
  13. uchar dis_play[4];


  14. //数码管显示函数

  15. void extern_16m()
  16. {
  17.   WDTCTL = WDTPW + WDTHOLD;
  18.     if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF)
  19.         {
  20.                 while(1);                // If calibration constants erased, trap CPU!!
  21.         }
  22. DCOCTL |= DCO0 + DCO1+DCO2;                                                                   //SMCLK选择LFXT1CLK
  23. // BCSCTL2 |= SELM_0;//MCLK采用1M的内部DCO   
  24. // BCSCTL2 |= DIVS_0;//SMCLK采用内部的时钟


  25. }

  26. void hc164_init()
  27. {
  28.   P1DIR |=BIT6+BIT7+BIT0+BIT1+BIT2+BIT3;                            // P1.0 output
  29.   
  30. }

  31. void sendbyte(uchar byte)
  32. {            
  33. uchar c,num;   
  34. num=byte;
  35. for(c=0;c<8;c++)        
  36. {     
  37. P1OUT&=~0x80;
  38. CLK0;   
  39. P1OUT|=num&0x80; //(0x80即十进制的128, 二进制的10000000 按位发送
  40. CLK1;         
  41. num<<=1;   
  42. }
  43. }


  44. void send_char(uchar weizhi,uchar byte)
  45. {
  46.   
  47.   P1OUT |= 0x0f;   
  48.   uchar c,send_byte;
  49.   send_byte=code_hex[byte];
  50. //  if((weizhi==0)&&(xianshi_flag==0))
  51. //   send_byte|=0x80;
  52.   sendbyte(send_byte);
  53.   c=weizhi&0x03;
  54.   P1OUT&=~(1<<c);
  55. }



  56. void init_TA()
  57. {

  58.   TACCR0 = 1000;                            //8ms中断一次
  59.   TACTL = TASSEL_2 + MC_1+ID_3;              // SMCLK, upmode,8分频,
  60.   TACCTL0 = CCIE;                           // TACCR0 interrupt enabled
  61. }


  62. void shumaguan()
  63. {
  64.    ++t;
  65. if(t>=4)
  66.    t=0;
  67.   send_char(t,dis_play[t]);
  68. }




  69. ////时钟模块
  70. #define DS1302_DIR  P1DIR
  71. #define DS1302_IN    P1IN
  72. #define DS1302_OUT  P1OUT

  73. #define DS1302_RST  BIT7
  74. #define DS1302_SCLK  BIT4
  75. #define DS1302_SDI  BIT5  //定义MSP320的端口

  76. #define DS1302_RST_LO  DS1302_OUT &= ~DS1302_RST
  77. #define DS1302_RST_HI  DS1302_OUT |= DS1302_RST
  78. #define DS1302_SCLK_LO  DS1302_OUT &= ~DS1302_SCLK
  79. #define DS1302_SCLK_HI  DS1302_OUT |= DS1302_SCLK
  80. #define DS1302_SDI_LO  DS1302_OUT &= ~DS1302_SDI
  81. #define DS1302_SDI_HI  DS1302_OUT |= DS1302_SDI


  82. void DS1302_Reset(void);
  83. void DS1302_WriteOneByte(unsigned char w_dat);
  84. void DS1302_WriteData(unsigned char addr,unsigned char w_dat);
  85. void DS1302_SettingData(void);
  86. void DS1302_GetData(unsigned char *str);
  87. unsigned char DS1302_ReadOneByte(void);
  88. unsigned char DS1302_ReadData(unsigned char addr);

  89. unsigned char Setting_Time[7]={ //bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
  90. 0x15,    //--------十位-------|-------个位--------|年份(当前07年)
  91. 0x04,    //                        |-----个位-----|星期(当前周日)
  92. 0x09,    //              |十位|-------个位--------|月份(当前07月)
  93. 0x10,    //         |--十位---|-------个位--------|日期(当前01号)
  94. 0x13,    //-12H|    |--十位---|-------个位--------|小时(当前18点)
  95. 0x00,    //    |-----十位-----|-------个位--------|分钟(当前20分)
  96. 0x21    //    |-----十位-----|-------个位--------|秒钟(当前30秒)
  97. };
  98. unsigned char ReadingData[7];  //读出来的数据,同SettingData定义与格式

  99. /****************************
  100. void main(void) {
  101. WDTCTL = WDTPW + WDTHOLD;
  102. DS1302_Reset();
  103. //DS1302_SettingData();
  104. while(1){
  105.   DS1302_GetData(ReadingData);
  106.   _NOP();
  107. };
  108. }
  109. ****************************/


  110. //DS1302复位
  111. void DS1302_Reset(void) {
  112. DS1302_DIR |=  DS1302_SCLK;

  113. DS1302_SCLK_LO;
  114. DS1302_RST_LO;
  115. // delay_us(10);
  116. DS1302_SCLK_HI;
  117. }

  118. //向DS1302写入一个字节
  119. void DS1302_WriteOneByte(unsigned char w_dat) {
  120. unsigned char temp;

  121. DS1302_RST_HI;
  122. DS1302_DIR |= DS1302_SDI;

  123. for(temp=8;temp>0;temp--) {
  124.   DS1302_SDI_LO;
  125.   if(w_dat&BIT0) DS1302_SDI_HI;
  126.   DS1302_SCLK_LO;
  127. //  delay_us(10);
  128.   DS1302_SCLK_HI;
  129. //  delay_us(10);
  130.   w_dat >>=1;
  131. }
  132. }

  133. //从DS1302中读取一个字节
  134. unsigned char DS1302_ReadOneByte(void) {
  135. unsigned char temp,rdata;
  136. rdata = 0x00;

  137. DS1302_RST_HI;
  138. DS1302_DIR &= ~DS1302_SDI;

  139. for(temp=0;temp<8;temp++){rdata >>= 1;//将移位放到前面,否则读不到最高一位
  140.   DS1302_SCLK_HI;
  141. // delay_us(10);
  142.   DS1302_SCLK_LO;
  143. //  delay_us(10);
  144.   if((DS1302_IN&DS1302_SDI)==DS1302_SDI)
  145.    rdata |= BIT7;
  146.   //rdata >>= 1;
  147. }

  148. return(rdata);
  149. }

  150. //向DS1302中写入地址后写入数据
  151. void DS1302_WriteData(unsigned char addr,unsigned char w_dat) {
  152. DS1302_RST_LO;
  153. DS1302_SCLK_LO;
  154. DS1302_RST_HI;

  155. DS1302_WriteOneByte(addr);  //写入地址
  156. DS1302_WriteOneByte(w_dat);  //写入数据

  157. DS1302_SCLK_HI;
  158. DS1302_RST_LO;
  159. }

  160. //向DS1302写入地址后,从DS1302中读取数据
  161. unsigned char DS1302_ReadData(unsigned char addr) {
  162. unsigned char r_dat;
  163. DS1302_RST_LO;
  164. DS1302_SCLK_LO;
  165. DS1302_RST_HI;

  166. DS1302_WriteOneByte(addr);  //写入地址
  167. r_dat = DS1302_ReadOneByte();  //读出数据

  168. DS1302_SCLK_LO;
  169. DS1302_RST_LO;

  170. return(r_dat);
  171. }

  172. //按照SettingData的设置设置DS1302的时间
  173. void DS1302_SettingData(void) {
  174. unsigned char temp;
  175. unsigned char addr = 0x8C;

  176. DS1302_WriteData(0x8E,0x00);  //写入控制命令,禁用写保护

  177. for(temp=0;temp<7;temp++) {
  178.   DS1302_WriteData(addr,Setting_Time[temp]);
  179.   addr -= 2;
  180. }

  181. DS1302_WriteData(0x8E,0x80);  //写入控制命令,启用写保护
  182. }

  183. //读取DS1302时间到ReadingData中
  184. void DS1302_GetData(unsigned char *str) {
  185. unsigned char temp;
  186. unsigned char addr = 0x8D;

  187. for(temp=0;temp<7;temp++) {
  188.   str[temp] = DS1302_ReadData(addr);//年
  189.   addr -= 2;
  190. }
  191. }

  192. void shumaguan_buff(void)
  193. {
  194.   if(++t3==30)
  195.   t3=0;
  196.   if(t3<5)t2=1;
  197.   else if(t3<10)t2=2;
  198.   else if(t3<20)t2=3;
  199.   else t2=4;
  200.   switch(t2)
  201.   {
  202.   case 1:dis_play[0]=2;dis_play[1]=0;dis_play[2]=ReadingData[0]/16;dis_play[3]=ReadingData[0]%16;break;                                       //显示年份
  203.   case 2:dis_play[0]=ReadingData[2]/16;dis_play[1]=ReadingData[2]%16;dis_play[2]=ReadingData[3]/16;dis_play[3]=ReadingData[3]%16;break;       //显示月份日期
  204.   case 3:dis_play[0]=ReadingData[4]/16;dis_play[1]=ReadingData[4]%16;dis_play[2]=ReadingData[5]/16;dis_play[3]=ReadingData[5]%16;break;       //显示时分
  205.   case 4:dis_play[0]=ReadingData[1]%16;dis_play[1]=14;dis_play[2]=ReadingData[6]/16;dis_play[3]=ReadingData[6]%16;break;                      //显示星期秒
  206.   default:break;
  207.   }
  208.   
  209. }


  210. #pragma vector=TIMER0_A0_VECTOR  
  211. __interrupt void TIMERA0_ISR() // the interrupt source is CC0
  212. {
  213.   ++t1;
  214.   if(t1>=125)
  215.     t1=0;
  216.   if(t1==0)
  217.   {
  218.     DS1302_GetData(ReadingData);
  219.     shumaguan_buff();
  220.   }  
  221. else
  222. shumaguan();
  223. }

复制代码



</c);

数码管-时钟.zip

177.62 KB, 下载次数: 20, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:117823 发表于 2016-6-4 02:13 来自手机 | 只看该作者
ds1302电源问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表