CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

I2C Problems on PIC16F18345

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

I2C Problems on PIC16F18345
PostPosted: Thu Mar 10, 2016 2:50 am     Reply with quote

Hi everybody.
Today I have bought a new PIC16F18345 to develop a IoT project like as an example of Microchip.
I want to use CCS C complier(ver5.051) to write a program, also the last time i don't use MPLAB IDE.
IN my project, I want to interface with DS1307+ RTC IC, I tried this with PIC18F4680 and success, but when I port program into PIC16F18345, I have a problems, the program is stop when calling I2C function.
I'm using led to debug this error, if I comment I2C command, the led is blink in main loop.
Please help me to solve this problem.

Main program:
Code:


#include "16F18345.h"

// Config 1
#fuses NOCLKOUT
#fuses FCMEN

// Config 2
#fuses MCLR
#fuses NOPUT
#fuses NOWDT
#fuses NOLPBOR
#fuses NOBROWNOUT
#fuses BORV24
#fuses NOPPS1WAY
#fuses NOSTVREN
#fuses NODEBUG

// Config 3
#fuses NOWRT
#fuses NOLVP

// Config 4
#fuses PROTECT
#fuses NOCPD

#use delay(internal=32000000)

#define DS1307_SDA  PIN_B4
#define DS1307_SCL  PIN_B6   
#include <ds1307.c>

unsigned int16 count_t0=0;
unsigned int8 stp_mt1=0, stp_mt2=0;
unsigned int1 update_mt1=0, update_mt2=1;

//===========================================================================================================
//===========================================================================================================
void main(void)
{
   delay_ms(100);
   setup_oscillator(OSC_HFINTRC_32MHZ|OSC_CLK_DIV_BY_1|OSC_CLOCK_SWITCH_HOLD ); 
   delay_ms(100);
   
   init_ds1307 ();

   sec = read_ds1307(0);
   
   write_ds1307 (0,sec & 0x7F);    // enable oscillator (bit 7 = 0)
   write_ds1307 (7, 0x10);          // 1HZ, SW out     
 
    for(;;)
   {
      output_toggle(PIN_B7);
      delay_ms(500);
   }

}
 
}

_________________
Begin Begin Begin !!!


Last edited by tienchuan on Thu Mar 10, 2016 7:46 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 10, 2016 6:17 am     Reply with quote

OK...
first, reduce program to just RTC related So delete all variables and main() code like those case() statements.
Second, do you have the correct I2C bus pullup resistors. For 5 V system, 3k3 or 4k7 should work.
Third, do you have a battery attached to the DS1307 ? It needs one to work normal.
Fourth, it isn't good to have the 'protect' fuse enabled until the FINAL code is 'up and running', even then ONLY for commercial products.
Fifth, post where you got the ds1307.c driver from. It may have errors in it.

Jay
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Mar 10, 2016 7:53 am     Reply with quote

Thanks for your reply.
In my circuit, have a pull-up resistor 4.7k with vdd 3.3v.
Also have a 3v battery's for DS1307.
I don't remember where I get this driver but I tested it work good on Pic18F.
I think maybe an error when config pin i2c.
I'll tried with your suggestion.
Thanks you
_________________
Begin Begin Begin !!!
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 10, 2016 8:20 am     Reply with quote

You need to look at the datasheet, page 411, figure 34-1 regarding the valid 'voltage vs frequency'.

It shows you cannot run that PIC at 32Mhz on a 3 volt supply. You should be able to run at 16 MHZ, so try that.

Also, when using any I2C device, use PCM P's 'I2C scanner' program to confirm the PIC 'sees' the I2C device. The scanner is located in the 'code library'. Like a 1Hz LED program, it's a 'must run' !

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Thu Mar 10, 2016 8:29 am     Reply with quote

Whoa.

Vdd=3.3v

The DS1307, is not rated to run at 3.3v. It requires it's Vdd to be at least 1.25*Vbat, before it will operate. 4.5v minimum (Vbat is allowed to be up to 3.5v).

Not going to work.
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Mar 10, 2016 8:33 am     Reply with quote

Ttelmah wrote:
Whoa.

Vdd=3.3v

The DS1307, is not rated to run at 3.3v. It requires it's Vdd to be at least 1.25*Vbat, before it will operate. 4.5v minimum (Vbat is allowed to be up to 3.5v).

