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 Help

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



Joined: 14 Jun 2007
Posts: 8

View user's profile Send private message

I2C Help
PostPosted: Thu Jun 14, 2007 4:40 pm     Reply with quote

Hey Guys,
I'm using a PIC18F2420. I would like a very simple slave code that returns a "1" when the master asks for data. I've been trying the code that came with the compiler but it's not working. Please help !
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 14, 2007 5:34 pm     Reply with quote

Read this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=28097&start=9
RFID



Joined: 14 Jun 2007
Posts: 8

View user's profile Send private message

PostPosted: Sun Jun 17, 2007 4:06 pm     Reply with quote

Thank you. This helped a little bit. However, I still can't communicate with my master (an external device not a PIC). I was able to communicate with another pre-programmed chip from Microchip. So the problem is in my code.

Here is the slave code I'm using:

#include "C:\Documents and Settings\almatrouk-07\Desktop\Test 2\Test2.h"

BYTE address, buffer[0x10];

#int_SSP
void SSP_isr()
{
BYTE incoming, state;

state = i2c_isr_state();

if(state < 0x80) //Master is sending data
{
if(input_state(PIN_B0)==1)
output_low(PIN_B0);
else
output_high(PIN_B0);

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
{
if(input_state(PIN_B1)==1)
output_low(PIN_B1);
else
output_high(PIN_B1);


i2c_write('E');

}
}



void main()
{

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
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);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
setup_low_volt_detect(FALSE);
setup_oscillator(False);

while (TRUE) {
delay_ms(400);
output_high(PIN_B3);
if(input_state(PIN_B2)==1)
output_low(PIN_B2);
else
output_high(PIN_B2);}
}

header file code:
#include <18F2420.h>
#device adc=8

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#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 LVP //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=32768)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,address=0xA0)

Questions:
1-The second byte of the I2C protocol is the register's address. Do I have to specify it or write 'E' in it? My master only reads info.
2- Can you tell me what I'm doing wrong, the program freezes as soon as it gets to the i2c_write command in the ISR?

Thank you in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 17, 2007 4:57 pm     Reply with quote

Quote:

#FUSES LVP//Low Voltage Programming on B3(PIC16) or B5(PIC18)

#use delay(clock=32768)

There are two immediate problems.

1. The "LVP" fuse setting can cause your PIC to lockup if the PGM pin
goes to a high level. The PIC will then enter programming mode.
Change that fuse to NOLVP. Do this in every program.

2. You're using an extremely slow clock for the i2c slave.
Look in the Electrical Characteristics section of the 18F2420 data
sheet. Look at parameter 100 in this table:
Quote:

TABLE 26-19: I2C BUS DATA REQUIREMENTS (SLAVE MODE)
100 kHz mode
PIC18FXXXX must operate at a minimum of 1.5 MHz


You didn't say what the i2c clock speed of your master is, but
there's no way that a PIC i2c slave running at 32 KHz is going to work.
RFID



Joined: 14 Jun 2007
Posts: 8

View user's profile Send private message

PostPosted: Sun Jun 17, 2007 9:20 pm     Reply with quote

I'm using a LEGO Mindstorms NXT Brick to make a sensor for a LEGO Mindstorms NXT. The baud rate of the LEGO Mindstorms I2C is 9600 bits/s. If you need more information please use the link below.
http://mindstorms.lego.com/Overview/nxtreme.aspx
Go to Hardware Developer Kit (HDK) and download the zip file. Then, open the LEGO Mindstorms Hardware Developer Kit PDF File and look at page 9 where they discuss the I2C protocol.

I did what you asked me to do. However, the program still does not work. Here is the code. I hope you can find the error and let me know. Your help is highly appreciated.

C code

#include "C:\Documents and Settings\almatrouk-07\Desktop\Test 3\Test3.h"

BYTE address, buffer[0x10];

#int_SSP
void SSP_isr()
{
BYTE incoming, state;

state = i2c_isr_state();

if(state < 0x80) //Master is sending data
{ if(input_state(PIN_B0)==1)
output_low(PIN_B0);
else
output_high(PIN_B0);

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
{
// if(input_state(PIN_B1)==1)
// output_low(PIN_B1);
// else
output_high(PIN_B1);

// buffer[address]=65;
// i2c_write(buffer[address]);
i2c_write('E');
}
// clear_interrupt(int_SSP);
}





void main()
{

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
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);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
setup_low_volt_detect(FALSE);
setup_oscillator(OSC_8MHZ);

while (TRUE) {
delay_ms(400);
output_high(PIN_B3);
if(input_state(PIN_B2)==1)
output_low(PIN_B2);
else
output_high(PIN_B2);}

}

