RTC rx8025 (I/O simulation IIC)

Source: Internet
Author: User

 

/*************************************** **************************************** ***********
* Function name: i2c_delay
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Void i2c_delay (uint32 waittime)
{
Uint32 uitemp = waittime * small;
While (uitemp --);
}
/*************************************** **************************************** ***********
* Function name: io_write
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Void io_write (uint16 data, uint16 level)
{
Switch (data)
{
Case i2c_sda:
{
Switch (level)
{
Case high:
Io0set | = i2c_sda;
I2c_delay (5 );
Break;
Case low:
Io0clr | = i2c_sda;
I2c_delay (5 );
Break;
}
} // End case
Break;
Case i2c_scl:
{
Switch (level)
{
Case high:
Io0set | = i2c_scl;
I2c_delay (5 );
Break;
Case low:
Io0clr | = i2c_scl;
I2c_delay (5 );
Break;
}
} // End case
Break;
}
}

/*************************************** **************************************** ***********
* Function name: i2c_stop
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Void i2c_stop (void) // I2C bus operation stop bit sending
{
Io_write (i2c_sda, low); // SDA = low

Io_write (i2c_scl, high); // SCL = high
I2c_delay (10 );

Io_write (i2c_sda, high); // SDA = high
I2c_delay (5 );

Io_write (i2c_scl, low); // SCL = low
I2c_delay (5 );
}

/*************************************** **************************************** ***********
* Function name: i2c_start
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Void i2c_start (void)
{
I2c_stop ();
I2c_delay (5 );

Io_write (i2c_sda, high); // SDA = high
I2c_delay (5 );
 
Io_write (i2c_scl, high); // SCL = high
I2c_delay (5 );
 
Io_write (i2c_sda, low); // SDA = low
I2c_delay (5 );
 
Io_write (i2c_scl, low); // SCL = low
I2c_delay (5 );
}

/*************************************** **************************************** ***********
* Function name: i2c_clock
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Void i2c_clock (void)
{
I2c_delay (5); // SCL = high-> low

Io_write (i2c_scl, high); // SCL = high
I2c_delay (5 );

Io_write (i2c_scl, low); // SCL = low
I2c_delay (2 );
}
/*************************************** **************************************** ***********
* Function name: i2c_clock_ask
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Uint8 i2c_clock_ask (void)
{
Uint8 status;

I2c_delay (5 );
Io_write (i2c_scl, high); // SCL = high
I2c_delay (5 );
 
Switch (io0pin & i2c_sda) & i2c_sda)
{
Case (1 <14 ):
Status = i2cnoack; // return i2cnoack
Break;

Case (0x00 ):
Status = i2cack; // return i2cack
Break;
}
Io_write (i2c_scl, low); // SCL = low
I2c_delay (5 );
Return (Status );
}

/*************************************** **************************************** ***********
* Function name: i2c_ack
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Void i2c_ack (uint8 status)
{
Switch (Status)
{
Case (i2cack ):
Io_write (i2c_sda, low); // SDA = low has a response
Break;

Case (i2cnoack ):
Io_write (i2c_sda, high); // SDA = high no response
Break;
}
I2c_delay (5 );

Io_write (i2c_scl, high); // SCL = high-> low
I2c_delay (5 );

Io_write (i2c_scl, low );
I2c_delay (5 );

Io_write (i2c_sda, high); // SDA = high
I2c_delay (5 );
}

/*************************************** **************************************** ***********
* Function name: i2c_send_8bit
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Uint8 i2c_send_8bit (uint8 value)
{
Uint8 i2c_loop;

For (i2c_loop = 0; i2c_loop <8; i2c_loop ++)
{
Switch (value <i2c_loop) & 0x80)
{
Case 0x80:
Io_write (i2c_sda, high );
Break;
Case 0x00:
Io_write (i2c_sda, low );
Break;
}
I2c_clock (); // sends a clock signal from the SCL Port
}
Io_write (i2c_sda, high); // release SDA
I2c_delay (10 );
Return (i2c_clock_ask (); // returns the ACK signal.
}

/*************************************** **************************************** ***********
* Function name: i2c_receive_8bit
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Uint8 i2c_receive_8bit (void)
{
Uint8 i2c_loop;
Uint8 value = 0x00;

Io_write (i2c_sda, high); // set the data line as the input method
 
For (i2c_loop = 0; i2c_loop <8; i2c_loop ++)
{
I2c_delay (5 );
Io_write (i2c_scl, low); // SCL = low-> high
I2c_delay (5 );

Io_write (i2c_scl, high );
I2c_delay (5 );

Value <= 1;
Switch (io0pin & i2c_sda) & i2c_sda)
{
Case (1 <14 ):
Value | = 0x01;
Break;

Case (0x00 ):
Value & = 0xfe;
Break;
}
I2c_delay (5 );
}
Io_write (i2c_scl, low); // SCL = low
I2c_delay (5 );
Return (value );
}

/*************************************** **************************************** ***********
* Function name: rtc8025_write_bytes
* Function description:
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Uint8 rtc8025_write_bytes (uint8 rtcaddr, uint8 Volue)
{
I2c_start (); // send the start bit to rtc

If (i2c_send_8bit (rtc8025_id) = i2cnoack) // send hardware address and write Signal
{
Return notok;
}
If (i2c_send_8bit (rtcaddr <4) & 0xf0) = i2cnoack) // the address of the sending operation register ("0 ")
{
Return notok;
}
If (i2c_send_8bit (Volue) = i2cnoack) // send hardware address and write Signal
{
Return notok;
}

I2c_stop (); // send a stop bit to rtc

Return (OK );
}
/*************************************** **************************************** ***********
* Function name: modify_rx8025time
* Function Description: clock Calibration
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Uint8 modify_rx8025time (rtc_struct * modify_time)
{
Uint8 h;

H = rtc8025_write_bytes (rtc8025_second, modify_time-> second); // modify "seconds"
If (H! = OK)
Return notok;

H = rtc8025_write_bytes (rtc8025_minute, modify_time-> minute); // modify "Minute"
If (H! = OK)
Return notok;

H = rtc8025_write_bytes (rtc8025_hour, modify_time-> hour); // modify "hour"
If (H! = OK)
Return notok;

H = rtc8025_write_bytes (rtc8025_day, modify_time-> day); // modify "date"
If (H! = OK)
Return notok;

H = rtc8025_write_bytes (rtc8025_month, modify_time-> month); // modify "month"
If (H! = OK)
Return notok;

H = rtc8025_write_bytes (rtc8025_year, modify_time-> year); // modify "year"
If (H! = OK)
Return notok;

Return (OK );
}

/*************************************** **************************************** ***********
* Function name: get_time
* Function Description: read time
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Uint8 get_time (rtc_struct * time_data)
{
Uint8 ucloop;
Uint8 Week;
Uint8 Pon;

For (ucloop = 0x01; ucloop> 0x00; ucloop --)
{
// Dummy_rtc.date.century = 0x00;
I2c_start (); // send the start bit to rtc
If (i2c_send_8bit (rtc8025_id) = i2cnoack) // send hardware address and write Signal
{
Continue;
}
If (i2c_send_8bit (uint8) (rtc8025_simple_read | (rtc8025_control2 <4) // send operation Register address and read mode
= I2cnoack)
{
Continue;
}
Pon = (i2c_receive_8bit () & rtc8025_pon)> 4; // power supply in read register 2
I2c_ack (i2cack );
Time_data-> second = i2c_receive_8bit (); // read "seconds"
I2c_ack (i2cack );
Time_data-> minute = i2c_receive_8bit (); // read "minutes"
I2c_ack (i2cack );
Time_data-> hour = i2c_receive_8bit (); // read "hour"
I2c_ack (i2cack );
Week = i2c_receive_8bit (); // read "Week"
I2c_ack (i2cack );
Time_data-> day = i2c_receive_8bit (); // read "date"
I2c_ack (i2cack );
Time_data-> month = i2c_receive_8bit (); // read "month"
I2c_ack (i2cack );
Time_data-> year = i2c_receive_8bit (); // read "year"
I2c_ack (i2cnoack );
Time_data-> century = 0x20; // The Century is set to 0x20.

I2c_stop (); // send a stop bit to rtc

}

Return OK;
}
/*************************************** **************************************** ***********
* Function name: rx8025_init
* Function Description: rx8025 Initialization
* Parameter: Parameter Name: input/output? Type description
*
* Return value:
* Author:
* Creation date: 2006-12-19
* Global variables:
* Global static variables:
* Local static variables:
* ---------------------------------------- Modification history ------------------------------------------
* Current version: modifier: modification date:
* Modification description:
**************************************** **************************************** **********/
Void rx8025_init (void)
{
Uint8 uctemp;
Io0dir | = i2c_sda | i2c_scl;
Io0clr = i2c_scl;

I2c_delay (1 );
I2c_stop ();
Uctemp = 0x20 | 0x02;
Rtc8025_write_bytes (rtc8025_control1, uctemp); // 24-hour mode setting, 1Hz frequency output
Uctemp = 0x20;
Rtc8025_write_bytes (rtc8025_control2, uctemp); // 2.1v Voltage Detection Mode and voltage effective mark (PON)
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.