#include "config.h"
//#include "p33FJ256MC710.h"
/***********************************************************************
** Time: liwenjin 2012.10
** Function:读取rx8025n内部寄存器
** Input:
** Output:
** Attention
***********************************************************************/
UINT16 rx8025_read_regs(BYTE Add, BYTE ReadDatatLength,BYTE *Data)
{
BYTE ErrorCode;
// Add=Add<<4;//地址(高4位)加传送模式00(低四位)
IdleI2C(); //Wait for bus Idle
StartI2C(); //Generate Start condition
WriteI2C(0x64); //发送写控制命令,0x64
IdleI2C(); //Wait for bus Idle
ErrorCode = ACKStatus(); //Return ACK Status
WriteI2C(Add); //Send High Address
IdleI2C(); //Wait for bus Idle
ErrorCode = ACKStatus(); //Return ACK Status
RestartI2C(); //Generate Restart
WriteI2C(0x65); //发送读控制命令
IdleI2C(); //Wait for bus Idle
ErrorCode = ACKStatus(); //Return ACK Status
getsI2C(Data,ReadDatatLength); //Read Length number of bytes to Data
NotAckI2C(); //send Not Ack
StopI2C(); //Send Stop Condition
return(ErrorCode);
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function:写rx8025n内部寄存器
** Input: ControlByte, Add, *wrptr.
** Output: None
** Attention:先发命令地址,再发地址,最后发数据
***********************************************************************/
INT rx8025_write_regs(BYTE Add,BYTE WriteDataLength,BYTE *wrptr)
{
BYTE ErrorCode;
// Add=Add<<4;//地址(高4位)加传送模式00(低四位)
IdleI2C(); //wait for bus Idle
StartI2C(); //Generate Start condition
WriteI2C(0x64); //send controlbyte for a write,0x64h
IdleI2C(); //wait for bus Idle
ErrorCode = ACKStatus(); //Return ACK Status
WriteI2C(Add); //send low address
IdleI2C(); //wait for bus Idle
ErrorCode = ACKStatus(); //Return ACK Status
putstringI2C(wrptr,WriteDataLength); //send data
IdleI2C(); //wait for bus Idle
StopI2C(); //Generate Stop
return(ErrorCode);
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function:从8025N获取时间
** Input:
** Output: None
** Attention:
***********************************************************************/
BYTE rx8025_get_time(strTIME *get_time)
{
BYTE date[7];
BYTE err;
err = rx8025_read_regs(RX8025_REG_SEC,7,date);
if (!err)return err; //err为0时表示错误,err=1表示正常接收
get_time->cSecond =(BYTE)bcd2bin(date[RX8025_REG_SEC]&0x7f);
get_time->cMinute =(BYTE)bcd2bin(date[RX8025_REG_MIN]&0x7f);
get_time->cHour = bcd2bin(date[RX8025_REG_HOUR] & 0x3f);
get_time->cDay = bcd2bin(date[RX8025_REG_MDAY] & 0x3f);
get_time->cMonth= bcd2bin(date[RX8025_REG_MONTH] & 0x1f);
get_time->sYear= bcd2bin(date[RX8025_REG_YEAR]);
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function:校正8025N的时间
** Input:
** Output: None
** Attention:
***********************************************************************/
INT Time_Adjust(strTIME *dt)
{
BYTE date[7];
//该8025假定能被4整除是闰年,但2100年不是闰年(BUG)
date[RX8025_REG_SEC] = bin2bcd(dt->cSecond);
date[RX8025_REG_MIN] = bin2bcd(dt->cMinute);
date[RX8025_REG_HOUR] = bin2bcd(dt->cHour);
// date[RX8025_REG_WDAY] = bin2bcd(dt->cWeek);
date[RX8025_REG_MDAY] = bin2bcd(dt->cDay);
date[RX8025_REG_MONTH] = bin2bcd(dt->cMonth);
date[RX8025_REG_YEAR] = bin2bcd(dt->sYear% 100);
return rx8025_write_regs(RX8025_REG_SEC,7,date);
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function:BCD转BIN
** Input:
** Output: None
** Attention:
***********************************************************************/
UINT16 bcd2bin(BYTE val)
{
return ((val&0x0f)+(val >> 4)*10);
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function:BIN转BCD
** Input:
** Output: None
** Attention:
***********************************************************************/
BYTE bin2bcd(unsigned val)
{
return ((val / 10) << 4) + val % 10;
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function:判断是否为闰年
** Input:
** Output: None
** Attention:
***********************************************************************/
BYTE is_leap_year(UINT16 year)
{
return (!(year % 4) && (year % 100)) || !(year % 400);
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function:初始化I2C(1)外设
** Input:
** Output: None
** Attention:Sets up Master mode, No slew rate control, 100Khz
***********************************************************************/
VOID InitI2C(VOID)
{
/*I2C2BRG=[(1/Fscl-PGD)*FCY]-2,当Fsc1=100khz时,1/fsc1=(1/100)*1000000=10000ns,PGD=130ns,即10000-130=9870
I2C2BRG=(9870*5.5296)/1000-2=52.5;*/
I2C2BRG = 0x0034;
//初始化IIC外设,设为主模式, No Slew Rate
I2C2CONbits.I2CEN=0;// 禁止I2Cx 模块
I2C2CONbits.DISSLW=1;//禁止斜率控制位
I2C2CONbits.SCLREL=1;//释放SCLx时钟
I2C2RCV = 0x0000;
I2C2TRN = 0x0000;
I2C2CONbits.I2CEN=1;// 使能 I2Cx 模块,并将 SDAx 和 SCLx 引脚配置为串口引脚
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function:StartI2C()
** Input:
** Output: None
** Attention: Generates an I2C Start Condition
***********************************************************************/
VOID StartI2C(VOID)
{
//This function generates an I2C start condition and returns status
//of the Start.
I2C2CONbits.SEN = 1; //Generate Start COndition
while (I2C2CONbits.SEN); //Wait for Start COndition
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function: RestartI2C()
** Input:
** Output: None
** Attention:Generates a restart condition and optionally returns status
***********************************************************************/
VOID RestartI2C(VOID)
{
I2C2CONbits.RSEN = 1; //Generate Restart
while (I2C2CONbits.RSEN); //Wait for restart
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function: StopI2C()
** Input:
** Output: None
** Attention:Generates a bus stop condition
***********************************************************************/
VOID StopI2C(VOID)
{
I2C2CONbits.PEN = 1; //Generate Stop Condition
while (I2C2CONbits.PEN); //Wait for Stop
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function: WriteI2C()
** Input:
** Output: None
** Attention:rites a byte out to the bus
***********************************************************************/
BYTE WriteI2C(BYTE byte)
{
//This function transmits the byte passed to the function
while (I2C2STATbits.TRSTAT); //Wait for bus to be idle
I2C2TRN = byte; //Load byte to I2C2 Transmit buffer
if(I2C2STATbits.IWCOL) /* If write collision occurs,return -1 */
return 1;
else
{
while (I2C2STATbits.TBF); //wait for data transmission
}
return 0;
}
/***********************************************************************
** Time: liwenjin 2012.10
** Function: IdleI2C()
** Input:
** Output: None
** Attention:Waits for bus to become Idle
***********************************************************************/
VOID IdleI2C(VOID)
{
while (I2C2STATbits.TRSTAT); //Wait for bus Idle
}
/************
- 1
- 2
- 3
前往页