// Set up the I/O ports
set_tris_gpio(0b00111000); // set GP3-GP5 as output, GP0-GP2 as input
output_low(pin_a2); // set CLK pin low
// Initialize the RTC module
i2c_start();
i2c_write(RTC_ADDR);
i2c_write(RTC_HOUR);
i2c_write(0x12); // set the initial time to 12:00:00
i2c_write(0x00);
i2c_write(0x00);
i2c_stop();
while(true)
{
// Read the time and date from the RTC module
i2c_start();
i2c_write(RTC_ADDR);
i2c_write(RTC_SEC);
i2c_start();
i2c_write(RTC_ADDR | 1);
sec =i2c_read(TRUE);
min = i2c_read(TRUE);
hour = i2c_read(TRUE);
day = i2c_read(TRUE);
month = i2c_read(TRUE);
year = i2c_read(FALSE);
i2c_stop();
// Display the time and date on the 7-segment display
output_high(pin_a0); // select the first digit
output_d(patterns[hour/10]); // display the tens digit of the hour
delay_ms(5);
output_low(pin_a0);
output_high(pin_a1); // select the second digit
output_d(patterns[hour%10]); // display the ones digit of the hour
delay_ms(5);
output_low(pin_a1);
output_high(pin_a2); // toggle the colon LED
delay_ms(500);
output_low(pin_a2);
delay_ms(500);
output_high(pin_a0); // select the third digit
output_d(patterns[min/10]); // display the tens digit of the minute
delay_ms(5);
output_low(pin_a0);
output_high(pin_a1); // select the fourth digit
output_d(patterns[min%10]); // display the ones digit of the minute
delay_ms(5);
output_low(pin_a1);
output_high(pin_a2); // toggle the colon LED
delay_ms(500);
output_low(pin_a2);
delay_ms(500);
output_high(pin_a0); // select the fifth digit
output_d(patterns[sec/10]); // display the tens digit of the second
delay_ms(5);
output_low(pin_a0);
output_high(pin_a1); // select the sixth digit
output_d(patterns[sec%10]); // display the ones digit of the second
delay_ms(5);
output_low(pin_a1);
output_high(pin_a2); // toggle the colon LED
delay_ms(500);
output_low(pin_a2);
delay_ms(500);
}
}