找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2771|回复: 1
收起左侧

MSP430f249读取DS1302时钟显示在1602和串口附仿真

[复制链接]
ID:732506 发表于 2020-5-12 06:26 | 显示全部楼层 |阅读模式
IAR写的MSP430f249读取DS1302时钟显示在1602和串口附仿真.
51hei.png

单片机源程序如下:
  1. #include  <msp430.h>
  2. #include  "cry1602.h"
  3. #include  "cry1602.c"
  4. #include  "DS1302.c"


  5. unsigned char counter=0;
  6. unsigned char second=0;
  7. unsigned char minute=0;
  8. unsigned char hour=0;
  9. //signed char miao=10;
  10. struct sTime bufTime; //time buffer

  11. /*write 1 bcd byte on screen,(x,y)-position on screen, bcd-BCD byte to display*/
  12. void Show2DigitNumber(unsigned char x, unsigned char y,unsigned char number)
  13. {
  14.         Disp1Char(x,y,(number/10+0x30  ));
  15.         Disp1Char(x+1,y,(number%10 +0x30 ));
  16.         
  17. }




  18. int main( void )
  19. {
  20.   
  21.   WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  22.   
  23.   if (CALBC1_1MHZ==0xFF)                                        // If calibration constant erased
  24.   {                                                                                       
  25.     while(1);                               // do not load, trap CPU!!       
  26.   }
  27.   DCOCTL = 0;                               // Select lowest DCOx and MODx settings
  28.   BCSCTL1 = CALBC1_1MHZ;                    // Set DCO
  29.   DCOCTL = CALDCO_1MHZ;
  30.   
  31.   P1DIR |= 0x01;                            // P1.0 output
  32.   CCTL0 = CCIE;                             // CCR0 interrupt enabled
  33.   CCR0 = 50000;
  34.   TACTL = TASSEL_2 + MC_2;                  // SMCLK, contmode
  35.   
  36.   //P6DIR &= ~BIT2;                           //P6.2 input
  37.   
  38.   P3SEL = 0x30;                             // P3.4,5 = USCI_A0 TXD/RXD
  39.   UCA0CTL1 |= UCSSEL_2;                     // SMCLK
  40.   UCA0BR0 = 104;                            // 1MHz 9600; (104)decimal = 0x068h
  41.   UCA0BR1 = 0;                              // 1MHz 9600
  42.   UCA0MCTL = UCBRS0;                        // Modulation UCBRSx = 1
  43.   UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
  44.   IE2 |= UCA0RXIE;                          // Enable USCI_A0 RX interrupt


  45.   InitDS1302();
  46.   /*  
  47.   P1DIR |= BIT0;//p1.0输出模式
  48.   P1DIR |= BIT1;//p1.1输出模式
  49.   P1DIR |= BIT7;//p1.7输出模式
  50.   
  51.   P1IE |= BIT2;//使能p1.2中断
  52.   P1IES |= BIT2;//下降沿触发
  53.   P1IFG &= ~BIT2;//清除p1.2中断
  54.   
  55.   P1IE |= BIT3;//使能p1.3中断
  56.   P1IES |= BIT3;//下降沿触发
  57.   P1IFG &= ~BIT3;//清除p1.3中断
  58.   
  59.   //P2DIR = 0XFF;P2OUT = 0XFF;
  60.   //P6DIR = 0XFF;P6OUT = 0XFF;
  61.   */
  62.   LcdReset();
  63.   Delay5ms();
  64.   DispStr(0,0,"Serial Output:");
  65.   DispStr(0,1,"00:00:00");
  66.   GetRealTime(&bufTime);  //get current time
  67.      minute=bufTime.min;minute=(minute>>4)*10+(minute&0x0f);
  68.      hour=bufTime.hour;hour=(hour>>4)*10+(hour&0x0f);

  69.      Show2DigitNumber(3,1,minute);
  70.      Show2DigitNumber( 0,1,hour);
  71.      
  72.      second=DS1302SingleRead(0); second=(second>>4)*10+(second&0x0f);
  73.      Show2DigitNumber(6,1,second); //DispStr(8,1," ");
  74.   // __enable_interrupt();
  75.   
  76.   __bis_SR_register(LPM0_bits + GIE);       // Enter LPM0, interrupts enabled

  77. }

  78. /*
  79. #pragma vector = PORT1_VECTOR
  80. __interrupt void P1_Interrupt()//P1口中断函数
  81. {
  82.   if(P1IFG&BIT2)//如果P1.2有中断
  83.   {
  84.     P1IFG &= ~BIT2;//清除P1.2引脚中断标志位
  85.     P1OUT ^= BIT0;//P1.0引脚取反
  86.     P1OUT ^= BIT7;//P1.7
  87.     while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  88.     UCA0TXBUF = 97;  
  89.     while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  90.     UCA0TXBUF = 62;
  91.     //Disp1Char(0,1,97);
  92.     //Disp1Char(1,1,62);
  93.   }
  94.   
  95.   if(P1IFG&BIT3)//如果P1.3有中断
  96.   {
  97.     P1IFG &= ~BIT3;//清除P1.3引脚中断标志位
  98.     P1OUT ^= BIT1;//P1.1引脚取反
  99.     P1OUT ^= BIT7;//P1.7
  100.     while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  101.     UCA0TXBUF = 50;
  102.     //Disp1Char(0,1,50);
  103.     //Disp1Char(1,1,32);
  104.   }
  105. }
  106. */



  107. void Read_RTC(void)                //读取 日历
  108. {

  109. second=DS1302SingleRead(0); second=(second>>4)*10+(second&0x0f);
  110. minute=DS1302SingleRead(1); minute=(minute>>4)*10+(minute&0x0f);
  111. hour=DS1302SingleRead(2); hour=(hour>>4)*10+(hour&0x0f);
  112. }


  113. // Timer A0 interrupt service routine
  114. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  115. #pragma vector=TIMERA0_VECTOR
  116. __interrupt void Timer_A (void)
  117. #elif defined(__GNUC__)
  118. void __attribute__ ((interrupt(TIMERA0_VECTOR))) Timer_A (void)
  119. #else
  120. #error Compiler not supported!
  121. #endif
  122. {
  123.   counter++;
  124.   if (counter==20)
  125.   {
  126.   counter=0;
  127. /*second++;     

  128.   if (second==60)
  129.     {
  130.        second=0;
  131.        minute++;
  132.        if (minute==60)
  133.         {
  134.           minute=0;
  135.           hour++;
  136.           if (hour==24)
  137.           { hour=0;}
  138.           Show2DigitNumber(0,1,hour); //DispStr(2,1,":");
  139.         }
  140.         Show2DigitNumber(3,1,minute); //DispStr(5,1,":");
  141.       
  142.       }
  143.   */
  144. //Read_RTC();
  145.   P1OUT ^= 0x01;                            // Toggle P1.0
  146.   second=DS1302SingleRead(0); second=(second>>4)*10+(second&0x0f);
  147.    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  148.     UCA0TXBUF = hour/10+0x30;
  149.    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  150.     UCA0TXBUF = hour%10+0x30;
  151.    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  152.     UCA0TXBUF = 58;
  153.    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  154.     UCA0TXBUF = minute/10+0x30;
  155.    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  156.     UCA0TXBUF = minute%10+0x30;
  157.    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  158.     UCA0TXBUF = 58;
  159.    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  160.     UCA0TXBUF = second/10+0x30;
  161.    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  162.     UCA0TXBUF = second%10+0x30;
  163.    while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?
  164.     UCA0TXBUF = 13;
  165.    
  166.    
  167. // Show2DigitNumber(0,1,hour); //DispStr(2,1,":");  
  168. // Show2DigitNumber(3,1,minute); //DispStr(5,1,":");
  169.       
  170.      Show2DigitNumber(6,1,second); //DispStr(8,1," ");
  171.   //miao = DS1302SingleRead(0);
  172.   //miao=(miao>>4)*10+(miao&0x0f);
  173. //miao=DS1302SingleRead(0); miao=(miao>>4)*10+(miao&0x0f);
  174.   if (second  ==0)
  175.      {
  176.   
  177.      GetRealTime(&bufTime);  //get current time
  178.      minute=bufTime.min;minute=(minute>>4)*10+(minute&0x0f);
  179.      hour=bufTime.hour;hour=(hour>>4)*10+(hour&0x0f);

  180.      Show2DigitNumber(3,1,minute);
  181. ……………………

  182. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
msp430+1602+1302+serial.zip (35.75 KB, 下载次数: 81)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:476215 发表于 2022-4-16 18:34 | 显示全部楼层
最近正好要找一个DS1302的例子参考下谢谢了
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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