Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Single Byte Transfer

Status
Not open for further replies.

sudhakar a

Junior Member level 3
Junior Member level 3
Joined
Jun 3, 2013
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,503
Hi i am trying to interface rtc ht1380 with M16c.In this data sheet write protect(wp) register,Clock halt flag and 3 pins REST,SCLK and DATO I/O have.I am trying to write and read only SECONDS Register in single byte transfer.But i am confused where and when use these 3 pins and WP and CH bits.plz... anybody help me.........



Regards
sudhakar
 

Is support I2c or SPI. I thing it is I2C so u refer I2C protocol details. First initalise then read process. U can refer any I2c code only device address & data address change as per IC rest all same
 

yes modern electronic is right u cant tx whole byte at tim u have follow i2c protocol.
so u can tx 1 bit at time otherwise look any rtc which tx parallely.
 

In ht1380 data sheet there is no discussion about the I2C Protocol.That is the serial communication.I wrote the code but i got the result junk values.I couldn't find out where is the wrong settings in write and read functions
 

Select 8 bit mode. Then send register address 111 and then send 00000000. So that WP is 0 and you can write to the device. To ensble clock to the device you have to make CH = 0. To do that send 000 as address and then send 00000000 i.e., 00000000 will be written to the seconds register. It is a very simple device. Same pin is used for read and write.
 

Thanks Jayanth for repluing me.I applied to the same procedure as tell u.But i got the junk values.I think the seconds value couldn't either write or read.plz tell me procedure about read and write functions
 

10000000 Command byte to write to seconds register

then send 00000000 This is to set sec value as 0 and CH = 0 to enable clock to the device.


send

10000010 Command byte to write to min register.

then send 00000000 to set min to 0

Send 10000100 Command byte to write hours register

then send data 10000000 to set hours to 0 and AM and 12 hour mode.

similarly write for date, month, day, tear register.


If you need you may disable clock to device while setting time.

To read sec register

send Command byte 10000001

then data is transmitted on I/O line (8-bits)

read in the data with 8 clock pulses.
 

Thanks Jayanth for replying me.I follwed as your procedure.But i got the junk values.I attached the my code for write and read functions.Plz verify that and send me modifications.
rtc_write(0x80,00); // write seconds
rtc_write(0x82,00); // write Minutes

rtc_read(0x81); // read seconds
rtc_read(0x83); // read minutes

void rtc_write(unsigned far char addr,unsigned far char data)
{
unsigned far char i;
RESET=1;
addr<<=1;
addr|=0x80;
for(i=8;i;i--)
{
if(addr&0x01)
DATA_IO=1;
else
DATA_IO=0;
SCLK=1;
addr>>=1;
SCLK=0;
}
for(i=8;i;i--)
{
if(data&0x01)
DATA_IO=1;
else
DATA_IO=0;
SCLK=1;
data>>=1;
SCLK=0;
}
RESET=0;
DATA_IO=0;
}

unsigned far char rtc_read(unsigned far char addr)
{
unsigned far char i,data;
RESET=1;
addr<<=1;
addr|=0x80;
for(i=8;i;i--)
{
SCLK=0;
if(addr&0x01)
DATA_IO=1;
else
DATA_IO=0;
SCLK=1;
addr>>=1;
//SCLK=0;
}
/*for(i=8;i;i--)
{
SCLK=0;
data>>=1;
if(DATA_IO)
data|=0x80;
SCLK=1;
}*/
SCLK=0;
RESET=0;
DATA_IO=0;
return addr;
}


Regards
sudhakar
 

Hai Jayanth i am waiting for your response.Plz.. help me,I am beginner in the microcontroller.Plz send me modifications of that functions
 

Hai Jayanth,still i am waiting for your response.Plz.. help me,I am beginner in the microcontroller.Plz send me modifications of that functions
 

Hai FvM,Thanks for replying me.I have to initial declarations of DATA_IO pins register directions.But i got the junk values.

#define SCLK_DIR pd0_1
#define DATA_IO_DIR pd0_0
#define RESET_DIR pd0_2
#define SCLK p0_1
#define DATA_IO p0_0
#define RESET p0_2

void Rtc_Init()
{
RESET_DIR=1;
SCLK_DIR=1;
DATA_IO_DIR=1;
RESET=0;
SCLK=0;
DATA_IO=0;
}
 

That's not what I meaned. DATA_IO_DIR has to be switched dynamically between send and receive, in the middle of rtc_read().
 

Hai FvM,Thanks for replying me.So what about rtc_write function? that is correct or wrong?

- - - Updated - - -

Hai FvM,,I have done the modifications of my code.But still i got the junk or null values.
plz verify my code and modify that and send modification code.plz...............Its urgently required

set_time(12, 20, 40, HOUR_MODE_12, AM);
get_time();



void RTC_Init()
{
RESET_DIR=1;
SCLK_DIR=1;
DATA_IO_DIR=1;
RESET=0;
SCLK=0;
DATA_IO=0;
}

void delay(unsigned int l)
{
//unsigned char i;

for(i=0; i<l; i++);
{
}
}

void clock(void)
{
delay();

SCLK = 1; // Start clock

delay();

SCLK = 0; // Clear SCL
}


