Using SPI2 to Control OLED display on the BasicIO board (from chipkit WiFire)

Home Forums MIPS Academic Forum / University Connected MCU Lab Using SPI2 to Control OLED display on the BasicIO board (from chipkit WiFire)

This topic contains 0 replies, has 1 voice, and was last updated by  Amit 4 years, 4 months ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #64580

    Amit
    Participant

    I recently had a problem in programming the OLED display to interactively position the cursor on a line/column and display characters starting at that position.
    This required changing the Data/#CMD signal: #CMD-to set the cursor position; Then Data-to display characters

    Setting the cursor position involves setting the PAGE (0xB0 command) and then setting the Column (0x0 + 0x10 commands). My implementation wouldn’t correctly set the cursor position.

    Here is the code segment I used for sending a byte over SPI2 to the OLED:
    void Spi2PutByte(BYTE bVal)
    {
    // Wait for transmitter to be ready
    while (SPI2STATbits.SPITBE == 0)
    ;

    // Write the next transmit byte.
    SPI2BUF = bVal;

    // Wait for Transmitter to Empty out
    while (SPI2STATbits.SPITBE == 0)
    ;

    }

    The problem turned out to be that this routine waits for the sent byte to clear the Transmitter register — but that happens immediately when the byte loads into the shift-register. However, the byte still hasn’t shifted out. Therefore, my routine returned too early and changed the Data/#CMD control signal while the previous byte was “mid-flight”.

    Thanks to Digilent support engineer Andrew Holzer for finding this.

    There are two possible fixes for this:

    1. Monitor the SPI2 SPIBUSY bit before exiting the routine. As such:
    void Spi2PutByte(BYTE bVal)
    {
    // Wait for transmitter to be ready
    while (SPI2STATbits.SPITBE == 0)
    ;

    // Write the next transmit byte.
    SPI2BUF = bVal;

    // Wait for SPI function to complete
    while (SPI2STATbits.SPIBUSY)
    ;

    }

    or

    2. Wait for the in-coming byte to be received (but this requires a dummy read of the SPI2BUF to clear the receiver status bit) as thus:

    void Spi2PutByte(BYTE bVal)
    {
    // Wait for transmitter to be ready
    while (SPI2STATbits.SPITBE == 0)
    ;

    SPI2BUF ; //Dummy read of SPI buffer to clear RCV status bits

    // Write the next transmit byte.
    SPI2BUF = bVal;

    // Wait for Receiver to get incoming byte
    while (SPI2STATbits.SPIRBF == 0)
    ;
    }

Viewing 1 post (of 1 total)

Forums are currently locked.