|
|
View previous topic :: View next topic |
Author |
Message |
armstrong800
Joined: 10 Oct 2006 Posts: 1
|
Need help on I2C slave please |
Posted: Tue Oct 10, 2006 11:19 pm |
|
|
Hello folks, I have just picked up the CCS C compiler software for my project. I am using one PIC 16F877 as a slave in a single master I2C configuration. A Motorola’s HCS12 acts as a master. First thing that made me uncomfortable was that the C language I learned before isn’t usable in CCS as it seems to have its own syntax. I like the idea of pre processor, but for a new comer like me I feel it is hard to understand how to use them even after read the CCS manual. And the example codes are a little too complicated for me now. I managed to tailor up this program from what I have learnt from several codes I found to initiate the slave function of the PIC but it is not working. My partner and I have been spending some time on this little thing. Would anyone please shed some light on how to correct this program? A lot thanks ahead.
My partner's master has been tested to work, so it must be my program that is wrong. Here it is. All I want is just being able to receive and transmit bytes.
#if defined(__PCM__) // preprocessor directive that chooses the compiler
#include <16F877.h>
#fuses XT,NOWDT,NOLVP
#use delay(clock=4000000)
//#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0xa0,FAST = 450000, FORCE_HW)
#byte SSPSTAT = 0x94
#byte SSPCON = 0x41
#byte SSPADD = 0x41
#byte PIE1 = 0x41
#byte PIR1 = 0x0c
#byte TRISC = 0x87
#byte SSPBUF = 0x13
#byte Inputt = 0x6800
#define Stop_bit PIN_d4
#define buffer_full PIN_d0
#INT_SSP
void i2c_slave_interrupt ()
{
byte input;
byte tempI2C;
input = SSPSTAT;
if (Input(Stop_bit)==1) // If the stop bit of the SPSTAT is a one then the slave is to transmit (why is that ?? Is a stop bit the last bit it receives??) {
//I2C Slave Transmission
SSPBUF = 0; // clear the SSPBUF
bit_set(SSPCON,4); // CKP, SSPBUFF must be write before
bit_clear(PIR1,3); //reset the interrupt flag
}
else
{
//I2C Slave Reception
tempI2C = SSPBUF;
bit_clear(PIR1,3); //reset the interrupt flag
}
}
void init_i2c_slave()
{
bit_set(SSPSTAT,7); // standard speed mode
bit_clear(SSPSTAT,6); // input level conform to I2C spec
bit_clear(SSPSTAT,0); // BF buffer full bit
//SCL and SDA as input
bit_set(TRISC,3);
bit_set(TRISC,4);
//SSPCON<5> enable bit (1)
//SSPCON<3> 0110 (slave, 7 bits address)
bit_clear(SSPCON,7); // WCOL Write Collision Detect bit
bit_clear(SSPCON,6); // SSPOV Receive Overflow Indicator bit
bit_set(SSPCON,5); // Synchronous Serial Port Enable
bit_set(SSPCON,4); // CKP
bit_clear(SSPCON,3); //0110: I2C slave mode: 7 bits address
bit_set(SSPCON,2);
bit_set(SSPCON,1);
bit_clear(SSPCON,0);
SSPADD=0x0E; // 0000 1110 -> 7 bits address= 0000 111
//SSP
bit_set(PIE1,3);
bit_clear(PIR1,3);
}
void main ()
{
init_i2c_slave();
SET_TRIS_B(0x00);
inputt = SSPBUF &0x01; // (just to see if the SSPBUF is ever filled)
while(1)
{
if (Inputt==1){
output_high (PIN_B7);
delay_ms(1000);
output_low (PIN_B7);
delay_ms(1000);
}
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 10, 2006 11:45 pm |
|
|
I suggest that you start with the CCS example, Ex_Slave.c.
Here's the file location:
c:\program files\picc\examples\ex_slave.c
Here's some simple master code in this post, which sends a single
character to a PIC running Ex_Slave.c, and then reads it back.
http://www.ccsinfo.com/forum/viewtopic.php?t=28097&start=9
Remember that you need pullup resistors (4.7K will work) on SDA
and SCL, and you need a ground connection between the two boards. |
|
|
|
|
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
|