View previous topic :: View next topic |
Author |
Message |
gokulrred
Joined: 22 Nov 2011 Posts: 32 Location: puducherry
|
how to start with spi communication? |
Posted: Tue Nov 22, 2011 12:37 am |
|
|
I am beginner to spi communication.
1. I don't know where I made mistake in the following program. (I have tried to send a bit from master to slave and display through slave).
2. I don't know how to connect wires in hardware part and what are all required.
Whether we have to write code for clock pulse generation or PIC do it on own?
plz explain from ABC....
Provide example code for the following cases if possible.
1. Master sends data. Slave receives and displays.
2. Master sends data. Slave re-send some data and master displays.
MASTER CODE:
Code: |
#include <18F4431.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
# define ssbar PIN_A0
// SPI mode definitions.
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
void main()
{
int data, result;
output_bit(PIN_A0,1);
delay_ms(3000);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_oscillator(False);
data=1;
delay_ms(3000);
output_bit(PIN_A0,0);
spi_write(data);
output_bit(PIN_A0,1);
delay_ms(3000);
output_bit(PIN_A0,0);
result=spi_read(0);
output_bit(PIN_A0,1);
printf("the added value is %x", result);
output_b(result);
}
|
SLAVE CODE:
Code: |
#include <18F4431.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
int8 command;
int ret;
#byte SSPBUF= 0xFC9
// SPI modes for setup_spi() function.
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
#int_SSP
SSP_isr()
{
ret=0;
command=SSPBUF;
switch(command)
{
case 1:
SSPBUF=1;
break;
default:
SSPBUF=0;
break;
}
ret=SSPBUF;
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_spi(SPI_SLAVE|SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
setup_oscillator(False);
output_bit(PIN_B0,ret);
delay_ms(30000);
}
|
|
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
|
gokulrred
Joined: 22 Nov 2011 Posts: 32 Location: puducherry
|
how to start with spi communication? |
Posted: Tue Nov 22, 2011 3:33 am |
|
|
@Geps:
hi,
I saw that pdf.
I could be able to understand the commands to be used.
I don't know how to use those commands at which places.
If I could get a sample program which is ready to run, it'll be useful for me to understand.
Provide me some basic starter example progs if possible. |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Tue Nov 22, 2011 3:51 am |
|
|
Quote: | EX_SPI.C
Communicates with a serial EEPROM using the H/W SPI module
EX_SPI_SLAVE.C
How to use the PIC's MSSP peripheral as a SPI slave. This example will talk to the ex_spi.c example. |
Located in your program directory |
|
|
gokulrred
Joined: 22 Nov 2011 Posts: 32 Location: puducherry
|
|
Posted: Tue Nov 22, 2011 3:57 am |
|
|
Is there any mistakes in the program done by me?? will it run?
I don't have ppl to check whether thats right or wrong? |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Tue Nov 22, 2011 5:10 am |
|
|
Flash it to a real chip and give it a go...... |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Nov 22, 2011 5:29 am |
|
|
Your program is not too bad but with computers it only works if there is no single bug left.
Here are some tips:
Code: | setup_oscillator(False); | You are using the external oscillator with fuse XT. A description of this function depends on the chips you are using and is described in the header files. I don't have the header files here but my guess is this function is doing nothing. Remove this line in both the master and sleve because it is confusing and making things more difficult to understand.
Code: | // SPI mode definitions.
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) | CCS is using their own method for defining the SPI configuration that is very confusing. The defines here above are a more common method for describing the 4 possible SPI configurations. But... you are never using these defines... Why?
One bug is that you are using a different configuration for the master and slave. It is very important that both use the same SPI mode! Change the master to: Code: | setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4); | and the slave to: Code: | setup_spi(SPI_SLAVE | SPI_SS_DISABLED | SPI_MODE_0); |
Now, here we see another problem. For the slave you have configured SPI_SS_DISABLED. Doing so saves you one I/O pin but makes it very difficult to synchronize the master and slave. Just assume there is a noise induced spike on your clock line, then the slave will clock in one bit too many and from that moment on the master and slave will be out of sync by 1 bit. This can be corrected in software but will cause you a lot of trouble. The common solution is to enable the Slave Select pin. Then, when the master wants to send data it activates the Select signal, sends the data, and disables the Select line again. The Slave uses the enabling/disabling to detect the start of a new message and is garuanteed to be synchronized at the start of the message.
These are the last two lines of your slave code. After running for 30 seconds the slave will exit main(). What happens then, is that your program stops. The compiler inserted here a sleep instruction and execution stops. A more common practice is to have an endless while-loop in your program so the program never stops execution. Example: Code: | void main()
{
setup instructions
..
while(TRUE) // run forever
{
do your send / receive
}
} |
Or when all the important stuff is happening in interrupts you can have an empty loop at the end of your program: Code: | void main()
{
setup instructions
..
while(TRUE)
{
// do nothing
}
} |
In the past several other discussions about master/slave communication have been going on. You can use the search function of this forum to find them, personally I find Google a better search tool when you add the term "site:ccsinfo.com/forum"
The already mentioned CCS examples are another good source of information.
For the rest I would say: experiment, make errors and learn. |
|
|
gokulrred
Joined: 22 Nov 2011 Posts: 32 Location: puducherry
|
|
Posted: Wed Nov 23, 2011 12:54 am |
|
|
Thank you ckielstra for the detailed reply.
I have gone through the reply.
Still I have doubts.
About clock:
Whether we have to generate clock in prog????
What are all the header files we have to include.????
Since am very beginner to all CCS, PIC, SPI I do not know how to proceed.
I understand the concept of SPI but while coming to programming I lag very very very much.
I do not know which bits has to be set and which has to be reset.
and more....
Can you please optimise the code such that it it ready to run ?
I have divided it to the following step by step targets.
1. Send one bit data from master to slave and display at slave port.
2. Send 8 bit data from master to slave and display at slave
3. Send 8 bit data from master to slave... receive some 8 bit data from slave and display at master.
As a first step I wanted to do the target 1.
Plz help me in this.
I'll try by myself for the rest of the targets.
If possible plz post the (ready to burn) code for the target 1. |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Wed Nov 23, 2011 3:04 am |
|
|
What do you think the files I pointed you to do exactly? |
|
|
gokulrred
Joined: 22 Nov 2011 Posts: 32 Location: puducherry
|
|
Posted: Wed Nov 23, 2011 3:50 am |
|
|
@geps
it involves 2 programs with EEPROM
so its little bit confusing when going to PIC
so oly... |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Wed Nov 23, 2011 3:54 am |
|
|
The second file simulates an EEPROM. IE it takes the values from the master, stores it, and then allows the master to fetch the data.
Change the storing to printing and you've got what you asked for. |
|
|
gokulrred
Joined: 22 Nov 2011 Posts: 32 Location: puducherry
|
|
Posted: Wed Nov 23, 2011 4:32 am |
|
|
I got the output for the target 1...
Thanks for the support.
Now working on the next.
I'll ask if I have doubts.
Thank you thank you very much..... |
|
|
|