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

I2C Slave

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



Joined: 30 Jan 2007
Posts: 56
Location: Viana do Castelo - Portugal

View user's profile Send private message

I2C Slave
PostPosted: Sun Oct 21, 2007 11:39 am     Reply with quote

Hi

I'm trying work with i2c slave for my school project.
I have 18F4685 in software master and 18F2550 in hardware slave.

First i'm trying with ex_Slave from CCS.

When the master tell the slave to write the slave the led turn on.
When the master tell the slave to read the slave don't turn on.

Master:

Code:

#include "pic18.h"
#use i2c(Master, sda=PIN_D7, scl=PIN_D6)

#include <pcf8833.c>

void main()
{
   int8 data;
   char escreve[30];

   TRISD=0b00000000;
   TRISB=0b01111110;
   delay_ms(1000);
   PORTB_RB0=1;
   glcd_init(65);
   glcd_cls(WHITE);
   sprintf(escreve,"ola");
   glcd_text57(5, 3, escreve, 1, BLACK, WHITE);

   // Write the letter 'B' to the slave board.
   i2c_start();
   i2c_write(0xa0);
   i2c_write(0x00);
   i2c_write('B');

   delay_ms(500);

   sprintf(escreve,"escreveu");
   glcd_text57(5, 20, escreve, 1, BLACK, WHITE);
   // Read from the slave board and display the data.
   i2c_start();
   i2c_write(0xa0);
   i2c_write(0x00);
   i2c_start();
   i2c_write(0xa1);
   data = i2c_read(0);
   i2c_stop();
     
   sprintf(escreve,"%c",data);
   glcd_text57(5, 40, escreve, 1, BLACK, WHITE);
     
while(1); 
}



Slave:

Code:

#use i2c(SLAVE, SDA=PIN_B0, SCL=PIN_B1, address=0xa0, FORCE_HW)

BYTE address, buffer[0x10];

#INT_SSP
void ssp_interupt ()
{
   BYTE incoming, state;

   state = i2c_isr_state();

   if(state < 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
      if(state == 1)                     //First received byte is address
         address = incoming;
      if(state == 2)                     //Second received byte is data
         buffer[address] = incoming;
         
         RB7=1;
   }
   if(state == 0x80)                       //Master is requesting data
   {
      i2c_write(buffer[address]);
      RB6=1;
   }
}

void main ()
{
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);
   TRISB=0b00111111;
   RB7=0;
   RB6=0;
   while (TRUE) {}
}

The leds is RB6 and RB7


What I make wrong.

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 21, 2007 1:11 pm     Reply with quote

Quote:

