Hi,
I have a 18F452 running @20Mhz with a 20MHz crystal; a HD44780 LCD in 4 bit mode connected to PORTD and PORTE a LED connected to PORTB.7 I have also a DS1307 connected through I2C to the 18F452 with 10k pullups at 100khz what I am seeing.. - without ds1307 functions are not called, I don't get a Square Wave output from the DS1307 -- fine. - when ds1307_init() is called, I do get a Square Wave output from the DS1307 -- fine. - when ds1307_read() is called, the LED is lit up completely indicating that MSSP is waiting for the Buffer to be full (BF) indefinitely. Now, If I don't look for that BF(Buffer Full) flag, all what I get from the ds1307_read() is 0xff Any Ideas, as to what could possibly be wrong in my code ? Thanks, Manu #include <p18f452.h> #include <stdio.h> #include <i2c.h> #include "delay.h" #include "LCD.h" #pragma config OSC = HS #pragma config PWRT = ON #pragma config WDT = OFF #pragma config LVP = OFF #define LED_PIN PORTBbits.RB7 void ds1307_write(unsigned char reg, unsigned char data) { unsigned char stat; IdleI2C(); /* wait for an idle bus */ StartI2C(); /* send START bit */ while (SSPCON2bits.SEN); /* wait till START is sent */ WriteI2C(0xd0); /* DS1307 I2C address */ AckI2C(); /* Slave ACK */ WriteI2C(reg); /* Slave register address */ AckI2C(); WriteI2C(data); /* data */ AckI2C(); StopI2C(); while (SSPCON2bits.PEN); /* wait till STOP is sent */ } void ds1307_read(unsigned char reg, unsigned char *data) { unsigned char i; IdleI2C(); StartI2C(); while (SSPCON2.bits.SEN); WriteI2C(0xd0); AckI2C(); WriteI2C(0x00); AckI2C(); StartI2C(); while (SSPCON2bits.SEN); WriteI2C(0xd1); AckI2C(); while (!SSPSTATbits.BF) { LED_PIN = 0; } LED_PIN = 1 *data = ReadI2C(); StopI2C(); while (SSPCON2bits.PEN); } #define DS1307_CTL_OUT (1 << 7) #define DS1307_CTL_SQWE (1 << 4) #define DS1307_1Hz (DS1307_CTL_OUT | DS1307_CTL_SQWE) void ds1307_init(void) { ds1307_write(0x00, 0x00); /* enable oscillator*/ ds1307_write(0x07, DS1307_1Hz); } void main(void) { TRISB = 0x00; TRISD = 0x00; TRISE = 0x00; ADCON1 = 0x0f; LED_PIN = 1; /* LED off */ lcd_init(); send_cmd(0x0c); /* turn OFF cursor */ OpenI2C(MASTER, SLEW_OFF); /* I2C Master */ SSPADD = 49; /* 100khz @ 20MHz */ ds1307_init(); set_cursor(1, 1); /* set cursor to R:1 C:1 */ printf("-- DS1307 --"); set_cursor(2, 1); while(1) { ds1307_read(0x00, &val); printf("S:%d ", val); LED_PIN = ~LED_PIN; /* blink LED */ delay250ms(); } } -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
Manu,
I have working code for the DS1337 which is the 1307 with internal crystal. I haven't used the hardware I2C so at first glance I can't say whether you have a problem there, but your init code is very different to mine. :: void ds1307_init(void) :: { :: ds1307_write(0x00, 0x00); /* enable oscillator*/ :: ds1307_write(0x07, DS1307_1Hz); Whilst I also set the time (which you don't have to, if I recall correctly you have to send an extra initialisation sequence. My code (not in C but in XCSB basic is below) /************************************************************************** *; **DS1337 automatically increments its' registers. ** *************************************************************************** */ proc ubyte init_rtc_1337() ubyte I2C_status I2C_master_write_start_condition() I2C_status = I2C_master_write_byte(WRITE_TO_SLV) if I2C_status != 0 then I2C_status = I2C_master_write_byte(DS1337_CNTRL_REG) //send control register address I2C_status = I2C_master_write_byte(DS1337_CNTRL_INIT)//send control string I2C_status = I2C_master_write_byte(DS1337_STATUS_INIT)//clear status register I2C_status = rtc_write(&rtc,DS1337_REG_NUM) //send time data to RTC I2C_master_write_stop_condition() endif return I2C_status endproc Where: /************************************************************************** ***; ** DS1337 specific, constant declarations. ** *************************************************************************** ***/ const DS1337_ADD = 0b_01101000 const WRITE_TO_SLV = DS1337_ADD << 1 const READ_SLV = WRITE_TO_SLV + 1 const DS1337_REG_NUM = 0x07 // const DS1337_BASE_REG = 0x00 //Seconds register. Chip auto-increments to next register and rolls over at 0x0f const DS1337_SET_T = DS1337_BASE_REG + 1 const DS1337_SET_A = 0x0B //const DS1337_ALARM_1_REG = 0x04 //const DS1337_ALARM_2_REG = 0x03 const DS1337_CNTRL_REG = 0x0E const DS1337_STATUS_REG = 0x0F const DS1337_CNTRL_INIT = 0x00 const DS1337_STATUS_INIT = 0x00 I'll have a closer look after I've got some sleep. Colin -- cdb, [hidden email] on 18/02/2011 Web presence: www.btech-online.co.uk Hosted by: www.justhost.com.au -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
In reply to this post by Manu Abraham-2
Hi Manu,
Had a quick glance and saw a couple of problems - doing a Project->Clean might be a good idea if you haven't already. Are there any more source files besides the MCHP ones referenced? Anyway, I have marked the things I found in the code below. On 18/02/2011 10:59, Manu Abraham wrote: > Any Ideas, as to what could possibly be wrong in my code ? > > Thanks, > Manu > > > > #include<p18f452.h> > #include<stdio.h> > #include<i2c.h> > #include "delay.h" > #include "LCD.h" > > > #pragma config OSC = HS > #pragma config PWRT = ON > #pragma config WDT = OFF > #pragma config LVP = OFF > > > #define LED_PIN PORTBbits.RB7 > > void ds1307_write(unsigned char reg, unsigned char data) > { > unsigned char stat; > > > IdleI2C(); /* wait for an idle bus */ > StartI2C(); /* send START bit */ > while (SSPCON2bits.SEN); /* wait till START is sent */ > WriteI2C(0xd0); /* DS1307 I2C address */ > AckI2C(); /* Slave ACK */ > WriteI2C(reg); /* Slave register address */ > AckI2C(); > WriteI2C(data); /* data */ > AckI2C(); > StopI2C(); > while (SSPCON2bits.PEN); /* wait till STOP is sent */ > } > > void ds1307_read(unsigned char reg, unsigned char *data) > { > unsigned char i; > > > IdleI2C(); > StartI2C(); > while (SSPCON2.bits.SEN); Dot in between SSPCON2 and bits is wrong, should be SSPCON2bits.SEN > WriteI2C(0xd0); > AckI2C(); > WriteI2C(0x00); > AckI2C(); > > > StartI2C(); > while (SSPCON2bits.SEN); > WriteI2C(0xd1); > AckI2C(); > > while (!SSPSTATbits.BF) { > LED_PIN = 0; > } > LED_PIN = 1 No semicolon to end statement above > *data = ReadI2C(); > StopI2C(); > while (SSPCON2bits.PEN); > } > > #define DS1307_CTL_OUT (1<< 7) > #define DS1307_CTL_SQWE (1<< 4) > > #define DS1307_1Hz (DS1307_CTL_OUT | DS1307_CTL_SQWE) > > void ds1307_init(void) > { > ds1307_write(0x00, 0x00); /* enable oscillator*/ > ds1307_write(0x07, DS1307_1Hz); > } > > > void main(void) > { > TRISB = 0x00; > TRISD = 0x00; > TRISE = 0x00; > ADCON1 = 0x0f; > LED_PIN = 1; /* LED off */ > > lcd_init(); > send_cmd(0x0c); /* turn OFF cursor */ > > OpenI2C(MASTER, SLEW_OFF); /* I2C Master */ > SSPADD = 49; /* 100khz @ 20MHz */ > > ds1307_init(); > > set_cursor(1, 1); /* set cursor to R:1 C:1 */ > printf("-- DS1307 --"); > > set_cursor(2, 1); > while(1) { > ds1307_read(0x00,&val); > printf("S:%d ", val); Where is val declared? > LED_PIN = ~LED_PIN; /* blink LED */ > delay250ms(); > } > } -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
Hi Oli,
On Fri, Feb 18, 2011 at 6:43 PM, Oli Glaser <[hidden email]> wrote: > Hi Manu, > > Had a quick glance and saw a couple of problems - doing a Project->Clean > might be a good idea if you haven't already. I almost do a Project->Clean, before flashing the pic, each time. > Are there any more source files besides the MCHP ones referenced? No, that's all. > Anyway, I have marked the things I found in the code below. > > On 18/02/2011 10:59, Manu Abraham wrote: >> Any Ideas, as to what could possibly be wrong in my code ? >> >> Thanks, >> Manu >> >> >> >> #include<p18f452.h> >> #include<stdio.h> >> #include<i2c.h> >> #include "delay.h" >> #include "LCD.h" >> >> >> #pragma config OSC = HS >> #pragma config PWRT = ON >> #pragma config WDT = OFF >> #pragma config LVP = OFF >> >> >> #define LED_PIN PORTBbits.RB7 >> >> void ds1307_write(unsigned char reg, unsigned char data) >> { >> unsigned char stat; >> >> >> IdleI2C(); /* wait for an idle bus */ >> StartI2C(); /* send START bit */ >> while (SSPCON2bits.SEN); /* wait till START is sent */ >> WriteI2C(0xd0); /* DS1307 I2C address */ >> AckI2C(); /* Slave ACK */ >> WriteI2C(reg); /* Slave register address */ >> AckI2C(); >> WriteI2C(data); /* data */ >> AckI2C(); >> StopI2C(); >> while (SSPCON2bits.PEN); /* wait till STOP is sent */ >> } >> >> void ds1307_read(unsigned char reg, unsigned char *data) >> { >> unsigned char i; >> >> >> IdleI2C(); >> StartI2C(); >> while (SSPCON2.bits.SEN); > > Dot in between SSPCON2 and bits is wrong, should be SSPCON2bits.SEN Actually a error in copy-paste, while trying to add in spaces instead of tabs This is the same as all other SSPCON2bits.SEN as referenced other in the same function. >> WriteI2C(0xd0); >> AckI2C(); >> WriteI2C(0x00); >> AckI2C(); >> >> >> StartI2C(); >> while (SSPCON2bits.SEN); >> WriteI2C(0xd1); >> AckI2C(); >> >> while (!SSPSTATbits.BF) { >> LED_PIN = 0; >> } >> LED_PIN = 1 > > No semicolon to end statement above The semicolon also exists, but just got vanished in the process.. >> *data = ReadI2C(); >> StopI2C(); >> while (SSPCON2bits.PEN); >> } >> >> #define DS1307_CTL_OUT (1<< 7) >> #define DS1307_CTL_SQWE (1<< 4) >> >> #define DS1307_1Hz (DS1307_CTL_OUT | DS1307_CTL_SQWE) >> >> void ds1307_init(void) >> { >> ds1307_write(0x00, 0x00); /* enable oscillator*/ >> ds1307_write(0x07, DS1307_1Hz); >> } >> >> >> void main(void) >> { >> TRISB = 0x00; >> TRISD = 0x00; >> TRISE = 0x00; >> ADCON1 = 0x0f; >> LED_PIN = 1; /* LED off */ >> >> lcd_init(); >> send_cmd(0x0c); /* turn OFF cursor */ >> >> OpenI2C(MASTER, SLEW_OFF); /* I2C Master */ >> SSPADD = 49; /* 100khz @ 20MHz */ >> >> ds1307_init(); >> >> set_cursor(1, 1); /* set cursor to R:1 C:1 */ >> printf("-- DS1307 --"); >> >> set_cursor(2, 1); >> while(1) { >> ds1307_read(0x00,&val); >> printf("S:%d ", val); > > Where is val declared? It's there in main itself.. unsigned char val = 0; but went missing >> LED_PIN = ~LED_PIN; /* blink LED */ >> delay250ms(); >> } >> } > > Additionally, I put the same source here as well. http://202.88.242.108:8000/test/main.c Any other thoughts ? Thanks, Manu -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
In reply to this post by CDB-3
Hi Colin,
On Fri, Feb 18, 2011 at 5:26 PM, cdb <[hidden email]> wrote: > Manu, > > I have working code for the DS1337 which is the 1307 with internal crystal. > > I haven't used the hardware I2C so at first glance I can't say whether you > have a problem there, but your init code is very different to mine. > > :: void ds1307_init(void) > :: { > :: ds1307_write(0x00, 0x00); /* enable oscillator*/ > > :: ds1307_write(0x07, DS1307_1Hz); > > Whilst I also set the time (which you don't have to, if I recall correctly > you have to send an extra initialisation sequence. My code (not in C but in > XCSB basic is below) > > /************************************************************************** > *; > **DS1337 automatically increments its' registers. > ** > *************************************************************************** > */ > > proc ubyte init_rtc_1337() > > ubyte I2C_status > > I2C_master_write_start_condition() > I2C_status = I2C_master_write_byte(WRITE_TO_SLV) > > if I2C_status != 0 then > > I2C_status = I2C_master_write_byte(DS1337_CNTRL_REG) //send control > register address > I2C_status = I2C_master_write_byte(DS1337_CNTRL_INIT)//send control > string > I2C_status = I2C_master_write_byte(DS1337_STATUS_INIT)//clear status > register > I2C_status = rtc_write(&rtc,DS1337_REG_NUM) //send time data to RTC > I2C_master_write_stop_condition() > > endif > > return I2C_status > > endproc > > Where: > > /************************************************************************** > ***; > ** DS1337 specific, constant declarations. > ** > *************************************************************************** > ***/ > > const DS1337_ADD = 0b_01101000 > const WRITE_TO_SLV = DS1337_ADD << 1 > const READ_SLV = WRITE_TO_SLV + 1 > const DS1337_REG_NUM = 0x07 // > const DS1337_BASE_REG = 0x00 //Seconds register. Chip auto-increments to > next register and rolls over at 0x0f > > const DS1337_SET_T = DS1337_BASE_REG + 1 > const DS1337_SET_A = 0x0B > //const DS1337_ALARM_1_REG = 0x04 > //const DS1337_ALARM_2_REG = 0x03 > const DS1337_CNTRL_REG = 0x0E > const DS1337_STATUS_REG = 0x0F > const DS1337_CNTRL_INIT = 0x00 > const DS1337_STATUS_INIT = 0x00 > > I'll have a closer look after I've got some sleep. > > Colin Thanks for your input. - The DS1307 and DS1337 are slightly different in terms of the registers on them, - but the I2C communication sequences on both of them are identical. - The DS1307 has an oscillator enable bit at 0x00 while the DS1337 has it at 0x0e - The DS1307 doesn't have the Alarm functionality of the DS1337 - Now there is the DS1337C which has a builtin crystal oscillator in comparison to the vanilla DS1337 ? Now, I fail to understand your init_rtc_1337() You do: - Master send START - Master Write DS1337_ADD - Master Write 0x0e - Master Write 0x00 - Master Write 7 bytes ??? All what you need to do is to enable the oscillator to initialize the RTC. Writing to the other registers are optional, which is for setting up time etc. I don't see how that affects the reads ? I can't identify what I am doing wrong, but ... Thanks, Manu -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
In reply to this post by Manu Abraham-2
On 18/02/2011 13:30, Manu Abraham wrote:
> Any other thoughts ? > > Just one right now - in the read routine, have you tried a *repeated* start after you write the address instead of a normal start? I do have some DS1307 code here (not based on MCHP library, though I can send you for reference if you want it) -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
On Fri, Feb 18, 2011 at 7:29 PM, Oli Glaser <[hidden email]> wrote:
> On 18/02/2011 13:30, Manu Abraham wrote: >> Any other thoughts ? >> >> > > Just one right now - in the read routine, have you tried a *repeated* > start after you write the address instead of a normal start? Just now after your comment, I tried replacing the second StartI2C() with RestartI2C() with no visible changes. > I do have some DS1307 code here (not based on MCHP library, though I can > send you for reference if you want it) That would be great. Thanks, Manu -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
In reply to this post by Oli Glaser
Hi,
On Fri, Feb 18, 2011 at 7:29 PM, Oli Glaser <[hidden email]> wrote: > On 18/02/2011 13:30, Manu Abraham wrote: >> Any other thoughts ? >> >> > > Just one right now - in the read routine, have you tried a *repeated* > start after you write the address instead of a normal start? > I do have some DS1307 code here (not based on MCHP library, though I can > send you for reference if you want it) I have been looking through it, but the communication sequences seemed looked much the same. So, I have been looking at the assembly level instructions in detail. This is where I landed up: in Microchip's i2c.h, I do not find any place where SSPCON2bits.RCEN is enabled ? A bit confused. Any idea why RCEN is not used ? Best Regards, Manu -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
On Sat, Feb 19, 2011 at 12:23 AM, Manu Abraham <[hidden email]> wrote:
> Hi, > > > On Fri, Feb 18, 2011 at 7:29 PM, Oli Glaser <[hidden email]> wrote: >> On 18/02/2011 13:30, Manu Abraham wrote: >>> Any other thoughts ? >>> >>> >> >> Just one right now - in the read routine, have you tried a *repeated* >> start after you write the address instead of a normal start? >> I do have some DS1307 code here (not based on MCHP library, though I can >> send you for reference if you want it) > > I have been looking through it, but the communication sequences seemed > looked much the same. > So, I have been looking at the assembly level instructions in detail. > > This is where I landed up: in Microchip's i2c.h, I do not find any > place where SSPCON2bits.RCEN is enabled ? > > A bit confused. Any idea why RCEN is not used ? After digging here and hunting there, eventually a delay did the trick. Phew .. Best Regards, Manu -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
On 18/02/2011 20:54, Manu Abraham wrote:
> On Sat, Feb 19, 2011 at 12:23 AM, Manu Abraham<[hidden email]> wrote: >> Hi, >> >> >> On Fri, Feb 18, 2011 at 7:29 PM, Oli Glaser<[hidden email]> wrote: >>> On 18/02/2011 13:30, Manu Abraham wrote: >>>> Any other thoughts ? >>>> >>>> >>> Just one right now - in the read routine, have you tried a *repeated* >>> start after you write the address instead of a normal start? >>> I do have some DS1307 code here (not based on MCHP library, though I can >>> send you for reference if you want it) >> I have been looking through it, but the communication sequences seemed >> looked much the same. >> So, I have been looking at the assembly level instructions in detail. >> >> This is where I landed up: in Microchip's i2c.h, I do not find any >> place where SSPCON2bits.RCEN is enabled ? >> >> A bit confused. Any idea why RCEN is not used ? > > After digging here and hunting there, eventually a delay did the trick. > > Phew .. > > Best Regards, > Manu Good stuff, so its working okay now? Out of interest (mainly wondering if it's a problem with the MC code) where did you put the delay? (had a quick look, but I can't see one in the code I sent, just waitforidle) -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
On Sat, Feb 19, 2011 at 2:54 AM, Oli Glaser <[hidden email]> wrote:
> On 18/02/2011 20:54, Manu Abraham wrote: >> On Sat, Feb 19, 2011 at 12:23 AM, Manu Abraham<[hidden email]> wrote: >>> Hi, >>> >>> >>> On Fri, Feb 18, 2011 at 7:29 PM, Oli Glaser<[hidden email]> wrote: >>>> On 18/02/2011 13:30, Manu Abraham wrote: >>>>> Any other thoughts ? >>>>> >>>>> >>>> Just one right now - in the read routine, have you tried a *repeated* >>>> start after you write the address instead of a normal start? >>>> I do have some DS1307 code here (not based on MCHP library, though I can >>>> send you for reference if you want it) >>> I have been looking through it, but the communication sequences seemed >>> looked much the same. >>> So, I have been looking at the assembly level instructions in detail. >>> >>> This is where I landed up: in Microchip's i2c.h, I do not find any >>> place where SSPCON2bits.RCEN is enabled ? >>> >>> A bit confused. Any idea why RCEN is not used ? >> >> After digging here and hunting there, eventually a delay did the trick. >> >> Phew .. >> >> Best Regards, >> Manu > > Good stuff, so its working okay now? > Out of interest (mainly wondering if it's a problem with the MC code) > where did you put the delay? (had a quick look, but I can't see one in > the code I sent, just waitforidle) Actually, I would have lost motivation and been searching all places as to what could be wrong. The code what you sent me (since it looked more the same with regards to the communication sequence), gave me a boost as to still look more deeper as to what probably could be wrong. I have factored out and put the now working one here: http://202.88.242.108:8000/test/ds1307.c http://202.88.242.108:8000/test/ds1307.h Thanks again, for the help. Best Regards, Manu -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist |
In reply to this post by Manu Abraham-2
|
In reply to this post by Manu Abraham-2
|
This post has NOT been accepted by the mailing list yet.
In reply to this post by Manu Abraham-2
Hi Manu,
Now, your link can't open please sent me or up date link again. http://202.88.242.108:8000/test/ds1307.c http://202.88.242.108:8000/test/ds1307.h Thank you very much. SURIYA T. |
Free forum by Nabble | Edit this page |