كود:
/*
* Project name:
RTC_Read (Reading date/time stamp from PCF8583 through I2C)
* Copyright:
(c) MikroElektronika, 2005-2008
* Revision History:
20050130:
- initial release;
* Description:
This project is simple demonstration how to read date and time from PCF8583
RTC (real-time clock). The code can be used with any MCU that has MSSP
module at PORTC. Date and time are printed at LCD.
* Test configuration:
MCU: PIC16F877A/PIC18F4520
Dev.Board: EasyPIC5
Oscillator: HS, 8.000MHz
Ext. Modules: RTC module (PCF8583), LCD 2x16 chars
SW: mikroC v8.0
* NOTES:
- In order to use the example, address pin A0 of PCF8583 must be set to 0V!
- For proper I2C communication, pins on PORTC must be in the pull-up mode,
and the LEDs on board switched OFF!
*/
unsigned char sec, min1, hr, day, mn, year;
char *txt, tnum[4];
void Zero_Fill(char *value) { // fill text repesentation
if (value[1] == 0) { // with leading zero
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}//~
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time(char *sec, char *min, char *hr, char *day, char *mn, char *year) {
I2C_Start();
I2C_Wr(0xA0);
I2C_Wr(2);
I2C_Repeated_Start();
I2C_Wr(0xA1);
*sec =I2C_Rd(1);
//while (I2C_Is_Idle() == 0) ;
*min =I2C_Rd(1);
//while (I2C_Is_Idle() == 0) ;
*hr =I2C_Rd(1);
//while (I2C_Is_Idle() == 0) ;
*day =I2C_Rd(1);
//while (I2C_Is_Idle() == 0) ;
*mn =I2C_Rd(0);
//while (I2C_Is_Idle() == 0) ;
I2C_Stop();
}//~
//-------------------- Formats date and time
void Transform_Time(char *sec, char *min, char *hr, char *day, char *mn, char *year) {
*sec = ((*sec & 0xF0) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0xF0) >> 4)*10 + (*hr & 0x0F);
*year = (*day & 0xC0) >> 6;
*day = ((*day & 0x30) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
}//~
//-------------------- Output values to LCD
void Display_Time(char sec, char min, char hr, char day, char mn, char year) {
ByteToStr(day,tnum);
txt = rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,6,txt);
ByteToStr(mn,tnum);
txt = rtrim(tnum);
Zero_Fill(txt);
LCD_Out(1,9,txt);
LCD_Chr(1,15,52+year);
ByteToStr(hr,tnum);
txt = rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,6,txt);
ByteToStr(min,tnum);
txt = rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,9,txt);
ByteToStr(sec,tnum);
txt = rtrim(tnum);
Zero_Fill(txt);
LCD_Out(2,12,txt);
}//~
//------------------ Performs project-wide init
void Init_Main() {
ADCON1 = 0x0F;
Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init_EP5, see Autocomplete
I2C_Init(100000); // initialize I2C
txt = "Date:"; // prepare and output static text on LCD
LCD_Out(1,1,txt);
LCD_Chr(1,8,':');
LCD_Chr(1,11,':');
txt = "Time:";
LCD_Out(2,1,txt);
LCD_Chr(2,8,':');
LCD_Chr(2,11,':');
txt = "200";
LCD_Out(1,12,txt);
LCD_Cmd(LCD_CURSOR_OFF); // cursor off
}//~
//----------------- Main procedure
void main() {
Init_Main(); // perform initialization
while (1) {
Read_Time(&sec,&min1,&hr,&day,&mn,&year); // read time from RTC(PCF8583)
Transform_Time(&sec,&min1,&hr,&day,&mn,&year); // format date and time
Display_Time(sec, min1, hr, day, mn, year); // prepare and display on LCD
Delay_ms(1000); // wait 1s
}
}
ارجو ان تكون الامور واضحة ..وفي حال اي استفسار فيمكن طرحه هنا للحصول على مساعدة الاعضاء الكرام ..ربنا يوفقك ..
واخر دعوانا ان الحمدلله رب العالمين .. وصلى الله على سيدنا محمد وعلى اله وصحبه وسلم..