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

[SOLVED] i2c problem with PIC18F86K22

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



Joined: 13 Aug 2004
Posts: 59
Location: Turkey

View user's profile Send private message Visit poster's website

[SOLVED] i2c problem with PIC18F86K22
PostPosted: Thu Sep 22, 2016 4:18 am     Reply with quote

Hello,
I have questions about i2c on PIC18F86K22,
I am using ccs 5.058 compiler.

My code snippets are as below
Code:

#define EEPROM_SDA  PIN_C4
#define EEPROM_SCL  PIN_C3
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL,FORCE_SW)//,FAST=400000)

this works fine

but when I change to
Code:

#define EEPROM_SDA  PIN_C4
#define EEPROM_SCL  PIN_C3
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL,FORCE_HW)//,FAST=400000)

It stops working, what could be the problem ?
PIN C3 and C4 are hardware i2c. Why ccs make problems when forced to hardware ? Any suggestions?

cheers
_________________
Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com

Do whatever you do with amateur spirit -
But always feel professional.


Last edited by scanan on Fri Sep 23, 2016 8:41 am; edited 1 time in total
temtronic



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

View user's profile Send private message

PostPosted: Thu Sep 22, 2016 5:03 am     Reply with quote

As shown, those 2 lines of code won't be the problem.
A small program that shows it 'stopping' is needed ...
What CAN cause it to 'stop' is having the I2C ISR enabled without a handler.
Download and run PCM P's I2C scanner program from the code library, see what happens.
Also it could be incorrect I2C bus resistors.

You need to show us more code...

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Sep 22, 2016 7:02 am     Reply with quote

Two important differences will apply when you switch to hardware.

First the input thresholds to the PIC will change. C3 & C4, running using the software I2C, will accept as a 'high', 2v, with the hardware, the high level will switch to 0.8*Vdd.
Second the speed may well increase. Since you are not actually specifying the speed.

The response to both of these then depends on your bus resistors, and the device attached. Remember at 3.3v the resistor needs to be smaller than on a 5v bus.
scanan



Joined: 13 Aug 2004
Posts: 59
Location: Turkey

View user's profile Send private message Visit poster's website

PostPosted: Fri Sep 23, 2016 1:24 am     Reply with quote

Here some code

Code:

#include <18F86K22.h>
#device adc=8

#include "..\libs\elf-2416.c"

#FUSES PLLEN,VREGSLEEP,INTRC_HP,SOSC_DIG,NOXINST,INTRC_IO,NOFCMEN,NOIESO //1
#FUSES PUT,NOBROWNOUT,NOWDT //2
#FUSES MSSPMSK7,NOMCLR //3
#FUSES NOSTVREN,BBSIZ2K,NODEBUG //4
#FUSES NOPROTECT,NOCPB,NOCPD //5
#FUSES NOWRT,NOWRTC,NOWRTB,NOWRTD,  //6
#FUSES NOEBTR,NOEBTRB //7

#use delay(clock=64000000)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS)
#define EEPROM_SDA  PIN_C4
#define EEPROM_SCL  PIN_C3
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL,FORCE_HW)

void main()
{
char data,i;

setup_wdt(WDT_OFF);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);                                               
setup_spi(SPI_SS_DISABLED);

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128|RTCC_8_BIT);// TIMER0
setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);
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);
setup_low_volt_detect(FALSE);             
setup_oscillator(OSC_16MHZ|OSC_PLL_ON);                                       
output_a(0x00);
output_b(0x00);
output_c(0x00);
output_d(0x00);
output_e(0x00);
output_f(0x01);
output_g(0x00);

set_tris_a(0x80);
set_tris_b(0xff);
set_tris_c(0x97);
set_tris_d(0x00);                                     
set_tris_e(0x00);     
set_tris_f(0x00);
set_tris_g(0x00);
set_tris_h(0x00);
set_tris_j(0x00);
EXT_INT_EDGE(0,L_TO_H);
enable_interrupts(INT_EXT);
enable_interrupts(INT_TIMER0); // ENABLE TIMER0
enable_interrupts(INT_TIMER1); // ENABLE TIMER1
enable_interrupts(GLOBAL);

do{
for(i=0;i<128;i++)
{
data=read_ext_eeprom(i);
printf("%x ",data);
}

}while(1==1);



this code is just to test the eeprom.
when the i2c is defined for hadware the program hang in the for loop, when used as software i2c it works corectly
the resistor used are 4K7
_________________
Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com

Do whatever you do with amateur spirit -
But always feel professional.
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Sep 23, 2016 2:03 am     Reply with quote

This line is wrong:

setup_spi(SPI_SS_DISABLED);

This enables the SPI, and disables 'slave select'.

The correct line is:

setup_spi(FALSE);

This could be interfering with the I2C hardware (since it is the same peripheral....).

4K7R, is too large for a 3.3v I2C. It's a 'typical' value for a short 5v bus, but a bit large even for this, unless capacitance is very low. The line must pull up to the Vih level, within 300nSec for a fast mode device. Assuming a bus capacitance of perhaps 100pF (input pins on both chips, trace between etc.). Gives Rp Max at about 3.8KR, and Rp Min at about 800R. Suggest 1K5R.

You enable INT_EXT, but set the pin for this to an output.

The code won't work without interrupt handlers for the three enabled interrupts.

Both I2C pins should be set as inputs if you are manually setting the TRIS. You have C4 set as output. Wrong. Data sheet section 21.4:

"The user must configure these pins as inputs by setting
the associated TRIS bits."
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