atmega32 dev board--efy march11.pdf

7
CONSTRUCTION 120 • MARCH 2011 ELECTRONICS FOR YOU WWW.EFYMAG.COM SAGAR G. YADAV TUTORIAL ON ATMEGA32 DEVELOPMENT BOARD PART 1 S.V.N. HARISH K. M icrocontrollers find ap- plications in various in- dustrial and domestic controls and processes. Therefore a good knowledge about them has become a necessity for elctronics en- gineers. Although a lot of theoritical knowledge related to the software and hardware aspects of microcon- trollers is available in the market, there is no easy alternative to a hands-on learning experience, and for that, a microcontroller develop- ment board is a must. Here we describe a development board that is based on the popular AVR ATmega32 microcontroller and meets the needs of both the beginners as well as professionals. Beginners may use the board to learn the basics of microcontrollers, while professionals may use it for developing microcontroller-based complex and smart circuit designs. Twelve sample codes given in this project will also help one to learn more about the inbuilt peripherals of the microcontrollers. The twelve program codes are Blink_LED, Chaser_Effect, Mov- ing_Display, ADC_test, 8bit_Timer, Switches, External_INT, EEPROM, Freq_meter, PWM, UART and TWI. Why AVR ATmega32? The AVR is fast, cheap, in-circuit programmable, and the develop- ment software is usually available for free as most of them are Open Source. Moreover, discussions and tutorials on the AVR family of processors are available on the Internet for solutions to develop- ment issues. Fig. 1: Circuit of ATmega32 development board

Upload: sagargy

Post on 25-Oct-2015

76 views

Category:

Documents


7 download

DESCRIPTION

Project Design published by Sagar G Yadav in EFY March 2011 Issue on ATMega 32 Development Board.

TRANSCRIPT

Page 1: Atmega32 Dev Board--EFY March11.pdf

cONSTRUcTION

120 • Mar c h 2011 • electronics for you w w w . e f y M a g . c o M

SAGAR G. YADAV

TUTORIAL ON ATMEGA32 DEVELOPMENT BOARD

Part 1

s.v.n. harish k.

Microcontrollers find ap-plications in various in-dustrial and domestic

controls and processes. Therefore a good knowledge about them has become a necessity for elctronics en-gineers. Although a lot of theoritical knowledge related to the software and hardware aspects of microcon-trollers is available in the market, there is no easy alternative to a hands-on learning experience, and for that, a microcontroller develop-ment board is a must.

Here we describe a development board that is based on the popular AVR ATmega32 microcontroller and meets the needs of both the beginners as well as professionals. Beginners may use the board to learn the basics of microcontrollers, while professionals may use it for developing microcontroller-based complex and smart circuit designs. Twelve sample codes given in this project will also help one to learn more about the inbuilt peripherals of the microcontrollers.

The twelve program codes are Blink_LED, Chaser_Effect, Mov-ing_Display, ADC_test, 8bit_Timer, Switches, External_INT, EEPROM, Freq_meter, PWM, UART and TWI.

Why AVR ATmega32? The AVR is fast, cheap, in-circuit programmable, and the develop-ment software is usually available for free as most of them are Open Source. Moreover, discussions and tutorials on the AVR family of processors are available on the Internet for solutions to develop-ment issues. Fig. 1: Circuit of atmega32 development board

Page 2: Atmega32 Dev Board--EFY March11.pdf

cONSTRUcTION

electronics for you • Mar ch 2011 • 121w w w . e f y M a g . c o M

The ATmega32 is a high-perfor-mance, low-power, 8-bit AVR micro-controller. It has 32 kB of in-system self-programmable flash, 2 kB of static random access memory (SRAM) and 1 kB of electrically erasable program-mable read-only memory (EEPROM). Some of the on-chip peripheral fea-tures are:

1. Two 8-bit timers/counters with separate prescaler and compare modes

2. One 16-bit timer/counter with separate prescaler, comparator mode and capture mode

3. Four pulse-width-modulated (PWM) channels

4. 8-channel, 10-bit analogue-to-

digital converter (ADC) 5. Byte-oriented, two-wire serial