Not going to work.

Hi
I'm connect VCC pin of DS1307 to VCC 5V, and pullup resistor 4.7K to 3.3V with SCL, SDA, SW.
Can I run a cỉrcuit with this config?
Thanks
_________________
Begin Begin Begin !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Thu Mar 10, 2016 9:02 am     Reply with quote

Why not run the processor at the same voltage?. Three supplies for two chips seems distinctly 'OTT'....

You really need s bit lower pull-up, I2C pull-ups are based on _current_, and with a lower supply voltage, the pull-up has to have a similarly lower value. 4K7, is OK with 5v, but with 3.3v, something like 2K2 is needed. You also should specify the speed in the I2C setup. If your chip is running at 32MHz, there is a good chance the I2C is running too fast for the RTC.
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Mar 10, 2016 6:53 pm     Reply with quote

Ttelmah wrote:
Why not run the processor at the same voltage?. Three supplies for two chips seems distinctly 'OTT'....

You really need s bit lower pull-up, I2C pull-ups are based on _current_, and with a lower supply voltage, the pull-up has to have a similarly lower value. 4K7, is OK with 5v, but with 3.3v, something like 2K2 is needed. You also should specify the speed in the I2C setup. If your chip is running at 32MHz, there is a good chance the I2C is running too fast for the RTC.

Thanks for your suggestions.
Beacause in my circuit'll builtin RF module and BLE module with 3.3V, so that I choose 3.3V for power supply for MCU and modules.
I choosed DS1307 because it's cheapest, easy to buy in my location.
I think I'll read back I2C datasheet and trying with your suggestion.
Thanks you.
_________________
Begin Begin Begin !!!
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Mar 10, 2016 7:01 pm     Reply with quote

temtronic wrote:
You need to look at the datasheet, page 411, figure 34-1 regarding the valid 'voltage vs frequency'.

It shows you cannot run that PIC at 32Mhz on a 3 volt supply. You should be able to run at 16 MHZ, so try that.

Also, when using any I2C device, use PCM P's 'I2C scanner' program to confirm the PIC 'sees' the I2C device. The scanner is located in the 'code library'. Like a 1Hz LED program, it's a 'must run' !

Jay

Thanks for your suggestion.
Really I have design this circuit have a same with IoT sensor Badge of Microchip example. And in our program, they config PIC run 32 MHZ with 3.3V power supply, sothat i think a power supply is enough.
About the way to detect I2C device, can you show me clear "PCM P's 'I2C scanner"?
Thanks you
_________________
Begin Begin Begin !!!
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 10, 2016 8:02 pm     Reply with quote

this...
http://www.ccsinfo.com/forum/viewtopic.php?t=49713
... should get you to his program.

It's a 'sticky' about 5,6 down in the code library.

I've downloaded uC current datasheet and it looks like it's good from 2.5 V nd run at 32MHz, so if you an get the 1Hz LED program to run then the PIC is OK.


Jay
tienchuan



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

PostPosted: Thu Mar 10, 2016 10:39 pm     Reply with quote

Hi everybody.
Today I tried to resolder a new DS1307 SMD IC, and feel amazing with result, I can get 1Hz pulse out from DS1307.
Sorry about this problems, maybe an IC buy from china not ensure about quality.
Adding, about SRAM of PIC16F18345, I intend using to save data when power loss, but in CCSC, Can I declare a new variable on SRAM and recovery it when power on again.
THanks for yours help.
_________________
Begin Begin Begin !!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Fri Mar 11, 2016 2:46 am     Reply with quote

When power goes off, the SRAM is lost. All your variables are in SRAM. This is the chips working memory. What you can do is put the chip to sleep. With this done, and power still applied, the CPU stops, but the SRAM is maintained. Done properly the chip will only draw tiny current like this.
You can copy a variable to the EEPROM, and then write this back. This is the standard 'power fail' way of working.
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Mar 11, 2016 6:06 am     Reply with quote

If you only need a few bytes saved, use the SRAM in the DS1307 ! It's already battery backed up and the driver has the functions to read/write to it SRAM. It has 56 bytes available for storage. That's almost 450 bits of information you can 'pack'. I use the DS1307's 1Hz SQW as an interrupt for my data loggers and solar control units. More than fast enough (temperature doesn't vary fast..) and accurate.

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group