anboli
Full Member level 2
- Joined
- Mar 9, 2012
- Messages
- 144
- Helped
- 4
- Reputation
- 8
- Reaction score
- 4
- Trophy points
- 1,298
- Activity points
- 2,513
hi,
im interfacing DS1307(RTC ic) and AT24c02(for memory) with at89s52, i had given the SCL and SDA separately to both the IC.
whether we can give like that or i need to give separately.
and i having the problem with memory ic, but no problem with RTC.
i can read the time from RTC but cant loading the data from memory..
im interfacing DS1307(RTC ic) and AT24c02(for memory) with at89s52, i had given the SCL and SDA separately to both the IC.
whether we can give like that or i need to give separately.
and i having the problem with memory ic, but no problem with RTC.
i can read the time from RTC but cant loading the data from memory..
//************************************************************************//
//
// North_Green = N_G
// South_Green = S_G
// East_Green = E_G
//
//
//
//
//
#include<at89x52.h>
#include"i2c_rtc.h"
#include"i2c_eeprom.h"
sbit SET = P1^0;
sbit INC = P1^1;
sbit DEC = P1^2;
sbit OK = P1^3;
sbit N_G = P1^4;
sbit E_G = P1^5;
sbit W_G = P1^6;
sbit S_G = P1^7;
sbit N_R = P2^0;
sbit E_R = P2^1;
sbit W_R = P2^2;
sbit S_R = P2^3;
sbit N_Y = P2^4;
sbit E_Y = P2^5;
sbit W_Y = P2^6;
sbit S_Y = P2^7;
unsigned char North_green,East_green,West_green,South_green;
int c,d;
unsigned char Sc,Mn,Hr,value1;
unsigned char north,south,east,west;
//*****************************************************************************//
void delay(unsigned int count);
void disp_north(unsigned char k);
void disp_east(unsigned char k);
void disp_west(unsigned char k);
void disp_south(unsigned char k);
void set_north();
void set_south();
void set_west();
void set_east();
void time_set();
void get_time();
void disp_min(unsigned char m);
void disp_hour(unsigned char n);
void disp_time(unsigned char m,n);
void set_time(unsigned char Sc,Mn,Hr);
void load();
void send_data_rom(unsigned char add,v1);
void read_data_rom(unsigned char addr);
////////***************************************************************************************
void delay(unsigned int count)
{
int i,j;
for(i=0;i<count;i++) // 100 ====> 1.5s && 1000 ====> 15s
for(j=0;j<200;j++);
}
//******************************************************************************//
void disp_north(unsigned char k)
{
unsigned char x,x1,x2;
x = k/100; //054 ===> 000
k = k%100; // 54
x1 = k/10; // 05
x2 = k%10; // 04
P0 = (0x60|x);
delay(5);
P0 = (0xE0|x1);
delay(5);
P0 = (0xF0|x2);
delay(5);
}
//******************************************************************************//
void disp_east(unsigned char k)
{
unsigned char x,x1,x2;
x = k/100; //054 ===> 000
k = k%100; // 54
x1 = k/10; // 05
x2 = k%10; // 04
P0 = (0xC0|x);
delay(5);
P0 = (0xD0|x1);
delay(5);
P0 = (0x80|x2);
delay(5);
}
//******************************************************************************//
void disp_west(unsigned char k)
{
unsigned char x,x1,x2;
x = k/100; //054 ===> 000
k = k%100; // 54
x1 = k/10; // 05
x2 = k%10; // 04
P0 = (0x70|x);
delay(5);
P0 = (0x40|x1);
delay(5);
P0 = (0x50|x2);
delay(5);
}
//*********************************************************************************//
void disp_south(unsigned char k)
{
unsigned char x,x1,x2;
x = k/100; //054 ===> 000
k = k%100; // 54
x1 = k/10; // 05
x2 = k%10; // 04
P0 = (0x30|x);
delay(5);
P0 = (0x10|x1);
delay(5);
P0 = (0x20|x2);
delay(5);
}
//********************************************************************************//
void disp_min(unsigned char m)
{
unsigned char min1;
min1 = m/10;
m = m%10;
P0 = (0xA0|min1);
delay(3);
P0 = (0x00|m);
delay(3);
}
void disp_hour(unsigned char n)
{
unsigned char hour1;
hour1 = n/10;
n = n%10;
P0 = (0x90|hour1);
delay(3);
P0 = (0xB0|n);
delay(3);
}
//*********************************************************************************//
void disp_time(unsigned char m,n)
{
unsigned char min1,hour1;
min1 = m/10;
m = m%10;
hour1 = n/10;
n = n%10;
P0 = (0x90|hour1);
P0 = (0xB0|n);
P0 = (0xA0|min1);
P0 = (0x00|m);
}
//*********************************************************************************//
void set_south()
{
unsigned char set_value_south=0;
while(1)
{
if(INC==0)
{
delay(100);
set_value_south=set_value_south+1;
disp_south(set_value_south);
if(set_value_south==257)set_value_south=0;
}
if(DEC==0)
{
delay(100);
set_value_south=set_value_south-1;
disp_south(set_value_south);
if(set_value_south==0)set_value_south=256;
}
if(OK==0)
{
delay(400);
South_green = set_value_south;
disp_north(North_green);
disp_east(East_green);
disp_west(West_green);
disp_south(South_green);
send_data_rom(0x06,0x00);
delay(10);
send_data_rom(0x06,South_green);
load();
get_time();
}
}
}
//******************************************************************************//
void set_west()
{
unsigned char set_value_west=0;
while(1)
{
if(INC==0)
{
delay(100);
set_value_west=set_value_west+1;
disp_west(set_value_west);
if(set_value_west==257)set_value_west=0;
}
if(DEC==0)
{
delay(100);
set_value_west=set_value_west-1;
disp_west(set_value_west);
if(set_value_west==0)set_value_west=256;
}
if(OK==0)
{
delay(400);
West_green = set_value_west;
disp_north(North_green);
disp_east(East_green);
disp_west(West_green);
disp_south(South_green);
send_data_rom(0x05,0x00);
delay(10);
send_data_rom(0x05,West_green);
set_south();
}
}
}
//******************************************************************************//
void set_east()
{unsigned char set_value_east=0;
while(1)
{
if(INC==0)
{
delay(100);
set_value_east = set_value_east+1;
disp_east(set_value_east);
if(set_value_east==257)set_value_east=0;
}
if(DEC==0)
{
delay(100);
set_value_east = set_value_east-1;
disp_east(set_value_east);
if(set_value_east==0)set_value_east=256;
}
if(OK==0)
{
delay(400);
East_green = set_value_east;
disp_north(North_green);
disp_east(East_green);
disp_west(West_green);
send_data_rom(0x04,0x00);
delay(10);
send_data_rom(0x04,East_green);
set_west();
}
}
}
//******************************************************************************//
void set_north()
{ unsigned char set_value_north = 0;
while(1)
{
if(INC==0)
{
delay(100);
set_value_north = set_value_north+1;
disp_north(set_value_north);
if(set_value_north==256)set_value_north=0;
}
if(DEC==0)
{
delay(100);
set_value_north = set_value_north-1;
disp_north(set_value_north);
if(set_value_north==0)set_value_north=256;
}
if(OK==0)
{
delay(400);
North_green = set_value_north;
disp_north(North_green);
disp_east(East_green);
send_data_rom(0x03,0x00);
delay(10);
send_data_rom(0x03,North_green);
set_east();
}
}
}
//******************************************************************************//
void time_set()
{
unsigned char min=0,hour=0;
while(1)
{
if(INC==0)
{
delay(100);
hour=hour+1;
disp_hour(hour);
if(hour==0x18)hour=0; //for hour - 18;
}
if(DEC==0)
{
delay(100);
hour=hour-1;
disp_hour(hour);
if(hour==0xFF)hour=0x18;
}
if(OK==0)
{
delay(400);
send_data_rom(0x01,0x00);
delay(10);
send_data_rom(0x01,hour);
while(1)
{
if(INC==0)
{
delay(100);
min=min+1;
disp_min(min);
if(min==0x3D)min=0;
}
if(DEC==0)
{
delay(100);
min = min-1;
disp_min(min);
if(min==0xff)min=0x3D;
}
if(OK==0)
{
delay(400);
send_data_rom(0x02,0x00);
delay(10);
send_data_rom(0x02,min);
disp_min(min);
disp_hour(hour);
set_north();
}
}
}
}
}
//*****************************************************
void read_data_rom(unsigned char addr)
{
start_rom();
send_byte_rom(0xA0);
aknowledge_rom();
send_byte_rom(addr);
aknowledge_rom();
start_rom();
send_byte_rom(0xA1);
aknowledge_rom();
value1 = read_byte_rom();
aknowledge_rom();
stop_rom();
}
//*****************************************************
void send_data_rom(unsigned char add,v1)
{
start_rom();
send_byte_rom(0xA0);
aknowledge_rom();
send_byte_rom(add);
aknowledge_rom();
send_byte_rom(v1);
aknowledge_rom();
stop_rom();
}
//******************************************************************************//
void load()
{unsigned char hr1,min1;
read_data_rom(0x01);
hr1 = value1;
read_data_rom(0x02);
min1 = value1;
if((hr1==Hr)&&(min1==Mn))
{
read_data_rom(0x03);
north = value1;
read_data_rom(0x04);
east = value1;
read_data_rom(0x05);
west = value1;
read_data_rom(0x06);
south = value1;
}
if(Hr==0x08)
{
north = 0x45;
south = 0x40;
west = 0x35;
east = 0x30;
}
}
//******************************************************************************//
void get_time()
{
while(1)
{
start();
send_byte(0xd0);
aknowledge();
send_byte(0x00);
aknowledge();
start();
send_byte(0xd1);
aknowledge();
Sc = read_byte();
aknowledge();
Mn = read_byte();
aknowledge();
Hr = read_byte();
aknowledge();
stop();
disp_time(Mn,Hr);
load();
if(SET==0)time_set();
}
}
//******************************************************************************//
void ext() interrupt 0
{
if(north>0)
{
north = north-1;
disp_north(north);
}
if(north==0)
{
if(east>0)
{
east = east-1;
disp_east(east);
}
if(east==0)
{
if(west>0)
{
west = west-1;
disp_west(west);
}
if(west==0)
{
if(south>0)
{
south = south-1;
disp_south(south);
if(south==0)c=1;
}
if(south==0)
{
load();
}
}
}
}
}
//******************************************************************************//
void set_time(unsigned char Sc,Mn,Hr)
{
unsigned char conreg = 0x90;
start();
send_byte(0xd0);
aknowledge();
send_byte(0x00);
aknowledge();
send_byte(Sc);
aknowledge();
send_byte(Mn);
aknowledge();
send_byte(Hr);
aknowledge();
send_byte(0x00);
aknowledge();
send_byte(0x01);
aknowledge();
send_byte(0x01);
aknowledge();
send_byte(0x01);
aknowledge();
send_byte(conreg);
aknowledge();
stop();
}
//******************************************************************************//
//
//
void main()
{
P0 = 0x00;
P1 = 0x0f;
P2 = 0x00;
IE = 0x81; //enabling the global interrupt and INT0
IT0 = 1; //
TCON = 0x01; // falling edge interrupt enabled
N_G = 0;
E_G = 0;
W_G = 0;
S_G = 0;
N_R = 0;
E_R = 0;
W_R = 0;
S_R = 0;
N_Y = 0;
E_Y = 0;
W_Y = 0;
S_Y = 0;
P0 = 0x60;
delay(100);
P0 = 0xE1;
delay(100);
P0 = 0xF2;
delay(100);
P0 = 0xC3;
delay(100);
P0 = 0xD4;
delay(100);
P0 = 0x85;
delay(100);
P0 = 0x76;
delay(100);
P0 = 0x47;
delay(100);
P0 = 0x58;
delay(100);
P0 = 0x39;
delay(100);
P0 = 0x10;
delay(100);
P0 = 0x21;
delay(100);
P0 = 0x92;
delay(100);
P0 = 0xB3;
delay(100);
P0 = 0xA4;
delay(100);
P0 = 0x05;
delay(100);
set_time(0x50,0x56,0x07);
while(1)
{
get_time();
}
}