interface (TWI) 6. Programmable serial USART

(universal synchronous asynchronous receiver transmitter)

7. Master/slave SPI serial interface 8. Programmable watchdog timer

with separate on-chip oscillator 9. On-chip analogue comparator

The development board This ATmega32 development board has the following features:

1. In-system programmable with an on-board serial-port-based pro-grammer

2 . O n -board regu-lated and fil-tered power supply with power indica-tor LED

3 . Eight LEDs, which are selectable through DIP

switches (provided on the board) for status or debugging purpose

4. Five tactile switches for external inputs or interrupts: a reset switch to safely reset the microcontroller exter-nally, two switches on external inter-rupt pins and two more for general purpose

5. On-board LCD connector (which can be used for other applications too) along with a preset to control the contrast

6. On-board PC-MCU serial port link for use of the USART module in the MCU for two-way communication with the PC

7. On-board piezobuzzer selectable through a jumper for status or debug-ging purpose

8. On-board two-wire serial EE-PROM of 4 kB (512x8) in addition to 1 kB of on-chip EEPROM

9. All the MCU ports are eas-ily accessible through single-line male header (berg strip connectors)

10. Two 8-pin male header strip connectors for providing power supply to external devices and sensors

11. On-board vari-able voltage source us-ing a preset, provided via a switch to PA7 for ADC test

Circuit description Fig. 1 shows the circuit of ATmega32 devel-opment board. At the heart of the circuit is the 40-pin ATmega32 MCU, which is connected to the board through a 40-pin dual in-line package (DIP) IC socket.

The MCU has four input/output (I/O) ports: Port A (pins 33 through 40), Port B (pins 1 through 8), Port C (pins 22 down through 29) and Port D (pins 14 through 21). Each of these port pins is made

Fig. 2: Power supply circuit for atmega32 development board

Fig. 3: an actual-size, single-side PCB for the atmega32 development board

Page 3: Atmega32 Dev Board--EFY March11.pdf

cONSTRUcTION

122 • Mar c h 2011 • electronics for you w w w . e f y M a g . c o M

available through external male header strips. In order to draw the regulated +5V supply for external circuits and sensors, a 16-pin header (CON1) is provided in the board.

The development board has self-contained regulated +5V supply circuit built around the 7805 voltage regula-tor and filtering capacitors to obtain a filtered, regulated power supply. A power-indicator LED is used to indi-cate the presence of power supply in the circuit. The power supply circuit

with 9V input to 7805 is shown in Fig. 2.

For easy debugging, the board has eight LEDs (LED2 through LED9) selectable through a DIP switch. All these LEDs are connected to Port B of the MCU through current-limit-ing resistors. When not in use, the LEDs can be disconnected from the circuit individually with the help of DIP switches, thus enabling the normal opera-tion of Port B for other applica-

tions. Five tactile pushbutton switches

are also provided for debugging pur-pose. All the five switches are active-low type and do not affect the normal operation of port pins. Switches S1, S2, S3 and S4 are connected to Port D pins PD0, PD1, PD2 and PD3, respectively. PD0 and PD1 form external interrupt pins of the MCU.

A power-on reset circuit is built around reset pin 9 with the help of resistor R10 and capacitor C3. Switch

S5 is connected to pin 9 for resetting the MCU externally.

At Port PD7 (pin 21), a piezobuzzer is connected via switching transistor T2 to further help in debugging. The buzzer can be discon-nected from the circuit at any point of time by removing shorting jumper JS.

In order to further enhance the user inter-face of the development board, a dedicated LCD port is provided on-board. To minimise use of I/O pins for LCD control, the display is made to function in 4-bit mode. In 4-bit mode, only four data pins are required in addition to RS, RW and E control pins. In total, seven I/O

port pins are required for functioning of the display.

Details about the LCD module and its interfacing with the MCU are pro-vided later in the article. Port C is used to interface the LCD in the develop-ment board. Port-C pins PC4, PC5, PC6 and PC7 form data pins, whereas PC2 and PC3 are used as RS and E control pins. Port-D pin PD6 is used to provide R/W control signal.

