|
|
View previous topic :: View next topic |
Author |
Message |
RBuilder
Joined: 27 Oct 2011 Posts: 10
|
I2C 18F4685 Force_HW - Issue |
Posted: Thu Oct 27, 2011 11:03 pm |
|
|
Hello there!
I see many posts regarding hardware I2C and Force_HW issues.
First, I should add that almost all of the issues i've read on this forum seem to have been resolved or the thread activity ceases. So that made it a bit difficult to actually find a solution to my problem. Most of the issues were people using #FAST_IO or setting TRIS. My code is incredibly simple.
I have a very very simple peice of code attempting to control a BlinkM over I2C. It works great with Force_SW, but locks up on the first I2C_Write() when Force_HW is set.
Here is my I2C decleration...
Code: |
Works:
#use i2c(master, sda=pin_c4, scl=pin_c3, force_sw)
Doesn't Work:
#use i2c(master, sda=pin_c4, scl=pin_c3, force_hw)
|
My circuit has 2 pull-up resistors to +5, Both are 4.7.K.
The code to send the command is..
Code: |
i2c_start();
i2c_write(0x00); // 0x00 is global addressing for BlinkM's
i2c_write('n');
i2c_write(20);
i2c_write(30);
i2c_write(120);
i2c_stop();
|
I am using Standard I/O with no timers.
Now, that should make the Blink turn a nice blue color. With Force_SW it works great. What's happening is the Pic crashes (or halts) after the very first write line (the write with the address 0x00 or any address at all).
The scope shows NO clock or anything from C3 or C4.
Here is my full code
Code: |
#include <18F4685.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES H4 //High speed osc with HW enabled 4X PLL
#FUSES PROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES PUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST
#use delay(clock=40000000)
// I2C Support
//#use i2c(master, sda=pin_c4, scl=pin_c3, force_sw)
#use i2c(master, sda=pin_c4, scl=pin_c3, force_hw)
void main() {
setup_adc_ports(AN0_TO_AN7);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(GLOBAL);
while (1) {
i2c_start();
i2c_write(0x00); // 0x00 is global addressing for BlinkM's
i2c_write('n');
i2c_write(20);
i2c_write(30);
i2c_write(120);
i2c_stop();
delay_ms(5000);
}
}
|
As you can see, i'm letting the compiler determine the TRIS. I'm also not doing anything different. I do have 2 pull-ups like I said. Why does this work with Force_SW but not with Force_HW?
I'm very confused. |
|
|
RBuilder
Joined: 27 Oct 2011 Posts: 10
|
|
Posted: Thu Oct 27, 2011 11:14 pm |
|
|
Okay.... So I wrote that post. And not even a few minutes later I had it working. So hopefully this will help someone else out.
the Project Wizard had created this Setup to disable SPI for me...
Code: |
setup_spi(SPI_SS_DISABLED);
|
Remove it!! It will steal your children. It's a bad command for I2C... Don't use it
HOURS I spent today... And i mean HOOOOOOOOOUUUUUURSSSSSSS like forever. And I figured disabling the SPI was correct, because the project wizard did that for me. Oh well!
Hopefully this will help someone |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19543
|
|
Posted: Fri Oct 28, 2011 2:36 am |
|
|
Well done.
You will find a huge number of posts where at some point people are told to remove this line. It is not even the right command to turn off SPI....
The command to disable SPI is:
setup_spi(FALSE);
But of course in your case you don't want SPI 'off', since it is the SPI hardware that handles I2C.
What the command was doing, was setting the bit pattern that handles 'slave select', which means it changes the settings of the SSPM bits in SSPCON1, to 0101. This bit pattern in I2C mode, is 'unused', so it stopped your I2C working.
The wizard, at times, is a real 'annoyance' in CCS.
Best Wishes |
|
|
gogutier
Joined: 12 Jul 2011 Posts: 4
|
|
Posted: Thu Jun 14, 2012 10:18 am |
|
|
excellent you saved my day. thanks!!! |
|
|
|
|
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
|