Discussion in "PIC Microcontroller Discussion" started by    Hanzala    Mar 13, 2008.
Thu Mar 13 2008, 02:55 pm
#1
I am reading the ADC of the master controller and sending the data to the slave through SPI.

Master is sending the data accurately, but slave is receiving in this fashion

Master send = 179
Slave read = 179 210 218 51 46 124 168 254 232 201 179 ..........
This means that I receive the correct data after 8 or 9 bytes as you can see the pattern shown above. Another thing is that I am sending this data on hyper terminal.

My code for master and slave is given below.

Master Code:


#include <16F877A.h>
//
#fuses NOWDT,NOPROTECT,XT,NOLVP,PUT
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
float val=0;
int8 data=0;





#int_TIMER0

void pick_sample(void)
{
set_timer0(150);
set_adc_channel(0);
delay_us(10);
val = read_adc();
data = (int8)val;

}


void main()
{

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);	
set_timer0(150);

enable_interrupts(int_rtcc);
enable_interrupts(global);

setup_adc_ports(all_analog);
setup_adc(adc_clock_internal);

setup_spi( SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H | SPI_CLK_DIV_16 );

while(1)
{

spi_write(data);
printf("\n\rdata = %u dataf = %f",data,val);

}
}



Slave code:

                                                   
//SLAVE program

#include <16F877A.h>
//
#fuses NOWDT,NOPROTECT,XT,NOLVP,PUT
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

int8 data=0;


void main()
{

setup_spi(SPI_SLAVE|SPI_H_TO_L|SPI_SS_DISABLED);



	output_high(PIN_C4);
    output_high (PIN_C5);
while(1)
	{
		
	if(SPI_DATA_IS_IN())
{
	  	data = spi_read();
                         
		output_b(data);
            	
      		output_toggle(PIN_D4);

		printf("\n\r data = %u",data);
}
}
}



Please help me in this regard. :-s


[ Edited Thu Mar 13 2008, 03:09 pm ]
Thu Mar 13 2008, 03:27 pm
#2
i do not understand this..
you are sending one byte and on receiver side you are getting bytes like 8 or 9 bytes...
Fri Mar 14 2008, 08:00 pm
#3
I am sending from master 179 continuously, while slave is receiving the correct byte after 8 or 9th byte.That is correct data received after every 8th or 9th byte. :-s
Sat Mar 15 2008, 10:11 am
#4
can you explain me in the slave code what are you doing here? and why?

output_b(data);
               
output_toggle(PIN_D4);

Mon Mar 17 2008, 10:43 am
#5
By toggling D4 I am just want to make sure that slave is receiving the data and I am sending this data on port B. I have attached LED's on Port B and I just want to see the data on portB as well.
Tue Mar 18 2008, 02:08 am
#6
try doing this way...
in slave controller setup SPI as..
setup_spi(SPI_SLAVE|SPI_L_TO_H|SPI_SS_DISABLED);

i mean.. L->H data transition.. please do it the right way..

The thing is.. master put data on o/p pin on H->L transition and slave read on L->H transition, this ensures that data is transfered properly..

Thats the reason you are getting correct data after exact 8 bits.. Everything will be fine if you just understand the reason..

Make the changes and try again..
Thu Apr 10 2008, 02:04 pm
#7
It is still not working properly. May be it is a synchronization problem. Does any one has a program which he has tried himself?

Can any one tell me that "if I donot use SPI_READ() function, but read the data from SSPBUF register", than how can I read SSPBUF in C?????? :mad
Mon Apr 14 2008, 06:52 pm
#8
hello,

Maybe the 'master' is running (and sending data) faster than the slave (because it has few instructions). Therefore, the data in the slave PIC gets corrupted. If I were you I would put a delay in the master and test it again.

BTW: Why did you put in the slave code the
output_high(PIN_C4);
output_high (PIN_C5);

pin_c4 is ther SDI and and C5 is the SDO


[ Edited Mon Apr 14 2008, 06:55 pm ]

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am