In order to communicate with the PC, the development board uses the USART module of the MCU. Func-tional details of this module are given later in the article. The RS-232 signals range between +12V and -12V and are inverted (-12V is a logical 1). Apply-ing this kind of voltage to the AVR is dangerous, so MAXIM’s MAX232 level converter is used between the DB9 female serial port and the MCU. The MAX232 uses capacitors to perform the necessary level shifting in order to receive and transmit signals. The same DB9 serial port connector is used to program the MCU by using the SPI

Fig. 5: aVR Studio output dialogue box

Fig. 4: Component layout for the PCB

Page 4: Atmega32 Dev Board--EFY March11.pdf

cONSTRUcTION

electronics for you • Mar ch 2011 • 123w w w . e f y M a g . c o M

defined hardware circuit for SPI pro-gramming. The same circuit is used in the development board too by making use of MOSI, MISO, SCK and RESET pins of the MCU. To limit the line volt-ages on the SPI interface lines, 5.1V zener diodes are used. DPDT switch S7 is used to switch between USART communication mode and SPI serial programming mode. A step-by-step procedure to program the MCU using PonyProg is provided in the latter part of the project design.

The microcontroller is clocked to a maximum frequency of 16 MIPS by using 16MHz quartz crystal X1. The crystal should always be used along with two capacitors of equal value (connected as shown in the circuit dia-gram) for a stable operation. The value chosen for these capacitors is 22 pF for a frequency of 16 MHz.

In order to test the MCU’s ADC module, a variable voltage source is made available on-board with the help of preset VR1. Switch S6 should be kept opened when the ADC applica-tion is not in use.

The 24C04 forms the external EEPROM circuit of the development board. It has a storage capacity of 4 kB (512x8). The EEPROM can be read/written using the TWI module avail-

Fig. 6: Debug platform and device window

Fig. 7: I/O port setup in PonyProg

able in ATmega32. For this purpose, connect SDA (Serial Data) and SCL (Se-rial Clock Input) pins of the EEPROM to corresponding SDA and SCL pins of the MCU via a DPDT switch. Ad-dress input pins A0, A1 and A2 of the 24C04 are held low to give a hardware address of 000.

To connect additional TWI devices to the development board, a 4-pin TWI male header strip (I2C port) is pro-vided along with power supply rails.

Construction The entire development board circuit can be assembled on a single PCB. An actual-size, single-side PCB for the same is shown in Fig. 3 and its component layout in Fig. 4. Solder by first connecting all the jumpers in the circuit, followed by smaller-size pas-sive components and then the bigger components.

Software program Before loading various application ma-chine codes into the microcontroller, you need to write the intended control-ler program in ‘C’ language. Compile it to obtain the hex code. Burn the gener-ated hex code into the microcontroller. To perform all these tasks in this project, you can use free Open Source software like WinAVR, AVR Studio 4 and PonyProg. The programs should be installed in the PC before starting the AVR applications.

WinAVR. WinAVR (pronounced as ‘whenever’) is a suite of executable, Open Source software development tools for the ATMEL AVR series of RISC microprocessors hosted on the Windows platform. It includes the GNU GCC compiler for C and C++ programming languages. Visit http://winavr.sourceforge.net/index.html for more details.

AVR Studio 4. In this project, we have used AVR Studio 4 (Version 4.13) to compile the code written in ‘C’ lan-guage and generate the hex code. It can also be used to simulate the program running on the microcontroller.

