I am try to communicate b/w two pic18f452:
I am sending package of data from master. slave should receive it properly. and send back data according to the received one:
here problem is I am getting data for some value like when I call the function:
sent_data(10,20,30);
I received it correctly but when I sent:
sent_data(150,180,180);
I am not receiving it communication is not establishing.
I need to received data correctly all the time.
PLZZZZZ help me out I extreme need plzzzz
plzz tell me what iam doing wrong I am using MicroC for coding
here is my master:
unsigned char theta1,theta2,theta3,Count2,Count3,datain,intiCom, ready,Thetax=0,Thetay,Thetaz,x,y,z1,reset;
unsigned char Write(char dataout);
void mapInput();
void SysInit();
void sent_data(unsigned char Theta1 ,unsigned char Theta2 , unsigned char Theta3 );
void main(){
SysInit();
intiCom=0;
PORTB=0xFF;
delay_ms(1000);
PORTB=0x00;
intiCom=Write(0x0F); // inititalize the communication: 1
//Send 0x0F if reviced is F0 the Communication line is stablish
PORTB = intiCom;
Delay_ms(500);
if(intiCom==0xF0)
{
while(1)
{
sent_data(150,180,180);
}
}
}
unsigned char Write(unsigned char dataout)
{
PORTD = dataout;
PORTC.B2=0; // SS select low slave is selected port C pin 2
delay_ms(10); // wait
SPI1_Write(dataout); // transmit the dataout
//while(SSPSTAT.BF); // wait till end of data transmission
delay_ms(50); // wait.
PORTC.B2=1;
return SSPBUF;
}
void SysInit()
{
TRISA=0xFF; // configure Port A as input
ADCON1=0x06;// disable ADC and make all the pins of Port A as digital input.
TRISD=0x00; // configure Port D as output
PORTD=0x00; // Clear all pin of Port D
TRISB=0x00; // configure Port B as output
PORTB=0x00; // Clear all pin of Port B
TRISC.B2=0; // Output for Slave select
TRISC.B3=0; // SCK output
TRISC.B4=1; // SDI input
TRISC.B5=0; // SDO output
PORTC.B2=1; // SS select high no slave is select
//Set SPI1 module to master mode, clock = Fosc/4, data sampled at the middle of interval,
//clock idle state low and data transmitted at low to high edge:
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); // Setup the spi
}
void sent_data(unsigned char Theta1 ,unsigned char Theta2 , unsigned char Theta3 )
{
ready=Write(0x10); //2
Delay_ms(50);
if(ready==0x70)
{
Thetax=Write(Theta1); //3
// PORTB=Thetax;
Delay_ms(50);
}
if(Thetax==0x30)
{
x=Write(0x05); //4
//PORTB=x;
Delay_ms(50);
}
if(x==0x40)
{
Thetay=Write(Theta2); //5
//PORTB=Thetay;
Delay_ms(50);
}
if(Thetay==0x50)
{
y=Write(0x06); //6
//PORTB=y;
Delay_ms(50);
}
if(y==0x60)
{
Thetaz=Write(Theta3); //7
//PORTB=Thetaz;
Delay_ms(50);
}
if(Thetaz==0x80)
{
z1=Write(0x08); //8
//PORTB=z1;
Delay_ms(50);
}
if((z1==0x90)&&(y==0x60)&&(x==0x40))
{
reset= Write(0x09); //9
z1=0;
y=0;
x=0;
Thetaz=0;
Thetay=0;
Thetax=0;
ready=0;
PORTB=Theta3;
}
}
here is salve code:
unsigned char Received(unsigned short dataout);
unsigned short datain,dataout;
unsigned char intiCom=0,ready=0,Thetax=0,x,Thetay,y,Thetaz,z1,up date;
void SysInit();
void InitTimer1();
void Interrupt();
void mydelay_10us(long int a);
unsigned long int total,high1,high2,high3,low,theta1,Required_delay1 =0,theta2;
unsigned long int Required_delay2=0,theta3,Required_delay3=0;
void main() {
//InitTimer1();
SysInit();
intiCom=0;
intiCom=Received(0xF0); //1
// inititalize the communication:
// Send 0x0F if reviced is F0 the Communication line is stablish
while(1)
{
ready=Received(0x70); //2 Ready for communication
//PORTB=ready;
if(ready==0x10)
{
Thetax= Received(0x30); //3 recive theta 1 is ready
//PORTB=Thetax;
delay_ms(30);
x = Received(0x40); //4 conform that Theta 1 has been recived
}
if(x==0x05)
{
Thetay=Received(0x50); //5 recive theta2 if reception of theta 1 is conformed
// PORTB=Thetay;
delay_ms(30);
y= Received(0x60); //6 conform that Theta 2 has been recived
}
if(y==0x06)
{
Thetaz=Received(0x80); //7 recive theta3 if reception of theta 2 is conformed
//PORTB=Thetaz;
delay_ms(30);
z1 = Received(0x90); // 8 conform that Theta 3 has been recived
}
if((z1==0x08)&&(y==0x06)&&(x==0x05)) // after conformation of all the theta 1 2 3
{
update=Received(0xA0); //9 if update comand is revieced the chage the value of angles.
delay_ms(30);
if(update==0x09)
{
theta1=(long int)Thetax;
theta2=(long int)Thetay;
theta3=(long int)Thetaz;
update=0;
z1=0;
y=0;
x=0;
ready=0;
PORTB=theta3;
}
}
}
}
unsigned char Received(unsigned short dataout)
{
while(SSPSTAT.BF);
datain= SPI1_Read(dataout);
delay_ms(60);
//PORTD=dataout;
//PORTB=datain;
return datain;
}
void SysInit()
{
ADCON1=0x06; // disable ADC and make all the pins of Port A as digital input
TRISA=0xFF;
PORTA=0x00;
TRISB=0x00;
PORTB=0x00;
TRISD=0x00;
PORTD=0x00;
TRISC.B6=0;
TRISC.B7=0;
TRISC.B4=1; //SDI input
TRISC.B3=1; //SCK input from master
TRISC.B5=0; //SDO output
SSPSTAT.SMP=0; //input data sampled at middle
SSPSTAT.CKE=0; // transition from idle to active
SPI1_Init_Advanced(_SPI_SLAVE_SS_ENABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
}
Always looking for source of knowledge
Urgent HElp require plzzz in Spi commiicaTION USEIN two PIC
General discussion on mikroElektronika website & forums.
Author
Message
-
- Posts: 1
- Joined: 20 Mar 2014 09:03
Urgent HElp require plzzz in Spi commiicaTION USEIN two PIC
#1 Post by AmmarAkhlaq » 20 Mar 2014 09:30
- petar.timotijevic
- mikroElektronika team
- Posts: 1739
- Joined: 19 Feb 2014 13:46
- Location: Serbia
- Contact:
Re: Urgent HElp require plzzz in Spi commiicaTION USEIN two
#2 Post by petar.timotijevic » 20 Mar 2014 22:38
Hello Ammar and welcome to the MikroE forum,
Please see examples on http://www.libstock.com
Master Slave SPI communication
http://www.libstock.com/projects/view/2 ... munication
If possible always use code tags for posting your source code in forum.
Best regards,
Peter
Please see examples on http://www.libstock.com
Master Slave SPI communication
http://www.libstock.com/projects/view/2 ... munication
If possible always use code tags for posting your source code in forum.
Best regards,
Peter
Return to “Website & Forums General Discussion”
Jump to
- NECTO Studio
- ↳ IDE
- ↳ IDE General
- ↳ IDE Beta Testing
- ↳ IDE Wish List
- ↳ ARM AI Compilers
- ↳ mikroC AI for ARM
- ↳ mikroC AI for ARM General
- ↳ mikroC AI for ARM Beta Testing
- ↳ mikroC AI for ARM Wish List
- ↳ PIC AI compilers
- ↳ mikroC AI for PIC
- ↳ mikroC AI for PIC General
- ↳ mikroC AI for PIC Beta Testing
- ↳ mikroC AI for PIC Wish List
- ↳ PIC32 AI Compilers
- ↳ mikroC AI for PIC32
- ↳ mikroC AI for PIC32 General
- ↳ mikroC AI for PIC32 Beta Testing
- ↳ mikroC AI for PIC32 Wish List
- ↳ dsPIC AI Compilers
- ↳ mikroC AI for dsPIC
- ↳ mikroC AI for dsPIC General
- ↳ mikroC AI for dsPIC Beta Testing
- ↳ mikroC AI for dsPIC Wish List
- ↳ AVR AI Compilers
- ↳ mikroC AI for AVR
- ↳ mikroC AI for AVR General
- ↳ mikroC AI for AVR Beta Testing
- ↳ mikroC AI for AVR Wish List
- ↳ Additional Software
- ↳ Visual TFT AI
- ↳ Visual TFT AI General
- ↳ Visual TFT AI Beta Testing
- ↳ Visual TFT AI Wish List
- PRO Compilers
- ↳ PIC PRO Compilers
- ↳ PIC Compilers General
- ↳ mikroC PRO for PIC
- ↳ mikroC PRO for PIC General
- ↳ mikroC PRO for PIC Beta Testing
- ↳ mikroC PRO for PIC Wish List
- ↳ mikroBasic PRO compiler for PIC
- ↳ mikroBasic PRO for PIC General
- ↳ mikroBasic PRO for PIC Beta Testing
- ↳ mikroBasic PRO for PIC Wish List
- ↳ mikroPascal PRO for PIC
- ↳ mikroPascal PRO for PIC General
- ↳ mikroPascal PRO for PIC Beta Testing
- ↳ mikroPascal PRO for PIC Wish List
- ↳ dsPIC PRO Compilers
- ↳ dsPIC Compilers General
- ↳ mikroC PRO for dsPIC30/33 and PIC24
- ↳ mikroC PRO for dsPIC30/33 and PIC24 General
- ↳ mikroC PRO for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroC PRO for dsPIC30/33 and PIC24 Wish List
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24 General
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroBasic PRO for dsPIC30/33 and PIC24 Wish List
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24 General
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroPascal PRO for dsPIC30/33 and PIC24 Wish List
- ↳ PIC32 PRO Compilers
- ↳ PIC32 Compilers General
- ↳ mikroC PRO for PIC32
- ↳ mikroC PRO for PIC32 General
- ↳ mikroC PRO for PIC32 Beta Testing
- ↳ mikroC PRO for PIC32 Wish List
- ↳ mikroBasic PRO for PIC32
- ↳ mikroBasic PRO for PIC32 General
- ↳ mikroBasic PRO for PIC32 Beta Testing
- ↳ mikroBasic PRO for PIC32 Wish List
- ↳ mikroPascal PRO for PIC32
- ↳ mikroPascal PRO for PIC32 General
- ↳ mikroPascal PRO for PIC32 Beta Testing
- ↳ mikroPascal PRO for PIC32 Wish List
- ↳ AVR PRO Compilers
- ↳ AVR Compilers General
- ↳ mikroC PRO for AVR
- ↳ mikroC PRO for AVR General
- ↳ mikroC PRO for AVR Beta Testing
- ↳ mikroC PRO for AVR Wish List
- ↳ mikroBasic PRO compiler for AVR
- ↳ mikroBasic PRO for AVR General
- ↳ mikroBasic PRO for AVR Beta Testing
- ↳ mikroBasic PRO for AVR Wish List
- ↳ mikroPascal PRO for AVR
- ↳ mikroPascal PRO for AVR General
- ↳ mikroPascal PRO for AVR Beta Testing
- ↳ mikroPascal PRO for AVR Wish List
- ↳ FT90x PRO Compilers
- ↳ FT90x Compilers General
- ↳ mikroC PRO for FT90x
- ↳ mikroC PRO for FT90x General
- ↳ mikroC PRO for FT90x Beta Testing
- ↳ mikroC PRO for FT90x Wish List
- ↳ mikroBasic PRO compiler for FT90x
- ↳ mikroBasic PRO for FT90x General
- ↳ mikroBasic PRO for FT90x Beta Testing
- ↳ mikroBasic PRO for FT90x Wish List
- ↳ mikroPascal PRO for FT90x
- ↳ mikroPascal PRO for FT90x General
- ↳ mikroPascal PRO for FT90x Beta Testing
- ↳ mikroPascal PRO for FT90x Wish List
- ↳ 8051 PRO Compilers
- ↳ 8051 Compilers General
- ↳ mikroC PRO for 8051
- ↳ mikroC PRO for 8051 General
- ↳ mikroC PRO for 8051 Beta Testing
- ↳ mikroC PRO for 8051 Wish List
- ↳ mikroBasic PRO compiler for 8051
- ↳ mikroBasic PRO for 8051 General
- ↳ mikroBasic PRO for 8051 Beta Testing
- ↳ mikroBasic PRO for 8051 Wish List
- ↳ mikroPascal PRO for 8051
- ↳ mikroPascal PRO for 8051 General
- ↳ mikroPascal PRO for 8051 Beta Testing
- ↳ mikroPascal PRO for 8051 Wish List
- ↳ ARM PRO Compilers
- ↳ ARM Compilers General
- ↳ mikroC PRO for ARM
- ↳ mikroC PRO for ARM General
- ↳ mikroC PRO for ARM Beta Testing
- ↳ mikroC PRO for ARM Wish List
- ↳ mikroBasic PRO for ARM
- ↳ mikroBasic PRO for ARM General
- ↳ mikroBasic PRO for ARM Beta Testing
- ↳ mikroBasic PRO for ARM Wish List
- ↳ mikroPascal PRO for ARM
- ↳ mikroPascal PRO for ARM General
- ↳ mikroPascal PRO for ARM Beta Testing
- ↳ mikroPascal PRO for ARM Wish List
- ↳ Older versions of Compilers
- ↳ Older versions of mikroC Compilers
- ↳ mikroC for PIC
- ↳ mikroC General
- ↳ mikroC Beta Testing
- ↳ mikroC Wish List
- ↳ mikroC for dsPIC30/33 and PIC24
- ↳ mikroC for dsPIC30/33 and PIC24 General
- ↳ mikroC for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroC for dsPIC30/33 and PIC24 Wish List
- ↳ RSC-4x mikroC
- ↳ RSC-4x mikroC General
- ↳ RSC-4x mikroC Wish List
- ↳ mikroC Beta testing
- ↳ Older versions of mikroBasic Compilers
- ↳ mikroBasic compiler for PIC
- ↳ mikroBasic General
- ↳ mikroBasic Beta Testing
- ↳ mikroBasic Wish List
- ↳ mikroBasic compiler for dsPIC30/33 and PIC24
- ↳ mikroBasic for dsPIC30/33 and PIC24 General
- ↳ mikroBasic for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroBasic for dsPIC30/33 and PIC24 Wish List
- ↳ mikroBasic for dsPIC30/33 and PIC24 Bug List
- ↳ Obsolete Project: mikroBasic compiler for AVR
- ↳ mikroBasic for AVR General
- ↳ mikroBasic for AVR Wish List
- ↳ mikroBasic Beta testing
- ↳ Older versions of mikroPascal Compilers
- ↳ mikroPascal compiler for PIC
- ↳ mikroPascal FAQ
- ↳ mikroPascal General
- ↳ mikroPascal Beta Testing
- ↳ mikroPascal Wish List
- ↳ mikroPascal Bug List
- ↳ mikroPascal compiler for dsPIC30/33 and PIC24
- ↳ mikroPascal for dsPIC30/33 and PIC24 General
- ↳ mikroPascal for dsPIC30/33 and PIC24 Beta Testing
- ↳ mikroPascal for dsPIC30/33 and PIC24 Wish List
- ↳ mikroPascal for dsPIC30/33 and PIC24 Bug List
- ↳ Obsolete Project: mikroPascal compiler for AVR
- ↳ mikroPascal for AVR General
- ↳ mikroPascal for AVR Wish List
- ↳ mikroPascal Beta testing
- ↳ Library Development Discussion
- ↳ Code Requests
- Additional Software
- ↳ Visual TFT Software
- ↳ Visual TFT General
- ↳ Visual TFT Beta Testing
- ↳ Visual TFT Wish List
- ↳ Visual GLCD Software
- ↳ Visual GLCD General
- ↳ Visual GLCD Beta Testing
- ↳ Visual GLCD Wish List
- ↳ Package Manager Software
- ↳ Package Manager General
- ↳ Package Manager Beta Testing
- ↳ Package Manager Wishlist
- ↳ GLCD Font Creator Software
- ↳ SpeakUp Software
- ↳ Timer Calculator
- ↳ CAN Calculator
- ↳ mikroBootloader
- Hardware
- ↳ Development Boards
- ↳ CODEGRIP programmer and debugger
- ↳ Mikromedia
- ↳ mikroProg™ programmer and debugger
- ↳ Development Boards Wishlist
- ↳ Review
- ↳ Click Boards release
- ↳ Hexiwear
- Website & Forums
- ↳ Libstock Discussion
- ↳ Product Announcements
- ↳ Website & Forums General Discussion
- ↳ Website & Forums Wishlist
- ↳ User Projects