|
|
View previous topic :: View next topic |
Author |
Message |
mindstorm88
Joined: 06 Dec 2006 Posts: 102 Location: Montreal , Canada
|
Clearing SPI buffer ? |
Posted: Tue Dec 09, 2008 10:52 am |
|
|
Hi guys, I have to synchronise my slave with incoming data, data is 24 bits, if on power up I do receive lets say the last 5 bits (not a complete bytes) how do I clear the buffer to be ready for the next incoming burst ???
It seems that spiread() will wait to receive bits to complete the byte before clearing the buffer !!!!
Thanks |
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 09, 2008 11:30 am |
|
|
This is where a slave select line really comes in.
It allows you to synchronise the slave with the master. Without this, you always have the potential problem of one starting at the wrong 'point' in the data.
Assuming you can't implement such a line, then what you need to do, is not start the SPI, till you are in the 'gap' between the bursts. Don't setup the SPI, at the start of the code (don't use the #use SPI option), instead monitor the clock line as a normal 'input'. Look for the gap between clocks. You will probably have something like 3 groups of eight clocks, then a pause. So, if you monitor the clock line, timing how long it is between the edges, till you see a gap 'longer' than the time between characters, then you are now in the gap. At this point call the setup_spi function to configure the SPI.
Best Wishes |
|
|
mindstorm88
Joined: 06 Dec 2006 Posts: 102 Location: Montreal , Canada
|
|
Posted: Tue Dec 09, 2008 2:44 pm |
|
|
Thanks Ttelmah
I'll try it !!! The reality is that I don't really need spi !!!
I'm receiving burst of 24 bits coordinate about every 200 ms from the
sensor. If I would know how to do it without spi it would be better as I
will have to hookup two of these sensors on the same micro then display
it on LED display !!!!
I'm newbie with serial communication !!! |
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 09, 2008 3:06 pm |
|
|
It is not clear from your post, what the 'sensor' actually sends?.
SPI, is a synchronous protocol. If the sensor is sending SPI data as a master, then it _must_ generate the clock for the data as well. Hence my post.
If it is not sending a clock, but uses SPI, then it will expect _you_ to send the clock, so the synchronisation comes under your control.
If there is no clock at all, then the inteface is not SPI.
Standard async serial, generates independant clocks at each end. With this, in your example, the first packet would be garbage, but soon as there is a break in the stream, the link would resynchronise.
Best Wishes |
|
|
mindstorm88
Joined: 06 Dec 2006 Posts: 102 Location: Montreal , Canada
|
|
Posted: Tue Dec 09, 2008 6:37 pm |
|
|
Ttelmah wrote: | It is not clear from your post, what the 'sensor' actually sends?.
|
You're right sorry about that
Ok, the sensor has 1 clk line and 1 data line , the clock is a burst of around 80 kHz every 200 ms in sync with data line, so every 200 ms I have 24 clock to receive 24 bits of raw data !!!
Thanks |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Tue Dec 09, 2008 9:46 pm |
|
|
mindstorm88 wrote: | [...the sensor has 1 clk line and 1 data line , the clock is a burst of around 80 kHz every 200 ms in sync with data line, so every 200 ms I have 24 clock to receive 24 bits of raw data !!!
|
OK, you can use SPI to receive that. Configure your PIC for slave SPI. Since you don't mention the sensor producing anything like a SS signal, you should use a time-out to verify the end of a 24-bit packet. If no SPI character is received for, say, 100 msec, then reset the SPI system by disabling it and re-enabling it. This will reset the bit counter in case it ever gets out of sync. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
mindstorm88
Joined: 06 Dec 2006 Posts: 102 Location: Montreal , Canada
|
|
Posted: Tue Dec 09, 2008 10:14 pm |
|
|
how do you disable it ??? |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Dec 10, 2008 1:32 am |
|
|
I don't see a specific CCS C built-in function for this, setting continuous receive bit in RCSTA directly should do
Code: | // PIC18 address
#bit CREN =0xfab.4
CREN=0;
CREN=1; |
|
|
|
Ttelmah Guest
|
|
Posted: Wed Dec 10, 2008 3:27 am |
|
|
Wrong peripheral Fvm. The CREN bit is for the UART, not the MSSP.
You can do it with the CCS functions, as:
Code: |
setup_spi(FALSE); //Turn the SPI off
setup_spi(SPI_SLAVE); //and back on
|
However the second line, is going to need to also have the correct sampling edge selected, which will be down to reading the sensor's data sheet, and comparing the clock levels and edges used to those generated by the different possible combinations at the PIC.
If the unit, mentions an SPI 'mode', then much easier to use the set of standard defines posted here by Ckielstra I think, as:
Code: |
#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
// MOTOROLA | MICROCHIP | CCS
//------------------------------------------------------------------------------
// SPI Mode 0,0 | CKP = 0, CKE = 1 | SPI_L_TO_H | SPI_XMIT_L_TO_H
// SPI Mode 0,1 | CKP = 0, CKE = 0 | SPI_L_TO_H
// SPI Mode 1,0 | CKP = 1, CKE = 1 | SPI_H_TO_L
// SPI Mode 1,1 | CKP = 1, CKE = 0 | SPI_H_TO_L | SPI_XMIT_L_TO_H
|
So if the unit uses mode 0,0, then you woud use:
setup_spi(SPI_MODE_0_0|SPI_SLAVE0;
For the second line.
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Dec 10, 2008 4:23 am |
|
|
You're right, sorry for that. SSPEN should be the correct SFR bit to disable MSSP, hopefully not affecting the other settings. |
|
|
mindstorm88
Joined: 06 Dec 2006 Posts: 102 Location: Montreal , Canada
|
|
Posted: Wed Dec 10, 2008 5:25 am |
|
|
Ttelmah wrote: | Wrong peripheral Fvm. The CREN bit is for the UART, not the MSSP.
You can do it with the CCS functions, as:
Code: |
setup_spi(FALSE); //Turn the SPI off
setup_spi(SPI_SLAVE); //and back on
|
|
For the second sensor , with sorftware port with #USE SPI will i be able to do the same ??? cause i must read 2 sensors !!!
BTW Ttelmah if you look few post up youll see that the sensor is not really spi it simply send data with a clock .
up to now i'm able to read the data with spi but not properly in sync |
|
|
Ttelmah Guest
|
|
Posted: Wed Dec 10, 2008 9:39 am |
|
|
That is SPI.....
It may only be part of the interface, but that is all that SPI involves. The key is though that somewhere in the sensor's data sheet, there must be data saying whether the line idles high or low, and which edge of the clock data should be sampled on. These two bits of data are 'essential'.
Software SPI, does not really support slave operation, only master.
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Dec 10, 2008 9:45 am |
|
|
Quote: | Software SPI, does not really support slave operation, only master. | That had been my general comment, too. The CCS C manual however mentions software SPI in slave mode, after stating, that software SPI is generally slow.
As a practical question, what's your SPI clock frequency? Also, there are apparently no means to break or resynchronize software SPI receive functions. This means, that the only chance would be in coding your own software SPI receive, that implements these options. |
|
|
Ttelmah Guest
|
|
Posted: Wed Dec 10, 2008 3:06 pm |
|
|
Yes.
This is why I said 'does not really', rather than 'does not'.
In slave mode, using software, the chip has to be sitting _waiting_ for the first active edge, in the read routine. It cannot do anything else, without losing data. It won't handle anywhere near the required 80KHz, even on the fastest PIC.
With the SS line, it can be used, by having the select line, trigger an interrupt, and then in the interrupt handler, waiting for the byte. However the speed still means you are in the routine for a long time...
Seriously, two solutions. Either choose a PIC with two hardware SPI ports. A few of the 18 chips have these, or use an external shift register to latch the data. The latter is really easy, then load the data in parallel from this.
You can also use the same approach outlined right at the start, of looking for the gap between the data packets, then releasing the clear line on the shift register, to synchronise.
Best Wishes |
|
|
mindstorm88
Joined: 06 Dec 2006 Posts: 102 Location: Montreal , Canada
|
|
Posted: Fri Dec 12, 2008 8:36 am |
|
|
Thanks guys, I succeeded in creating my own receiving firmware, synchronisation is good now, worst case is losing the first packet when switching from 1 sensor to the other as I don't know how I could read both at the same time !!! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|