I am trying to make a spy in SPI SLAVE MODE with a microchip PIC16F887 (or PIC18F4455/PIC18F4550) @ 20Mhz which would read these bytes and store them in an array of 10 values and display them on the graphic LCD of the development board every 1 second.

This 8-bytes-word appears on the SPI bus each second.

My problem is for the SPI read part. It doesn't work.

Each second, instead of reading 10 values, it only read 1 which correspond to nothing.
In SPI, the ENABLE PIN should be set to '0' beetween each byte. Here, this is not the case.

In my scan, this is not the case. could it be a source of problem?

I am looking for help.

Thank you.

Brice.

05-21-2009, 08:21 AM

Civic Modz

looks like you are well on the right track, you have worked out all the commands. Im wondering if you have set up the slave correctly, ie mode, frequency selection, either way I wonder if it would be easier to create your own routine if your only reading the data... just a thought.

05-21-2009, 02:34 PM

coucouillu

Hi,

The PIC is in SLAVE mode, no frequency needed because it is in slave mode, this is the clock of the master which determine the frequency.

I am trying to make my own routine because SPI librairies do not work.

I need some ideas to make my program.

05-21-2009, 08:16 PM

Civic Modz

It shouldnt be that hard to make a simple routine. The routine will obviously start when the enable pin is low, and end when high. You would just have to get the state of the 'MISO' data on every 'CLOCK' cycle (low-to-high) and shift it into a buffer. When the enable line is high you can then proccess the 10 bytes received. The easiest was is to start with a flow chart.

hope this helps.

05-24-2009, 06:33 AM

coucouillu

The problem is that the ENABLE pin should go high then low beetween each byte.

This is not the case.

05-24-2009, 06:56 AM

Civic Modz

1 Attachment(s)

That's ok, you can use the low-to-high enable signal to tell you that all bytes have been received. With the PIC micros you can generate an interrupt on a low-to-high pin change on the CLOCK pin. Then every interrupt you get (1 CLOCK cycle) you then read the value of MISO then shift that bit into a buffer. When you fill up 10 8-bit buffers you will have your decoded bytes. Hop this makes sence :)

I have attached a pdf showing you how to decode the data, this is as much help I could give without writing the code myself. The PDF is an exploded image of the 2nd byte sent from your device.

Thank you Civic Modz for your schematic. I understood how bytes are read.

The problem is that is not that simple. It seems that it only scan 1 and only 1 byte instead of 10 bytes and the byte received seems corrupted.

I actually use PIC16F887 @ 20MHz.

I use EASYPIC5 from MikroElectronica with MicroBasic software.

Here are parameters I used :

SSPCON = %00110100
SSPCON = %b7|b6|...|b0

Bit 7, WCOL: Write Collision Detect bit. In Slave mode :
1 = The SSPBUF register is written while it is still transmitting the previous word (must be cleared in software)0 = No collision

Bit 6 SSPOV: Receive Overflow Indicator bit. In SPI mode :
1 = A new byte is received while the SSPBUF register is still holding the previous data. In case of overflow, the data in SSPSR
is lost. Overflow can only occur in Slave mode. In Slave mode, the user must read the SSPBUF, even if only transmitting
data, to avoid setting overflow. In Master mode, the overflow bit is not set since each new reception (and transmission) is
initiated by writing to the SSPBUF register (must be cleared in software).0 = No overflow

Bit 5 SSPEN: Synchronous Serial Port Enable bit. In SPI mode:
In both modes, when enabled, these pins must be properly configured as input or output1 = Enables serial port and configures SCK, SDO, SDI and SS as the source of the serial port pins
0 = Disables serial port and configures these pins as I/O port pins

Bit 4 CKP: Clock Polarity Select bit. In SPI mode :1 = Idle state for clock is a high level
0 = Idle state for clock is a low level

Bit 5 D/A: Data/Address bit (I2C mode only)
1 = Indicates that the last byte received or transmitted was data
0 = Indicates that the last byte received or transmitted was address

Bit 4 P: Stop bit
(I2C mode only. This bit is cleared when the MSSP module is disabled, SSPEN is cleared.)
1 = Indicates that a Stop bit has been detected last (this bit is ‘0’ on Reset)
0 = Stop bit was not detected last

Bit 3 S: Start bit
(I2C mode only. This bit is cleared when the MSSP module is disabled, SSPEN is cleared.)
1 = Indicates that a Start bit has been detected last (this bit is ‘0’ on Reset)
0 = Start bit was not detected last

Bit 2 R/W: Read/Write bit information (I2C mode only)
This bit holds the R/W bit information following the last address match. This bit is only valid from the address match to
the next Start bit, Stop bit, or not ACK bit.
In I2 C Slave mode:
1 = Read
0 = Write
In I2 C Master mode:
1 = Transmit is in progress
0 = Transmit is not in progress
OR-ing this bit with SEN, RSEN, PEN, RCEN, or ACKEN will indicate if the MSSP is in Idle mode.
bit 1 UA: Update Address bit (10-bit I2C mode only)
1 = Indicates that the user needs to update the address in the SSPADD register
0 = Address does not need to be updated
bit 0 BF: Buffer Full Status bit
Receive (SPI and I2 C modes):
1 = Receive complete, SSPBUF is full
0 = Receive not complete, SSPBUF is empty
Transmit (I2 C mode only):
1 = Data transmit in progress (does not include the ACK and Stop bits), SSPBUF is full
0 = Data transmit complete (does not include the ACK and Stop bits), SSPBUF is empty

I have noticed that in PIC18F, this is implemented an means :SPPIF: Streaming Parallel Port Read/Write Interrupt Flag bit(1)
1 = A read or a write operation has taken place (must be cleared in software)
0 = No read or write has occurred

Bit 6 ADIF: A/D Converter Interrupt Flag bit
1 = A/D conversion complete (must be cleared in software)
0 = A/D conversion has not completed or has not been started

Bit 3 SSPIF: Master Synchronous Serial Port (MSSP) Interrupt Flag bit
1 = The MSSP interrupt condition has occurred, and must be cleared in software before returning from the
Interrupt Service Routine. In SPI mode :
A transmission/reception has taken place
0 = No MSSP interrupt condition has occurred

Bit 0 RBIF: PORTB Change Interrupt Flag bit
1 = When at least one of the PORTB general purpose I/O pins changed state (must be cleared in software)
0 = None of the PORTB general purpose I/O pins have changed state

05-24-2009, 09:42 AM

Civic Modz

My idea was to write it from scratch instead of using the SPI hardware in the pic.

I assume the software IDE has some kind of simulator in it so you can step through code and set breakpoints etc, without the need of any hardware.

Try,
Enable the interrupt for the IRQ pin
Set INTEDG in OPTION register to '1' (interrupt on rising edge)

with your debugger/simulator your code should enter the interrupt routine on every low-to-high pin change. Then you can work from there.

I haven't used easyPIC5 but i assume its the same as all those PICBASIC, BASICSTAMP, ARDUINO etc type PIC's with pre-written routines. So there might be a routine that utilizes the interrupt on pin change. Try looking at an IR receiver routine that might help.

I had a look at the EASYPIC5, its that big development board right. I'd like to help more but not really keen to purchase the board. Though if you can send me a link for the Microbasic IDE and I will see if I can help further.