10250 - Serial Peripheral Interface

2011.06.30

SPI 

This SPI (Serial Peripheral Interface) is my favourite serial port. Technically it functions as a parallel to series shift register on output and vice versa on input. Of course it has other additional features. In fact it is getting more complicated nowadays. It takes more understanding to use compared to a GPIO port. Otherwise, we would have started using it from the beginning, as we can reduce the amount of hardware connection. In fact, I am very inclined to start the next course with the SPI as the starting point.

Merely using its output function will serve us a lot of purposes. For output function, only two pins are involved, the data output pin (MOSI) and the clock signal pin. Just connect these two pins to a piece of 74HC164 (a serial to parallel shift register) and the 8 output pins of the 74HC164 can drive 8 LED’s etc, assuming that the other pins of the 74HC164 are properly dealt with.

            STM8S105                  74HC164                    Others

            Vdd                 to         Vdd and /MR (Reset)
            Vss                  to         Vss

            SClk(PC5)       to         Clock
            MOSI(PC6)    to         A1 and A2 (A and B)

                                                Qh, … Qa       to  Resistor  to  Anode of LED

                                                [Each output is connected to a resistor (330) and then
                                                  to the anode of an LED, with the cathode ground.]

In stead of the 8 LED’s, you can put a 7-segment display. In fact, you can cascade the 74HC164 to more 74HC164’s in a chain and number of the output pins is almost limitless.
The output pins are suitable for LED’s and some devices and not for motors etc.

Of course the simplicity of the hardware translates into complexity in the software. Not really complex once you get used to the SPI. And here is the simplest program to turn on 4 of the 8 LED’s.

//  m32spi22  SPI  8 LED's  Minimum code 

#include <iostm8s105c6.h>

int main( void ){

  SPI_CR2 = 0x03 ;  // 0000 0011 2line 0 NoCRC TX 0 Full Software '1'
  SPI_CR1 = 0x7C ;  // 0111 1100 MSB ON Slow Mstr SCk=0 Rising
  while((SPI_SR&0x02)==0x00); // Wait if TXE=0
  SPI_DR = 0x53 ;             // 01010011 

  return 0;
}

Standalone

Many years ago, I came across a group of students doing a microprocessor-based project using an emulator. An emulator, costing thousands of dollars, was the fore-runner of the programmer/debugger. The project was successful, except that it must be attached to the emulator, otherwise it could not run properly.

The prototype could not run standalone.

Such things can still happen.

Normally, after a program is downloaded into the STM8S105 kit, it is test-run from the IDE. If the USB connector is safety removed and then reconnected, the program will automatically run immediately as programmed. However, the following program will not run properly after the USB is removed and re-connected.

//  main151  SPI  8 LED's  Error

#include <iostm8s105c6.h>

int main( void ){

  PC_ODR = 0x00 ;   // Clear all outputs
  PC_CR1 = 0xE0 ;   // 1110 0000 PC7=PU   PC6=PP   PC5=PP
  PC_CR2 = 0x60 ;   // 0110 0000 PC7=NInt PC6=Fast PC5=Fast
  PC_DDR = 0x60 ;   // 0110 0000 PC7=MISO PC6=MOSI PC5=SCk
  SPI_CR1 = 0x7C ;  // 0111 1100 MSB ON Slow Mstr SCk=0 Rising
  SPI_CR2 = 0x03 ;  // 0000 0011 2line 0 NoCRC TX 0 Full Software '1'
  SPI_ICR = 0x00 ;  // No interrupts
 
  while((SPI_SR&0x02)==0x00); // Wait if TXE=0
  SPI_DR = 0x53 ; 


  return 0;
}

However, if you swap the two statements involving the two SPI control registers, SPI_CR1 and SPI_CR2 then the program works the same either from IDE or after re-connection. The manual does state that there is an error in the program. But it is difficult to see how the program works from IDE.

The STM8S-Discovery kit actually consists of two parts the programmer/debugger part and the microcontroller part. In serious applications, testing of the programs may have to be done after the programmer/debugger part of the board is removed from the microcontroller part. This can be easily done as the kit was designed so that the two parts can be broken off lightly. But it would take some work to connect the two together again.