:: مهندس متميز ::
تاريخ التسجيل: Oct 2015
المشاركات: 620
|
|
نشاط [ مشتاق الله ]
قوة السمعة:0
|
|
30-04-2016, 06:48 AM
المشاركة 1
|
|
بسم الله الرحمن الرحيم
السلام عليكم و رحمة الله و بركاته
مشروع ساعة اليكترونية باستخدام
Atmega 8
و مترجم ميكرو سي
و RTC DS1307
مع امكانية تعديل الزمن و التاريخ .
و المشروع مفتوح المصدر فى سبيل لله لمن اراد تغيير فيه او الانتفاع منه.
صورة البروتيوس :

نسخة من البرنامج :
كود:
/* Project name: ADDRESS IS (D0) FOR WRITE --- AND (D1) FOR READ.
RTC_Write (Demonstration on working with the RTC Module and Software I2C routines)
* Copyright:
mikro elektonica - some Addition ãÔÊÇÞ Çááå
* Description:
This project is simple demonstration how to set date and time on DS1307
RTC (real-time clock).
* Test configuration:
MCU: ATmega8
Oscillator: internal Clock, 8.0000 MHz
ext. modules: tc2 on portc
SW: mikroC PRO for AVR
* NOTES:
- I2C communication lines should be connected to pull-up resistors.
*/
#define but_up pind0_bit
#define but_dn pind1_bit
#define but_st pind2_bit
void update () ;
void Read_Time();
void Transform_Time();
void Display_Time();
void Init_Main();
void RTC_READ();
// Software I2C connections
sbit Soft_I2C_Scl_Output at PORTC0_bit;
sbit Soft_I2C_Sda_Output at PORTC1_bit;
sbit Soft_I2C_Scl_Input at PINC0_bit;
sbit Soft_I2C_Sda_Input at PINC1_bit;
sbit Soft_I2C_Scl_Direction at DDC0_bit;
sbit Soft_I2C_Sda_Direction at DDC1_bit;
// End Software I2C connections
void main() { DDRD = 0XF8 ; PORTD = 0XFF;
EEPROM_Write(0,0); EEPROM_Write(1,0); EEPROM_Write(2,0);EEPROM_Write(3,0); EEPROM_Write(4,0); EEPROM_Write(5,16);
Init_Main(); // Perform initialization
while (1) { // Endless loop
Read_Time(); // Read time from RTC(DS1307)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
Delay_ms(100); // Wait 1 second
if (but_st==0) {delay_ms(200) ; update();Init_Main();}
}
}
void update () ;
char seconds, minutes, hours, day, month, year; // Global date/time variables
// LCD module connections
sbit LCD_RS at PORTB0_bit;
sbit LCD_EN at PORTB1_bit;
sbit LCD_D4 at PORTB2_bit;
sbit LCD_D5 at PORTB3_bit;
sbit LCD_D6 at PORTB4_bit;
sbit LCD_D7 at PORTB5_bit;
sbit LCD_RS_Direction at DDB0_bit;
sbit LCD_EN_Direction at DDB1_bit;
sbit LCD_D4_Direction at DDB2_bit;
sbit LCD_D5_Direction at DDB3_bit;
sbit LCD_D6_Direction at DDB4_bit;
sbit LCD_D7_Direction at DDB5_bit;
// End LCD module connections
unsigned char sec, min1, hr, week_day, mn;
char *txt, tnum[4];
char date;
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time() {
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xD0); // Address DS1307, see DS1307 datasheet
Soft_I2C_Write(0); // Start from address 0
Soft_I2C_Start(); // Issue repeated start signal
Soft_I2C_Write(0xD1); // Address DS1307 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
day = Soft_I2C_Read(1); // Read year/day byte
date = soft_i2c_read(1);
month = Soft_I2C_Read(1); // Read weekday/month byte
year = soft_i2c_read(0);
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 & 0x10) >> 4)*10 + (hours & 0x0F); // Transform hours
date = ((date & 0x30) >> 4)*10 + (date & 0x0F); // Transform day
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); // Transform month
year = ((year & 0xF0) >> 4)*10 + (year & 0x0F); // Transform year
}
//-------------------- Output values to LCD .
void Display_Time() {
Lcd_Chr(1, 6, (date / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1, 7, (date % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1, 9, (month / 10) + 48);
Lcd_Chr(1,10, (month % 10) + 48);
Lcd_Chr(1,14, (year / 10) + 48); // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,15, (year % 10) + 48);
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() {
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(1,1,"Date:"); // Prepare and output static text on LCD
Lcd_Chr(1,8,':');
Lcd_Chr(1,11,':');
Lcd_Out(1,12,"20");
Lcd_Out(2,1,"Time:");
Lcd_Chr(2,8,':');
Lcd_Chr(2,11,':');
}
//----------------- Main procedure
void RTC_READ() {
Init_Main(); // Perform initialization
while (1) { // Endless loop
Read_Time(); // Read time from RTC(DS1307)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
Delay_ms(100); // Wait 1 second
if (but_st==0) {delay_ms(200) ; update();Init_Main();}
}
}
void display();
void RTC_write();
//*************************************
char key_input (){ // reads keys
while (1){
if (but_up==0) {delay_ms(200) ;return 'u'; }
if (but_dn==0) {delay_ms(200) ;return 'd'; }
if (but_st==0) {delay_ms(200) ;return 's'; }
}
}
//*********** BCD ******
char BCDTODEC(char BCD){
char tem ;
tem=bcd&0x0f ;
tem=(bcd>>4)*10+tem ;
return tem ;
}
char DECTOBCD(char dec){
char tem ;
tem=(dec/10) ; tem=(tem<<4) ;
tem+=dec%10;
return tem ;
}
//*****************************************
void check_limit(char byte){ char mem; char txt[3];
switch(byte){
case 5: {
if(EEPROM_Read(byte)<=16){EEPROM_write(byte,16);} break; // YEAR LIMIT
}
case 4: {
if((EEPROM_Read(byte)<=1) || (EEPROM_Read(byte)>12)) {EEPROM_write(byte,1);} ; break; // MONTH LIMIT
}
case 3: {
if((EEPROM_Read(byte)<=1) || (EEPROM_Read(byte)>31)) {EEPROM_write(byte,1);} ; break; // DAY LIMIT
}
case 2: { if(EEPROM_Read(byte) >=12 && EEPROM_Read(byte)<=23 ){ EEPROM_WRITE(6,1);} // FOR PM . // HOUR LIMIT
else { EEPROM_WRITE(6,0); } // FOR AM .
if((EEPROM_Read(byte)<1) || (EEPROM_Read(byte)>24)) {EEPROM_write(byte,1);} ; break;}
case 1: {
if((EEPROM_Read(byte)<=0) || (EEPROM_Read(byte)>59)) {EEPROM_write(byte,0);} ; break; // MINUTE LIMIT
}
}
}
//***************************************
void update(){ char count=0 ,back=0 ;
signed char byte=5 ; // increment / dec. values
lcd_cmd(_LCD_CLEAR);Lcd_out(1,1,"DATE : YEAR = ") ;
while(1){
Lcd_Chr(2, 6, (EEPROM_Read(byte) / 10) + 48); // Print tens digit of day variable
Lcd_Chr(2, 7, (EEPROM_Read(byte) % 10) + 48);
back=key_input();
check_limit(byte);
switch (back){
case 'u': { delay_ms(100); EEPROM_write(byte,EEPROM_Read(byte)+1);break;}
case 'd': { delay_ms(100); EEPROM_write(byte,EEPROM_Read(byte)-1);break;}
case 's': {
delay_ms(200);byte-- ;
if(byte==4){ Lcd_out(1,1,"DATE : MONTH = ") ;}
else if(byte==3){ Lcd_out(1,1,"DATE : DAY = ") ;}
else if(byte==2){ Lcd_out(1,1,"TIME : HOUR = ") ;}
else if(byte==1){ Lcd_out(1,1,"TIME : MINUTE = ") ;}
if(byte<=0){return ; break ;}
break;
}
}
check_limit(byte);
RTC_write();
}
}
//************** write ****
void RTC_write(){
SOFT_I2C_INIT();
if(EEPROM_Read(2) > 12 ){ EEPROM_WRITE(2,(EEPROM_Read(2)-12));EEPROM_WRITE(6,1); }
SOFT_I2C_START();
SOFT_I2C_WRITE(0XD0); // select ds1307
SOFT_I2C_WRITE(0); // select address to write (hour add to adjust 12h am/pm)
soft_i2c_write(DECTOBCD(EEPROM_Read(0x80))); // second=0 and bit7=1 for pause of DS1307
soft_i2c_write(DectoBcd(EEPROM_Read(1))); // write minutes
if (EEPROM_READ(6)==0){ soft_i2c_write((DectoBcd(EEPROM_Read(2)))| 1<<6 & ~(1<<5) ); } // write hour NOW = AM
if (EEPROM_READ(6)==1){ soft_i2c_write((DectoBcd(EEPROM_Read(2)))| 1<<6 | 1<<5 ); } // write hour NOW = PM ,
soft_i2c_write(DectoBcd(6)); // write day week
soft_i2c_write(DectoBcd(EEPROM_Read(3))); // write date
soft_i2c_write(DectoBcd(EEPROM_Read(4))); // write month
soft_i2c_write(DectoBcd(EEPROM_Read(5))); // write year
Soft_I2C_Stop();
Soft_I2C_Start(); // issue start signal
Soft_I2C_Write(0xD0); // address DS1307
Soft_I2C_Write(0); // start from word at address 0
Soft_I2C_Write(0); // write 0 to REG0 (enable counting + 0 sec)
Soft_I2C_Stop(); // issue stop signal*/
}
الحمد لله الذى بنعمته تتم الصالحات
ان تتلو القران اى تتبعه ولا يتبعك
كلنا سوريا-حلب
|