| ??? 05/11/09 09:20 Read: times |
#165229 - It looks like you should read when clock is high Responding to: ???'s previous message |
It is a lot more likely that people would read your code if you posted it properly:
Looking at the data sheet timing diagram, it looks as if you should read the DATA_IO shortly after the +ve edge of the SCLK. I may have mis-interpreted the timing. You obviously need to make sure you can read and write reliably to the HT1380 registers.
#include "8051.h"
#include "serial.h"
#define SCLK P2_0
#define DATA_IO P2_1
#define RESET_ P2_2
#define YELLOW_LED P3_6
#define RED_LED P3_7
unsigned char serial_storage = 0x00;
unsigned char display;
void usec_wait(unsigned char);
void msec_wait(unsigned char);
void sec_wait(unsigned char);
void convert_and_send(unsigned char);
unsigned char receive_convert(void);
//**************************************************************
//DELAY FUNCTIONS - USEC_WAIT ; MSEC_WAIT ; SEC_WAIT
//**************************************************************
void usec_wait(unsigned char k)
{
while (k != 0) {
k--;
}
}
//////////////////////////////////////////////////////
void msec_wait(unsigned char j)
{
while (j != 0) {
usec_wait(165);
j--;
}
}
//////////////////////////////////////////////////////
void sec_wait(unsigned char sec_count)
{
while (sec_count != 0) {
msec_wait(250);
msec_wait(250);
msec_wait(250);
msec_wait(250);
sec_count--;
}
}
//////////////////////////////////////////////////////
//**************************************************************
//SERIAL INTERRUPT FUNCTION
//**************************************************************
void serial_interrupt() interrupt 4
{
if (TI)
TI = 0;
if (RI) {
serial_storage = SBUF;
usec_wait(1);
RI = 0;
RED_LED = 1;
}
}
//********************************************************************************
//FUNCTION THAT SENDS ADDRESS OR DATA_IO AFTER CONVERTING IT TO BITS TO THE 1380
//********************************************************************************
void convert_and_send(unsigned char hex_data1)
{
bit sending_bit;
unsigned char comparing_byte = 0x01, i, storing_byte = 0x00;
SBUF = hex_data1;
msec_wait(10);
for (i = 0; i < 8; i++) {
SCLK = 0;
storing_byte = (hex_data1 & comparing_byte);
if (storing_byte == 0x00) {
sending_bit = 0;
} else {
sending_bit = 1;
}
//send bit
DATA_IO = sending_bit; //Clock to DATA_IO delay is 250ns (max)
comparing_byte = comparing_byte << 1;
SCLK = 1; //this is kept here to save the time in giving dalay after DATA_IO sent, instead in that time calculations are done
}
serial_storage = 0x00;
usec_wait(2);
}
//*************************************************************************************************
//FUNCTION THAT RECEIVES DATA FROM 1380 AND THEN CONVERTS IT TO CHARACTER AND PASSES TO ITS CALLER
//*************************************************************************************************
unsigned char receive_convert(void)
{
unsigned char comparing_byte = 0x00, received_data = 0x00, i = 0;
bit receiving_bit;
YELLOW_LED = 0;
sec_wait(2);
YELLOW_LED = 1;
sec_wait(1);
for (i = 0; i < 8; i++) {
DATA_IO = 1;
SCLK = 1;
SCLK = 0;
usec_wait(1);
receiving_bit = DATA_IO;
if (receiving_bit == 0) {
comparing_byte = 0x00;
} else {
comparing_byte = 0x80; //comparing byte is 10000000 as the bit is to be placesd in the MSB position
}
received_data = received_data | comparing_byte;
received_data = received_data >> 1;
}
return (received_data);
}
void main()
{
initialize_serial(); //INITIALIZES THE SERIAL PORT
RED_LED = 0; //BOTH LED's GLOW WHEN THE RTC IS BEING SET
YELLOW_LED = 0;
RESET_ = 1;
msec_wait(2);
convert_and_send(0x8E); //SETS THE WP BIT AS ZERO
msec_wait(1);
convert_and_send(0x00);
RESET_ = 0;
msec_wait(2);
RESET_ = 1;
msec_wait(2);
convert_and_send(0x80); //SETS THE CH BIT AS ZERO
msec_wait(1);
convert_and_send(0x00);
usec_wait(1);
RESET_ = 0;
RED_LED = 1;
sec_wait(4); //waiting time given so that oscilator can start up to speed and gets ready to recieve time
RED_LED = 0;
RESET_ = 1;
msec_wait(2);
convert_and_send(0x80); //10000000,00010000-sec:10
msec_wait(1);
convert_and_send(0x10);
RESET_ = 0;
usec_wait(1);
RESET_ = 1;
msec_wait(2);
convert_and_send(0x82); //10000010,00010000-min:10
msec_wait(1);
convert_and_send(0x10);
RESET_ = 0;
usec_wait(1);
RESET_ = 1;
msec_wait(2);
convert_and_send(0x84); //10000100,00010000-hour:10,24hr mode
msec_wait(1);
convert_and_send(0x10);
RESET_ = 0;
usec_wait(1);
RESET_ = 1;
msec_wait(2);
convert_and_send(0x86); //10000110,00010000-date:10
msec_wait(1);
convert_and_send(0x10);
RESET_ = 0;
usec_wait(1);
RESET_ = 1;
msec_wait(2);
convert_and_send(0x88); //10001000,00010000-month:05
msec_wait(1);
convert_and_send(0x05);
RESET_ = 0;
usec_wait(1);
RESET_ = 1;
msec_wait(2);
convert_and_send(0x8A); //10001010,00010000-day of week:friday ie 7
msec_wait(1);
convert_and_send(0x07);
RESET_ = 0;
usec_wait(1);
RESET_ = 1;
msec_wait(2);
convert_and_send(0x8C); //10001100,00001000-year:08
msec_wait(1);
convert_and_send(0x08);
RESET_ = 0;
usec_wait(1);
RESET_ = 1;
msec_wait(2);
convert_and_send(0x8E); //SETS THE WP BIT AS ONE PREVENT DATA_IO FROM BEING WRITTEN ONTO THE REGISTERS
msec_wait(1);
convert_and_send(0x80);
RESET_ = 0;
RED_LED = 1; //BOTH LED's TURN OFF WHEN THE RTC TIME HAS BEEN SET
YELLOW_LED = 1;
while (1) //INFINITE LOOP
{
RED_LED = 0; //This shows that only the while loop is running and system is waiting for command(REGISTER ADDRESS)
msec_wait(500);
if (serial_storage != 0x00) {
SCLK = 0;
RED_LED = 1; //This shows that the serial_storage has the character
RESET_ = 1;
convert_and_send(serial_storage); //sending the address of the register that we want to read from
msec_wait(10);
display = receive_convert(); //putting the read DATA_IO into display
RESET_ = 0;
SBUF = display; //sending the read DATA_IO serially to the computer
msec_wait(2);
display = 0x00;
} else {
RED_LED = 1;
msec_wait(500);
}
}
}
|



