 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Sep 19, 2005 8:35 am |
|
|
| Sure you can. |
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Mon Sep 19, 2005 8:51 am |
|
|
but i am assigning
data1=slave_buffer[0];
data2=slave_buffer[1];
...
...
data1,data2,... (my variables)
and i still couldnt reach the end...
when i opened up this entry, it was one week ago.. |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Sep 19, 2005 9:23 am |
|
|
Here is something for you to try. Its a cut and paste of other code so there might be some errors for you to work out.
| 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 SSPBUF;
#locate SSPBUF=0x13
unsigned char SSPCON;
#locate SSPCON=0x14
struct {
unsigned char SSPM0:1;
unsigned char SSPM1:1;
unsigned char SSPM2:1;
unsigned char SSPM3:1;
unsigned char CKP:1;
unsigned char SSPEN:1;
unsigned char SSPOV:1;
unsigned char WCOL:1;
} SSPCONbits ;
#locate SSPCONbits=0x14
unsigned char SSPSTAT;
#locate SSPSTAT=0x94
struct {
unsigned char BF:1;
unsigned char UA:1;
unsigned char R_W:1;
unsigned char S:1;
unsigned char P:1;
unsigned char D_A:1;
unsigned char CKE:1;
unsigned char SMP:1;
} SSPSTATbits ;
#locate SSPSTATbits=0x94
int8 GotData = FALSE;
int8 address, buffer[0x10];
#INT_SSP
void ssp_interupt ()
{
static int8 index = 0; // num of bytes received
static int8 data = 0; // data read from SSPBUF
// Make sure the buffer has not overflowed
if (SSPCONbits.SSPOV)
{
data = SSPBUF;
SSPCONbits.SSPOV = 0;
return;
}
// Check for a stop bit
if (SSPSTATbits.P)
{
// Do whatever you want on stop
}
else
{
// were we addressed in read mode
if (SSPSTATbits.R_W)
{
// This is where you would assign data to be read from this pic
SSPBUF = 0;
// Release the clock
SSPCONbits.CKP=1;
}
// See if we were addressed
else if (SSPSTATbits.BF)
{
// process the first byte which is the address
if (!SSPSTATbits.D_A)
{
// Read the data
data = SSPBUF;
index = 0;
}
// we received data
else
{
// Read the data
data = SSPBUF;
if (index < sizeof(buffer)
{
buffer[index] = data;
index++;
// Cheap and dirty way of saying that we have received 2 bytes of data
// so that main can process them.
if (index == 2)
{
GotData = TRUE;
}
}
}
}
}
}
void main(void)
{
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);
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)
{
// We received data and the flag was set in the ISR
if (GotData)
{
// Disable the int while we access the data
disable_interrupts(INT_SSP);
// take action base on the register address received.
switch (buffer[0])
{
case 5:
output_a(buffer[1]);
break;
case 6:
output_b(buffer[1]);
break;
case 7:
output_c(buffer[1]);
break;
case 8:
output_d(buffer[1]);
break;
}
GotData = FALSE;
enable_interrupts(INT_SSP);
}
}
}
|
|
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Wed Sep 21, 2005 8:25 am |
|
|
i ve done all the dirty ways but still still no wayyyy
i ve tried to fix your code into mine , i think i made it but i doesnt seem to make "index=2".. and this means, slave cant go into interrupt...
okey take a look at this please...
| Code: |
#include <16F877.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, PUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,address=0xa0)
int1 yon; // duz gitmek icin 1, geriye gitmek icin 0// yada bir pin define edilebilir.
void i2c_interrupt_handler(void);
void write_i2c(unsigned char transmit_byte);
#INT_SSP
void ssp_interupt ()
{
i2c_interrupt_handler();
}
#define RX_BUF_LEN 10
unsigned char slave_buffer[RX_BUF_LEN];
int buffer_index;
int debug_state;
int1 gotdata=0;
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;
temp_sspstat = PIC_SSPSTAT & i2c_mask; // mask out the unnecessary bits
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 = PIC_SSPBUF; /* 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 = PIC_SSPBUF; /* Get the byte from the SSP */
slave_buffer[buffer_index] = this_byte; /* Put it into the buffer */
buffer_index++; /* Increment the buffer pointer */
if (buffer_index==2)
{
gotdata=1;
output_high(pin_b7);
}
/* 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. */
}
void generate_pwm (int yatay,dikey)
{
long duty;
if ((yatay < 131) & (yatay > 125))
{
if (dikey > 131)
{
yon=1;
duty= dikey ;
}
if ((dikey < 131) & (dikey > 125))
{
duty= 0;
}
if ((dikey < 125 ) & (dikey > 1))
{
yon=0;
duty= 255 - (2*dikey) ;
}
}
if ((yatay < 125) & (yatay > 1)) ////////////yatay durum2
{
if ((dikey<131)& (dikey>125))
{
yon=0;
duty=128; /// saga donus icin sag motor geriye gider
}
if (dikey>131)
{
yon=1;
duty=128+yatay;
}
if ((dikey<125) & (dikey > 1))
{
yon=0;
duty= 128+yatay;
}
}
if (yatay > 131) /////// yatay durum 3
{
if ((dikey<131)& (dikey>125))
{
yon=1;
duty=128; /// sola donus icin..
}
if (dikey>131)
{
yon=1;
duty=dikey;
}
if ((dikey<125) & (dikey > 1))
{
yon=0;
duty=255- (2*dikey);
}
}
set_pwm1_duty(duty);
}
void main()
{
int dikey=250;
int yatay=250;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,255,1);
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
set_tris_b(0x00);
output_b(0);
enable_interrupts(GLOBAL);
//debug_state = 0;
enable_interrupts(INT_SSP);
while(1)
{
if (gotdata)
{
disable_interrupts(INT_SSP);
yatay= slave_buffer[0];
dikey= slave_buffer[1];
gotdata = 0;
enable_interrupts(INT_SSP);
}
generate_pwm(yatay,dikey);
}
}
|
the logic is similar to yours, can you please show whatelse should i do |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Sep 21, 2005 9:18 am |
|
|
| It doesn't look anything like mine so I'm opting out. |
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Thu Sep 22, 2005 2:49 am |
|
|
i ve told you that the logic is similar to yours.
5 possible receiption conditions are taken into consideration, on the second one where the slave_buffer (yours is "buffer[]") has the incoming data, i ve added "gotdata" term when index=0 (address taken), index=1 (data taken), and in the main part i ve added
if (gotdata)
{
variable1= slave_buffer[0]; // first data after address
variable2= slave_buffer[1]; // second data
gotdata=0;
}
and then i ve called the pwm routine with the parameters variable1 and variable2.
it is easy to understand and strongly correlated with your code.. |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Sep 22, 2005 6:06 am |
|
|
| asunca wrote: | i ve told you that the logic is similar to yours.
5 possible receiption conditions are taken into consideration, on the second one where the slave_buffer (yours is "buffer[]") has the incoming data, i ve added "gotdata" term when index=0 (address taken), index=1 (data taken), and in the main part i ve added
if (gotdata)
{
variable1= slave_buffer[0]; // first data after address
variable2= slave_buffer[1]; // second data
gotdata=0;
}
and then i ve called the pwm routine with the parameters variable1 and variable2.
it is easy to understand and strongly correlated with your code.. |
I took my frigin time to write you a program to test and you didn't or didn't post the test results! Yours is difficult to follow with all those "magic numbers". Why should I waste my time trying to figure out why yours doesn't work since the one I gave you is running in 10s of thousands of pics for 8 years or so. |
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Mon Sep 26, 2005 4:25 am |
|
|
thank you mark for your valuable suggestions and helps. by the weekend i am finished with it. i ve found out that the master was decleared as fast and the slave was slow. the main problem was about master's speed i ve put 15ms delays in every command line of i2c transmit routine. that worked. each step was tried one by one. that was a hard work for me but the success came with it.
ps: you havent spent your precious time for nothing |
|
 |
