Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] [Help Needed] how to set the time of RTC using PIC18F4520

Status
Not open for further replies.

sarah.sanchez

Junior Member level 2
Joined
May 16, 2011
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,588
Hello,

Does anyone know how to set time in RTC? We're using PIC18F4520 in MikroC Pro. We tried the code below but the output in our LCD is one that is counting very fast that we cannot see the data anymore. Please help me on this endeavor. Any help is greatly appreciated. Thank you.


char seconds, minutes, hours, day, month, year; // Global date/time variables

// Software I2C connections
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
// End Software I2C connections

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

void Set_Time() {
Soft_I2C_Init();
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // Address PCF8583
Soft_I2C_Write(0); // Start from word at address 0 (configuration word)
Soft_I2C_Write(0x80); // Write 0x80 to config. (pause counter...)
Soft_I2C_Write(0); // Write 0 to cents word
Soft_I2C_Write(0); // Write 0 to seconds word
Soft_I2C_Write(0x30); // Write 0x30 to minutes word
Soft_I2C_Write(0x11); // Write 0x11 to hours word
Soft_I2C_Stop(); // Issue stop signal

Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // Address PCF8530
Soft_I2C_Write(0); // Start from word at address 0
Soft_I2C_Write(0); // Write 0 to config word (enable counting)
Soft_I2C_Stop(); // Issue stop signal
}
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time() {

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); // Address PCF8583 for reading R/W=1

seconds = Soft_I2C_Read(1); // Read seconds byte
minutes = Soft_I2C_Read(1); // Read minutes byte
hours = Soft_I2C_Read(1); // Read hours 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

}

//-------------------- Output values to LCD
void Display_Time() {

Lcd_Chr(2, 6, (hours / 10) + 48);
Lcd_Chr(2, 7, (hours % 10) + 48);
Lcd_Chr(2, 9, (minutes / 10) + 48);
Lcd_Chr(2,10, (minutes % 10) + 48);
Lcd_Chr(2,12, (seconds / 10) + 48);
Lcd_Chr(2,13, (seconds % 10) + 48);
}

//------------------ Performs project-wide init
void Init_Main() {

TRISB = 0;
PORTB = 0xFF;
TRISB = 0xFF;
//SPPCON = 0x00;
INTCON = 0x00;
ADCON1 = 0x0F;

Soft_I2C_Init(); // Initialize Soft I2C communication
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off

Lcd_Out(2,1,"Time:");
Lcd_Chr(2,8,':');
Lcd_Chr(2,11,':');

}

//----------------- Main procedure
void main() {
Delay_ms(500);

Init_Main(); // Perform initialization
Set_Time();
while (1) { // Endless loop

Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top