You have no items in your shopping cart.

Subtotal: 0.00

How to Interface Stepper Motor with AVR Development Board

AVR Development Board

The AVR Development Board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports In-system programming (ISP) which is done through Parallel/Printer port.

ATMEL’s AVR (Atmega8535), AVR Development Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of High speed 8-bit Microcontrollers.

SPI (Serial Peripheral Interface)

Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It can also be used for communication between two microcontrollers.

EEPROM

EEPROM (electrically erasable programmable read-only memory) is user-modifiable read-only memory (ROM) that can be erased and reprogrammed (written to) repeatedly through the application of higher than normal electrical voltage. It is a type of non-volatile memory used in computers and other electronic devices to store small amounts of data that must be saved when power is removed, e.g., calibration tables or device configuration.

Interfacing SPI - EEPROM

Fig. 1 shows how to interface the SPI-EEPROM to microcontroller. With an SPI connection there is always one master device (usually a microcontroller) which controls the peripheral devices. Typically there are three lines common to all the devices,

  • Master In Slave Out (MISO) - The Slave line for sending data to the master,
  • Master Out Slave In (MOSI) - The Master line for sending data to the peripherals,
  • Serial Clock (SCK) - The clock pulses which synchronize data transmission generated by the master, and
  • Slave Select pin - the pin on each device that the master can use to enable and disable specific devices. When a device's Slave Select pin is low, it communicates with the master. When it's high, it ignores the master.

These allow you to have multiple SPI devices sharing the same MISO, MOSI, and CLK lines.



Fig.1 Interfacing SPI-Ethernet to Microcontroller

 

The controller designed controls the EEPROM device through SPI protocol. The SPI Controller here acts as a master device and controls EEPROM which acts as a slave. The read-write operations are accomplished by sending a set of control signals including the address and/or data bits. The control signals must be accompanied with proper clock signals.

Interfacing SPI – EEPROM with AVR Development

We now want to Read, write and Erase EEPROM by using SPI in AVR Development Board. Wiring up an SPI based EEPROM to the SPI port is relatively simple. The basic operation of the SPI based EEPROM's is to send a command, such as WRITE, followed by an address and the data. In WRITE operation, the EEPROM to store the data.

In SPI, the clock signal is controlled by the master device AVR Development Board. All data is clocked in and out using this pin.

These lines need to be connected to the relevant pins on the AVR Development Board. Any unused IO pin can be used for CS, instead pull this pin high.

In AVR Development Kit, four nos. of EEPROM lines are controlled by SPI Enabled drivers. The SPI Lines Chip Select of CS (PORTB.4), serial clock of CLK (PORTB.7), serial input data of MISO (PORTB.6) and serial output data of MOSI (PORTB.5) connected to the SPI based serial EEPROM IC. The EEPROM read & write operations are done in AVR Development Kit by using these CS, CLK, MOSI, MISO SPI lines.

Pin Assignment with AVR Development

 

HARDWARE PIN

OUT

CONNECTIONS

OUTPUT

CS

PORTB.4

Turn ON SPI Pin of CONFIG-II switch SW6.

Connect Serial cable between USART Section in

the Board and PC. Open HyperTerminal

Press RESET once

The string “SPI Test Program”

will be displayed in Hyper-

Terminal

MOSI

PORTB.5

MISO

PORTB.6

SCK

PORTB.7

 



Circuit Diagram to Interface SPI–EEPROM with AVR





Source Code

The Interfacing SPI – EEPROM with AVR Development program is very simple and straight forward that read, write and erase operations in EEPROM by using SPI & the value is displayed in serial port. A delay is occurring in every single data read or write in EEPROM. The delay depends on compiler how it optimizes the loops as soon as you make changes in the options the delay changes.

C Program with SPI – EEPROM using AVR

*********************************************************************************************

Title : Program to read, write & erase of SPI - EEPROM

*********************************************************************************************

