View previous topic :: View next topic |
Author |
Message |
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
I2C Problems on PIC16F18345 |
Posted: Thu Mar 10, 2016 2:50 am |
|
|
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: 9241 Location: Greensville,Ontario
|
|
Posted: Thu Mar 10, 2016 6:17 am |
|
|
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
|
|
Posted: Thu Mar 10, 2016 7:53 am |
|
|
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: 9241 Location: Greensville,Ontario
|
|
Posted: Thu Mar 10, 2016 8:20 am |
|
|
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: 19537
|
|
Posted: Thu Mar 10, 2016 8:29 am |
|
|
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
|
|
Posted: Thu Mar 10, 2016 8:33 am |
|
|
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: 19537
|
|
Posted: Thu Mar 10, 2016 9:02 am |
|
|
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
|
|
Posted: Thu Mar 10, 2016 6:53 pm |
|
|
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
|
|
Posted: Thu Mar 10, 2016 7:01 pm |
|
|
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: 9241 Location: Greensville,Ontario
|
|
Posted: Thu Mar 10, 2016 8:02 pm |
|
|
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
|
|
Posted: Thu Mar 10, 2016 10:39 pm |
|
|
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: 19537
|
|
Posted: Fri Mar 11, 2016 2:46 am |
|
|
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: 9241 Location: Greensville,Ontario
|
|
Posted: Fri Mar 11, 2016 6:06 am |
|
|
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 |
|
|
|