|
|
View previous topic :: View next topic |
Author |
Message |
GDetienne
Joined: 20 Sep 2003 Posts: 47 Location: Brussel - Belgium
|
18F4680 and I2C |
Posted: Wed Feb 06, 2008 9:32 am |
|
|
Hello,
I use some code find (some years ago) on this forum.
With this code I commmunicate (I2C) between a master (16F88) and two slaves (16F876A). Everything are OK.
Now I wish add one slave more, one 18F4680. And now nothing are working.
After some research and build a minimum test program in the 18F4680, I see that the C3 (SCL) is tired to 0 and stop the communication between the other Pic.
Here is this little test program, only initialisation of the Pic (no ISR routine).
Quote: | #include <18F4680.h>
#use delay(clock=32000000)
#fuses NOWDT, H4, NOPROTECT, NOBROWNOUT, NODEBUG, NOLVP, MCLR,NOCPD, NOWRT, NOWRTD,STVREN,NOCPB, LPT1OSC,NOPBADEN //HS
#use i2c (slave, address= 0xC0, sda = Pin_C4, scl = pin_C3, slow)
#byte SSPBUF=0xFC9 // for 16Fxxx 0x13
#byte SSPCON1=0xFC6 // for 16Fxxx 0x14
#byte SSPADD=0xFC8 // for 16Fxxx 0x93
#byte SSPSTAT=0xFC7 // for 16Fxxx 0x94
#bit GCEN = 0xFC5.7 // for 16Fxxx 0x91.7
#bit SSPOV = 0xFC6.6 // for 16Fxxx 0x14.6
#bit WCOL = 0xFC6.7 // for 16Fxxx 0x14.7
#bit CKP = 0xFC6.4 // for 16Fxxx 0x14.4
#bit BF = 0xFC6.0 // for 16Fxxx 0x94.0
#bit sen = 0xFC5.0
#define SLAVE_ADDRESS 0xC0
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_tris_C (0xFF);
GCEN = 1;
SSPCON1 = 0x36;
SSPADD = SLAVE_ADDRESS;
SSPSTAT = 0;
WCOL = 0;
SSPOV = 0;
sen = 1;
enable_interrupts(INT_SSP);
while (1)
{
}
}
|
The problem come after the line : SSPCON1 = 0x36
From the data sheet it's correct and the same as 16F serie. Only specified that the port must be set as input.
Only difference between 16F and 18F is the byte sen in SSPCON2 must be set.
I use CCS version 3.217 and two resitors of 4,7 K between +5V and SCL and SDA.
If some body could give me the correct way.
Thanks in advance. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Feb 06, 2008 10:14 am |
|
|
A few things here that I noticed right off.
You don't have an ISR routine for your I2C;
#int_SSP
SSP_isr()
You also have not enabled global interrupts.
You are doing your slave_address incorrectly. You don't need to assign it manually since you're already doing it in your #use i2c() declaration.
The value, that you are inserting into SSPCON1 is not valid. The lower four bits can only go from 0 - 5. Any number higher than that is invalid. You might want to double check the spec. sheet (page 191of482). This may be causing your problem.
Ronald |
|
|
GDetienne
Joined: 20 Sep 2003 Posts: 47 Location: Brussel - Belgium
|
|
Posted: Wed Feb 06, 2008 11:12 am |
|
|
Thanks for your quick answer.
I agree for the ISR, global interrupt .... missing. The problem occur during the initialisation.
When I read the spec on the page 191, I see the parameters for SPI connection. For I2C I think the correc setting is at page 198.
There : bit 3-0 must be 0110 : I2C slave mode, 7 bit address.
bit 4 , CKP, is 1 release clock
bit 5 , SSPEN, is 1 enable serial pins, not I/O pins
bit 6 and 7 are 0
If it's correct the value of SSPCON1 is 0x36. Or I have foget something ...
Thanks for your help. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 06, 2008 11:32 am |
|
|
Quote: |
#use i2c (slave, address= 0xC0, sda = Pin_C4, scl = pin_C3, slow)
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(FALSE); <== Delete this line.
setup_wdt(WDT_OFF);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_tris_C (0xFF);
GCEN = 1;
SSPCON1 = 0x36;
SSPADD = SLAVE_ADDRESS;
SSPSTAT = 0;
WCOL = 0;
SSPOV = 0;
sen = 1;
|
The line for setup_spi(FALSE) destroys the hardware i2c setup done by
the #use i2c() statement. Delete it.
Then you don't need to do the manual setup of the registers. You can
get rid of your register setup code above. |
|
|
GDetienne
Joined: 20 Sep 2003 Posts: 47 Location: Brussel - Belgium
|
|
Posted: Wed Feb 06, 2008 5:03 pm |
|
|
Thanks PCM for your help. I try tomorrow. |
|
|
|
|
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
|