rtc-4543.c

来自「MSP430单片机与时钟芯片通讯程序」· C语言 代码 · 共 85 行

C
85
字号
#include <msp430x14x.h>
#include"F147-HT1621(061010).h"
#define CE_4543    0x20
#define WR_4543    0x10
#define SCL_4543   0x40
#define DATA_4543  0x80


void Send_4543(uchar i,uchar command)
{
	uchar t,temp;
	P1DIR |= DATA_4543;
	for (t=i; t>0; t--)
	{
		temp = command & 0x1;
		if (temp)
			P1OUT |= DATA_4543;
		else
			P1OUT &= ~DATA_4543;
		command >>= 1;
		P1OUT &=~ SCL_4543;		
		delay(1);
		P1OUT |= SCL_4543;
		
	}
	P1OUT |= DATA_4543;
	P1DIR &= ~DATA_4543;
}

uchar Receive_4543(uchar i)
{
	P1DIR &= ~DATA_4543;
	uint timer = 0;
	do
	{
		P1OUT |= SCL_4543;
		timer >>= 1;
		if(P1IN & DATA_4543)
			timer |= 0x80;
		P1OUT &=~ SCL_4543;
		i--;
	}while(i>0);
	P1DIR |= DATA_4543;
	return (timer);
}

void read_rtc4543(uchar *time)
{
	P1OUT &= ~ WR_4543;
	delay(1);
	P1OUT |= CE_4543;
	*time++ = Receive_4543(8) & 0x7F;
	*time++ = Receive_4543(8) & 0x7F;
	*time++ = Receive_4543(8) & 0x3F;
	*time++ = Receive_4543(4) & 0x07;
	*time++ = Receive_4543(8) & 0x3F;
	*time++ = Receive_4543(8) & 0x1F;
	*time   = Receive_4543(8) & 0xFF;
	delay(1);
	P1OUT &= ~CE_4543;
	delay(1);
	P1OUT |= WR_4543;
}

void write_rtc4543(uchar *time)
{
//	uchar	i;
	P1OUT |= WR_4543;
	delay(1);
	P1OUT |= CE_4543;
	delay(1);
	Send_4543(8,*time++);
	Send_4543(8,*time++);
	Send_4543(8,*time++);
	Send_4543(4,*time++);
	Send_4543(8,*time++);
	Send_4543(8,*time++);
	Send_4543(8,*time);
	delay(1);
	P1OUT &= ~CE_4543;
	delay(1);
	P1OUT &= ~WR_4543;
}

⌨️ 快捷键说明

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