H file

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

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC //Internal RC Osc
#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 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=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA0)

Note: I was using an internal oscillator to do so.
Ttelmah
Guest







PostPosted: Mon Jun 18, 2007 3:10 am     Reply with quote

Obvious comment.
You do have the pull-up resistors on the I2C lines?.

Best Wishes
RFID



Joined: 14 Jun 2007
Posts: 8

View user's profile Send private message

PostPosted: Mon Jun 18, 2007 11:32 am     Reply with quote

Yes I did. I have tested my circuit with a pre-programmed PIC from microchip and it works. Therefore, the problem is in the software for sure. However, I'm not using an external oscillator. I don't know if that might cause some problems.
mskala



Joined: 06 Mar 2007
Posts: 100
Location: Massachusetts, USA

View user's profile Send private message

PostPosted: Mon Jun 18, 2007 1:00 pm     Reply with quote

Another dumb question. Are you sure that address 0xA0 is the same one that the pre-programmed chip is using or that the master expects?
RFID



Joined: 14 Jun 2007
Posts: 8

View user's profile Send private message

PostPosted: Mon Jun 18, 2007 1:11 pm     Reply with quote

The pre-programmed chip has an address of A2. However, I have changed the master's program to call the 0XA0 address.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 18, 2007 1:12 pm     Reply with quote

Tell us the exact sequence of i2c bytes that are coming from the
Lego cube module. The Lego manual shows many commands.
Which specific one are you testing now ? Post the sequence of
start bit, slave address byte, data bytes, stop bit, re-start bit (if any).
The CCS slave code can't handle everything. That's why we need
to know the specifics of the i2c command that you're testing.
RFID



Joined: 14 Jun 2007
Posts: 8

View user's profile Send private message

PostPosted: Mon Jun 18, 2007 1:52 pm     Reply with quote

We looked at this with the logic analyzer and can see that the Master [NXT] I2C Read stops dead after the first two bytes are sent to the slave (PIC).
1st byte: 0xA1 (Read)
2nd byte: 0x02 ("Register Address")
SCL stays low (held by PIC?)

It's as if the PIC doesn't have anything to send back and holds SCL low.
On the PIC we tried to set an LED high to show code entering the "if (state == 0x80)" case, but LED stayed low!
It's as if when the Read interrupt happens, state <> 0x80 as we expect, and we have not allowed for state > 0x80...
?

Cheers!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 18, 2007 2:06 pm     Reply with quote

Disconnect the Lego module from your Slave PIC. Get another PIC
board and program it with the following Master code. Then connect
the Master PIC to the Slave PIC. Run it and see if the master can
talk to the slave. This test will prove if your slave board is working.
Link to Master test code:
http://www.ccsinfo.com/forum/viewtopic.php?t=28097&start=9

Make sure you connect:
- Master SDA to slave SDA
- Master SCL to slave SCL
- Master board's ground to Slave board's ground
- A 4.7K pullup to +5v on each of SDA and SCL (2 resistors total).
Ttelmah
Guest







PostPosted: Mon Jun 18, 2007 3:21 pm     Reply with quote

Minor beware on the latter, if you then move back to the Lego unit.
The Lego stuff, uses a really slow I2C rate, with 4.7K series resistors. Hence the pull-ups, have to be large (they recommend 82K!).
The clock rate shouldn't cause a problem (provided the chip is working - simple LED flash type test called for...), since I2C, is a 'synchronous' bus, with the rate dependant on the master device.

Best Wishes
RFID



Joined: 14 Jun 2007
Posts: 8

View user's profile Send private message

PostPosted: Mon Jun 18, 2007 5:35 pm     Reply with quote

Thank you guys, I will work on your suggestions and keep you updated.
RFID



Joined: 14 Jun 2007
Posts: 8

View user's profile Send private message

PostPosted: Mon Jun 18, 2007 8:38 pm     Reply with quote

PCM programmer. I think you are right. Your program did not work either which makes me think there is a hardware error. Have you used an external oscillator for your slave? If so what was the frequency... My frequencies are 20MHz for the Master (external oscillator) and 8MHz for the slave. I'm using the PICdem 2 Plus for the master and 28-pin board for the slave here are the data sheets.

http://ww1.microchip.com/downloads/en/DeviceDoc/PICDEM%202Plus%20Old%20Version%2051275b.pdf

http://ww1.microchip.com/downloads/en/DeviceDoc/41301A.pdf

I connected the SCL and SDA and checked them more than once. The picdem 2 plus has two built in pull-up resistors so that was given and you can check the data sheet if you want to. I powered the slave from the Master's board and had a common ground between the two boards. I think that the oscillators are the main problems. What do you think ?
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