标题:
MSP430f249读取DS1302时钟显示在1602和串口附仿真
[打印本页]
作者:
hotpet
时间:
2020-5-12 06:26
标题:
MSP430f249读取DS1302时钟显示在1602和串口附仿真
IAR写的MSP430f249读取DS1302时钟显示在1602和串口附仿真.
51hei.png
(21.21 KB, 下载次数: 125)
下载附件
2020-5-12 14:28 上传
单片机源程序如下:
#include <msp430.h>
#include "cry1602.h"
#include "cry1602.c"
#include "DS1302.c"
unsigned char counter=0;
unsigned char second=0;
unsigned char minute=0;
unsigned char hour=0;
//signed char miao=10;
struct sTime bufTime; //time buffer
/*write 1 bcd byte on screen,(x,y)-position on screen, bcd-BCD byte to display*/
void Show2DigitNumber(unsigned char x, unsigned char y,unsigned char number)
{
Disp1Char(x,y,(number/10+0x30 ));
Disp1Char(x+1,y,(number%10 +0x30 ));
}
int main( void )
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
if (CALBC1_1MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
P1DIR |= 0x01; // P1.0 output
CCTL0 = CCIE; // CCR0 interrupt enabled
CCR0 = 50000;
TACTL = TASSEL_2 + MC_2; // SMCLK, contmode
//P6DIR &= ~BIT2; //P6.2 input
P3SEL = 0x30; // P3.4,5 = USCI_A0 TXD/RXD
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600; (104)decimal = 0x068h
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
InitDS1302();
/*
P1DIR |= BIT0;//p1.0输出模式
P1DIR |= BIT1;//p1.1输出模式
P1DIR |= BIT7;//p1.7输出模式
P1IE |= BIT2;//使能p1.2中断
P1IES |= BIT2;//下降沿触发
P1IFG &= ~BIT2;//清除p1.2中断
P1IE |= BIT3;//使能p1.3中断
P1IES |= BIT3;//下降沿触发
P1IFG &= ~BIT3;//清除p1.3中断
//P2DIR = 0XFF;P2OUT = 0XFF;
//P6DIR = 0XFF;P6OUT = 0XFF;
*/
LcdReset();
Delay5ms();
DispStr(0,0,"Serial Output:");
DispStr(0,1,"00:00:00");
GetRealTime(&bufTime); //get current time
minute=bufTime.min;minute=(minute>>4)*10+(minute&0x0f);
hour=bufTime.hour;hour=(hour>>4)*10+(hour&0x0f);
Show2DigitNumber(3,1,minute);
Show2DigitNumber( 0,1,hour);
second=DS1302SingleRead(0); second=(second>>4)*10+(second&0x0f);
Show2DigitNumber(6,1,second); //DispStr(8,1," ");
// __enable_interrupt();
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
}
/*
#pragma vector = PORT1_VECTOR
__interrupt void P1_Interrupt()//P1口中断函数
{
if(P1IFG&BIT2)//如果P1.2有中断
{
P1IFG &= ~BIT2;//清除P1.2引脚中断标志位
P1OUT ^= BIT0;//P1.0引脚取反
P1OUT ^= BIT7;//P1.7
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = 97;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = 62;
//Disp1Char(0,1,97);
//Disp1Char(1,1,62);
}
if(P1IFG&BIT3)//如果P1.3有中断
{
P1IFG &= ~BIT3;//清除P1.3引脚中断标志位
P1OUT ^= BIT1;//P1.1引脚取反
P1OUT ^= BIT7;//P1.7
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = 50;
//Disp1Char(0,1,50);
//Disp1Char(1,1,32);
}
}
*/
void Read_RTC(void) //读取 日历
{
second=DS1302SingleRead(0); second=(second>>4)*10+(second&0x0f);
minute=DS1302SingleRead(1); minute=(minute>>4)*10+(minute&0x0f);
hour=DS1302SingleRead(2); hour=(hour>>4)*10+(hour&0x0f);
}
// Timer A0 interrupt service routine
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(TIMERA0_VECTOR))) Timer_A (void)
#else
#error Compiler not supported!
#endif
{
counter++;
if (counter==20)
{
counter=0;
/*second++;
if (second==60)
{
second=0;
minute++;
if (minute==60)
{
minute=0;
hour++;
if (hour==24)
{ hour=0;}
Show2DigitNumber(0,1,hour); //DispStr(2,1,":");
}
Show2DigitNumber(3,1,minute); //DispStr(5,1,":");
}
*/
//Read_RTC();
P1OUT ^= 0x01; // Toggle P1.0
second=DS1302SingleRead(0); second=(second>>4)*10+(second&0x0f);
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = hour/10+0x30;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = hour%10+0x30;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = 58;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = minute/10+0x30;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = minute%10+0x30;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = 58;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = second/10+0x30;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = second%10+0x30;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
UCA0TXBUF = 13;
// Show2DigitNumber(0,1,hour); //DispStr(2,1,":");
// Show2DigitNumber(3,1,minute); //DispStr(5,1,":");
Show2DigitNumber(6,1,second); //DispStr(8,1," ");
//miao = DS1302SingleRead(0);
//miao=(miao>>4)*10+(miao&0x0f);
//miao=DS1302SingleRead(0); miao=(miao>>4)*10+(miao&0x0f);
if (second ==0)
{
GetRealTime(&bufTime); //get current time
minute=bufTime.min;minute=(minute>>4)*10+(minute&0x0f);
hour=bufTime.hour;hour=(hour>>4)*10+(hour&0x0f);
Show2DigitNumber(3,1,minute);
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
msp430+1602+1302+serial.zip
(35.75 KB, 下载次数: 82)
2020-5-12 06:26 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
qee654
时间:
2022-4-16 18:34
最近正好要找一个DS1302的例子参考下谢谢了
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1