📄 upsd_st85.c
字号:
/* `=========================================================================`
***************************************
**** * * ****
Title: UPSD_ST85
File name: upsd_st85.c
Project name: UPSD_ST85
*** ****
****************** * ******************
**** ****
Author: Petr PFEIFER
MPG Prague, Czech Republic
**** * * ****
***************************************
$Version: 1.023 Build: 2004-02-07,22:28:47
Description:
============
ST85 Timer Read/Write Routines for DK3200 Board.
Notes:
======
-none
..........................
. .
. ****************** .
. **PPPPPPPPPPPPPPPP .
. *PPPP*******PP**** .
. **PPP********PP*** .
. ***PPP******PP**** .
. *****PPP****PP**** .
. *****PPP****PP**** .
. PPPPPPPP****PP**** (R) .
. .
..........................
=-=-=-=-=-=
=-=-=-=-=-=-=-=-=-=-=-=-=
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Copyright (c) 2004 ST Microelectronics
This code/file is provided as is and has no warranty,
implied or otherwise. You are free to use/modify any of the provided
code at your own risk in your applications with the expressed limitation
of liability (see below) so long as your product using the code
contains at least one uPSD products (device).
LIMITATION OF LIABILITY:
========================
NEITHER STMicroelectronics NOR ITS VENDORS OR AGENTS
SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
=-=-=-=-=-=-=-=-=-=-=-=-=
=-=-=-=-=-=
For current information on uPSD products,
please consult our pages on the World Wide Web:
www.st.com/psm
- - - - - - - - - - - -
STMicroelectronics GROUP OF COMPANIES
Australia - Brazil - China - Czech Republic - Finland - France - Germany
Hong Kong - India - Italy - Japan - Malaysia - Malta - Morocco - Singapore
Spain - Sweden - Switzerland - United Kingdom - U.S.A.
http://www.st.com
`========================================================================` */
#pragma CODE // include assembly in .lst file
#pragma NOAREGS
#include "upsd3200.h"
#include "upsd_hardware.h"
#include "upsd_st85.h"
#include "upsd_i2c.h"
#include "upsd_timer.h" // timer fns for timeout, etc.
extern xdata unsigned char i2c_xmit_buf[256];// message xmit buffer
extern xdata unsigned char i2c_rcv_buf[256]; // message rcv buffer
unsigned int j; // array pointer
extern bit i2c_init_flag; // callable status flag bits
#define ST85_I2C_ADDRESS 0xD0
void uPSD_read_clock_init (void)
/******************************************************************************
Function : void uPSD_read_clock_init ()
Parameters : (void)
Description: initializes the ST85
******************************************************************************/
{
upsd_i2c_rcv(ST85_I2C_ADDRESS,0x00,15); // read all registers
if (
((i2c_rcv_buf[1]&0x7F)>0x59) || ((i2c_rcv_buf[2]&0x7F)>0x59) || ((i2c_rcv_buf[3]&0x3F)>0x23) ||
((i2c_rcv_buf[1]&0x0F)>0x09) || ((i2c_rcv_buf[2]&0x0F)>0x09) || ((i2c_rcv_buf[3]&0x0F)>0x09) )
//in case of bad RTC data after power-on or the very first usage ..
{
i2c_xmit_buf[0x00] = 0x00; // ST85 mseconds
i2c_xmit_buf[0x01] = 0x00; // ST85 seconds
i2c_xmit_buf[0x02] = 0x00; // ST85 mins
i2c_xmit_buf[0x03] = 0x00; // ST85 hours
i2c_xmit_buf[0x04] = 0x01; // ST85 day of week
i2c_xmit_buf[0x05] = 0x01; // ST85 date
i2c_xmit_buf[0x06] = 0x01; // ST85 month
i2c_xmit_buf[0x07] = 0x03; // ST85 year
i2c_xmit_buf[0x08] = 0x00; // ST85 control=00
i2c_xmit_buf[0x09] = 0x00; // ST85 watchdog=0x00
i2c_xmit_buf[0x0A] = 0x1f; // ST85 alm month=0x1f (enable alarm int.)
i2c_xmit_buf[0x0B] = 0xff; // ST85 alm date=0x3f
i2c_xmit_buf[0x0C] = 0x3f; // ST85 alm hour=0x3f (clr HT bit)
i2c_xmit_buf[0x0D] = 0x7f; // ST85 alm min=0x7f
i2c_xmit_buf[0x0E] = 0x7f; // ST85 alm sec=0x7f
i2c_xmit_buf[0x0F] = 0x00;
i2c_xmit_buf[0x10] = 0x00;
i2c_xmit_buf[0x11] = 0x00;
i2c_xmit_buf[0x12] = 0x00;
i2c_xmit_buf[0x13] = 0x00;
upsd_i2c_xmit (ST85_I2C_ADDRESS,0x00,0x14); // send to ST85
}
upsd_i2c_rcv(ST85_I2C_ADDRESS,0x01,1); // read all registers
if (i2c_rcv_buf[0x01]>127) // if STOP bit is set
{
i2c_xmit_buf[1] = i2c_rcv_buf[1] & 127; // clear ST bit if set
upsd_i2c_xmit(ST85_I2C_ADDRESS,0x01,1); // send to ST85
}
upsd_i2c_rcv(ST85_I2C_ADDRESS,0x0C,1); // read all registers
if ((i2c_rcv_buf[0x0C]&0x40)>0) // if HT bit is set
{
i2c_xmit_buf[0x0C] = i2c_rcv_buf[0x0C] & 0xBF; // clear HT bit if set
upsd_i2c_xmit(ST85_I2C_ADDRESS,0x0C,1); // send to ST85
}
}
void uPSD_st85_write (void)
/******************************************************************************
Function : void uPSD_st85_write ()
Parameters : (void)
Description: write all to M41ST85
******************************************************************************/
{
// set up xmit buffer, and send chars via i2c
i2c_xmit_buf[0] = 0x00; // ST85 msecs=00
i2c_xmit_buf[1] = 0x00; // ST85 secs=00
// i2c_xmit_buf[2] = 0x42; // ST85 mins
// i2c_xmit_buf[3] = 0x12; // ST85 hours
// i2c_xmit_buf[4] = 0x03; // ST85 dow
// i2c_xmit_buf[5] = 0x15; // ST85 date
// i2c_xmit_buf[6] = 0x10; // ST85 month
// i2c_xmit_buf[7] = 0x02; // ST85 year
i2c_xmit_buf[8] = 0x00; // ST85 control=00
i2c_xmit_buf[9] = 0x00; // ST85 watchdog=0x00
i2c_xmit_buf[10] = 0x1f; // ST85 alm month=0x1f (enable alarm int.)
i2c_xmit_buf[11] = 0xff; // ST85 alm date=0x3f
i2c_xmit_buf[12] = 0x3f; // ST85 alm hour=0x3f (clr HT bit)
i2c_xmit_buf[13] = 0x7f; // ST85 alm min=0x7f
i2c_xmit_buf[14] = 0x7f; // ST85 alm sec=0x7f
upsd_i2c_xmit(ST85_I2C_ADDRESS,0x00,15);
// send 15 bytes for st85: addr 0xD0, @ 0x00-0x0E
}
void uPSD_i2c_st85_read_clock (void)
/******************************************************************************
Function : void uPSD_i2c_st85_read_clock ()
Parameters : (void)
Description: reads clock (all registers)
******************************************************************************/
{
upsd_i2c_rcv (ST85_I2C_ADDRESS, 0x00, 16);
}
void Increment_hour(void)
/******************************************************************************
Function : void Increment_hour()
Parameters : (void)
Description: Increments hour
******************************************************************************/
{
data unsigned char tmp_hour;
upsd_i2c_rcv (ST85_I2C_ADDRESS, 0x00, 4);
tmp_hour = Conv_BCD_to_Int(i2c_rcv_buf[3]);
// read current hour setting, conver BCD to integer
tmp_hour = tmp_hour>=23?0:tmp_hour+1;
i2c_xmit_buf[0x00] = 0;
i2c_xmit_buf[0x01] = 0;
i2c_xmit_buf[0x02] = i2c_rcv_buf[2];
i2c_xmit_buf[0x03] = Conv_Int_to_BCD(tmp_hour);
upsd_i2c_xmit(ST85_I2C_ADDRESS,0x00,4); // Write in ST85
}
void Increment_minutes(void)
/******************************************************************************
Function : void Increment_minutes()
Parameters : (void)
Description: Increments minute
******************************************************************************/
{
data unsigned char tmp_mins;
upsd_i2c_rcv (ST85_I2C_ADDRESS, 0x00, 4);
tmp_mins = Conv_BCD_to_Int(i2c_rcv_buf[2]);// read current minutes setting
tmp_mins = tmp_mins>=59?0:tmp_mins+1;
i2c_xmit_buf[0x00] = 0;
i2c_xmit_buf[0x01] = 0;
i2c_xmit_buf[0x02] = Conv_Int_to_BCD(tmp_mins);
i2c_xmit_buf[0x03] = i2c_rcv_buf[3];
upsd_i2c_xmit(ST85_I2C_ADDRESS,0x00,4); // Write in ST85
}
unsigned char Conv_Int_to_BCD(int i)
/******************************************************************************
Function : unsigned char Conv_Int_to_BCD()
Parameters : (int i)
Description: Convers an integer number to timekeeper BCD format
******************************************************************************/
{
if (i <= 9) return (i);
if (i <= 19) return (i+0x06);
if (i <= 29) return (i+0x0C);
if (i <= 39) return (i+0x12);
if (i <= 49) return (i+0x18);
if (i <= 59) return (i+0x1E);
return (0); //error
}
int Conv_BCD_to_Int(unsigned char BCD)
/******************************************************************************
Function : int Conv_BCD_to_Int()
Parameters : (unsigned char BCD)
Description: Convers BCD format to an integer number
******************************************************************************/
{
if (BCD <= 0x09) return (BCD);
if (BCD <= 0x19) return (BCD-6);
if (BCD <= 0x29) return (BCD-12);
if (BCD <= 0x39) return (BCD-18);
if (BCD <= 0x49) return (BCD-24);
if (BCD <= 0x59) return (BCD-30);
return (0); //error
}
/* *************************************************************************
*** ***
** *** End of File *** **
*** ***
************************************************************************* */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -