This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

LPC935 seems a lazy cow in software SPI(?)

Hello to all friends, once again

Simple issue. Connect an Atmel 45DB321 flash and try to make an emulated SPI software flow just to read this little budy's device ID. aAll good but when the timer interrupt hits the core, sending 4 bytes makes this system to crawl like a lazy cow (About 100us to send all bytes). Also there is the A/D Single conversion mode enabled started by timer overflow with auto reload. It also reads 4 bits from port P0 and do or not do the SPI transaction. Simple but still fuzzy to understand why it take so long :( Internal clock is enabled :)

Take a look of entire code (Sorry for any inconvenience about C mistakes )

Ty all, just to see this post,
Best regards
Timothy

#include <REG936.H>
#define READ_STATUS 0xD7
#define READ_DEVID 0x9F


sbit Status_LED = P2^0;                                 // Initialize P2.0 as the status led
sbit REC_LED    = P0^7;                                 // Initialize P0.7 as the record led
sbit BUSY_LED   = P0^6;                                 // Initialize P0.5 as the busy led
sbit PLAY_LED   = P0^5;                                 // Initialize P0.6 as the play led

sbit SPI_MOSI   = P2^2;                 // Initialize P2.2 as the MOSI signal (Output)
sbit SPI_MISO   = P2^3;                 // Initialize P2.3 as the MISO signal (Input)
sbit SPI_SS     = P2^4;                 // Initialize P2.4 as the SS signal (Output)
sbit SPI_SPICLK = P2^5;                 // Initialize P2.5 as the SPICLK signal (Output)


unsigned char data overflow_count;      // Define system interrupt as a variable
unsigned char data keyb_input    ;      // Keyboard input register(8bit)
unsigned int  data Counter_us    ;      // Microsecond counter(16bit)-ISR usage
unsigned char data Task          ;      // Special user register (8bit)-Task manager usage
unsigned char data SPI_DATA      ;      // SPI data register (8bit)
unsigned char data AD_Sample     ;      // Current 8bit sample from A/D module
unsigned char data Readout       ;      // Current SPI Byte recieved


void Delay_20ms(void);
void Write_SPI(unsigned char Temp1);

main (void)
{

  P0M1 = 0x00;
  P0M2 = 0x00;
//Port1 configuration
  P1M1 = 0x00;
  P1M2 = 0x00;
//Port2 configuration
  P2M1 = 0x02;
  P2M2 = 0x00;


  TMOD = 0x02;
  TAMOD= 0x00;
  TL0  = 0x44;
  TH0  = 0x44;
  IEN0 = 0x82;
  TCON = 0x10;


  ADINS  = 0x40;
  ADCON1 = 0x24;
  ADMODA = 0x10;
  ADMODB = 0x60;
  ADCON0 = 0x00;
  Task   = 0;
  P2     = 0;


Delay_20ms();                                                   // Startup delay for 20ms to let flash memory to recover
while (1)                                                               // Loop forever ("Super loop")
  {


          if (keyb_input == 0x04)                       // Rec key pressed (?) - (Press Suspected)
                        {
                          REC_LED  = 1;
                          BUSY_LED = 0;
                          PLAY_LED = 0;
                          Task     = 0x01;                      // Modify Task register (TaskID:0x01)
                     }
                        else

                          Task     = 0x00;                      // Modify Task register (TaskID:0x00)
                          REC_LED  = 0;
                          BUSY_LED = 0;
                          PLAY_LED = 0;
  }
}


void timer0_ISR (void) interrupt 1
        {
     IEN0 = 0x82;                                       // Enable interrupts on timer0/Disable global interrupts
         Status_LED = 1;                                        // Open Status led
         Counter_us++;                                          // Increment microsecond counter(16bit)
         keyb_input = P0;                       // Read keyboard
         keyb_input = keyb_input & 0x07;    // Isolate keyboard input bit pattern
         ADMODB = 0x60;                                         // ADC mode for AD-Module1
          while((ADCON1 & 0x08) == 0);              // Wait for end of conversion
                  {
                  }
           if ( Task == 0x01)
            {
                   ADCON1 &= 0xF7;                          // Clear EOC flag
           SPI_SS =0;                                   // Open communication with flash
                   Readout = READ_DEVID;
                   Write_SPI(Readout);
                   Readout = 0X00;
                   Write_SPI(Readout);
                   Readout = 0X00;
                   Write_SPI(Readout);
                   Readout = 0X00;
                   Write_SPI(Readout);
                   SPI_SS =1;                                   // Close communication with flash
            }
        else
           P1 = Readout;
           Status_LED = 0;                                      // Close status led
     IEN0 = 0x82;                                       // Enable interrupts on timer0/Disable global interrupts
        }


void Delay_20ms(void)
        {
                Counter_us = 0;                                 // Reset Counter_us 16bit register
                        while (Counter_us <= 400)    // Wait until 20ms elapsed
                                {

                                }
        }


void Write_SPI(unsigned char Readout)
        {
          unsigned char i;                                      // Local variable
      SPI_SPICLK = 0;                                   // SPI Mode0
          SPI_MOSI = 0;                             //
                   for(i = 8; i >0 ; i--)
            {
             if (Readout & 0x80)            // Current bit7 is "1" (?)
              SPI_MOSI = 1;                             // Set SPI_MOSI line
               else                                             // Else
              SPI_MOSI = 0;                             // Reset SPI_MOSI line
              Readout <<= 1;                      // Shift next bit into bit7

              SPI_SPICLK = 1;                   // SPI Slave latches input data bit
               if (SPI_MISO)                    // Check if SPI_MISO line is set
               Readout |= 0x01;                 // Modify Readout register
              SPI_SPICLK = 0;                   // SPI Slave shifts out next output data bit
                        }
          SPI_MOSI = 0;                             // Clear SPI_MOSI line
        }

0