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 Communication Between Two pic18f4685's

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



Joined: 17 Feb 2007
Posts: 2

View user's profile Send private message

I2C Communication Between Two pic18f4685's
PostPosted: Wed Aug 22, 2007 4:13 pm     Reply with quote

Hi,
I have spent several days trying hookup two pic18f4685's via I2C, different code has allowed me to hookup to a DS1631. Can someone show me where I have gone wrong? I am using IDE 4.010, have two 4.7k pullup resistors and have sda to sda and scl to scl hooked up.

Master Code:
#include "E:\Misc\CCS\CNC\CNC.h"
#include "E:\Misc\CCS\CNC\Flex LCDx4.h"
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use I2C(master, sda=PIN_C4, scl=PIN_C3)
void main() {
int BoardAddress;
while (TRUE){
BoardAddress=0xa0;
delay_ms(150);
i2c_start();
i2c_write(BoardAddress); //Address
delay_ms(15);
i2c_write(20);
delay_ms(15);
i2c_stop();
}
}

Slave Code:
#include "E:\Misc\CCS\CNC\CNC.h"
#include "E:\Misc\CCS\CNC\Flex LCDx4.h"
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0)
BYTE address = 0;
#INT_SSP
void ssp_interupt ()
{
address=1;
}
void main ()
{
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
lcd_init();
lcd_gotoxy(1, 1);
printf(lcd_putc," Tim ");
while (TRUE) {
lcd_gotoxy(1, 1);
printf(lcd_putc," Tim %u ", address);
}
}

Common Code:
#include <18F4685.h>
#device adc=8 //sets if adc return 8 bit or 10 bit
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#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 STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES BBSIZ4K //4K 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=32000000) //The delay is wrong

Thank you for your time.
Tim Klassen
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 22, 2007 4:41 pm     Reply with quote

Run the Slave at a minimum of 8 MHz (higher is preferred), and try
it with the CCS Ex_Slave.c example code. Use the master code
linked to in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=30905&start=3


Quote:
I am using IDE 4.010

I don't know how that relates to the compiler version, but if your compiler
version is 4.010, then you better get a later version. Look at the top of
the .LST file to see your version. The .LST file is located in your project
directory. Vs. 4.010 is too early in the release cycle for vs. 4.
Tim Klassen



Joined: 17 Feb 2007
Posts: 2

View user's profile Send private message

PostPosted: Fri Aug 24, 2007 5:51 am     Reply with quote

Hi, I finally have some good news, the program works and I will be able to use it, but I am confused by the order of writing from master to slave:

I am unsure where "14" ends up and why the slave address is sent last. Here is printf output on computer from slave:
data 78 123 160 \0A
states 0 1 2 3 0 128 129 \0A
count 7 \0A \0D
\0D

Master Writing:

i2c_start();
i2c_write(0xA0);
i2c_write(14);
i2c_write(78);
i2c_write(123);
i2c_stop();



Master Code:

#include "E:\Misc\CCS\CNC\CNC.h"
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3, slow)
void main()
{
int8 data;
setup_oscillator(OSC_8MHZ|OSC_NORMAL|OSC_PLL_Off);
// Write the letter 'B' to the slave board.
delay_ms( 100 );
i2c_start();
i2c_write(0xA0);
i2c_write(14);
i2c_write(78);
i2c_write(123);
i2c_stop();
// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data);
while(1){}
}

Slave Code:

#include "E:\Misc\CCS\CNC\CNC.h"
#use delay(clock=32000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0)
#byte OSCTUNE = 0xF9B //multiplies processor clock
#bit PLLEN = OSCTUNE.6 //multiplies processor clock
BYTE address, buffer[0x10];
int count = 0;
int state1=0, state2=0, state3=0, state4=0, state5=0, state6=0, state7=0;
int8 data=0, data1=0, data2=0;
#INT_SSP
void ssp_interupt ()
{
BYTE incoming, state;
count++;
state = i2c_isr_state();
state7=state6;
state6=state5;
state5=state4;
state4=state3;
state3=state2;
state2=state1;
state1=state;
if(state < 0x80) //Master is sending data
{
incoming = i2c_read();
if(state == 1){
data = incoming;
}
if(state == 2){
data1 = incoming;
}
if(state == 3){
data2 = incoming;
}
}
if(state == 0x80) //Master is requesting data
{
/// i2c_write(buffer[address]);
i2c_write('t');
}
}
void main()
{
int tom=0;
//setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_PLL_OFF);
setup_oscillator(OSC_8MHZ|OSC_NORMAL|OSC_PLL_ON);
//setup_oscillator(OSC_8MHZ|OSC_NORMAL|OSC_PLL_Off);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while(1){
delay_ms( 500 );
printf("data %u %u %u \n\r", data, data1,data2);
printf("states %u %u %u %u %u %u %u \n\r", state7, state6, state5, state4, state3, state2, state1);
printf("count %u \n\r", count);
}
}

Using same common files as before.


Corrections required to acheive this output:
1) run slave at 32mhz.
2) delay transmission from master so slave can get booted up.

Master and slave can both send and receive.

Tim Klassen
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