hi,
we are working on a project that uses PCF8583 RTC and soft I2C of PIC18F4455 to send data to SCL and SDA at port B1 and B0 respectively. we made use of portD for the LCD display of the date and time (since we are required to display those) and portB for the RTC. can you help us out in our code. it does not work properly as expected. it displays:
Date: 45.25.2013
Time: @5.@5.@5
will it be possible that we only have some errors in getting the data? can you suggest ways on how we can eliminate the error. THANK YOU VERY MUCH!
also, there is this issue with our hardware. the LCD does not display anything upon turning on the supply, but after holding the capacitor (22pF) attached to the oscillator, it eventually works but there are times that it does not work at all. we don't think that it has anything to do with the 8Mhz clock because we tried several oscillators already.
here's our code.
char seconds, minutes, hours, day, month, year; // Global date/time variables
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time() {
Soft_I2C_Config(&PORTB, 0, 1); // Initialize full master mode
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // Address PCF8583, see PCF8583 datasheet
Soft_I2C_Write(2); // Start from address 2
Soft_I2C_Start(); // Issue repeated start signal
Soft_I2C_Write(0xA1);
seconds = Soft_I2C_Read(1); // Read seconds byte
minutes = Soft_I2C_Read(1); // Read minutes byte
hours = Soft_I2C_Read(1); // Read hours byte
day = Soft_I2C_Read(1); // Read year/day byte
month = Soft_I2C_Read(0); // Read weekday/month byte
Soft_I2C_Stop(); // Issue stop signal
}
//-------------------- Formats date and time
void Transform_Time() {
seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); // Transform seconds
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); // Transform months
hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F); // Transform hours
year = (day & 0xC0) >> 6; // Transform year
day = ((day & 0x30) >> 4)*10 + (day & 0x0F); // Transform day
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); // Transform month
EEPROM_Write(0x20, seconds);
EEPROM_Write(0x30, minutes);
EEPROM_Write(0x40, hours);
EEPROM_Write(0x50, year);
EEPROM_Write(0x60, day);
EEPROM_Write(0x70, month);
}
//-------------------- Output values to LCD
void Display_Time() {
Lcd_Chr(1, 6, (EEPROM_Read(0x60) / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1, 7, (EEPROM_Read(0x60) % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1, 9, (EEPROM_Read(0x70) / 10) + 48);
Lcd_Chr(1,10, (EEPROM_Read(0x70) % 10) + 48);
Lcd_Chr(1,15, EEPROM_Read(0x50) + 48); // Print year variable (start from year 2010)
Lcd_Chr(2, 6, (EEPROM_Read(0x40) / 10) + 48);
Lcd_Chr(2, 7, (EEPROM_Read(0x40) % 10) + 48);
Lcd_Chr(2, 9, (EEPROM_Read(0x30) / 10) + 48);
Lcd_Chr(2,10, (EEPROM_Read(0x30) % 10) + 48);
Lcd_Chr(2,12, (EEPROM_Read(0x20) / 10) + 48);
Lcd_Chr(2,13, (EEPROM_Read(0x20) % 10) + 48);
}
//------------------ Performs project-wide init
void Init_Main() {
TRISD = 0;
PORTD = 0xFF;
TRISD = 0xFF;
//PORTE = 0x00;
//CCP1CON = 0x00;
//INTCON = 0x00;
ADCON1 = 0x0F;
SPPCON = 0x00;
SPPCFG = 0x00;
UCON = 0x00;
SSPCON1 = 0x20;
Soft_I2C_Config(&PORTB, 0, 1); // Initialize Soft I2C communication
Lcd_Init(&PORTD); // Initialize LCD
Lcd_Cmd(Lcd_CLEAR); // Clear LCD display
Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,1,"Date:"); // Prepare and output static text on LCD
Lcd_Chr(1,8,'.');
Lcd_Chr(1,11,'.');
Lcd_Chr(1,16,'.');
Lcd_Out(2,1,"Time:");
Lcd_Chr(2,8,':');
Lcd_Chr(2,11,':');
Lcd_Out(1,12,"201"); // start from year 2010
}
//----------------- Main procedure
void main() {
Delay_ms(500);
Init_Main(); // Perform initialization
while (1) { // Endless loop
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
}
}