void rtc_write(unsigned far char reg, unsigned far char data)
{

unsigned far char i;
//unsigned char final_16b_data;


unsigned char final_16bit_data = ( ((data << 8) & 0xFF00) | (((reg<<1) | 0x80) & 0xFF) );
//Send_Char_Uart2(final_16bit_data);
//Send_Char_Uart2(data);
//Send_Char_Uart2(reg);
RESET = 1;

for(i=0;i<16;i++)
{
if(final_16bit_data & 0x01)
DATA_IO=1;
else
DATA_IO=0;

clock();

final_16bit_data >>=1;
}
RESET = 0;
Send_Char_Uart2(final_16bit_data);
delay();

}

unsigned char rtc_read(unsigned char reg)
{

unsigned char i, data;
//Send_Char_Uart2(reg);
reg = ((reg << 1) | 0x81);
// Send_Char_Uart2(reg);
data = 0;
RESET = 1;

for(i=0;i<8;i++)
{
if(reg & 0x01)
DATA_IO=1;
else
DATA_IO=0;

clock();

reg >>=1;
}

for(i=0;i<8;i++)
{
data <<=1;
data |= DATA_IO;
clock();
}

RESET = 0;

delay();

return reg;

}



void dis_prot_en_osc(void)
{
rtc_write(0x00, 0x00); //disable write protection
rtc_write(0x80, 0x00); //enable oscillator
}


void en_prot(void)
{
rtc_write(0x8E, 0x00); //enable write protection
}


void set_time(char hour, char min, char sec, char hour_mode, char am_or_pm) //takes decimal values...
{

char seconds=0, minutes=0, hours=0;

seconds = ( (((sec/10) & 0x7) << 4) | ((sec%10) & 0xF) );
// Send_Char_Uart2(seconds);
minutes = ( (((min/10) & 0x7) << 4) | ((min%10) & 0xF) );
//Send_Char_Uart2(minutes);

if(hour_mode == HOUR_MODE_12)
hours = ( (HOUR_MODE_12 << 7) |
(0x0 << 6) |
(am_or_pm << 5) |
(((hour/10) & 0x1) << 4) |
((hour%10) & 0xF) );
else //24 hour mode
hours = ( (HOUR_MODE_12 << 7) |
(0x0 << 6) |
(((hour/10) & 0x3) << 4) |
((hour%10) & 0xF) );

dis_prot_en_osc();
rtc_write(0x80, seconds);
rtc_write(0x82, minutes);
rtc_write(0x84, hours);
en_prot();

}


void get_time(void) //output will be in decimal
{

char seconds=0, minutes=0, hours=0;
char second_output, minute_output, hour_output, hour_mode_output, am_or_pm_output;

seconds = rtc_read(0x81);
minutes = rtc_read(0x83);
hours = rtc_read(0x85);

second_output = ( (((seconds >> 4) & 0x7)*10) + (seconds & 0xF) );
minute_output = ( (((minutes >> 4) & 0x7)*10) + (minutes & 0xF) );
Send_Char_Uart2(second_output); // printf statement
Send_Char_Uart2(minute_output); // printf statement
hour_mode_output = ((hours >> 7) & 0x1) ? HOUR_MODE_12 : HOUR_MODE_24;
if(hour_mode_output == HOUR_MODE_12)
{
hour_output = ( (((hours >> 4) & 0x1)*10) + (hours & 0xF) );
am_or_pm_output = ((hours >> 5) & 0x1) ? PM : AM;
}

else //24 hour mode
{
hour_output = ( (((hours >> 4) & 0x3)*10) + (hours & 0xF) );
Send_Char_Uart2(hour_output); // printf statement
}


}
 

rtc_write() looks right at first sight. I didn't yet look at the WP handling specifications.

I have done the modifications of my code
Still no dynamical DAT_IO_DIR switching.
 

Hai FvM,please modify my code where the error occur and send that code to me.Its urgently requird me
plz.........plz............................plz..............................................

regards
sudhakar
 

Something like this:
(Please notice the code tags that are required by Edaboard forum rules)
Code:
unsigned char rtc_read(unsigned char reg)
{
unsigned char i, data;
//Send_Char_Uart2(reg);
reg = ((reg << 1) | 0x81);
// Send_Char_Uart2(reg);
data = 0; 
RESET = 1;

for(i=0;i<8;i++)
{
if(reg & 0x01)
DATA_IO=1;
else
DATA_IO=0;

clock();

reg >>=1;
}

DATA_IO_DIR=0;
for(i=0;i<8;i++)
{
data <<=1;
data |= DATA_IO;
clock();
}
RESET = 0;
DATA_IO_DIR=1;
return reg;
}
 

Hai FvM,Thanks for replying me.what about write function.In the write function the seconds,minutes values are written but got the values 00 00.The same vaules are read the read function and i am finally got the results 00 00 values.I attached the write function code plz verify that code and if any modifications are required plz modify and send to me modification code


void rtc_write(unsigned far char reg, unsigned far char data)
{

unsigned far char i;
//unsigned char final_16b_data;


unsigned char final_16bit_data = ( ((data << 8) & 0xFF00) | (((reg<<1) | 0x80) & 0xFF) );
//Send_Char_Uart2(final_16bit_data);
//Send_Char_Uart2(data);
//Send_Char_Uart2(reg);
RESET = 1;

for(i=0;i<16;i++)
{
if(final_16bit_data & 0x01)
DATA_IO=1;
else
DATA_IO=0;

clock();

final_16bit_data >>=1;
}
RESET = 0;
Send_Char_Uart2(final_16bit_data);
delay();

}
 

Hai FvM,thanks for replying me,i got the seconds value that increments upto 59 sec and restarts from 0 sec.
thank u ..:)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top