8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems

 
8051 microcontroller 8051 microcontroller
Forums

Moderators: Ajay, Junied , abbas1707, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph
Author Post
Hanzala
Thu Mar 13 2008, 02:25AM
 User Offline
Registered Member #6605
Joined: Thu Mar 13 2008, 02:00AM

Posts: 4
Thanked 0 times in 0 posts
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:


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:

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.




[ Edited Thu Mar 13 2008, 02:39AM ]
Back to top


Ajay
Thu Mar 13 2008, 02:57AM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 3375
Thanked 616 times in 583 posts
i do not understand this..
you are sending one byte and on receiver side you are getting bytes like 8 or 9 bytes...

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


Hanzala
Fri Mar 14 2008, 07:30AM
 User Offline
Registered Member #6605
Joined: Thu Mar 13 2008, 02:00AM

Posts: 4
Thanked 0 times in 0 posts
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.
Back to top


Ajay
Fri Mar 14 2008, 09:41PM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 3375
Thanked 616 times in 583 posts
can you explain me in the slave code what are you doing here? and why?

CODE:
output_b(data);
               
output_toggle(PIN_D4);
 



www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


Hanzala
Sun Mar 16 2008, 10:13PM
 User Offline
Registered Member #6605
Joined: Thu Mar 13 2008, 02:00AM

Posts: 4
Thanked 0 times in 0 posts
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.
Back to top


Ajay
Mon Mar 17 2008, 01:38PM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 3375
Thanked 616 times in 583 posts
try doing this way...
in slave controller setup SPI as..
CODE:
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..

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


Hanzala
Thu Apr 10 2008, 01:34AM
 User Offline
Registered Member #6605
Joined: Thu Mar 13 2008, 02:00AM

Posts: 4
Thanked 0 times in 0 posts
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??????
Back to top


Rocha
Mon Apr 14 2008, 06:22AM
 User Offline
Registered Member #7221
Joined: Mon Apr 14 2008, 06:12AM

Posts: 1
Thanked 0 times in 0 post
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:25AM ]
Back to top


 

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System

8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems