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

Problems with I2C!!! HELP

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







Problems with I2C!!! HELP
PostPosted: Wed Dec 19, 2007 3:24 am     Reply with quote

Hi! I have some problems with the bus I2C! I've a PIC16F876A for the master and a PIC18F4520 for the SLAVE. Please, anyone can help me and tell me what on my code y wrong?

I tried everything but it's imposible! help please

This is my code:

***************MASTER*****************************

Code:
#include <16F876A>
#device adc=8

#use delay(clock=4000000)

#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //Crystal osc <= 4mhz
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected


#use i2c(Master,sda=PIN_C4,scl=PIN_C3)

#include "C:\Documents and Settings\bizintek01\Mis documentos\I2C\Master\master.h"


#byte port_a=5
#byte port_b=6
#byte port_c=7
#byte port_d=8
#byte port_e=9





//#org 0x1F00, 0x1FFF void loader16F876A(void) {}
static int a,b,c=25,d=15,e,f;


void main()
{

set_tris_a(0x00);
set_tris_b(0x00);
set_tris_c(0x00);

while(1)
{


i2c_start();
i2c_write(0xc0); //direccion de slave

a=i2c_read(0x00); //leo buffer
b=i2c_read(0x01);

i2c_stop();

}


}


***************SLAVE*********************************

Code:
#include <18F4520>
#device adc=8

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //Crystal osc <= 4mhz
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES DEBUG //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 PBADEN //PORTB pins are configured as analog input channels on RESET
#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)
#use i2c(Slave,sda=PIN_C4,scl=PIN_C3,address=0xc0)

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;
}
if(state == 0x80) //Master is requesting data
{
i2c_write(buffer[address]);
}
}

int i;


void main()
{

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
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_oscillator(False);

// TODO: USER CODE!!


set_tris_a(0);
set_tris_b(0);


address = 0x00;
for (i=0;i<0x10;i++)
buffer[i] = 0x00;

enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);

while (TRUE)
{
buffer[0x00]=0;
buffer[0x01]=1; //doy valores al buffer para luego poder leerlos
}
}



Thank you
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Wed Dec 19, 2007 9:46 am     Reply with quote

Quote:
***************MASTER*****************************

#FUSES NOPUT //No Power Up Timer Change to PUT - good thing to have


set_tris_a(0x00); <--- these are not needed and require fast_io
set_tris_b(0x00); <---
set_tris_c(0x00); <---


i2c_start(); // try placing delays between each command at first
delay_us(50);
i2c_write(0xc0); //direccion de slave
delay_us(50);
a=i2c_read(0x00); //leo buffer <--- this statement tells the slave that play time is over, the zero tells it the Master is finished
delay_us(50);
b=i2c_read(0x01); <---this one should have 0x00 in it
delay_us(50);
i2c_stop();

// place a delay here, in your while() loop, of around 500ms to 1 second. You're looping rather fast. Give the slave some time to breath while you figure things out.




***************SLAVE*********************************

#FUSES NOPUT //No Power Up Timer <--- use PUT here too
#FUSES PBADEN // <--- use NOPBADEN if you're not using the ADC


set_tris_a(0); <--- not needed
set_tris_b(0); <---




I wasn't very thorough but these are what stood out for now.

Ronald


Last edited by rnielsen on Wed Dec 19, 2007 11:51 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 19, 2007 11:05 am     Reply with quote

Use the CCS example file code, Ex_Slave.c, for the Slave PIC.
It's in this directory:
Quote:
c:\Program Files\picc\Examples\Ex_Slave.c


Use the code in this post for the Master PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=32368&start=3
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