Hello Magnatron, I have a friend that shows me a sample of SPI communication using 1 Master and 1 Slave. So I'm sure that I learn a little bit about some setting of SPI into MCU but I know that it's not enough just to know the settings. Can you give me at least a simple code for my project using SPI Master to Slave V/V? The Logic is like I said on above. The Master is like a switch, if I toggle ON or if I toggle OFF RA0 , RB0 will send series of pulses. Let's say that If RA0 is Toggled ON, RB0 will output A_ON[] = { 1,0,1,1,0,0,1,1 };
Else If RA0 is toggled OFF, RB0 will output A_OFF[] = { 1,1,1,0,0,1,1,1 };
The Slave is only accepting inputs and checking the type of the data received from MASTER. Let's say that I set PORTD as Input, so If I get RB0 form Master and CONNECT it in RD0 of Slave, The Slave will check whether its A_ON or A_OFF by comparing the data receive from the MASTER and the array A_ON[] = { 1,0,1,1,0,0,1,1 }; and A_OFF[] = { 1,1,1,0,0,1,1,1 }; .....
I'm not thinking of using the EUSART of MCU because I have a plan for that.
Btw... This is my friend's code that he create just to show me an example of SPI through MCU. (hahaha Even if I really don't understand some codes that he puts on)
------------------------------MASTER-------------------------------
void main()
{
INTCON.GIE = 1;
ADCON1 = 7;
CMCON = 7;
TRISA = 0b00001111;
TRISC = 0b00010000;
TRISD = 0b00000000;
portd.f1 = 1;
SSPSTAT.SMP = 0;
SSPSTAT.CKE = 0;
SSPCON.SSPEN = 1;
SSPCON.CKP = 1;
SSPCON.SSPM3 = 0;
SSPCON.SSPM2 = 0;
SSPCON.SSPM1 = 0;
SSPCON.SSPM0 = 0;
while(1)
{
portd.f1 = 0;
SSPCON.WCOL = 0;
SPI1_Write(porta.f0 + porta.f1 * 2 + porta.f2 * 4 + porta.f3 * 8);
portd.f1 = 1;
}
}
------------------------------Slave-------------------------------
void main()
{
unsigned int buffer, gdata = 0;
TRISA = 0b00100000;
TRISC = 0b00011000;
TRISD = 0b00000000;
TRISB = 0b00000000;
ADCON1 = 7;
CMCON = 7;
INTCON.GIE = 1;
SSPSTAT.SMP = 0;
SSPSTAT.CKE = 0;
SSPCON.SSPEN = 1;
SSPCON.CKP = 1;
SSPCON.SSPM3 = 0;
SSPCON.SSPM2 = 1;
SSPCON.SSPM1 = 0;
SSPCON.SSPM0 = 0;
UART1_Init(9600);
while (1)
{
gdata = 2;
if (SSPSTAT.BF)
{
gdata = SSPBUF;
SSPCON.SSPOV = 0;
}
portb.f0 = gdata % 2;
gdata /= 2;
portb.f1 = gdata % 2;
gdata /= 2;
portb.f2 = gdata % 2;
gdata /= 2;
portb.f3 = gdata % 2;
gdata /= 2;
Delay_Ms(50);
}
}
Please help me, I need it badly. Regards to all