PonyProg. PonyProg (Version

Fig. 8: Device selection dialogue box

Fig. 9: Security and configuration bits setting window

serial programming facility available in the MCU.

Popular Open Source software PonyProg is used for easy program-ming of the MCU. The same software can also be used to set and read all the flag bits in the MCU, to read/write the on-chip EEPROM.

PonyProg also comes with a well-

Page 5: Atmega32 Dev Board--EFY March11.pdf

cONSTRUcTION

124 • Mar c h 2011 • electronics for you w w w . e f y M a g . c o M

Fig. 10: Error message

code. This will make all the Port B pins to go high. Now generate a time delay of 500 ms using the WaitMs( ) function. Once the time delay is generated, all the Port B pins need to be cleared to

switch off the LEDs. For this, load 0x00 (complement of 0xFF) to Port B. The entire process repeats unconditionally inside a ‘while’ loop to generate the LED blinking effect.

Load your application pro-gram into the ATmega32 chip as follows:

1. Open AVR Studio. You will get a window as shown in Fig. 5. Click ‘New Project’ option.

2. Select AVR GCC and enter a name and destination folder for your project.

3. Select AVR simulator in ‘Debug platform’ window and

Atmega32 in ‘Device’ window (refer Fig. 6).

4. Start writing your first program ‘blink_LED.c’ in the AVR Studio editor window. Configure the required op-tions in ‘Tools’ and ‘Project’ menus.

5. Go to ‘Build’ menu and click ‘Build’ option to compile the code. If there is no error, the hex code will generate.

6. Connect the ATmega32 develop-ment board to the serial port of the PC through the serial cable. On the board, set switch S7 to its ‘program’ position. Switch on the power supply. Run PonyProg and select ‘Interface Setup’ option from ‘Setup’ menu.

7. Set the I/O port as shown in Fig. 7.

Click ‘Probe’ button to check whether you have correctly connected the development board to the PC. A ‘Test OK’ message should appear. It will ensure that you have correctly connected the serial cable to the board, the power supply is turned on, switch S7 is turned to its ‘program’ position and you have chosen the correct COM port.

8. Select AVR Micro and ATmega32 options from ‘Device Selection’ menu

in the toolbar (see Fig. 8).9. Click ‘Security and Configura-

tion Bits’ (see Fig. 9). Click ‘Read’ but-ton to read the bits. Now click ‘Write.’

10. Open the hex file generated by AVR Studio. Go to ‘Command’ menu and click ‘Write All’ in PonyProg.

11. Click ‘Yes’ to start writing the code into the chip. Slide all the switches on the DIP-SW switch to ‘On’ position. If all the LEDs glow, you have completed the first application successfully. Otherwise, you will get an error message as shown in Fig. 10.

To solve the problem, ensure that the power is ‘on,’ switch S7 is in ‘Program’ mode and the serial cable is properly connected. Now check whether you have selected the correct device and the fuse-bit settings. Fig. 11 shows the prototype of ATmega32 development board.

Example 2: Sequential light chaser effectThe source code (Chaser_Effect.c) for generating a sequential chaser effect on the LEDs (LED2 through LED9) is also included in the EFY-CD. It makes the LEDs connected to Port B of the MCU light up in a sequential manner back and forth.

Example 3: LCD interfacingThe LCD model described here is based on the HD44780 microcontroller from Hitachi and shows messages in two lines of 16 characters each. It dis-plays all English alphabets, Greek let-ters, punctuation marks, mathematical symbols, etc. Every character consists of a 5×8 or 5×10 dot matrix. In our project, we have used a 5×8 character display, which is, in fact, the most com-monly used.

Preset VR2 is used for contrast control. If there is no character display on the LCD, vary the preset to adjust the contrast. A 12-pin female header is used on the LCD module to connect the development board. On the board, a female header strip is used for LCD connection. Refer to the datasheet of the LCD module for different functions

Fig. 11: Prototype of atmEga32 development board

2.07C) is a serial device programmer software with a user-friendly GUI framework available for Windows 95, 98, 2000, NT, XP and Intel Linux. It supports I²C Bus, Microwire, SPI EEPROM, the Atmel AVR and Micro-chip PIC micro. For details, visit www.lancos.com/e2p/ponyprog2000.html

Example 1: Blinking LEDsInclude the following header files in the code to successfully compile the blinking-LED application program (blink_LED.c):

1. <avr/io.h> to include appropri-ate I/O definitions for the MCU

2. <avr/interrupt.h> to enable all the interrupt-handling functions (not used here).

3. <util/delay.h> to generate time delays

Also include a user-defined func-tion (WaitMs( )) to generate time delays in milliseconds. The parameter to be passed is an unsigned integer value indicating the time delay to be produced in milliseconds.

In the main function, first configure Port B pins as output pins. For this, set the corresponding bits in the DDRB register. After that, load 0xFF in the