aaaaamartin
Joined: 17 Apr 2005 Posts: 39 Location: Germany Stuttgart
|
|
Posted: Tue Sep 27, 2005 1:28 am |
|
|
Hello,
I somewhere else read, and do believe myself, that
the speed does depend only on the master.
Slow in the slave is meaningless.
Anyway one could stretch the clock in the slave code.
I'm at the moment trying to make Mark's slave code working.
Well it does, by putting in the delays in the master, but believe me,
I would never comsider it workin, just because I put delays in the master code.
There has to be another way...
When I leave out delays , the master gets stuck, cause there is SSPOV set
in the slave, and the slave ceases acking then.
I know, we can go without delays at full speed.
I once got it to work, using built in I2C functions, and
a slightly modified example, posted early 2005 by Ttelmah.
I don't know why Marks example hangs without delays, but I will
try to figure this out today, and post any results to a new thread.
I hope I get done today, otherwise I'll ask for help here.
A nice day to everybody,
Martin |
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Tue Sep 27, 2005 3:35 am |
|
|
i ve looked through the extee. c external eeprom example. they used i2c_write() commands without delays. but they' ve written
#use i2c(master, sda=.., scl=...)
is this fast or slow??? then there must be a way as you said to throw the delays . |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 27, 2005 6:40 am |
|
|
| aaaaamartin wrote: | Hello,
I somewhere else read, and do believe myself, that
the speed does depend only on the master.
Slow in the slave is meaningless.
Anyway one could stretch the clock in the slave code.
I'm at the moment trying to make Mark's slave code working.
Well it does, by putting in the delays in the master, but believe me,
I would never comsider it workin, just because I put delays in the master code.
There has to be another way...
When I leave out delays , the master gets stuck, cause there is SSPOV set
in the slave, and the slave ceases acking then.
I know, we can go without delays at full speed.
I once got it to work, using built in I2C functions, and
a slightly modified example, posted early 2005 by Ttelmah.
I don't know why Marks example hangs without delays, but I will
try to figure this out today, and post any results to a new thread.
I hope I get done today, otherwise I'll ask for help here.
A nice day to everybody,
Martin |
Do you have any other interrupts happening? Are you disabling ints at anytime? The first check in the int handler is the check for the SSPOV. If it is set, then it should get cleared. There is the possibility that between the check and when the data is actually read another byte comes in. You could read the status at the start. Then read the data.
You should not need any delays.
Last edited by Mark on Tue Sep 27, 2005 6:50 am; edited 1 time in total |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 27, 2005 6:48 am |
|
|
| 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 SSPBUF;
#locate SSPBUF=0x13
unsigned char SSPCON;
#locate SSPCON=0x14
struct {
unsigned char SSPM0:1;
unsigned char SSPM1:1;
unsigned char SSPM2:1;
unsigned char SSPM3:1;
unsigned char CKP:1;
unsigned char SSPEN:1;
unsigned char SSPOV:1;
unsigned char WCOL:1;
} SSPCONbits ;
#locate SSPCONbits=0x14
unsigned char SSPSTAT;
#locate SSPSTAT=0x94
struct SSPSTATBITS {
unsigned char BF:1;
unsigned char UA:1;
unsigned char R_W:1;
unsigned char S:1;
unsigned char P:1;
unsigned char D_A:1;
unsigned char CKE:1;
unsigned char SMP:1;
} ;
int8 GotData = FALSE;
int8 address, buffer[0x10];
#INT_SSP noclear
void ssp_interupt ()
{
static int8 index = 0; // num of bytes received
static int8 data = 0; // data read from SSPBUF
struct SSPSTATBITS statbits;
statbits = SSPSTAT;
data = SSPBUF;
clear_interrupt(INT_SSP);
// Make sure the buffer has not overflowed
if (SSPCONbits.SSPOV)
{
data = SSPBUF;
SSPCONbits.SSPOV = 0;
return;
}
// Check for a stop bit
if (statbits.P)
{
// Do whatever you want on stop
}
else
{
// were we addressed in read mode
if (statbits.R_W)
{
// This is where you would assign data to be read from this pic
SSPBUF = 0;
// Release the clock
SSPCONbits.CKP=1;
}
// See if we were addressed
else if (statbits.BF)
{
// process the first byte which is the address
if (!statbits.D_A)
{
// Read the data
data = SSPBUF;
index = 0;
}
// we received data
else
{
// Read the data
data = SSPBUF;
if (index < sizeof(buffer)
{
buffer[index] = data;
index++;
// Cheap and dirty way of saying that we have received 2 bytes of data
// so that main can process them.
if (index == 2)
{
GotData = TRUE;
}
}
}
}
}
}
void main(void)
{
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);
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)
{
// We received data and the flag was set in the ISR
if (GotData)
{
// Disable the int while we access the data
disable_interrupts(INT_SSP);
// take action base on the register address received.
switch (buffer[0])
{
case 5:
output_a(buffer[1]);
break;
case 6:
output_b(buffer[1]);
break;
case 7:
output_c(buffer[1]);
break;
case 8:
output_d(buffer[1]);
break;
}
GotData = FALSE;
enable_interrupts(INT_SSP);
}
}
}
|
|
|
 |
asunca
Joined: 09 Sep 2005 Posts: 36
|
|
Posted: Tue Sep 27, 2005 12:53 pm |
|
|
i ve tried this code and it is ok but what i wanna ask is when the master is selected fast, my master code is like ;
i2c_start();
delay_ms(15);
i2c_write(0xa0); // slave Device address
delay_ms(15);
i2c_write(0x06); // port b adress in slave 16f877
delay_ms(15);
i2c_write(0x01); // led on 3. bit
delay_ms(15);
i2c_stop();
my slave didnt work properly before i put those delays,
what should i do to getrid of those when master is fast??? |
|
 |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 27, 2005 1:18 pm |
|
|
Try running the slave at 20MHz. Or not using fast mode.
400KHz for 9 bits takes about 22.5us to transfer a byte. A PIC running at 4MHz operates each instruction at 1us. The interrupt handler takes longer than that. |
|
 |
Guest
|
|
Posted: Tue Sep 27, 2005 8:52 pm |
|
|
Mark,
I'm not yet done, thanks for positng the code.
May I ask something on that ?
Why use slave & slow ?
Why and how use noclear here ?
Why dissable_interrupts in main (), you might loose data ?
Thanks so far
Martin |
|
 |
|
|
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
|