|
|
View previous topic :: View next topic |
Author |
Message |
roger01
Joined: 31 Mar 2011 Posts: 4
|
SPI problem incorrect data transfer |
Posted: Thu Mar 31, 2011 2:39 am |
|
|
Good day,
I'm having a problem with the data that is fed by the slave to the master.
the data sent by the slave is already shift to the right.
When the Slave send 0x02 the master will receive 0x01 and if the slave write 0x01 the output in the master is 0x00.
I'm using two pic16f877a for the master and slave.
I'm using CCS PCW.
I simulate it to Proteus and program through the hardware.
Master
Code: |
#include <16F877a.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=8000000)
#define slave_select PIN_C1
#define SPI_MODE_0_0 0x4000
#define SPI_MODE_0_1 0x0000
#define SPI_MODE_1_0 0x0010
#define SPI_MODE_1_1 0x4010
#byte PORTB = 6
#use standard_io(a)
#use standard_io(b)
#use standard_io(c)
void main() {
char b;
set_tris_b(0);
setup_spi(spi_master | spi_l_to_h | spi_clk_div_4);
output_high(pin_c2);
PORTB=0;
while(true){
output_high(slave_select);
output_low(slave_select);
b=spi_read(0);
output_b(b); //shift
output_high(slave_select);
delay_us(100);
}
output_low(pin_c2);
}
|
Code: |
#include <16F877.H>
#device adc=8
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#byte SSPBUF = 0x13
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
int8 data;
#int_ssp
void ssp_isr(void)
{
SSPBUF =data;
}
void main()
{
int8 i=0,value[8];
value[0]=0x01;
value[1]=0x02;
value[2]=0x04;
value[3]=0x08;
value[4]=0x10;
value[5]=0x20;
value[6]=0x40;
value[7]=0x80;
setup_spi(SPI_SLAVE | SPI_MODE_0);
while(true){
delay_ms(20);
for(i=0;i<=7;i++){
data =value[i];
output_B(data);
delay_ms(20);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
}
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Thu Mar 31, 2011 2:50 am |
|
|
_Keep the two ends using the same mode_.....
You have the mode definitions on the slave. Load these into the master, and use the same mode for the master as the slave. Currently the slave is using mode0, while the master is using mode1.
The master is using the opposite clock 'edge' to the slave, so gets the bits shifted...
Add the mode definitions from the slave, and setup the master with:
setup_spi(SPI_MASTER| SPI_MODE_0 | SPI_CLK_DIV_4);
Best Wishes |
|
|
roger01
Joined: 31 Mar 2011 Posts: 4
|
Save the spi_read to Array |
Posted: Fri Apr 01, 2011 2:30 am |
|
|
I already change the mode in setup_spi in both master and slave.
Thanks.
Another Problem
I change my master program in order for it to save all the eight bytes that will be send by the slave but the output in portB where I output the saved data from the array is not correct. What do you think is the problem/s?
Master Code
Code: |
#include <16F877a.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=8000000)
#define slave_select PIN_C1
#byte PORTB = 6
#use standard_io(a)
#use standard_io(b)
#use standard_io(c)
void main() {
char b[8]={0,0,0,0,0,0,0,0},i=0;
set_tris_b(0);
//PORTB=0;
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
output_high(pin_c2);
while(true){
//b[0]
output_high(slave_select);
output_low(slave_select);
delay_us(100);
b[0]=spi_read(0);
output_b(b[i]);
output_d(i);//shift
output_high(slave_select);
output_high(PIN_D2);
output_low(pin_d0);
//b[1]
output_high(slave_select);
output_low(slave_select);
delay_us(100);
b[1]=spi_read(0);
output_b(b[i]);
output_d(i);//shift
output_high(slave_select);
output_high(PIN_D2);
output_low(pin_d0);
//b[2]
output_high(slave_select);
output_low(slave_select);
delay_us(100);
b[2]=spi_read(0);
output_b(b[i]);
output_d(i);//shift
output_high(slave_select);
output_high(PIN_D2);
output_low(pin_d0);
//b[3]
output_high(slave_select);
output_low(slave_select);
delay_us(100);
b[3]=spi_read(0);
output_b(b[i]);
output_d(i);//shift
output_high(slave_select);
output_high(PIN_D2);
output_low(pin_d0);
//b[4]
output_high(slave_select);
output_low(slave_select);
delay_us(100);
b[4]=spi_read(0);
output_b(b[i]);
output_d(i);//shift
output_high(slave_select);
output_high(PIN_D2);
output_low(pin_d0);
//b[5]
output_high(slave_select);
output_low(slave_select);
delay_us(100);
b[5]=spi_read(0);
output_b(b[i]);
output_d(i);//shift
output_high(slave_select);
output_high(PIN_D2);
output_low(pin_d0);
//b[6]
output_high(slave_select);
output_low(slave_select);
delay_us(100);
b[6]=spi_read(0);
output_b(b[i]);
output_d(i);//shift
output_high(slave_select);
output_high(PIN_D2);
output_low(pin_d0);
//b[7]
output_high(slave_select);
output_low(slave_select);
delay_us(100);
b[7]=spi_read(0);
output_b(b[i]);
output_d(i);//shift
output_high(slave_select);
output_high(PIN_D2);
output_low(pin_d0);
break;
}
while(true){
for(i=0;i<=7;i++){
output_B(b[i]);
delay_ms(100);
}
}
}
|
|
|
|
|
|
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
|