#include<mega8535.h> #include<stdio.h> #include<delay.h> #define CS PORTB.4 #define MOSI PORTB.5 #define MISO PINB.6 #define SCK PORTB.7 #define TXIF UCSRA.5 #define FOSC 16000000L //Oscillator Frequency 16Mhz #define USART_Baud 9600 #define BaudH ((unsigned int)(((FOSC/16/USART_Baud)-1) >> 8)) #define BaudL ((unsigned int)(((FOSC/16/USART_Baud)-1) & 0xFF)) /*SPI_COMMANDS*/ #define READ 0x03 #define WRITE 0x02 #define WRDI 0x04 #define WREN 0x06 #define RDSR 0x05 #define WRSR 0x01 unsigned char i,j; unsigned char Msg[]="SPI TEST Program"; void Serial_init(); void SPi_init(); void SPi_WRITE(unsigned char); unsigned char SPi_RDSR(); unsigned char SPi_READ(unsigned char); void main() { unsigned char x; DDRC = 0xff; DDRB = 0xbf; Serial_init(); SPi_init(); delay_ms(10); while(!SPi_RDSR()); SPi_WRITE(0x00); delay_ms(10); while(1) { x=0; while(x<16) { putchar(SPi_READ(x)); ++x; delay_ms(50); } putchar('\r'); } } void SPi_init() { CS = 1; MOSI = 0; SCK = 0; MISO = 1; } unsigned char SPi_RDSR() { unsigned char Data=0x05; CS=0; for(i=0;i<8;i++) { MOSI=(Data & 0x80)?1:0; delay_us(5); SCK=1; Data=Data<<1; delay_us(5); SCK=0; } for(i=0; i<8; i++) { SCK=1; Data|=((MISO & 1)?1:0); Data=Data<<1; delay_us(3); SCK=0; delay_us(5); } CS=1; return !Data; } void SPi_WRITE(unsigned char Addr) { unsigned char Data=WREN; int AH=WRITE; AH=(AH<<8)+Addr; CS=0; for(i=0;i<8;i++) { MOSI=(Data & 0x80)?1:0; delay_us(5); SCK=1; Data=Data<<1; delay_us(5); SCK=0; } CS=1; CS=0; for(i=0;i<16;i++) { MOSI=(AH & 0x8000)?1:0; delay_us(5); SCK=1; AH=AH<<1; delay_us(5); SCK=0; } for(i=0;i<16;i++) { Data=Msg[i]; for(j=0;j<8;j++) { MOSI=(Data & 0x80)?1:0; delay_us(5); SCK=1; Data=Data<<1; delay_us(5); SCK=0; } } CS=1; } unsigned char SPi_READ(unsigned char Addr) { int Data=READ; unsigned char RData=0; Data=(Data<<8)|Addr; while(!SPi_RDSR()); CS=0; for(i=0;i<16;i++) { MOSI=(Data & 0x8000)?1:0; delay_us(5); SCK=1; Data=Data<<1; delay_us(5); SCK=0; } for(i=0; i<8; i++) { RData=RData<<1; delay_us(5); SCK=1; RData|=((MISO & 1)?1:0); delay_us(5); SCK=0; } CS=1; return RData; } void Serial_init(void) { UCSRA = 0x00; //Status Register UCSRB = 0x18; //Transmit Enable,Receive enable,interrupts disabled UCSRC = 0x86; //8 databits,no parity,single stop UBRRH = BaudH; UBRRL = BaudL; printf("\033[2J"); } #define _ALTERNATE_PUTCHAR_ void putchar(unsigned char Data) { while(TXIF==0); UDR = Data; }

To compile the above C code you need the CodeVision AVR software. The software has it’s own IDE and built-in AVR gcc- Compiler. They must be properly installed and a project with correct settings must be created in order to compile the code. To compile the above code, the C file must be added to the project.

In CodeVision AVR software, you can develop or debug the project without any hardware setup. You must compile the code for generating HEX file. In debugging Mode, you want to check the port output without microcontroller Board.

The AVR Code Blaster software is used to download the hex file into your microcontroller through Parallel port.

Testing the SPI – EEPROM with AVR

Give +12V power supply to AVR Development Board; the EEPROM device is connected with the AVR Development Board. First check the entire EEPROM device fixed properly. A serial cable is connected between the microcontroller and PC. In PC, open the Hyper Terminal for displaying the values from EEPROM through SPI.

The Read & Write operations are performed in EEPROM with EEPROM address. When the EEPROM address is correct, then only you can write, read, and erase data’s correctly in EEPROM.

If any data is not coming in Hyper Terminal, then you just check the serial cable is working or not. Otherwise you just check the code with debugging mode in CodeVisionAVR.

General Information

  • For proper working use the components of exact values as shown in Circuit file.
  • Solder everything in a clean way. A major problem arises due to improper soldering, solder jumps and loose joints. Use the exact value crystal shown in schematic.
  • More instructions are available in following articles,

    User Manual for ATMEGA Development Kit

    How to create & Debug a Project in CodeVision AVR.