Page 6: Atmega32 Dev Board--EFY March11.pdf

cONSTRUcTION

electronics for you • Mar ch 2011 • 125w w w . e f y M a g . c o M

of the pins.LCD memory. The LCD contains

three memory blocks: DDRAM (dis-play data RAM), CGROM (character generator ROM) and CGRAM (charac-ter generator RAM).

DDRAM. DDRAM is a sort of RAM where data can be written to and read from, but loses its contents when the power is removed. It is used to store the characters (up to 80) that need to be displayed (see Fig. 12).

If the cursor is on, it is displayed at the location that is currently addressed. When a character appears at the cursor position, it automatically moves to the next addressed location.

CGROM. It contains the default character map with all the charac-ters that can be displayed on the screen. Each character is assigned to one memory location. Addresses of CGROM locations match the characters of ASCII. For example, if the program currently being executed encounters a command “send character P to port,” binary value 0101 0000 appears on the port. This value is ASCII equivalent of character P which will be displayed on the LCD. This applies to all the letters of the alphabet (capital and small), but not to the numbers. In order to display the numbers correctly, it is necessary to add decimal number 48 to each of the values prior to sending them to the LCD.

For details of the LCD memory, refer to the datasheet of HD44780 con-

troller included in the EFY-CD.LCD connections. Depend-

ing on the number of lines used for connecting an LCD to the microcontroller, there are 8-bit and 4-bit modes of LCD connec-tion. The appropriate mode is selected at the beginning of the

operation in a process called ‘initialisa-tion.’ 8-bit LCD mode uses D0 through D7 output lines to transfer the data.

In 4-bit mode, only four higher bits (D4 through D7) are used for com-munication, while others may be left unused. Each data is sent to the LCD in two steps: four higher bits are sent first (normally via D4 through D7 lines), followed by four lower bits. Ini-tialisation enables the LCD to link and interpret the received bits correctly. In our development board, we have used 4-bit mode of LCD connection. The main purpose of 4-bit LCD mode is to save the valuable I/O pins of the microcontroller.

LCD initialisation. The content of the LCD memory is automatically cleared when it is powered up. It takes approximately 15 ms to clear the memory data. After that, the display is ready to operate.

Usually, automatic reset is done without any problem. If for any reason the power supply voltage does not reach its full value within 10 ms, the display starts performing completely unpredictably.

The LCD is initialised through software instructions. Typically, driver files for the LCD module consist of the lcd.h header file and the accompany-ing lcd.c source file. Once you include driver files for the LCD module, all that you need to know in order to operate the module are a few user-defined functions as given below:

extern void lcd_init(uint8_t

dispAttr);

Initialize display and select type

of cursor.

extern void lcd_clrscr(void);

Clear display and set cursor to home

position.

extern void lcd_home(void);

Set cursor to home position.

extern void lcd_gotoxy(uint8_t x,

uint8_t y);

Set cursor to specified position.

extern void lcd_putc(char c);

Display character at current cursor

position.

extern void lcd_puts(const char *s);

Display string without auto linefeed.

extern void lcd_puts_p(const char

*progmem_s);

Display string from program memory

without auto linefeed.

extern void lcd_showvalue(unsigned

char num);

Displays the decimal 3digit value

of num.

extern void lcd_command(uint8_t cmd);

Send LCD controller instruction

command.

extern void lcd_data(uint8_t data);

Send data byte to LCD controller.

Declarations and user-defined functions are included in lcd.h and lcd.c driver files. We have included a small sample project file named ‘mov-ing_display.c’ to explain the working of these functions. The program prints a moving text message on the LCD module. To do this, it reads the mes-sage from the flash memory of the MCU and scrolls it on the LCD panel continuously.

First of all, you need to include header files in the program as follows: #include <avr/io.h>

#include <avr/pgmspace.h>

#include <util/delay.h>

#include “lcd.h”

Here, pgmspace.h is an AVR-spe-cific header file to incorporate program space string utilities in the program. For details of different functions avail-able in a particular header file, refer to the avr-libc ‘Reference’ manual (in-cluded in the EFY-CD). As mentioned

