siva raja

plz check this code for DS1307 interface

Discussion created by siva raja on Apr 10, 2007
Latest reply on Apr 10, 2007 by MIKE YETSKO
hi all
   i used the port b pins for interfacing the RTC
can any one check this code and inform me about my mistake
  
#include <mc9s12xdt512.h>     /* derivative information */
#include <stdio.h>
#include "Std_Types.h"
#define SDA      PORTB_PB5//PTC_PTC6
#define SCL      PORTB_PB6//PTC_PTC5
#define DIR_SDA  DDRB_DDRB5//DDRC_DDRC6
#define DIR_SCL  DDRB_DDRB6//DDRC_DDRC5
#define SPEED  4              
#define HOLD   1                       
#define SLAVE_ADDRESS   0xD0  
#define LEN             58    
byte temp, dummy, RD_data, WR_data, ACK_flag, w, i;
byte own_sl_addr = 0xD0; 
byte RAM_Data[LEN];
byte RTC_reg[7] @0x700;   
byte Cntr_reg  @0x707;
typedef struct
{
 byte Sec;
 byte Min;
 byte Hrs;
 byte Day;
 byte Date;
 byte Month;
 byte Year;
}sRTC;
volatile sRTC RTC @ 0x700s;   
#define Seconds         RTC.Sec
#define Minutes         RTC.Min
#define Hours           RTC.Hrs
#define Day             RTC.Day
#define Date            RTC.Date
#define Month           RTC.Month
#define Year            RTC.Year
#define RTC_REG_START   0x00
#define NV_RAM_START    0x08 
/* Function definition */
void Init_Time_Default(void);
byte GetByte(void);
void WriteIICbus(byte data);
void Write_Control_reg(byte ctrl);
void Read_ACK(void);
void Write_ACK(void);
void StartBit(void);
void StopBit(void);
void Wait_time(byte time);
void Wait_sh(byte time);
void Init_Time_Default(void)  // see datasheet of DS1307;
{
 Seconds = 0x20;
 Minutes = 0x10;
 Hours = 0x12;
 Day =0x4;      // day of week;
 Date = 0x11;
 Month = 0x5;
 Year = 0x6;
}
//********************************************************************
void Write_Control_reg(byte ctrl)  // writes Cntr_reg to RTC;
{
 ACK_flag = 0;
 StartBit();
 WriteIICbus(SLAVE_ADDRESS);
 asm("nop");
 Read_ACK();
 asm("nop");
 if(ACK_flag !=0)
 {
  StopBit();
  return;
 }
 WriteIICbus(7);  // write address of the Control register;
 Read_ACK();
 if(ACK_flag !=0)
 {
  StopBit();
  return;
 }
 WriteIICbus(ctrl);  // write value into Control register;
 Read_ACK();
 if(ACK_flag !=0)
 {
  StopBit();
  return;
 }
 StopBit();
}
//********************************************************************
void Read_Control_reg(void)   // reads Cntr_reg from RTC;
{
 ACK_flag = 0;
 StartBit();
 WriteIICbus(SLAVE_ADDRESS); // RW bit = 0;
 asm("nop");
 Read_ACK();
 asm("nop");
 if(ACK_flag !=0)
 {
  StopBit();
  return;
 }
 WriteIICbus(7);  // write address of the Control register;
 Read_ACK();
 if(ACK_flag !=0)
 {
  StopBit();
  return;
 }
 StartBit();
 WriteIICbus(SLAVE_ADDRESS | 1); // RW bit = 1;
 Read_ACK();
 if(ACK_flag !=0)
 {
  StopBit();
  return;
 }
 DIR_SDA = 0;
 Cntr_reg = GetByte();  // read value from the Control register and save it;
 StopBit();
}
//********************************************************************
void WriteRTC(void)
{
 ACK_flag = 0;
 StartBit();
 WriteIICbus(SLAVE_ADDRESS);
 asm("nop");
 Read_ACK();
 asm("nop");
 if(ACK_flag !=0)
 {
  StopBit();
  return;
 }
 WriteIICbus(0);  // Starts write from address "0" in RTC (Seconds);
 Read_ACK();
 if(ACK_flag !=0)
 {
  StopBit();
  return;
 }
 for(i=0;i<7;i++)
 {
  WriteIICbus(RTC_reg[i]);
  Read_ACK();
  if(ACK_flag !=0)
  {
    StopBit();
    return;
  }
 }
 StopBit();
}
//********************************************************************
byte ReadRTCbyte(byte addr)
{
 ACK_flag = 0;
 StartBit();
 WriteIICbus(SLAVE_ADDRESS); // RW bit = 0;
 asm("nop");
 Read_ACK();
 asm("nop");
 if(ACK_flag !=0)
 {
  StopBit();
  return 0;
 }
 WriteIICbus(addr);
 Read_ACK();
 if(ACK_flag !=0)
 {
  StopBit();
  return 0;
 }
 StartBit();
 WriteIICbus(SLAVE_ADDRESS | 1); // RW bit = 1;
 Read_ACK();
 if(ACK_flag !=0)
 {
  StopBit();
  return 0;
 }
 DIR_SDA = 0;
 RD_data = GetByte();
 StopBit();
 return RD_data;
}
//********************************************************************
byte GetByte(void)
{
 byte tempb, i;
 
 tempb = 0;
 for(i=0;i<8;i++)
 {
  tempb = tempb << 1;
  SCL = 1;
  Wait_sh(SPEED);
  tempb = tempb | SDA;
  SCL = 0;
  Wait_sh(SPEED - HOLD);
 }
 return tempb;
}
//********************************************************************
void WriteIICbus(byte data)
{
 byte tempb, i;
 
 tempb = data;
 for(i=0;i<8;i++)
 {
  SDA = ((tempb & 0x80)>>7);
  tempb = tempb << 1;
  Wait_sh(HOLD);
  SCL = 1;
  Wait_sh(SPEED);
  SCL = 0;
  Wait_sh(SPEED - HOLD);
 }
}
//********************************************************************
void Read_ACK(void)
{
 DIR_SDA = 0;
 Wait_sh(HOLD);
 SCL = 1;
 Wait_sh(SPEED);
 ACK_flag = SDA;
 SCL = 0;
 DIR_SDA = 1;
}
//********************************************************************
void Write_ACK(void)
{
 DIR_SDA = 1;
 SDA = 0;
 Wait_sh(HOLD);
 SCL = 1;
 Wait_sh(SPEED);
 SCL = 0;
 Wait_sh(HOLD);
}
//********************************************************************
void StartBit(void)
{
 SDA = 1;
 SCL = 1;
 DIR_SDA = 1;
 DIR_SCL = 1;
 Wait_sh(SPEED);
 Wait_sh(SPEED);
 SDA = 0;
 Wait_sh(SPEED);
 SCL = 0;
 Wait_sh(SPEED);
}
//********************************************************************
void StopBit(void)
{
 Wait_sh(HOLD);
 SCL = 1;
 Wait_sh(SPEED);
 SDA = 1;
}
//***************************************************************************
void Wait_time(byte time)
{
 const word k = 250;
 word i, temp_t = (time*k);
 for (i=0;i<temp_t;i++)
    {
    asm("nop");
    }
}
//********************************************************************
void Wait_sh(byte time)
{
 word i, temp_t = time;
 for (i=0;i<temp_t;i++)
    {
    asm("nop");
    }
}
//********************************************************************
void main (void)
{
 DDRB_DDRB6=1 ;
 DDRB_DDRB5=1 ;
 PERP= 0xFF;
 
 EnableInterrupts; /* enable interrupts */
 
 Write_Control_reg(3);  // squarewave output 32.768kHz at RTC SQW pin;
 Init_Time_Default();
 WriteRTC();
 for(;:smileywink:
 {
 
  Hours   = ReadRTCbyte(2);
  Minutes = ReadRTCbyte(1);
  Seconds = ReadRTCbyte(0); 
   
  
 } /* loop forever */
 /* please make sure that you never leave this function */
 

Outcomes