// Write the letter 'B' to the slave board.
i2c_start();
i2c_write(0xa0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();

delay_ms(500);

You need a 'stop' statement. It's missing from your code. Add the
line shown in bold above.


Also, you don't really need to put in TRIS statements, and you don't
need to write directly to bits to turn on the LEDs. If you use
"Standard i/o" mode and also use the CCS pin i/o functions, then the
compiler will setup the TRIS for you. "Standard i/o" mode is the
default mode of the compiler. You don't need to specify it.
Just use CCS functions, such as:
Code:
output_high(PIN_B6);
output_low(PIN_B6);
output_high(PIN_B7);
output_low(PIN_B7);


Make sure you have pull-up resistors on SDA and SCL, and a ground
connection between the two PIC boards. You can use 4.7K ohms for
the pull-up resistors.
joven



Joined: 30 Jan 2007
Posts: 56
Location: Viana do Castelo - Portugal

View user's profile Send private message

PostPosted: Sun Oct 21, 2007 1:44 pm     Reply with quote

hi

I've put the "i2c_stop();" continue not working.

The two pics are in the same circuit.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 21, 2007 1:53 pm     Reply with quote

1. Post your compiler version.

2. Post the #include, #fuses, and #use delay() statements for both
the Master and Slave programs.

3. Post the statements which declare 'RB6' and 'RB7'.
joven



Joined: 30 Jan 2007
Posts: 56
Location: Viana do Castelo - Portugal

View user's profile Send private message

PostPosted: Sun Oct 21, 2007 2:36 pm     Reply with quote

The compiler version is 4.049

#BYTE PORTB=0xF81
#BIT RB7=PORTB.7
#BIT RB6=PORTB.6

Master:

Code:


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

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES NOSTVREN                 //Stack full/underflow will not cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#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 NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES BBSIZ1K                  //1K words Boot Block size
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(clock=20000000)

#include "pic18.h"
#use i2c(Master, sda=PIN_D7, scl=PIN_D6)

#include <pcf8833.c>

//See the program below which shows some corrections for your code.
//The following program works OK with EX_SLAVE.C

void main()
{
   int8 data;
   char escreve[30];
 
   TRISD=0b00000000;
   TRISB=0b01111110;
   delay_ms(1000);
   PORTB_RB0=1;
   glcd_init(65);
   glcd_cls(WHITE);
   sprintf(escreve,"ola");
   glcd_text57(5, 3, escreve, 1, BLACK, WHITE);

   // Write the letter 'B' to the slave board.
   i2c_start();
   i2c_write(0xa0);
   i2c_write(0x00);
   i2c_write('B');
   i2c_stop();

   delay_ms(500);

   sprintf(escreve,"escreveu");
   glcd_text57(5, 20, escreve, 1, BLACK, WHITE);
   // Read from the slave board and display the data.
   i2c_start();
   i2c_write(0xa0);
   i2c_write(0x00);
   i2c_start();
   i2c_write(0xa1);
   data = i2c_read(0);
   i2c_stop();
     
   sprintf(escreve,"%c",data);
   glcd_text57(5, 40, escreve, 1, BLACK, WHITE);
     
while(1); 
}


Slave:
Code:


#include <18F2550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN,NOPBADEN,WRTB,MCLR,NOCPD,NOWRTC

#use delay(clock=48000000)// full speed USB device

// START OF bootloader definition
#define LOADER_END   0x7FF
#define LOADER_SIZE   0x6FF
#build(reset=LOADER_END+1, interrupt=LOADER_END+9)
#org 0, LOADER_END {} // nothing will replace the bootloader memory space
// END OF bootloader definition

#include "picram18Fx550.h"

#use i2c(SLAVE, SDA=PIN_B0, SCL=PIN_B1, address=0xa0, FORCE_HW)

BYTE address, buffer[0x10];

#INT_SSP
void ssp_interupt ()
{
   BYTE incoming, state;

   state = i2c_isr_state();

   if(state < 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
      if(state == 1)                     //First received byte is address
         address = incoming;
      if(state == 2)                     //Second received byte is data
         buffer[address] = incoming;
         
         RB7=1;
   }
   if(state == 0x80)                       //Master is requesting data
   {
      i2c_write(buffer[address]);
      RB6=1;
   }
}

void main ()
{
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);
   TRISB=0b00111111;
   RB7=0;
   RB6=0;
   while (TRUE)
  {
  }
}

joven



Joined: 30 Jan 2007
Posts: 56
Location: Viana do Castelo - Portugal

View user's profile Send private message

PostPosted: Tue Oct 23, 2007 12:08 pm     Reply with quote

can be not working because of fuses?
robotam



Joined: 13 Nov 2007
Posts: 15

View user's profile Send private message

PostPosted: Mon Nov 19, 2007 9:53 am     Reply with quote

i2c_start();
i2c_write(0xa0);
i2c_write(0x00);
i2c_start();
i2c_write(0xa1);
data = i2c_read(0);
i2c_stop();

I dont understand why i2c_write(0xa1); what this value 0xa1 means?
joven



Joined: 30 Jan 2007
Posts: 56
Location: Viana do Castelo - Portugal

View user's profile Send private message

PostPosted: Mon Nov 19, 2007 2:53 pm     Reply with quote

is the mode you read/write, can easly find a example on I2C eeprom
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Fri Jan 11, 2008 6:09 pm     Reply with quote

robotam wrote:
I dont understand why i2c_write(0xa1); what this value 0xa1 means?


0xA1 = 0xA0 | 0x01, where 0xA0 is the I2C address of the slave defined in the #use i2c(slave, ... , address=0xA0), and 0x01 is a R/W# bit.
_________________
Read the label, before opening a can of worms.
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