ADGAN
Full Member level 5
- Joined
- Oct 9, 2013
- Messages
- 295
- Helped
- 4
- Reputation
- 8
- Reaction score
- 4
- Trophy points
- 18
- Activity points
- 1,837
#include <stdlib.h>
#include <i2c.h>
char tnum[4],txt;
void start1307s(){
OpenI2C1(SLAVE_7_STSP_INT,SLEW_OFF);
//START SIGNAL FOR DS1307***********
StartI2C1() ;// issue start signal
WriteI2C1(0xD0); // address PCF8530
WriteI2C1(0); // start from word at address 0
WriteI2C1(0); // write 0 to config word (enable counting)
StopI2C1 (); // issue stop signal
//**********************************
}
void Zero_Fill(char *value) { // fill text representation
if (value[1] == 0) { // with leading zero
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}
//--------------------- Reads time and date information from RTC (DS1307)
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
StartI2C1();
WriteI2C1(0xD0);
WriteI2C1(0);
RestartI2C1 ();
WriteI2C1(0xD1);
*sec =ReadI2C1();
*min =ReadI2C1();
*hr =ReadI2C1();
*week_day =ReadI2C1();
*day =ReadI2C1();
*mn =ReadI2C1();
*year =ReadI2C1();
StopI2C1 ();
}
//-------------------- Formats date and time
void Transform_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
*sec = ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day = ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
*year = ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}
//-------------------- Output values to LCD
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
switch(week_day){
case 1: strcpypgm2ram(txt,"Sun"); break;
case 2: strcpypgm2ram(txt,"Mon"); break;
case 3: strcpypgm2ram(txt,"Tue"); break;
case 4: strcpypgm2ram(txt,"Wed"); break;
case 5: strcpypgm2ram(txt,"Thu"); break;
case 6: strcpypgm2ram(txt,"Fri"); break;
case 7: strcpypgm2ram(txt,"Sat"); break;
}
LCD_TextOut(3,4,txt);
btoa(day,tnum);
Zero_Fill(txt);
LCD_TextOut(3,7,txt);
btoa(mn,tnum);
Zero_Fill(txt);
LCD_TextOut(3,11,txt);
btoa(year,tnum);
Zero_Fill(txt);
LCD_TextOut(3,17,txt);
}
unsigned char sec, min1, hr, week_day, day, mn, year;
void __init(void){
LCD_Init();
Init_Delay();
}
char txt1[] = "ENERGY CONSUMPTION";
void main(void)
{
LCD_TextOut(0,1,txt1);
start1307s();
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Display_Time(sec, min1, hr, week_day, day, mn, year);
#include <delays.h>
#include <i2c.h>
#define STROBE {E = 1; Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); E = 0;};
char tnum[4],txt;
void readDS1307(char *sec, char *min1, char *hr, char *week_day, char *day, char *mn, char *year){
StartI2C1();
IdleI2C1();
WriteI2C1(0xD0);
IdleI2C1();
WriteI2C1(0x00);
IdleI2C1();
RestartI2C1();
IdleI2C1();
WriteI2C(0xD1);
IdleI2C();
*sec = ReadI2C1();
AckI2C1();
*min1 = ReadI2C1();
AckI2C1();
*hr = ReadI2C1();
AckI2C1();
*week_day = ReadI2C1();
AckI2C1();
*day = ReadI2C1();
AckI2C1();
*mn = ReadI2C1();
AckI2C1();
*year = ReadI2C1();
NotAckI2C1();
StopI2C1();
}
void writeDS1307(){
StartI2C1();
IdleI2C1();
WriteI2C1(0xD0);
IdleI2C1();
WriteI2C1(0x00);
IdleI2C1();
WriteI2C1(0x80);
IdleI2C1();
WriteI2C1(0x41); //minutes
IdleI2C1();
WriteI2C1(0x21); // hours
IdleI2C1();
WriteI2C1(0x10); //day
IdleI2C1();
WriteI2C1(0x18); //date
IdleI2C1();
WriteI2C1(0x10); //month
IdleI2C1();
WriteI2C1(0x13); //year
IdleI2C1();
Delay1KTCYx(25);
StopI2C1();
StartI2C1();
IdleI2C1();
WriteI2C1(0xD0);
IdleI2C1();
WriteI2C1(0x00);
IdleI2C1();
WriteI2C1(0x00); //minutes
IdleI2C1();
Delay1KTCYx(25);
StopI2C1();
}
void Zero_Fill(char *value) { // fill text representation
if (value[1] == 0) { // with leading zero
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}
//-------------------- Formats date and time
void Transform_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
*sec = ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day = ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
*year = ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}
//-------------------- Output values to LCD
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
switch(week_day){
case 1: LCD_ConstTextOut(2,4,"Sun"); break;
case 2: LCD_ConstTextOut(2,4,"Mon"); break;
case 3: LCD_ConstTextOut(2,4,"Tue"); break;
case 4: LCD_ConstTextOut(2,4,"Wed"); break;
case 5: LCD_ConstTextOut(2,4,"Thu"); break;
case 6: LCD_ConstTextOut(2,4,"Fri"); break;
case 7: LCD_ConstTextOut(2,4,"Sat"); break;
}
btoa(sec,tnum);
Zero_Fill(txt);
LCD_TextOut(2,7,txt);
btoa(day,tnum);
Zero_Fill(txt);
LCD_TextOut(2,9,txt);
btoa(hr,tnum);
Zero_Fill(txt);
LCD_TextOut(2,9,txt);
btoa(mn,tnum);
Zero_Fill(txt);
LCD_TextOut(2,11,txt);
btoa(year,tnum);
Zero_Fill(txt);
LCD_TextOut(2,17,txt);
}
void main(void)
{
OpenI2C1(MASTER,SLEW_OFF);
Delay10KTCYx(25);
writeDS1307();
while(1){
readDS1307(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Display_Time(sec, min1, hr, week_day, day, mn, year);
lastdate_check(day, mn, year, last_date);
lastmonth_check(day, mn, year);
lastyear_check(day, mn, year);
}
}
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 btoa(sec,tnum); Zero_Fill(txt); LCD_TextOut(1,11,txt); btoa(min,tnum); Zero_Fill(txt); LCD_TextOut(1,8,txt); btoa(hr,tnum); Zero_Fill(txt); LCD_TextOut(1,5,txt); btoa(day,tnum); Zero_Fill(txt); LCD_TextOut(2,11,txt); btoa(mn,tnum); Zero_Fill(txt); LCD_TextOut(2,8,txt); btoa(year,tnum); Zero_Fill(txt); LCD_TextOut(2,5,txt);
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 while(1){ readDS1307(); Transform_Time(sec, min1, hr, week_day, day, mn, year); Display_Time(sec, min1, hr, week_day, day, mn, year); lastdate_check(day, mn, year, last_date); lastmonth_check(day, mn, year); lastyear_check(day, mn, year); }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?