sw_i2c and ds1307

sw_i2c and ds1307

Postby phoenix2000 » Thu Jul 10, 2014 5:53 am

hello everybody ;)
i 'm trying to run ds1307 with software i2c .after so much effort i could do it.but there is a thing which is unsolved for me ,and it's that i don't understand why should i use restart instruction to get the result(instead of using start)? and by the way , it 's good to know that in MPLAB_C18_Libraries.pdf example for sw_i2c it started with SWStartI2C(); (not SWRestartI2C();).
so why?should we always use SWRestartI2C(); at first? or it is only for ds1307?
ps:the code bellow ,make a 1 Hz wave on out pin.
Code: Select all
#include <P18f4550.h>
#include <stdio.h>
#include <stdlib.h>
#include <delays.h>
#include "sw_i2c.h"

//*****************************************

#pragma config WDT=OFF
#pragma config FOSC=HS// our crystal is 4 MHZ
#pragma config CPUDIV=OSC1_PLL2
#pragma config LVP=OFF
//*****************************************
unsigned char var=0;
//***************************************
void main(void)
{
 ADCON1=0X0F; //All analog port are digitaled

  SWRestartI2C();
  //SWStartI2C();  //with this code it didn't work!!
  var=SWPutcI2C(0xD0);// control byte
  SWAckI2C();
  var=SWPutcI2C(0x07); // word address
  SWAckI2C();
  var=SWPutcI2C(0x10); // data
  SWAckI2C();
  SWStopI2C();
}

User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: sw_i2c and ds1307

Postby ric » Thu Jul 10, 2014 9:25 am

Is that your entire test program?
Where is your initialisation code setting pin directions, initial states etc. ?
I suspect you are just getting away with using the "restart" call which does more initialisation for you. If the pins were in the right state to start with, then the "start" call would work also.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 434
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: sw_i2c and ds1307

Postby phoenix2000 » Fri Jul 11, 2014 5:02 am

thanks for your answer .yep .it was all because of the codes in swstri2c.c file that i added.and as you said in swrsti2c.c there were more codes and it was more complete.but why??!i also understand that there isn't any different betwen start and restart.in other word restart is a start cammand without command to stop.so then why microchip define another .c file for this and why codes in swstri2c.c and swrsti2c.c differs from each other ?
User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: sw_i2c and ds1307

Postby ric » Fri Jul 11, 2014 5:29 am

There is a difference.
START assumes that SDA and SCL are high, and outputs a start sequence, which consists of pulling SDA down, then SCL down.
RESTART assumes the bus is already active, i.e. both SDA and SCL are already low. It does: SDA high, SCL high, SDA low, SCL low
This is different to sending a STOP and then a START, which would be: SCL high, SDA high, SDA low, SCL low

In a system with only a single master, there is no functional difference between:
sending a STOP then a START
and
sending RESTART.

The only time you actually need to use RESTART is in a multi-master system, to make sure another master does not try to grab the bus between the STOP and the START.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 434
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: sw_i2c and ds1307

Postby phoenix2000 » Fri Jul 11, 2014 7:12 am

yeah ,you are right but then ,why it couldn't work with start command first.Is it because of that , at first we turn on the micro the clock pin is not set?!by the way in data sheet it says that on power on reset the pins are input so it cause to set state at the beginig for clock(becaus it is pulled up)
in bellow i put the code of start and restart of microchip library c.18 v3.47
Code: Select all
#include <p18cxxx.h>
#include <delays.h>
#include "i2c_data.h"

/********************************************************************
*     Function Name:    void SWStartI2C(void)                       *
*     Return Value:     void                                        *
*     Parameters:       void                                        *
*     Description:      Send I2C bus start condition.               *
********************************************************************/
void SWStartI2C( void )
{
  DATA_LAT = 0;                   // set data pin latch to 0
  DATA_LOW;                       // set pin to output to drive low
  Delay10TCY();                   // user may need to modify based on Fosc
}


Code: Select all

#include <p18cxxx.h>
#include <delays.h>
#include "i2c_data.h"

/********************************************************************
*     Function Name:    void SWRestartI2C(void)                     *
*     Return Value:     void                                        *
*     Parameters:       void                                        *
*     Description:      Send I2C bus restart condition.             *
********************************************************************/
void SWRestartI2C( void )
{
  SCLK_LAT = 0;                   // set clock pin latch to 0
  CLOCK_LOW;                      // set clock pin to output to drive low
  DATA_HI;                        // release data pin to float high
  Delay10TCY();                   // user may need to modify based on Fosc
  CLOCK_HI;                       // release clock pin to float high
  Delay10TCY();                   // user may need to modify based on Fosc
  DATA_LAT = 0;                   // set data pin latch to 0
  DATA_LOW;                       // set data pin output to drive low
  Delay10TCY();                   // user may need to modify based on Fosc
}
User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: sw_i2c and ds1307

Postby phoenix2000 » Sat Jul 12, 2014 8:38 pm

isn't any idea ?!!i'm waiting...no body ever used sw_i2c library of microchip? :(
User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: sw_i2c and ds1307

Postby ric » Sat Jul 12, 2014 11:38 pm

Not me. I find libraries a waste of time. It's far quicker to read the datasheet and do your own code. Then you know what it does, rather than spending more time just trying to work out how you are meant to use poorly documented library routines.

For starters, that START routine only takes SDA low, when a complete START condition also requires SCL to go low.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 434
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: sw_i2c and ds1307

Postby Olin Lathrop » Sun Jul 13, 2014 12:32 am

phoenix2000 wrote:no body ever used sw_i2c library of microchip?


Maybe somebody, but using library routines for something that simple is just silly. As you found out, you don't know what those routines really do, and they often don't integrate nicely into the rest of your system. It takes more time and trouble to vet someone else's code for something this simple than to just write it yourself. Then you have something that does exactly what you want and you know exactly what it does.
User avatar
Olin Lathrop
Verified identity
 
Posts: 48
Joined: Fri May 30, 2014 3:38 pm
Location: Littleton, MA USA
PIC experience: Professional 5+ years with MCHP products

Re: sw_i2c and ds1307

Postby phoenix2000 » Sun Jul 13, 2014 12:43 am

thanks for your help ,dear Ric
yep .maybe you are right but i prefer to use an open source library which is developed by professional engineers in microchip company,analyze and change it by considering my need.any way thank u again for answering my question ,it helped me so much :D ;)
User avatar
phoenix2000
 
Posts: 20
Joined: Thu Jul 10, 2014 3:36 am
PIC experience: EE Student

Re: sw_i2c and ds1307

Postby ric » Sun Jul 13, 2014 12:49 am

phoenix2000 wrote:...
developed by professional engineers in microchip company,


looking at the code in the libraries, more often the expression "vacation students" comes to mind.

Certainly, look at the source in the libraries to get ideas, but don't take it as always the best way to achieve a goal.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 434
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Next

Return to SSP (IIC, SPI)

Who is online

Users browsing this forum: No registered users and 1 guest

cron