Continue to Site

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.

  • 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.

Hi Tech C software I2C DS1307 not working.

Status
Not open for further replies.

john blue

Member
I display the RTC clock to a hyperterminal but managed to get all 0. The clock doesnt increment and also doesnt display the preset values. Anyone can please help? Thanks in advance

**broken link removed**

**broken link removed**

Code:
#include <htc.h>
 #include <stdio.h>
 #include <conio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "../peripheral/sw_i2c.h"
 #include "../hardware/display.h"
 #include "../hardware/buttons.h"
 #include "../hardware/resistors.h"
 #include "../main/settings.h"
 #include "../utility/delay.h"
 #include "../main/isr.h"
 #include "../modes/ds1307.h"
 #include "../serial_lib.h"
 
 //==========================
 // initial DS1307
 //==========================
 void init_DS1307()
 {
    DS1307_SCL = 0;
    DS1307_SDA = 0;
    
    
    //Set Time
     write_DS1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
     write_DS1307(1,0x10); //write min 27
     write_DS1307(2,0x01); //write hour 14
     write_DS1307(3,0x02); //write day of week 2:Monday
     write_DS1307(4,0x05); // write date 17
     write_DS1307(5,0x01); // write month 6 June
     write_DS1307(6,0x09); // write year 8 --> 2008
     write_DS1307(7,0x10); //SQWE output at 1 Hz
     write_DS1307(0,0x00); //Reset second to 0 sec. and start Oscillator
 
  }
  
 //==========================
 // write data one byte to
 // DS1307
 //==========================
 void write_DS1307(unsigned short address, unsigned short data)
 {    
     short int status;
     
     di();
    
    SWStartI2C();
    SWWriteI2C(0xd0);
    SWWriteI2C(address);
    SWWriteI2C(data);
    SWStopI2C();
    SWRestartI2C();
    status=SWWriteI2C(0xd0);
    while(status==1)
    {
          SWStartI2C();
          status=SWWriteI2C(0xd0);
    }
    ei();
 }
 //==========================
 // read data one byte from D3107
 //==========================
 unsigned short read_DS1307(unsigned short address)
 {
     unsigned short data;
     
     di();
    SWStartI2C();
    SWWriteI2C(0xd0);
    SWAckI2C();
    SWWriteI2C(address);
    SWAckI2C();
    SWRestartI2C();
    SWWriteI2C(0xd1);
    SWAckI2C();
    data = SWGetsI2C(0,0);
    SWStopI2C(); 
    ei();
    return(data);
   
 }
 
 unsigned short myBcd2Dec(unsigned short bcd){
 return ((bcd >> 4)*10+(bcd & 0x0F));
 }
 
 void ds1307(void) {
     
     unsigned short sec,min,hour,day,date,month,year;
     
     init_comms();
     SYNC = 0;
     BRGH = 0;
     SPBRG = 32;//9600
     
     display_clear();
    //DelayMs(50);
    init_DS1307(); // initial DS1307
    //write_DS1307(0, sec); //SQWE output at 1 Hz
    //sec=read_DS1307(0);
    //write_DS1307(0,sec & 0x7F); // enable oscillator(bit 7 =0)
            
 
     
     
 
     while (BUTTON_1 || BUTTON_2) {
         
         
         
         if (!BUTTON_1 && BUTTON_2) {
         
         
         
         }
         while (BUTTON_1 && !BUTTON_2) {
             
              sec=read_DS1307(0);   // read second
                 min=read_DS1307(1);   // read minute
              hour=read_DS1307(2);  // read hour
              day=read_DS1307(3);   // read day
              date=read_DS1307(4);  // read date
             month=read_DS1307(5); // read month
                year=read_DS1307(6);  // read year
             putch(0x0c);
             printf("Time : %02d:%02d:%02d\r\n",hour,min,sec);
              printf("Day  : %02d\r\n",day);
              printf("Date : %02d/%02d/20%02d\r\n",date,month,year); 
              DelayMs(1000);
         }
         
     
     }    
             
     
 }
 
You have to put a small 10ms delay after each write (for internal housekeeping) or else it fails... The DS1307 expects an unsigned char address as well...

As to the clock... It wont run until the MSB of the sec's register is set to 0, pending a correct write cycle...

One last thing... ISIS has issues with pullups... Change the 10k's to PULLUP (digital) type
 
Last edited:
Yep. changed. It seems that the software I2C in Hi Tech C is not working. Am I missing out something? I have tried the software SPI as well. It is not working also.
 
I dont know where the pins are defined as I dont know where you got the software library...My version hasn't got sw_spi or sw_i2c!!

Try these routines Mike_Pearces_I2C_routines.zip

look at the start of the file....the defines have been commented out... The reason is so YOU can define the pins you are going to use.
 
Last edited:
I am using Hi Tech C v9.66. Thanks for the routine. I shall try them. Attached is the routine from Hi Tech C :)
 

Attachments

  • I2CTest.zip
    22.4 KB · Views: 310
Have you modified the sw_i2c.h file to tell the compiler which pins you are using?

This bit,
Code:
#elif defined(__18F2455) || defined(__18F2550) || \
      defined(__18F4455) || defined(__18F4550) || \
	  defined(__18F2450) || defined(__18F2458) ||\
	  defined(__18F2553) || defined(__18F4450) ||\
	  defined(__18F4458) || defined(__18F4553) 

#define  DATA_LOW   TRISB0 = 0; // define macro for data pin output
#define  DATA_HI    TRISB0 = 1; // define macro for data pin input
#define  DATA_LAT   LATB0        // define macro for data pin latch
#define  DATA_PIN   RB0         // define macro for data pin

#define  CLOCK_LOW  TRISB1 = 0; // define macro for clock pin output
#define  CLOCK_HI   TRISB1 = 1; // define macro for clock pin input
#define  SCLK_LAT   LATB1        // define macro for clock pin latch
#define  SCLK_PIN   RB1         // define macro for clock pin

Mike.
 
Last edited:
yup.

#define SW_CS_PIN PORTDbits.RD2 // Chip Select
#define TRIS_SW_CS_PIN TRISD2
#define SW_DIN_PIN PORTDbits.RD1 // Data in
#define TRIS_SW_DIN_PIN TRISD1
#define SW_DOUT_PIN PORTDbits.RD3 // Data out
#define TRIS_SW_DOUT_PIN TRISD3
#define SW_SCK_PIN PORTDbits.RD0 // Clock
#define TRIS_SW_SCK_PIN TRISD0
 
How will that overide the .h file? See edit above.

Edit, that is the spi code you have posted. This is the I²C thread.

Mike.
 
Last edited:
I've only just downloaded hitech for pic18....I normally use c18 and leave hitech for pic16 and pic12


Just remind me... Overriding the putch(); function ? Can you tell me or do I need to find it?


OK several issues.... Firstly ALL delays in sw_12c.c need to be modified to suit the FOSC, secondly if using portb make sure ADCON1 is set correctly

Third.. in the write routine the start() before the restart() is not correct... just delete the line...

lastly the swgets() in the read_ds1307() routine should be swgetc() as gets expects a length as a parameter.

You should be good to go then..
 
Last edited:
Sorry for the late reply,

here is the putch function. its needed for printf also.

Code:
void putch(unsigned char byte) {
	/* output one byte */
	while(!TRMT)	/* set whilst TX in progress */
		continue;
	TXREG = byte;
}

apparently, i just found out that no read and write from the I2C as it fails the clock test. not sure why.

Another thing, how do i modify the delay? thanks :)
 
Last edited:
The FOSC constant is set for 4mhz in the library....Once I set it to 20mhz, I increased the delays by multiplying by 5 (By the way its always best to create your own delayMs() and delayUs() for your own dev board, that way delays are constant).
 
Status
Not open for further replies.

Latest threads

Back
Top