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 support@ccsinfo.com

Problem!!! How can I use I2C on PIC16F818?

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



Joined: 05 Jan 2006
Posts: 2

View user's profile Send private message

Problem!!! How can I use I2C on PIC16F818?
PostPosted: Thu Jan 05, 2006 3:39 am     Reply with quote

I am using PIC16F818 as master I2C. I have a problem to set the PIN of SDA and SCK of I2C. I cannot use PIN B0 to B3 as the SDA or SCK ports. However, I still can use PIN B4 to B7 instead. I don't know why it is. I do really want to use PIN B1 for setting the SDA port. If someone knows how to set it, please help me. The code following is my code for 16f818 as master i2c.

I connect 4k7 Pull-up on both two lines (SDA,SCK). However, the voltage of PIN B1 is not 5v, it is only 2v instead. I tried on PIN B0,B2,B3 which is the same problem. I cannot pull-up those ports. when I change to B4,B5,B6,B7, it gives a good result. I can run I2C well. But I need to use on B1 for SDA!!!.

** Notice that form datasheet of 818, the type of Port B is TTL when it is set to output mode, and ST when set to input mode. I don't know that it is the root cause or not.

Code:

//---------------------------------------------------------------------------------------
//   master.c file
//----------------------------------------------------------------------------------
#include <16F818.h>
#device adc=8
#fuses NOWDT,HS, PUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B3,rcv=PIN_B2,bits=8)
//------------------ Define-----------------------
#define EEPROM_SDA PIN_B1 //it does not work with PIN B1, cannot pull-up
#define EEPROM_SCL PIN_B4 //it has no problem on this pin

//------------------ i2c setup--------------------
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)

//------------------main-------------------------
#include <input.c>
#include "2401.c"

void initial();


void main() {

   BYTE value, cmd;
   EEPROM_ADDRESS address;
   BOOLEAN ready;   
   init_ext_eeprom();
   printf("\r---Welcome---\n\r");   
   while(!ext_eeprom_ready());
   printf("\r\n<==EEPROM is now ready to use==>\n");   
 
 do {
      do {
        printf("\r\nRead or Write: ");
         cmd=getc();
         cmd=toupper(cmd);
         putc(cmd);
      } while ( (cmd!='R') && (cmd!='W') );

      printf("\n\rLocation: ");
      address = gethex();
      if(cmd=='R')
         printf("\r\nValue: %X\r\n",READ_EXT_EEPROM( address ) );

      if(cmd=='W') {
         printf("\r\nNew value: ");
         value = gethex();
         printf("\n\r");
         WRITE_EXT_EEPROM( address, value );
      }
   printf("\r\n<-------------End---------->\n");
   } while (TRUE);
}

//---------------------------------------------------------------------------
// 2401.c file
//---------------------------------------------------------------------------

#define i2c_address 0x00

#define EEPROM_ADDRESS byte
#define EEPROM_SIZE   128

void init_ext_eeprom() {
   output_float(EEPROM_SCL);
   output_float(EEPROM_SDA);
}

BOOLEAN ext_eeprom_ready() {
   int1 ack;
   i2c_start();            // If the write command is acknowledged,
   ack = i2c_write(i2c_address);  // then the device is ready.
   i2c_stop();
   return !ack;
}

void write_ext_eeprom(BYTE address, BYTE data) {
   while(!ext_eeprom_ready());
   i2c_start();
   i2c_write(i2c_address);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
}


