Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help! SPI microcontroller interfacing (TWO 16f877a)

Status
Not open for further replies.

RuH_iranga

Junior Member level 3
Joined
Nov 4, 2010
Messages
31
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
sri lanka
Activity points
1,480
Hi all
I am trying to communicate two 16f877a PICs via SPI. But there is a problem. Simply the master write integer 3 into Slave, slave read it and check that value is greater than int 7. If Yes it blink 2 LEDs on RB6 and RB7, else RB6 LED is on.
two codes work correctly on PROTEUS simulator. But in real arrangement, If I send value larger than 7 it blink correctly and some times when I powered two microcontrollers it flash only RB6 as well. Please help me to find where the mistake.

my code

MASTER

Code:
// code for spi master

int i;

void main()
{
trisc.f3=0;
trisc.f4=1;
trisc.f5=0;
trisa.f2=0;
porta.f2=1;
delay_ms(10);
porta.f2=0;

     Spi_Init();
     while(1)
       {
            //int y=7;
            Spi_Write(3);
           portc.f3=0;
       }
}

SLAVE

Code:
    // code for spi slave


void main()
{
unsigned int buffer, gdata =0;
int c=7;

trisb.f7=0;
trisb.f6=0;
trisa.f5=1;
trisc.f4=1;
trisc.f5=0;
trisc.f3=1;

Spi_Init();

while(1)
{
         if(Spi_Read(buffer)>=7)
         {
                 portb.f7=1;
                 portb.f6=0;
                 Delay_ms(70);
                 portb.f7=0;
                 portb.f6=1;
                 delay_ms(70);
         }
         else portb.f7=0;
         portc.f3=0;
}
}

Please help me to operate this correctly.
Thank you!
 
Last edited:

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
I don't know what compiler you are using, I assume the 'Spi_Init()' function call is a built in library function.

Shouldn't you initialize one as a master and the other as a slave?
Both are initialized with the same function call?
 

RuH_iranga

Junior Member level 3
Joined
Nov 4, 2010
Messages
31
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
sri lanka
Activity points
1,480
I used MikroC compiler. Default Spi_init() initiate followings.
Default settings are: Master mode, clock Fosc/4, clock idle state low, data transmitted on low to high edge, and input data sampled at the middle of interval.

Advanced settings allows below types only
MASTER_OSC_DIV4 // Master clock=Fosc/4
MASTER_OSC_DIV16 // Master clock=Fosc/16
MASTER_OSC_DIV64 // Master clock=Fosc/64
MASTER_TMR2 // Master clock source TMR2
SLAVE_SS_ENABLE // Master Slave select enabled
SLAVE_SS_DIS // Master Slave select disabled

thank you
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
Try changing one to a slave.

Call Spi_Init(),

and then set the SSPM bits in the SSPCON reg to

0101 = SPI Slave mode, clock = SCK pin. SS pin control disabled. SS can be used as I/O pin.

SSPCON &= 0xf0;
SSPCON |= 0x05;
 

RuH_iranga

Junior Member level 3
Joined
Nov 4, 2010
Messages
31
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
sri lanka
Activity points
1,480
Thank you btbass!
But still problem remains. Now I'm going to do this without inbuilt functions. and try to do this manually. I'll come with my results soon. And I want to know how can I add Helpful icon.
Thank you again.

---------- Post added at 20:38 ---------- Previous post was at 18:41 ----------

Still no results, please someone help me.
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
Post your code so we can take a look.
 

Ram Prasadh

Member level 2
Joined
Feb 16, 2011
Messages
51
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,725
Try using Fosc/16 0r Fosc/64 in master.... send your sspstat and sspcon configiration.
 

RuH_iranga

Junior Member level 3
Joined
Nov 4, 2010
Messages
31
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
sri lanka
Activity points
1,480
latest code, but still problem remains, do you have any working example code or link.

Code:
  // code for spi slave


void main()
{
unsigned int buffer, gdata =0;
int c=7;

trisb.f7=0;
trisb.f6=0;
trisa.f5=1;
trisc.f4=1;
trisc.f5=0;
trisc.f3=1;

Spi_Init_Advanced(SLAVE_SS_DIS, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);


while(1)
{
         if(Spi_Read(buffer)>=7)
         {
                 portb.f7=1;
                 portb.f6=0;
                 Delay_ms(10);
                 portb.f7=0;
                 portb.f6=0;
                 delay_ms(10);
         }
         else {portb.f7=0;
         portb.f6=1;
         delay_ms(10);}

}
}

Code:
// code for spi master


void main()
{
trisc.f3=0;            // Clock out from master
trisc.f4=1;            // Data in From slave
trisc.f5=0;            // Data out To slave
//trisa.f2=0;
//porta.f2=1;
//delay_ms(10);
//porta.f2=0;

     Spi_Init();
     while(1)
       {

            Spi_Write(2);

       }
}

thanks
 

RuH_iranga

Junior Member level 3
Joined
Nov 4, 2010
Messages
31
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
sri lanka
Activity points
1,480
Hi friends!
I found a problem in one of my 20Mhz crystal oscillator, I think fixing that will be fixed my problem.

thanks.
 

Ram Prasadh

Member level 2
Joined
Feb 16, 2011
Messages
51
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,725
change your oscillator configuration in your ide to HS oscillator. If you are burning your code in a universal programmer like topwin,select HS oscillator in the configuration bits there also.since you are using a 20Mhz oscillator,set your sspcon register in Fosc/16. It will work
 

RuH_iranga

Junior Member level 3
Joined
Nov 4, 2010
Messages
31
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
sri lanka
Activity points
1,480
Hi Ram Prasadh

thank you, I' ll try and post my results, but I have exams in these two weeks. As soon as possible I will change my configuration, my burner software is picpgm.
thank you
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top