• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

simplest 8051 interfacing with DS1307

Status
Not open for further replies.
Hi,

All i need to know is from where i can get a simplest program in C for interfacing 8051 or any other micro with DS1307.

Regards,

Simran
 

Pommie

Well-Known Member
Most Helpful Member
Hi Simran,

Do you have the basic I²C routines? I can post C routines to access the DS1307 but you would need the basic I2C library.

Mike.
 
Well...

hi,

i am not having basic i2c routines....

please help me in my other doubt...

what is wrong in my this program... see...

Code:
#include<reg51.h>
void main(void)
{
  sbit ravi = P1^0;
  while(1)
  {
  ravi = 1;
  ravi = 0;
  }
}
the error i am getting is :
 

Attachments

Pommie

Well-Known Member
Most Helpful Member
I'm sorry but I'm not familiar with 8051. My guess is that the above should be,

Code:
#include<reg51.h>
#define ravi P1.0     //ravi = port1 bit 0

void main(void)
{
  while(1)
  {
  ravi = 1;
  ravi = 0;
  }
}
Mike.
 

dipta

New Member
____________________________________________________________
#include<reg51.h>
sbit ravi = P1^0; /// define the port out side main....
void main(void)
{

while(1)
{
ravi = 1;
ravi = 0;
}
}
______________________________________________

the cycle will be so fast that you will not see any change externally .........

better give it a try with a delay in between them
 

naveencali

New Member
_________________________________________________________________
#include<reg51.h>

void main(void)
{
sbit ravi = P1^0;
while(1)
{
ravi = 1;
delay ();
ravi = 0;
delay();
}
}
__________________________________________________________________

So Mrs Simran

You might have got the correct reply for the above
you should add delay

NaveenCali
 

engineerfaisal

New Member
Hello,

Can any one look at my code and guess what i am doing wrong with my code.

I cannot read neither write to ds1307.

connected through port d bits 5 and 6

crystall oscillator 10Mhz but using hspll

have pull up resistors on both lines sda and scl



//************************************************************************************//
//************************************************************************************//
// //
// Header: 8722 Project //
// PIC used: PIC18F8722 //
// Start Date: 1st Dec 2009
// Function for I2C //
//************************************************************************************//
//************************************************************************************//
//** HEADER FILES ********************************************************************/
#include <p18F8722.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <usart.h>
#include <timers.h>
#include <delays.h>
#include <i2c.h>
//** CONFIGURATION BITS **************************************************************/
#pragma config OSC = HSPLL
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config DEBUG = ON
//** FUNCTION PROTOTYPES *************************************************************/
void Init_Ports(void);
void Configure_I2C2(void);

void I2C2_Write(unsigned char Add, unsigned char Data);
void Write(unsigned char Input_Data);
unsigned char I2C2_Read(unsigned char add);

void SCL_high(void);
void SCL_low(void);
//** DEFINITIONS *********************************************************************/
#define IniDs1307 0x00
#define AddRDs1307 0xD1
#define AddWDs1307 0xD0
#define CLEAR 0
#define SET 1
#define BRGValue 0x63

//** GLOBALS *************************************************************************/

unsigned char Second, Minutes, Hours, Day, Date, Month, Year;
//** MAIN CODE ***********************************************************************/
void main(void)
{
Init_Ports();
Configure_I2C2 ();


I2C2_Write(0x07,0x00); // control register add 07, 00 as i am not using swqe, rs0, rs1

I2C2_Write(0x00,0x05);// control register for seconds,also CH=0 initialized for first time (bit 7), add = 0x00, value assigned 0x05
I2C2_Write(0x01,0x07);// control register for minutes, add = 0x01, value assigned 0x07 (just to see whther the RTC works)
I2C2_Write(0x02,0x06);// control register for Hours, add = 0x02, value assigned 0x06

I2C2_Write(0x03,0x02);// control register for Day, add = 0x03, value assigned 0x02

I2C2_Write(0x04,0x03);// control register for Day, add = 0x04, value assigned 0x03
I2C2_Write(0x05,0x02);// control register for Month, add = 0x05, value assigned 0x02
I2C2_Write(0x06,0x04);// control register for Year, add = 0x06, value assigned 0x04

Second = I2C2_Read(0x00);
Minutes = I2C2_Read(0x01);
Hours = I2C2_Read(0x02);

Day = I2C2_Read(0x03);
Date = I2C2_Read(0x04);
Month = I2C2_Read(0x05);
Year = I2C2_Read(0x06);
StopI2C2();

while(SET)
{
Nop();
}
}

//*************************************************************************************
void Init_Ports(void)
{
PORTD = CLEAR;
LATD = 0b01100000;
TRISD = 0b01100000; // intialized RD6(SCL2) AND RD5(SDA2).
}
//*************************************************************************************
void Configure_I2C2 (void)
{
SSP2CON1 = 0b00001000;
SSP2STAT = 0b10000000;
SSP2ADD = 0x63;

//SSP2ADD = (BYTE) _I2C_BAUD_COUNT;
SSP2CON2bits.ACKSTAT = 1;
SSP2CON1bits.SSPEN = 1;
OpenI2C2(MASTER, SLEW_OFF);
}
//***********************************writes data to add***************************************************
void I2C2_Write(unsigned char Add, unsigned char Data)
{
PIR3bits.SSP2IF= CLEAR;

StartI2C2();

Write(0xD0);
IdleI2C2();

Write(Add);
IdleI2C2();

Write(Data);
IdleI2C2();

StopI2C2();
}
//***********************function of write***********************************
void Write(unsigned char Input_Data)
{
unsigned char data, i, Outbit;
data = Input_Data;

for (i = 0; i <= 7; i++)
{
Outbit=data&0x80;
PORTDbits.RD5 = Outbit;
data = data << 1;
SCL_high();
SCL_low();
}

while (SSP2CON2bits.ACKSTAT == 1) { }



}
//**************reads add and returs data**********************************
unsigned char I2C2_Read(unsigned char add)
{
unsigned char dataa;

StartI2C2();
Write(0xd1); // to write
IdleI2C2();



Write(add); // address to read
IdleI2C2();



if (DataRdyI2C2())
{
getcI2C2();
}
dataa = SSP2BUF;

while (SSP2CON2bits.ACKSTAT == 1) { }
PIR1bits.SSPIF = 0; // clear flag


NotAckI2C2();
StopI2C2();

//dataa = (dataa & 0x0f) + (dataa>>4)*10; // chuyenthanh so thap phan
return (dataa);
}
//*************************************************
void SCL_high(void)
{
PORTDbits.RD6 = 1;
Delay1KTCYx(1000);
}

//*************************************************
void SCL_low(void)
{
PORTDbits.RD6= 0;
Delay1KTCYx(1000);
}
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top