Fig. 12: a typical 2×16 LCD module

Fig. 13: DDRam

Page 7: Atmega32 Dev Board--EFY March11.pdf

cONSTRUcTION

126 • Mar c h 2011 • electronics for you w w w . e f y M a g . c o M

earlier, lcd.h is the driver header file for the LCD module. Add the lcd.c external C source file along with the moving_display.c application source file under ‘Source Files’ directory in the AVR Studio platform.

After including the required header files, initialise your LCD module in the main function with the required display attributes using the lcd_init (uint8_t dispAttr) function. The avail-able display attributes are: LCD_DISP_OFF (for LCD off), LCD_DISP_ON (display on, cursor off), LCD_DISP_ON_CURSOR (display on, cursor on) and LCD_DISP_ON_CURSOR_BLINK (display on, cursor on flashing).

In our program (moving_display.c), we have used the LCD_DISP_ON attribute to initialise the display. After initialising the display, clear it using the lcd_clrscr (void) function. In order to point the cursor at a desired location in the display, use the lcd_gotoxy (uint8_t x, uint8_t y) function. Here ‘x’ and ‘y’ denote the coordinates. For example, if you need to point the cursor to fifth position in the first row of the LCD module, the corresponding ‘x’ and ‘y’ coordinates would be 0 and 4, respectively. The lcd_putc (char c) function is used to print a single character at the current cursor location. Similarly, the lcd_puts (const char *s) function prints a string pointed to by ‘s’ from the current cur-sor location.

After successfully burning the hex code into the microcontroller, the message “ELECTRONICS FOR

YOU—Asia’s Largest Selling Electron-ics Magazine” can be seen scrolling continuously on the LCD module.

Example 4: Analogue-to-digital converter moduleThe adc_test.c source code is provided to test the working of the analogue-to-digital converter (ADC) module in ATmega32 MCU. The code measures the analogue voltage applied at the analogue input pin (PA7) of ATmega32 and prints the voltage value on the LCD module, correct up to three deci-mal places.

To test the code, first close switch S6 in the board so that preset VR1 is connected to the MCU. VR1 feeds an analogue voltage between 0V and 4.96V to pin 33 (PA7) of the MCU.

The algorithm used in the source code is as follows: first, take 256 sam-ples of the analogue voltage applied at input pin 33 (PA7) and convert them into corresponding digital ADC values using the inbuilt ADC module. Then take the average of these 256 values to obtain a more accurate value. The average ADC value obtained is used to measure the input voltage as follows:

ADC = VIN ×1024

VREF The entire cycle is repeated with

the help of the ADC interrupt routine. The ADC interrupt routine is executed each time the analogue-to-digital con-version completes. In order to enable the ADC interrupt routine, set ‘ADC Interrupt Enable’ bit in ADCSRA reg-ister and ‘Global Interrupt Enable’ bit

in SREG register. The interrupt routine is defined in the source code using ISR ( ) macro defined in the interrupt.h header file.

ADC operation. The ADC converts an analogue input voltage into a 10-bit digital value through successive ap-proximation. The minimum value rep-resents GND and the maximum value represents the voltage on AREF pin minus 1 LSB. Optionally, AVCC or an internal 2.56V reference voltage may be connected to AREF pin by writing to the REFSn bits in ADMUX register. Refer to the datasheet for further de-tails and operations.

Example 5: Timer moduleThe 8bit_timer.c source code is provid-ed to test the timer/counter0 module. It lights up the LEDs connected to Port B of the MCU in a sequential pattern to and fro. A program (blink_LED.c) to light up the LEDs has already been dis-cussed. The program uses ‘while’ and ‘for’ loop combinations to produce the sequential pattern. In the 8bit_timer.c code, use the timer/counter0 module to do the same task.

Here the timer/counter0 module is made to operate in ‘clear timer on com-pare’ mode. Clock source for the coun-ter is the MCU frequency prescaled by a factor of 64. The comparator match interrupt routine is called every time the counter reaches the count of 250. That is, with every 250th count, 1 ms elapses.

To be concluded next month...