|
|
View previous topic :: View next topic |
Author |
Message |
piclab Guest
|
Control relay using RTC |
Posted: Wed Oct 21, 2009 9:30 pm |
|
|
Hi everybody
I want to control relay using RTC such as open relay at 12.00 and off relay about 20 minute and open relay at 18.00 and off relay about 20 minute.
I have a program with this instructions, that do RTC.
How can I change the source code?
Code: |
#include <16f877.h>
#include <INPUT.C>
#include <stdlib.h>
#use i2c(master,sda=PIN_C4,scl=PIN_C3)
#define ADDR_DS1307 0xD0
typedef struct
{
BYTE sec;
BYTE min;
BYTE hr;
BYTE day;
BYTE date;
BYTE month;
BYTE year;
}
DS1307_RTC;
DS1307_RTC RTC;
/****************************Prototypes*/
void DS1307_Write(unsigned char ctl,unsigned char dat);
BYTE DS1307_Read(unsigned char ctl);
void DS1307_WriteDate(void);
void DS1307_WriteTime(void);
void DS1307_ReadDate(void);
void DS1307_ReadTime(void);
/***************DS1307 Write***********************/
void DS1307_Write(unsigned char ctl,unsigned char dat)
{
i2c_start();
i2c_write(ADDR_DS1307);
i2c_write(ctl);
i2c_write(dat);
i2c_stop();
}
/********************DS1307 Read***********************/
BYTE DS1307_Read(unsigned char ctl)
{
BYTE dat;
i2c_start();
i2c_write(ADDR_DS1307);
i2c_write(ctl);
i2c_start();
i2c_write(ADDR_DS1307+1);
dat=i2c_read(0);
i2c_stop();
return(dat);
}
/***********************DS1307 Write*************************/
void DS1307_WriteDate(void)
{
DS1307_Write(0x04,RTC.date);
DS1307_Write(0x05,RTC.month);
DS1307_Write(0x06,RTC.year);
}
/////////////////*********DS1307 Write TIME*************/
void DS1307_WriteTime(void)
{
DS1307_Write(0x00,RTC.sec);
DS1307_Write(0x01,RTC.min);
DS1307_Write(0x02,RTC.hr);
}
/////////////////*******DS1307 Read Date*//////////////
void DS1307_ReadDate(void)
{
RTC.date=DS1307_Read(0x04);
RTC.month=DS1307_Read(0x05);
RTC.year=DS1307_Read(0x06);
}
/////////////***********DS1307 Read Time**********/
void DS1307_ReadTime(void)
{
RTC.sec=DS1307_Read(0x00);
RTC.min=DS1307_Read(0x01);
RTC.hr=DS1307_Read(0x02);
}
void DS1307_SetDate(void)
{
printf("\f\rDate :");
RTC.date=gethex();
printf("\f\rMonth :");
RTC.month=gethex();
printf("\f\rYear: ");
RTC.year=gethex();
}
void DS1307_SetTime(void)
{
printf("\f\rHour : ");
RTC.hr=gethex();
printf("\f\rMin : ");
RTC.min=gethex();
printf("\f\rSec : ");
RTC.sec=gethex();
}
////////////*********Main Function***************///////
void main()
{
char ch;
ch=getchar();
printf("\f\rSet Real Time Clock (y or n):");
if(ch=='y')
{
DS1307_SetDate();
DS1307_WriteDate();
DS1307_SetTime();
DS1307_WriteTime();
}
while(1)
{
DS1307_ReadDate();
DS1307_ReadTime();
printf("\f\rDS1307 Real Time Clock\n");
printf("\rDate:%2X/%2X/2X\n",RTC.date,RTC.month,RTC.year);
printf("\rTime:%2X/%2X/%2X\n",RTC.hr,RTC.min,RTC.sec);
delay_ms(1000);
}
}
|
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Oct 22, 2009 2:30 am |
|
|
The most difficult part is to create an RTC-program. Well, that's what you already have. From you question I understand you didn't write this program yourself or you would have known how to adapt it.
Sorry, this forum is here to help people with programming problems. We are not going to do your school home work.
Try to make the required changes yourself. If you can't get this to work than post your non-working code and we'll help you further improve on it. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|