Wednesday, May 26, 2010

Using SPI on Low-End Microcontroller

SPI is a simple and efficient inter-IC communication bus. A lot of peripheral chips such as Real Time Clock and EEPROM come with SPI or I2C bus. If there is no special reason, I prefer to use SPI than I2C because it is faster and simpler. It is also very easy to emulate in software.

Last time, I used 10MHz SPI LED driver chip with low end 4MHz microcontroller. Design priority was cost efficiency. Microcontroller cost less than a dollar but it had enough flash to store the firmware and a few display fonts. At first, I used interrupt and circular buffers to send and receive to and from SPI bus. I just wrote to the buffer and let the hardware and interrupt handled all the communication tasks as I usually do with slower long distance buses such as RS232 and CAN bus. It was OK in normal condition. The problem was that I wanted to update big 96x16 dot-matrix LED at the frame rate of 125Hz and the CPU utilization was very high. Consequently, it could not perform fast enough when it was executing some simple graphic manipulation tasks such as scrolling the text. Later, I realized that the most used SPI function where CPU spent most of its time was not efficient. Using hardware interrupt is more efficient normally, but it was different in this case- slow CPU with very fast and heavily used SPI. For each byte to SPI, send and receive interrupt functions which cost a lot of CPU cycles had to be performed. I found polling or emulation is faster than using interrupt to send a byte to SPI. Polling is still limited to the bus speeds supported by the hardware. After I modified the firmware to improve SPI function and it worked well. According to my experience, let me highlight some advantages of emulating SPI .
  1. It can sometimes be better in performance to emulate SPI in software.
  2. It is more reliable because it is simpler and it can avoid potential pitfalls of using interrupt.
  3. It is faster, easier and less error prone to write a simple code rather than reading datasheet for variety of register settings for every new microcontroller you encountered.
  4. Most importantly, it is portable and it is not dependent on hardware.
SPI does not have formal standard. It is just like a shift register. One can easily understand once s/he sees the timing diagram. The following is an example C function to send and receive a byte of SPI data.
//-------------------------------------
unsigned char spi(unsigned char d)
{
    unsigned char i;
    SCLK=1;
    EN=1;
    for(i=0;i<8;i++)
    {
        MOSI=(d & 0x80)?1:0;        
        //Delay(period/2)-optional for slower SPI bus speed
        SCLK=0;
        d<<=1;        
        d|=MISO;
        //Delay(period/2)-optional for slower SPI bus speed
        SCLK=1;
    }
    EN=0;
    return d;
}
//-------------------------------------

Monday, May 24, 2010

Astable Multivibrator using Op-amp

One of my friends wanted to calculate the oscillation frequency of an op-amp circuit. He had measured the output frequency of the circuit and it was 109 kHz. He asked my help to derive the relation between the oscillation frequency and its passive components. Although I have been away from analog circuits for a long time, I agreed to have a look. Then, he took the picture of circuit diagram using his hand phone and sent to me using MMS.
The basic idea behind the circuit is simple, so I calculated the frequency, took the following pictures using my hand phone and sent back to him.

Saturday, May 1, 2010

Choosing name for our daughter

Here are a few things that we considered when we were choosing a name for our newborn baby. Most of the idea is inspired from the book 'Conception, pregnancy and birth' by Dr. Miriam Stoppard.
  1. We hoped that the name is suitable for her at all stages of life.
  2. We took extra caution not to have any reason why our child might be teased because of the name we've chosen.
  3. We also think that the name should have good meaning.
  4. In case of nationality and traditions, we liked to give her a Myanmar name that follows traditions. For example, to have a first name that matches with the weekday she was born.
  5. We like a name because of its sound. It should be naturally harmonious and sounds good like a sweet music. It should also be read smoothly like a poem.
  6. We like to have one word from father's name and one word from mother's name in the name of our baby.
  7. Although we might be influenced by many considerations, we had to remind ourselves that the name we were choosing was for our baby, and hopefully it will please her throughout the whole of her life.