📄 ds1340.c
字号:
#include<p24hj256GP210.h>
#include"Global.h"
#include<string.h>
//***********************************************************************************
// MACRO
//***********************************************************************************
#define ACK 0
#define NACK 1
#define ADDRTC 0xD0 /* I睠 slave address */
//***********************************************************************************
// PIN ASSIGNMENT
//***********************************************************************************
#define SCL PORTGbits.RG2 /* I睠 pin definitions */
#define SDA PORTGbits.RG3
//***********************************************************************************
// RTC DATA STRUCTURE
//***********************************************************************************
struct RTC_DATA
{
unsigned char Second; //second 0-59
unsigned char Minute; //Minute 0-59
unsigned char Hour; //Hour 0-24
unsigned char Day; // Day(sunday ,monday ...)
unsigned char Date; //Date 0-31
unsigned char Month; // Month 1-12
unsigned char Year; // Year 00-99
};
//***********************************************************************************
// FUNCTION DECLARATION
//***********************************************************************************
void I2C_start(); // Start I2C
void I2C_stop(); //Stop I2C
void I2C_write(unsigned char Data); // Write Single byte on I2C
unsigned char I2C_read(unsigned char Data); // Read One Byte
void WriteByte(unsigned char Add,unsigned char Data); // Write one byte to passed address
unsigned char ReadByte(unsigned char Add); //Read one byte from the given byte
void InitDS1340(); //Initialize the DS1340 RTC
void WriteDS1340(unsigned char *WriteData); //Write Time to the RTC
void ReadDS1340(unsigned char *ReadData); //Read Time F rom the RTC
unsigned char Bcd2Hex(unsigned char BCDValue); // Bcd to hex conversion
unsigned char Hex2Bcd(unsigned char bcd_value); //hex to BCD conversion
//***********************************************************************************
// EXTERNAL FUNCTION
//***********************************************************************************
extern void DelayCycle(unsigned int Cycle);
//******************************************************************************
//Function Name : I2C_start()
//Type : User defined
//Return Type : void
//Arguments : none
//Details : Generate Start condition for the I2C
//Autor : Nibu
//******************************************************************************
void I2C_start()
{
TRISGbits.TRISG3=0; // Dataline output
SDA = 1;
DelayCycle(5);
SCL = 1; /* Initiate start condition */
DelayCycle(5);
SDA = 0;
}
//******************************************************************************
//Function Name : I2C_stop()
//Type : User defined
//Return Type : void
//Arguments : none
//Details : Generate Stop condition for the I2C
//Autor : Nibu
//******************************************************************************
void I2C_stop()
{
TRISGbits.TRISG3=0; // Dataline output
SDA = 0; SDA = 0; SDA = 0; SDA = 0; /* Initiate stop condition */
DelayCycle(5);
SCL = 1; SCL = 1; SDA = 1;
}
//******************************************************************************
//Function Name : I2C_write(unsigned char Data)
//Type : User defined
//Return Type : void
//Arguments : unsigned char Data
//Details : Write one byte
//Autor : Nibu
//******************************************************************************
void I2C_write(unsigned char Data)
{
unsigned char i;
TRISGbits.TRISG3=0; // Dataline output
SCL = 0;
DelayCycle(10);
for (i = 1; i <= 8; i++) //send 8bit data
{
SDA = (Data >> 7);
DelayCycle(5);
SCL = 1;
Data = Data << 1; /* increase SCL high time */
DelayCycle(5);
SCL = 0;
}
SDA = 1; /* Release the SDA line */
DelayCycle(5);
SCL = 0;
DelayCycle(5);
SCL = 1;
DelayCycle(5);
// if(SDA) printf("Ack bit missing %02x",(unsigned int)Data);
SCL = 0;
}
//******************************************************************************
//Function Name : I2C_read(unsigned char Data)
//Type : User defined
//Return Type : unsigned char
//Arguments : unsigned char Data
//Details : Read one byte
//Autor : Nibu
//******************************************************************************
unsigned char I2C_read(unsigned char Data)
{
unsigned char ReadData, i;
TRISGbits.TRISG3=1; // set dataline as input
SDA = 1; /* Let go of SDA line */
DelayCycle(5);
SCL = 0;
for (i = 1; i <= 8; i++) /* read the msb first */
{
SCL = 1;
DelayCycle(5);
ReadData = ReadData << 1;
ReadData =ReadData | (unsigned char)SDA;
DelayCycle(5);
SCL = 0;
}
SDA = Data; /* Hold SDA low for acknowledge */
DelayCycle(5);
SCL = 0;
DelayCycle(5);
SCL = 1;
DelayCycle(5);
if(Data == NACK) SDA = 1; /* SDA = 1 if next cycle is reset */
SCL = 0;
DelayCycle(5);
SDA = 1; /* Release the SDA line */
return ReadData;
}
//******************************************************************************
//Function Name : ReadByte(unsigned char Add)
//Type : User defined
//Return Type : unsigned char
//Arguments : unsigned char Data
//Details : Read one byte
//Autor : Nibu
//******************************************************************************
unsigned char ReadByte(unsigned char Add) /* -- read one byte of data from the specified address -- */
{
unsigned char data;
I2C_start(); //start I2c
I2C_write(ADDRTC); // Write RTC add (write mode)
I2C_write(Add); // Write Address
I2C_start(); //Start I2c
I2C_write(ADDRTC | 1); // Write RTC add (Read mode)
data=I2C_read(ACK); // Read I2c in ACK mode
I2C_stop(); // Stop I2C
return data; // retun data
}
//******************************************************************************
//Function Name : WriteByte(unsigned char Add,unsigned char Data)
//Type : User defined
//Return Type : void
//Arguments : unsigned char Add,unsigned char Data
//Details : Wrte one byte to the specified address
//Autor : Nibu
//******************************************************************************
void WriteByte(unsigned char Add,unsigned char Data) /* -- write one byte of data to the specified address -- */
{
I2C_start(); //start I2C
I2C_write(ADDRTC); // Write the Adress(write mode)
I2C_write(Add); // Write address
I2C_write(Data); // Write Data
I2C_stop(); // Stop I2C
}
//******************************************************************************
//Function Name : InitDS1340()
//Type : User defined
//Return Type : void
//Arguments : none
//Details : Initilize the DS1340
//Autor : Nibu
//******************************************************************************
void InitDS1340()
{
TRISGbits.TRISG3=0; //Dataline output
TRISGbits.TRISG2=0; //clock line as output
// I2C_start(); /* The following Enables the Oscillator */
// I2C_write(ADDRTC); /* address the part to write */
// I2C_write(0x00); /* position the address pointer to 0 */
// I2C_write(0x00); /* write 0 to the seconds register, clear the CH bit */
// I2C_stop();
//// I2C_start();
//// I2C_write(ADDRTC); /* write slave address + write */
//// I2C_write(0x00); /* write register address, 1st clock register */
//
//// I2C_write(0x00); // init second
//// I2C_write(0x00); // init minute
//// I2C_write(0x00); // init hour
//// I2C_write(0x01); // init Day
//// I2C_write(0x01); //init Date
//// I2C_write(0x01); // init month
//// I2C_write(0x00); // init Year
// I2C_start(); /* address pointer wraps at 7, so point to flag register */
// I2C_write(ADDRTC); /* write slave address + write */
// I2C_write(0x09); /* write register address, control register */
// I2C_write(0); /* clear OSF */
// I2C_start(); /* address pointer wraps at 7, so point to flag register */
// I2C_write(ADDRTC); /* write slave address + write */
// I2C_write(0x08); /* write register address, control register */
// I2C_write(0); /* clear OSF */
// I2C_stop();
}
//******************************************************************************
//Function Name : WriteDS1340(unsigned char *WriteData)
//Type : User defined
//Return Type : void
//Arguments : unsigned char *WriteData(pointer of the RTC structure)
//Details : Write the time to the RTC
//Autor : Nibu
//******************************************************************************
void WriteDS1340(unsigned char *WriteData)
{
struct RTC_DATA RTC;
memcpy(&RTC,WriteData,sizeof(struct RTC_DATA)); // copy the time data to local RTC var
// convert all the bcd data to hex
RTC.Second=Bcd2Hex(RTC.Second);
RTC.Minute=Bcd2Hex(RTC.Minute);
RTC.Hour=Bcd2Hex(RTC.Hour);
RTC.Day=Bcd2Hex(RTC.Day);
RTC.Date=Bcd2Hex(RTC.Date);
RTC.Month=Bcd2Hex(RTC.Month);
RTC.Year=Bcd2Hex(RTC.Year);
I2C_start();
I2C_write(ADDRTC); /* write slave address + write */
I2C_write(0x00); /* write register address, 1st clock register */
I2C_write(RTC.Second); // load second
I2C_write(RTC.Minute); // load minute
I2C_write(RTC.Hour); // load hour
I2C_write(RTC.Day); // load Day
I2C_write(RTC.Date); // load Date
I2C_write(RTC.Month); // load month
I2C_write(RTC.Year); // load Year
I2C_stop();
}
//******************************************************************************
//Function Name : ReadDS1340(unsigned char *ReadData)
//Type : User defined
//Return Type : void
//Arguments : unsigned char *ReadData(pointer of the RTC structure)
//Details : Read the time from the RTC
//Autor : Nibu
//******************************************************************************
void ReadDS1340(unsigned char *ReadData)
{
struct RTC_DATA RTC;
//read Time and date and convert to bcd
RTC.Second = Hex2Bcd(ReadByte(0x00)&0x7F);
RTC.Minute = Hex2Bcd(ReadByte(0x01)&0x7F);
RTC.Hour = Hex2Bcd(ReadByte(0x02)&0x3F);
RTC.Day = Hex2Bcd(ReadByte(0x03)&0x07);
RTC.Date = Hex2Bcd(ReadByte(0x04)&0x3F);
RTC.Month = Hex2Bcd(ReadByte(0x05)&0x1F);
RTC.Year = Hex2Bcd(ReadByte(0x06));
memcpy(ReadData,&RTC,sizeof(struct RTC_DATA)); //copy the read time to Read data
// printf("\n%d, %d, %d, %d, %d, %d, %d ",RTC.Second,RTC.Minute,RTC.Hour,RTC.Day,RTC.Date,RTC.Month,RTC.Year);
}
//******************************************************************************
//Function Name : Bcd2Hex(unsigned char BCDValue)
//Type : User defined
//Return Type : unsigned char
//Arguments : unsigned char BCDValue
//Details : convert BCD to Hex
//Autor : Nibu
//******************************************************************************
unsigned char Bcd2Hex(unsigned char BCDValue)
{
unsigned char temp;
unsigned char HexVal;
temp = BCDValue;
HexVal = 0;
while(1)
{
// Get the tens digit by doing multiple subtraction
// of 10 from the binary value.
if(temp >= 10)
{
temp -= 10;
HexVal += 0x10;
}
else // Get the ones digit by adding the remainder.
{
HexVal += temp;
break;
}
}
return(HexVal);
}
//******************************************************************************
//Function Name : Hex2Bcd(unsigned char HexVal)
//Type : User defined
//Return Type : unsigned char
//Arguments : unsigned char HexVal
//Details : convert hex to Bcd
//Autor : Nibu
//******************************************************************************
// Input range - 00 to 99.
unsigned char Hex2Bcd(unsigned char HexVal)
{
unsigned char temp;
temp = HexVal;
// Shifting upper digit right by 1 is same as multiplying by 8.
temp >>= 1;
// Isolate the bits for the upper digit.
temp &= 0x78;
return(temp + (temp >> 2) + (HexVal & 0x0f));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -