Help understanding SPI

Discussion in 'Embedded Systems and Microcontrollers' started by Hamilton, Jun 24, 2008.

  1. Hamilton

    Thread Starter Member

    Jun 20, 2008
    I’m really stuck and out of tricks to try and can not spend another full week trying to fix it.

    I understand all 8051 MCUs are somewhat different when it comes to extended capabilities (e.g. SPI) . Whether you’re a pro at AVR, PIC, SiLABS, etc take a look at the following flow and based on your experience tell me what I may be missing to get an output for a SPI port. While it may not apply to my device at least I’ll know to look to see if it does or doesn’t.

    1.) Set clock to crystal, divider to crystal/1 and tick speed to drive timers at crystal/8
    2.) Set Port to drive /SlaveSelect to GPIO, Pull-up, and output
    3.) Set Port for MOSI & SCLK to output and peripheral
    4.) Set Port for MISO to input and peripheral
    5.) Set baud rate to 19,200 (there is a M and E register)
    6.) Set USART to SPI mode, CPOL=0, CPHA=1, MSB first
    8.) Flush buffer, clear TX and RX flags
    9.) /SlaveSelect set low to enable slave
    10.) Write to SPI buffer
    11.) Wait for TX empty flag
    12.) Set SlaveSelect to disable slave

    What I’m not doing: setting what I think are pure UART settings (STOP, START, Parity, number of bits (8 | 9). I do not use timers or interrupts except the TX and RX flags.

    I have a good C debugger and can watch the assembly code and registers change as I single step. I ‘m watching the ports on a logic analyzer and have a Tek O’scope. I can confirm the reg values I set in code are in fact correctly stored in registers.

    I simply can not get a serial data flow. SCLK goes low when the port is set to peripheral and stays there. More details are in earlier post titled Ti Chipcon CC1110/CC2510 SPI problem
  2. Arm_n_Legs

    Active Member

    Mar 7, 2007
    Below a sample code for SPI connection between a Atmel AT89C5131 with a AD7823 via SPI. The AD7823 is a ADC which passes the data via SPI. Conversion is initiated by activating the CONVERT pin on the AD7823 which is connected to P1.4 of the AT89C5131.

    void delay(unsigned int y){

    void main(){
    P14=0; // Power Down ADC
    SPCON = 0xF5; // Initialise AT89C5131 SPI port
    P14=1; // Wake up ADC
    P14=0; // Start conversion
    delay(100); // Wait for ADC to complete
    SPDAT = 0xaa; // Send dummy data out of MOSI
    while (SPSTA&0x80==0); // wait for data to shift out.
    x = SPDAT; // Received SPI data now in x
    dummy = SPSTA; // dunno why.. but without this statement, cannot work