📄 upsd_st85.c
字号:
/*---------------------------------------------------------------------------- Title: upsd_st85.cDate: October 8, 2002Author: Alec BathDescription: ST85 Read/Write Routines for DK3200 Board10/18/02 PCL Modified to utlize Switches 1 and 2 to set the clock on the DK3200 boardCopyright 2002 ST MicroelectronicsThis example demo code is provided as is and has no warranty,implied or otherwise. You are free to use/modify any of the providedcode at your own risk in your applications with the expressed limitationof liability (see below) so long as your product using the code containsat 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 ORCONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OROTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.----------------------------------------------------------------------------*/ #pragma CODE // include assembly in .lst file#include "upsd3200.h"#include "upsd_hardware.h"#include "upsd_st85.h"#include "upsd_i2c.h"#include "lcd_io.h"#include "upsd_timer.h" // timer fns for timeout, etc.extern xdata unsigned char i2c_xmit_buf[256]; // message xmit bufferextern xdata unsigned char i2c_rcv_buf[256]; // message rcv bufferunsigned int j; // array pointerextern bit i2c_init_flag; // callable status flag bits//--------------------------------// Test M41ST85 I2C interface//--------------------------------void uPSD_st85_write (void){ // 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 (0xD0,0x00,15); // send 15 bytes for st85: addr 0xD0, @ 0x00-0x0E }void uPSD_read_clock_init (void){ int init_hour; int init_mins; int init_secs; upsd_i2c_rcv (0xD0, 0x01, 3); init_secs = i2c_rcv_buf[1]; if ((init_secs & 0x80) != 0) { i2c_xmit_buf[1] = i2c_rcv_buf[1] & 0x00; // Clear the Stop bit if set upsd_i2c_xmit (0xD0,0x01, 1); } i2c_xmit_buf[12] = i2c_rcv_buf[12] & 0xBF; // Start the clock by clear HT bit in buffer upsd_i2c_xmit (0xD0,0x0C, 1); init_hour = Conv_BCD_to_Int(i2c_rcv_buf[3]); // read current hour setting, conver BCD to integer init_mins = Conv_BCD_to_Int(i2c_rcv_buf[2]); // read current minutes setting if((init_hour >23) || (init_mins >59)){ i2c_xmit_buf[2] = 0x00; // Set minute to 0 i2c_xmit_buf[3] = 0x00; // Set hour to 0 uPSD_st85_write(); // Write to ST85 timer0_delay(1000); } uPSD_i2c_st85_read_clock(); // Read & Print ST85}void uPSD_i2c_st85_read_clock (void){ upsd_i2c_rcv (0xD0, 0x00, 16); printfLCD("Current Time Is:\n"); // display on LCD printfLCD(" "); print_hex(i2c_rcv_buf[3]); // print Hours printfLCD(":"); print_hex(i2c_rcv_buf[2]); // print Mins printfLCD(":"); print_hex(i2c_rcv_buf[1]); // print Secs printfLCD("\n");}void set_hour(void) { int tmp_hour; tmp_hour = Conv_BCD_to_Int(i2c_rcv_buf[3]); // read current hour setting, conver BCD to integer if (tmp_hour >= 23) tmp_hour = 0; else tmp_hour = tmp_hour + 1; i2c_xmit_buf[3] = Conv_Int_to_BCD(tmp_hour); // Write the updated hours to ST85 uPSD_st85_write(); }void set_minutes(void) { int tmp_mins; tmp_mins = Conv_BCD_to_Int(i2c_rcv_buf[2]); // read current minutes setting if (tmp_mins >= 59) tmp_mins = 0; else tmp_mins = tmp_mins+1; i2c_xmit_buf[2] = Conv_Int_to_BCD(tmp_mins); // Write the updated minutes to ST85 uPSD_st85_write(); }/******************************************************************************//* function: Conv_Int_to_BCD */ /* descripition: Convers an integer number to timekeeper BCD format */ /* input: int integ *//* output: unsigned char *//******************************************************************************/unsigned char Conv_Int_to_BCD(int integ) { char tmp_buf; tmp_buf = (unsigned char)(integ); if (integ <=9) return(tmp_buf); if ((integ >= 10) & (integ <= 19)) return (tmp_buf+0x06); if ((integ >= 20) & (integ <= 29)) return (tmp_buf+0x0c); if ((integ >= 30) & (integ <= 39)) return (tmp_buf+0x12); if ((integ >= 40) & (integ <= 49)) return (tmp_buf+0x18); if ((integ >= 50) & (integ <= 59)) return (tmp_buf+0x1E); }/******************************************************************************//* function: Conv_BCD_to_Int */ /* descripition: Convers BCD format to an integer number */ /* input: unsigned char BCD *//* output: integer *//******************************************************************************/int Conv_BCD_to_Int(unsigned char BCD) { int tmp_buf1; tmp_buf1 = (int)(BCD); if (BCD <=0x9) return(tmp_buf1); if ((BCD >= 0x10) & (BCD <= 0x19)) return (tmp_buf1-6); if ((BCD >= 0x20) & (BCD <= 0x29)) return (tmp_buf1-12); if ((BCD >= 0x30) & (BCD <= 0x39)) return (tmp_buf1-18); if ((BCD >= 0x40) & (BCD <= 0x49)) return (tmp_buf1-24); if ((BCD >= 0x50) & (BCD <= 0x59)) return (tmp_buf1-30); if ((BCD >= 0x60) & (BCD <= 0xFF)) return (60); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -