Tuesday, July 06, 2010

Serial Port Interface Communication (SPI)

Theory of Operation

The Serial Peripheral Interface Bus or SPI bus is a synchronous serial data link used to communicate between two or more microcontroller and devices supporting SPI mode data transfer. Devices communicate in master/slave mode where the master device initiates the data frame. Multiple slave devices are allowed with individual slave select (chip select) lines.

This communication protocol consists of folliwng lines or pins,

  1. MOSI : Master Out Slave In (Tx for Master and Rx for Slave)
  2. MISO : Master In Slave Out (Rx for Master Tx for Slave)
  3. SCK : Serial Clock (Clock line)
  4. SS : Slave Select (To select Slave chip) (if given 0 device acts as slave)

Master: This device provides the serial clock to the other device for data transfer. As a clock is used for the data transfer, this protocol is Synchronous in nature. SS for Master will be disconnected.

Slave: This device accepts the clock from master device. SS for this has to be made 0 externally.

avr spi

avr spi













Setting up SPI in Microcontroller

avr spi

Master Microcontroller

Open Code Wizard and go to SPI tab. Enable SPI and choose Master in SPI Type.

Select clock rate depending upon your data transfer speed requirement.

Keep other settings as default.

avr spi

Slave Microcontroller

Open Code Wizard and go to SPI tab. Enable SPI and choose Slave in SPI Type.

Select clock rate depending upon your data transfer speed requirement.

Keep other settings as kept in the master microcontroller.

Data Transfer Functions

You can transmit or receive 1 byte of data at a time.

Transmit Data

spi(1 byte data);

Example: spi(‘A’);

Receive Data

c = spi(0); // c is 1 byte variable

Example: char c = spi(0);

Interrupts

Interrupt

An interrupt is a signal that stops the current program forcing it to execute another program immediately. The interrupt does this without waiting for the current program to finish. It is unconditional and immediate which is why it is called an interrupt.avr interrupt

The whole point of an interrupt is that the main program can perform a task without worrying about an external event.

For example if you want to read a push button connected to one pin of an input port you could read it in one of two ways either by polling it or by using interrupts.

Polling

Polling is simply reading the button input regularly. Usually you need to do some other tasks as well e.g. read an RS232 input so there will be a delay between reads of the button. As long as the delays are small compared to the speed of the input change then no button presses will be missed.

If however you had to do some long calculation then a keyboard press could be missed while the processor is busy. With polling you have to be more aware of the processor activity so that you allow enough time for each essential activity.

Hardware Interrupt

By using a hardware interrupt driven button reader the calculation could proceed with all button presses captured. With the interrupt running in the background you would not have to alter the calculation function to give up processing time for button reading.

The interrupt routine obviously takes processor time – but you do not have to worry about it while constructing the calculation function.

You do have to keep the interrupt routine small compared to the processing time of the other functions - it’s no good putting tons of operations into the ISR. Interrupt routines should be kept short and sweet so that the main part of the program executes correctly e.g. for lots of interrupts you need the routine to finish quickly ready for the next one.

Hardware Interrupt or polling?

The benefit of the hardware interrupt is that processor time is used efficiently and not wasted polling input ports. The benefit of polling is that it is easy to do.

Another benefit of using interrupts is that in some processors you can use a wake-from-sleep interrupt. This lets the processor go into a low power mode, where only the interrupt hardware is active, which is useful if the system is running on batteries.


Hardware interrupt Common terms

Terms you might hear associated with hardware interrupts are ISR, interrupt mask, non maskable interrupt, an asynchronous event, and interrupt vector and context switching.

Setting up Hardware Interrupt in Microcontroller

avr interrupt cvavr

There are 3 external interrupts in Atmega 16. They are

  • INT0 : PD2, Pin 16
  • INT1 : PD3, Pin 17
  • INT2 : PB2, Pin 3

There are 3 modes of external Interrupts,

  • Low Level: In this mode interrupt occurs whenever it detects a ‘0’ logic at INT pin. To use this, you should put an external pull up resistance to avoid interrupt every time.

  • Falling Edge: In this mode interrupt occurs whenever it detects a falling edge that is ‘1’ to ‘0’ logic change at INT pin.

  • Rising Edge: In this mode interrupt occurs whenever it detects a falling edge that is ‘0’ to ‘1’ logic change at INT pin.

Functions of Interrupt Service Routine

After generating the code, you can see the following function,

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here

}

You can place your code in the function, and the code will be executed whenever interrupt occurs.