BYTE read_ext_eeprom(BYTE address) {
   BYTE data;

   while(!ext_eeprom_ready());
   i2c_start();
   i2c_write(i2c_address);
   i2c_write(address);
   i2c_start();
   i2c_write(i2c_address+1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}
jaremethpp
Guest







reply
PostPosted: Thu Jan 05, 2006 5:24 am     Reply with quote

After I reconnect the circuit to bread board(before I used PICDEM2 Plus for demo), I have a curious result. I can use PIN B1 and B4 for I2C, however PIN B1 is SCK and PIN B4 is SDA. If I switch B1 and B4 functions, the circuit does not work well. So, I think something wrong on ccs software. But I am happy that at least I can use B1 for SCK and B4 for SDA, even though the circuit need to be switch lines. If someone know the root cause, please let me know why???
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Jan 05, 2006 5:26 am     Reply with quote

First: Please remove your double post from the top of this forum.

Pin B1 and B4 are the pins used for the hardware I2C module. By default the #use I2C command creates a software I2C driver and I assume this is causing a conflict. Add FORCE_HW to your #use I2C line.
jaremethp



Joined: 05 Jan 2006
Posts: 2

View user's profile Send private message

PostPosted: Thu Jan 05, 2006 6:16 am     Reply with quote

ckielstra wrote:
First: Please remove your double post from the top of this forum.

Pin B1 and B4 are the pins used for the hardware I2C module. By default the #use I2C command creates a software I2C driver and I assume this is causing a conflict. Add FORCE_HW to your #use I2C line.


ckielstra: Thanks for your answer. I am sorry for double post. I am very new here. I don't know how to delete the previous post because someone has already replied. As you suggestion, I revised my new code with FORCE_HW in #use I2C line, however it is very interesting that I have to switch the line B1 and B4 for using i2c. I don't know why...... if you know more please let me know. I think that it may be the bug????
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Jan 05, 2006 7:55 am     Reply with quote

Quote:
however it is very interesting that I have to switch the line B1 and B4 for using i2c. I don't know why......
What do you mean? B1 = SDA and B4 = SCL doesn't work? Or the other way around?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 05, 2006 3:23 pm     Reply with quote

Quote:
I cannot use PIN B0 to B3 as the SDA or SCK ports.
However, the voltage of PIN B1 is not 5v, it is only 2v instead.
when I change to B4,B5,B6,B7, it gives a good result. I can run I2C well.
I used PICDEM2 Plus for demo.

Download the PicDem2-Plus User's Guide:
http://ww1.microchip.com/downloads/en/DeviceDoc/51275b.pdf

Go to page 20 in the Acrobat Reader, and zoom in on the upper right
corner of the schematic. It shows four LEDs connected to RB0-RB3.
These LEDs can be disabled by removing jumper J6.

-------------------
When something weird happens with a PIC pin, you should do this:

1. First look in the PIC data sheet. Check if the pin is also used by
another module, such as a comparator. If so, put in a line of code
(by using a CCS function) to disable that module. Also check
if the pin can be configured as analog or digital. If so, put in a line
of code to configure it for the proper mode.

2. Look at schematic for your board. Check if there is a external
circuit connected to the PIC pin. Is so, then disconnect it or
disable it.
jaremethpp
Guest







PostPosted: Thu Jan 05, 2006 7:22 pm     Reply with quote

PCM programmer wrote:
Quote:
I cannot use PIN B0 to B3 as the SDA or SCK ports.
However, the voltage of PIN B1 is not 5v, it is only 2v instead.
when I change to B4,B5,B6,B7, it gives a good result. I can run I2C well.
I used PICDEM2 Plus for demo.

Download the PicDem2-Plus User's Guide:
http://ww1.microchip.com/downloads/en/DeviceDoc/51275b.pdf

Go to page 20 in the Acrobat Reader, and zoom in on the upper right
corner of the schematic. It shows four LEDs connected to RB0-RB3.
These LEDs can be disabled by removing jumper J6.

-------------------
When something weird happens with a PIC pin, you should do this:

1. First look in the PIC data sheet. Check if the pin is also used by
another module, such as a comparator. If so, put in a line of code
(by using a CCS function) to disable that module. Also check
if the pin can be configured as analog or digital. If so, put in a line
of code to configure it for the proper mode.

2. Look at schematic for your board. Check if there is a external
circuit connected to the PIC pin. Is so, then disconnect it or
disable it.


Thanks for your answers. I see the datasheet of PICDEM and found the jumper to disable on-board LED. Now, I can use 16f818 connecting I2C on PICDEM2. However, the problem is that if I use B1 for SDA and B2 for SCL, it doesn't work. But B1 for SCL and B4 for SDL is the good result.
No work | Work
B1 => SDA | B1=> SCL
B4 => SCL | B4=> SDA

If you know why, please let me know. Thanks for every answers.
Regards,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 05, 2006 10:51 pm     Reply with quote

Do you guarantee that you have pull-up resistors (2.2K to 4.7K is OK)
on both i2c pins ? Can you check the connections ?
Guest








PostPosted: Fri Jan 06, 2006 2:27 am     Reply with quote

PCM programmer wrote:
Do you guarantee that you have pull-up resistors (2.2K to 4.7K is OK)
on both i2c pins ? Can you check the connections ?


Yes, I have already put resister pull-up on i2c pins with 4k7.
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