 |
 |
| View previous topic :: View next topic |
| Author |
Message |
asunca
Joined: 09 Sep 2005 Posts: 36
|
| i2c master slave |
Posted: Mon Sep 12, 2005 2:01 am |
|
|
ive been working for too long to finish the work on pic communication. i followed the suggestions and i still couldnt make the circuit work. here is my code :
master
| Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use i2c(Master,fast,sda=PIN_C4,scl=PIN_C3)
void main()
{
port_b_pullups(FALSE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_b(0x00);
output_high(pin_b7);
Delay_Ms(250);
Delay_Ms(250);
Delay_Ms(250);
Delay_Ms(250);
output_low(pin_b7);
i2c_start();
i2c_write(0xa0); // slave Device address
i2c_write(0x06); // port b adress in slave 16f877
i2c_write(0x01); // led on 3. bit
i2c_stop();
output_high(pin_b6); // shows i2c finished
while(1);
}
|
and the slave .....
| Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,restart_wdt,address=0xa0)
unsigned char read_i2c(void);
void i2c_interrupt_handler(void);
void i2c_initialize(void);
void i2c_error(void);
void write_i2c(unsigned char transmit_byte);
#INT_SSP
void ssp_interupt ()
{
i2c_interrupt_handler();
}
#define RX_BUF_LEN 32
unsigned char slave_buffer[RX_BUF_LEN];
int buffer_index;
int comms_error;
int debug_state;
void i2c_interrupt_handler(void)
{
unsigned char i2c_mask = 0x2D; /* 0010 1101 */
unsigned char temp_sspstat;
unsigned char this_byte;
unsigned char tx_byte;
int x;
/* Mask out the unnecessary bits */
temp_sspstat = PIC_SSPSTAT & i2c_mask;
switch (temp_sspstat)
{
/* Write operation, last byte was an address, buffer is full */
case 0x09: /* 0000 1001 */
/* Clear the receive buffer */
for (x=0; x<RX_BUF_LEN; x++)
{
slave_buffer[x] = 0x00;
}
buffer_index = 0; /* Clear the buffer index */
this_byte = read_i2c(); /* Do a dummy read of PIC_SSPBUF */
debug_state = 1;
break;
/* Write operation, last byte was data, buffer is full */
case 0x29: /* 0010 1001 */
/* Point to the buffer */
this_byte = read_i2c(); /* Get the byte from the SSP */
slave_buffer[buffer_index] = this_byte; /* Put it into the buffer */
buffer_index++; /* Increment the buffer pointer */
/* Get the current buffer index */
/* Subtract the buffer length */
/* Has the index exceeded the buffer length? */
if (buffer_index >= RX_BUF_LEN)
{
buffer_index = 0; /* Yes, clear the buffer index. */
}
debug_state = 2;
break;
/* Read operation; last byte was an address, buffer is empty */
case 0x0C: /* 0000 1100 */
buffer_index = 0; /* Clear the buffer index */
/* Point to the buffer */
tx_byte = slave_buffer[buffer_index]; /* Get byte from the buffer */
write_i2c(tx_byte); /* Write the byte to PIC_SSPBUF */
buffer_index++; /* increment the buffer index */
debug_state = 3;
break;
/* Read operation; last byte was data, buffer is empty */
case 0x2C: /* 0010 1100 */
/* Get the current buffer index */
/* Subtract the buffer length */
/* Has the index exceeded the buffer length? */
if (buffer_index >= RX_BUF_LEN)
{
buffer_index = 0; /* Yes, clear the buffer index */
}
/* Point to the buffer */
/* Get the byte */
tx_byte = slave_buffer[buffer_index];
write_i2c(tx_byte); /* Write to PIC_SSPBUF */
buffer_index++; /* increment the buffer index */
debug_state = 4;
break;
/* A NACK was received when transmitting data back from the master. */
/* Slave logic is reset in this case. R_W=0, D_A=1, and BF=0. */
/* If we don't stop in this state, then something is wrong!! */
case 0x28: /* 0010 1000 */
debug_state = 5;
break;
/* Something went wrong!! */
default:
i2c_error();
break;
}
}
void i2c_error(void)
{
comms_error = 1;
}
void write_i2c(unsigned char transmit_byte)
{
unsigned char write_collision = 1;
while (PIC_SSPSTAT & PIC_SSPSTAT_BIT_BF) /* Is BF bit set in PIC_SSPSTAT? */
{
/* If yes, then keep waiting */
}
while (write_collision)
{
/* If not, then do the i2c_write. */
PIC_SSPCON1 &= ~PIC_SSPCON1_BIT_WCOL; /* Clear the WCOL flag */
PIC_SSPBUF = transmit_byte;
/* Was there a write collision? */
if (PIC_SSPCON1 & PIC_SSPCON1_BIT_WCOL)
{
/* Yes there was a write collision. */
write_collision = 1;
}
else
{
/* NO, there was no write collision. */
/* The transmission was successful */
write_collision = 0;
}
}
PIC_SSPCON1 |= PIC_SSPCON1_BIT_CKP; /* Release the clock. */
}
/* This function returns the byte in SSPBUF */
unsigned char read_i2c(void)
{
return PIC_SSPBUF;
}
void main()
{
port_b_pullups(FALSE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
set_tris_b(0x00);
output_high(pin_b7);
debug_state = 0;
while(1);
}
|
slow=50000 doesnt work. pullups are 5k .. ithnik the interrupt routine doesnt work. i need a new one... |
|
 |
Guest
|
|
Posted: Mon Sep 12, 2005 3:47 am |
|
|
A couple of generic suggestions.
The PIC16F877 does not have a very deep stack. Your application is unnecessarly wasting stack depth. In your interrrupt handler the only think you do is call the real handler. This wastes both CPU cyles and stack depth.
The i2c_error function wastes stack depth and CPU cycles. I think this could be more efficiently handled as a #define
Similarly for the read_ic2() function... |
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Mon Sep 12, 2005 5:27 am |
|
|
now i tried with the last changes. i ve gone further..
the master tranmits(using scope), and the addressing mechanism is true.. and the slave goes into interrupt but doesnt read the incoming data. i think interrupt routine that i hardly found still doesnt work.
i need an efficient one. what am i going to do?? |
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Tue Sep 13, 2005 1:06 am |
|
|
i ve changed my slave interrupt routine to a more efficient one. now when the interrupt comes from the master, slave goes into interrupt it is okey but it doesnt respond to master's order. master tells to set output high pin_b0 but icouldnt do this. here they are..
master
| Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use I2C(Master,fast,sda=PIN_C4,scl=PIN_C3)
void main()
{
port_b_pullups(FALSE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_b(0x00);
output_high(pin_b7);
Delay_Ms(250);
Delay_Ms(250);
Delay_Ms(250);
Delay_Ms(250);
output_low(pin_b7);
i2c_start();
i2c_write(0xa0); // slave Device address
Delay_Ms(5);
i2c_write(0x06); // port b adress in slave 16f877
Delay_Ms(5);
i2c_write(0x01); // led on 0. bit
Delay_Ms(5);
i2c_stop();
output_high(pin_b6); // shows i2c finished
while(1);
}
|
and the slave....
| Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use I2C(Slave,Slow,sda=PIN_C4,scl=PIN_C3,address=0xa0)
typedef enum {NOTHING, CONTROL_READ,
ADDRESS_READ, READ_COMMAND_READ} I2C_STATE;
I2C_STATE fState;
BYTE address, buffer[0x10];
#INT_SSP
void ssp_interupt ()
{
BYTE incoming;
if (i2c_poll() == FALSE) {
if (fState == ADDRESS_READ) { //i2c_poll() returns false on the
i2c_write (buffer[address]);//interupt receiving the second
fState = NOTHING; //command byte for random read operation
}
}
else {
incoming = i2c_read();
if (fState == NOTHING){
fState = CONTROL_READ;
}
else if (fState == CONTROL_READ) {
address = incoming;
fState = ADDRESS_READ;
}
else if (fState == ADDRESS_READ) {
buffer[address] = incoming;
fState = NOTHING;
}
}
output_high(pin_b6);
}
void main()
{
int i;
port_b_pullups(FALSE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
fState = NOTHING;
address = 0x00;
for (i=0;i<0x10;i++)
buffer[i] = 0x00;
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
set_tris_b(0x00);
output_high(pin_b7);
while(1);
}
|
|
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Tue Sep 13, 2005 1:07 am |
|
|
i ve changed my slave interrupt routine to a more efficient one. now when the interrupt comes from the master, slave goes into interrupt it is okey but it doesnt respond to master's order. master tells to set output high pin_b0 but icouldnt do this. here they are..
master
| Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use I2C(Master,fast,sda=PIN_C4,scl=PIN_C3)
void main()
{
port_b_pullups(FALSE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_b(0x00);
output_high(pin_b7);
Delay_Ms(250);
Delay_Ms(250);
Delay_Ms(250);
Delay_Ms(250);
output_low(pin_b7);
i2c_start();
i2c_write(0xa0); // slave Device address
Delay_Ms(5);
i2c_write(0x06); // port b adress in slave 16f877
Delay_Ms(5);
i2c_write(0x01); // led on 0. bit
Delay_Ms(5);
i2c_stop();
output_high(pin_b6); // shows i2c finished
while(1);
}
|
and the slave....
| Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use I2C(Slave,Slow,sda=PIN_C4,scl=PIN_C3,address=0xa0)
typedef enum {NOTHING, CONTROL_READ,
ADDRESS_READ, READ_COMMAND_READ} I2C_STATE;
I2C_STATE fState;
BYTE address, buffer[0x10];
#INT_SSP
void ssp_interupt ()
{
BYTE incoming;
if (i2c_poll() == FALSE) {
if (fState == ADDRESS_READ) { //i2c_poll() returns false on the
i2c_write (buffer[address]);//interupt receiving the second
fState = NOTHING; //command byte for random read operation
}
}
else {
incoming = i2c_read();
if (fState == NOTHING){
fState = CONTROL_READ;
}
else if (fState == CONTROL_READ) {
address = incoming;
fState = ADDRESS_READ;
}
else if (fState == ADDRESS_READ) {
buffer[address] = incoming;
fState = NOTHING;
}
}
output_high(pin_b6);
}
void main()
{
int i;
port_b_pullups(FALSE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
fState = NOTHING;
address = 0x00;
for (i=0;i<0x10;i++)
buffer[i] = 0x00;
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
set_tris_b(0x00);
output_high(pin_b7);
while(1);
}
|
|
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 13, 2005 5:09 am |
|
|
| According to your code, there's nothing in there to set B0. How is the pic supposed to know what to do if you don't tell it? |
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Tue Sep 13, 2005 8:18 am |
|
|
i couldnt get you. master transmits:
*slave's address (A0h)
*slave's portb (at address 06h)
*and puts the value 0x01 in portb ( led on pin_b0 )
arent they true??
maybe wrong..
because ican send the slaves address well and the make the interrupt..
then what should i do???? |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 13, 2005 8:50 am |
|
|
| The slave code is where I was referring. There's nothing there to control the port. |
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Wed Sep 14, 2005 4:52 am |
|
|
but the slave's portb has already been set as output. what else should be done other than this?
or i got you wrong??
or you got me wrong?? |
|
 |
Ttelmah Guest
|
|
Posted: Wed Sep 14, 2005 5:09 am |
|
|
The point that was being made, is that there is no instruction anywhere in the slave code, to control pin B0.
In your summary, you say "and puts the value 0x01 in portb ( led on pin_b0 )". However there is no instruction in the slave to actually do this. You transfer the data byte to the 'buffer', but at no point is this byte sent to portB...
Best Wishes |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Sep 14, 2005 7:29 am |
|
|
| The PIC doesn't operate like an EEPROM. You don't just send it an address and data byte and it gets written to it. The data coming in is just data. The slave must receive the data and then do something with it. The something is controlled by your code. That means that you have to write the code to do whatever task you want. |
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Thu Sep 15, 2005 1:57 am |
|
|
oh i see. this is the point of view.
now, i received the the data. but into which buffer?? is it the sspsr?? or something like that??
do you mean, in the interrupt routine i must add the extra code when i get the data or address into the shift register??
simple to say:
sspr has 0x06 then this value must be assigned to a variable say "adres"
then sspr has 0x01 then this value must be assigned to an 8bit variable say "value". then what is the last thing to do : output_?(value) ???
making a switch case including the addresses of the ports??? |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Sep 15, 2005 6:35 am |
|
|
If adres is a pointer then to assign value to it, it would be like this:
|
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Thu Sep 15, 2005 6:47 am |
|
|
i ve done those you told me but awfully iam not done. ive been struggling since the morning!!
the code doesnt seem to work altough the changes. i declared a new array "data" and tried to assign the buffer values into the array. then the code looked like output_b(data[0]) at the end..
master is the nearly the same except holding delays longer.. i am sending the slave...
| Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,address=0xa0)
unsigned char data[4];
unsigned char read_i2c(void);
void i2c_interrupt_handler(void);
void i2c_initialize(void);
void i2c_error(void);
void write_i2c(unsigned char transmit_byte);
#INT_SSP
void ssp_interupt ()
{
i2c_interrupt_handler();
}
#define RX_BUF_LEN 32
unsigned char slave_buffer[RX_BUF_LEN];
int buffer_index;
int comms_error;
int debug_state;
void i2c_interrupt_handler(void)
{
unsigned char i2c_mask = 0x2D; /* 0010 1101 */
unsigned char temp_sspstat;
unsigned char this_byte;
unsigned char tx_byte;
int x;
/* Mask out the unnecessary bits */
temp_sspstat = PIC_SSPSTAT & i2c_mask;
output_bit(pin_e0,1);
switch (temp_sspstat)
{
/* Write operation, last byte was an address, buffer is full */
case 0x09: /* 0000 1001 */
/* Clear the receive buffer */
for (x=0; x<RX_BUF_LEN; x++)
{
slave_buffer[x] = 0x00;
}
buffer_index = 0; /* Clear the buffer index */
this_byte = read_i2c(); /* Do a dummy read of PIC_SSPBUF */
debug_state = 1;
break;
/* Write operation, last byte was data, buffer is full */
case 0x29: /* 0010 1001 */
/* Point to the buffer */
this_byte = read_i2c(); /* Get the byte from the SSP */
slave_buffer[buffer_index] = this_byte; /* Put it into the buffer */
data[buffer_index] = slave_buffer[buffer_index];////////////////
buffer_index++; /* Increment the buffer pointer */
/* Get the current buffer index */
/* Subtract the buffer length */
/* Has the index exceeded the buffer length? */
if (buffer_index >= RX_BUF_LEN)
{
buffer_index = 0; /* Yes, clear the buffer index. */
}
debug_state = 2;
break;
/* Read operation; last byte was an address, buffer is empty */
case 0x0C: /* 0000 1100 */
buffer_index = 0; /* Clear the buffer index */
/* Point to the buffer */
tx_byte = slave_buffer[buffer_index]; /* Get byte from the buffer */
write_i2c(tx_byte); /* Write the byte to PIC_SSPBUF */
buffer_index++; /* increment the buffer index */
debug_state = 3;
break;
/* Read operation; last byte was data, buffer is empty */
case 0x2C: /* 0010 1100 */
/* Get the current buffer index */
/* Subtract the buffer length */
/* Has the index exceeded the buffer length? */
if (buffer_index >= RX_BUF_LEN)
{
buffer_index = 0; /* Yes, clear the buffer index */
}
/* Point to the buffer */
/* Get the byte */
tx_byte = slave_buffer[buffer_index];
write_i2c(tx_byte); /* Write to PIC_SSPBUF */
buffer_index++; /* increment the buffer index */
debug_state = 4;
break;
/* A NACK was received when transmitting data back from the master. */
/* Slave logic is reset in this case. R_W=0, D_A=1, and BF=0. */
/* If we don't stop in this state, then something is wrong!! */
case 0x28: /* 0010 1000 */
debug_state = 5;
break;
}
}
void write_i2c(unsigned char transmit_byte)
{
unsigned char write_collision = 1;
while (PIC_SSPSTAT & PIC_SSPSTAT_BIT_BF) /* Is BF bit set in PIC_SSPSTAT? */
{
/* If yes, then keep waiting */
}
while (write_collision)
{
/* If not, then do the i2c_write. */
PIC_SSPCON1 &= ~PIC_SSPCON1_BIT_WCOL; /* Clear the WCOL flag */
PIC_SSPBUF = transmit_byte;
/* Was there a write collision? */
if (PIC_SSPCON1 & PIC_SSPCON1_BIT_WCOL)
{
/* Yes there was a write collision. */
write_collision = 1;
}
else
{
/* NO, there was no write collision. */
/* The transmission was successful */
write_collision = 0;
}
}
PIC_SSPCON1 |= PIC_SSPCON1_BIT_CKP; /* Release the clock. */
}
/* This function returns the byte in SSPBUF */
unsigned char read_i2c(void)
{
return PIC_SSPBUF;
}
void main()
{
unsigned char i;
port_b_pullups(FALSE);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
set_tris_e(0x00);
set_tris_d(0x00);
set_tris_b(0x00);
output_high(pin_b7);
debug_state = 0;
for (i=0;i<4;i++)
{
data[i]=0x00;
}
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while(1)
{
output_b(data[1]);
}
}
|
|
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Mon Sep 19, 2005 1:13 am |
|
|
| can not we have the incoming data in the buffer into a user defined array? and use thsi 8-bit array for our general purposes?? |
|
 |
|
|
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
|