Vous êtes sur la page 1sur 7

CONSTRUCTION

PART 1

TUTORIAL ON ATMEGA32 DEVELOPMENT BOARD

RIS S.V.N. HA

H K.

SAGAR G. YADAV

icrocontrollers find applications in various industrial and domestic controls and processes. Therefore a good knowledge about them has become a necessity for elctronics engineers. Although a lot of theoritical knowledge related to the software and hardware aspects of microcontrollers is available in the market, there is no easy alternative to a hands-on learning experience, and for that, a microcontroller development 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, Moving_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 development 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 development issues.

Fig. 1: Circuit of ATmega32 development board W W W. E F Y m AG . CO m

1 2 0 M A R C H 2 0 1 1 E L E C T RO N I C S F O R YO U

ConstruCtion
The ATmega32 is a high-performance, low-power, 8-bit AVR microcontroller. It has 32 kB of in-system self-programmable flash, 2 kB of static random access memory (SRAM) and 1 kB of electrically erasable programmable read-only memory (EEPROM). Some of the on-chip peripheral features 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-todigital 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 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 externally, two switches on external interrupt 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 debugging purpose 8. On-board two-wire serial EEPROM of 4 kB (512x8) in addition to 1 kB of on-chip EEPROM 9. All the MCU ports are easily 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 variable voltage source using a preset, provided via a switch to PA7 for ADC test

The development board

Fig. 2: Power supply circuit for

This ATmega32 development board has the following features: 1. In-system programmable with an on-board serial-port-based programmer 2. Onboard regulated and filtered power supply with power indicator LED 3. Eight LEDs, which are selectable ATmega32 development board through DIP

Circuit description
Fig. 1 shows the circuit of ATmega32 development board. At the heart of the circuit is the 40-pin ATmega32 MCU, which is connected to the board through a 40pin 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
e l e c t ro n i c s f o r yo u M A R C H 2 0 1 1 1 2 1

Fig. 3: An actual-size, single-side PCB for the ATmega32 development board W W W. E F Y m AG . CO m

CONSTRUCTION
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 disconnected from the circuit at any point of time by removing shorting jumper JS. In order to further enhance the user interface of the development board, a dedicated LCD port is provided onboard. 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 provided later in the article. Port C is used to interface the LCD in the development 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. functional 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). Applying this kind of voltage to the AVR is dangerous, so MAXIMs 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
W W W. E F Y m AG . CO m

Fig. 4: Component layout for the PCB

Fig. 5: AVR Studio output dialogue box

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 selfcontained regulated +5V supply circuit built around the 7805 voltage regulator and filtering capacitors to obtain a filtered, regulated power supply. A power-indicator LED is used to indicate the presence of power supply in the circuit. The power supply circuit

tions. Five tactile pushbutton switches are also provided for debugging purpose. All the five switches are activelow 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

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-limiting resistors. When not in use, the LEDs can be disconnected from the circuit individually with the help of DIP switches, thus enabling the normal operation of Port B for other applica-

1 2 2 M A R C H 2 0 1 1 E L E C T RO N I C S F O R YO U

ConstruCtion
serial programming facility available in the MCU. Popular Open Source software PonyProg is used for easy programming 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 welldefined hardware circuit for SPI programming. 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 voltages 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 diagram) for a stable operation. The value chosen for these capacitors is 22 pF for a frequency of 16 MHz. In order to test the MCUs 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 application 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 available in ATmega32. For this purpose, connect SDA (Serial Data) and SCL (Serial Clock Input) pins of the EEPROM to corresponding SDA and SCL pins of the MCU via a DPDT switch. Address 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 provided 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 passive components and then the bigger components.

Fig. 6: Debug platform and device window

Software program
Before loading various application machine codes into the microcontroller, you need to write the intended controller program in C language. Compile it to obtain the hex code. Burn the generated 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 language and generate the hex code. It can also be used to simulate the program running on the microcontroller. PonyProg. PonyProg (Version

Fig. 7: I/O port setup in PonyProg

Fig. 8: Device selection dialogue box

Fig. 9: Security and configuration bits setting window W W W. E F Y m AG . CO m

e l e c t ro n i c s f o r yo u M A R C H 2 0 1 1 1 2 3

CONSTRUCTION
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 Fig. 10: Error message 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 program 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. Fig. 11: Prototype of ATMeGA32 development board 3. Select AVR simulator in Debug platform window and Atmega32 in Device window (refer 2.07C) is a serial device programmer Fig. 6). software with a user-friendly GUI 4. Start writing your first program framework available for Windows blink_LED.c in the AVR Studio editor 95, 98, 2000, NT, XP and Intel Linux. window. Configure the required opIt supports IC Bus, Microwire, SPI tions in Tools and Project menus. EEPROM, the Atmel AVR and Micro5. Go to Build menu and click chip PIC micro. For details, visit www. Build option to compile the code. lancos.com/e2p/ponyprog2000.html If there is no error, the hex code will Example 1: Blinking LEDs generate. 6. Connect the ATmega32 developInclude the following header files in ment board to the serial port of the PC the code to successfully compile the through the serial cable. On the board, blinking-LED application program set switch S7 to its program position. (blink_LED.c): Switch on the power supply. Run 1. <avr/io.h> to include appropriPonyProg and select Interface Setup ate I/O definitions for the MCU option from Setup menu. 2. <avr/interrupt.h> to enable all 7. Set the I/O port as shown in the interrupt-handling functions (not Fig. 7. used here). Click Probe button to check 3. <util/delay.h> to generate time whether you have correctly connected delays the development board to the PC. A Also include a user-defined funcTest OK message should appear. It tion (WaitMs( )) to generate time will ensure that you have correctly delays in milliseconds. The parameter connected the serial cable to the board, to be passed is an unsigned integer the power supply is turned on, switch value indicating the time delay to be S7 is turned to its program position produced in milliseconds. and you have chosen the correct COM In the main function, first configure port. Port B pins as output pins. For this, set 8. Select AVR Micro and ATmega32 the corresponding bits in the DDRB options from Device Selection menu register. After that, load 0xFF in the
1 2 4 M A R C H 2 0 1 1 E L E C T RO N I C S F O R YO U

in the toolbar (see Fig. 8). 9. Click Security and Configuration Bits (see Fig. 9). Click Read button 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 effect


The 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 interfacing


The LCD model described here is based on the HD44780 microcontroller from Hitachi and shows messages in two lines of 16 characters each. It displays all English alphabets, Greek letters, punctuation marks, mathematical symbols, etc. Every character consists of a 58 or 510 dot matrix. In our project, we have used a 58 character display, which is, in fact, the most commonly 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
W W W. E F Y m AG . CO m

ConstruCtion
troller included in the EFY-CD. LCD connections. Depending on the number of lines used for connecting an LCD to the microcontroller, there are 8-bit and 4-bit modes of LCD connection. The appropriate mode is selected at the beginning of the
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 Fig. 13: DDRAM 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.

Fig. 12: A typical 216 LCD module

of the pins. LCD memory. The LCD contains three memory blocks: DDRAM (display data RAM), CGROM (character generator ROM) and CGRAM (character 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 characters 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 conW W W. E F Y m AG . CO m

operation in a process called initialisation. 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 communication, 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. Initialisation 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 accompanying 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 userdefined functions as given below:

Declarations and user-defined functions are included in lcd.h and lcd.c driver files. We have included a small sample project file named moving_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 message 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-specific header file to incorporate program space string utilities in the program. For details of different functions available in a particular header file, refer to the avr-libc Reference manual (included in the EFY-CD). As mentioned

e l e c t ro n i c s f o r yo u M A R C H 2 0 1 1 1 2 5

CONSTRUCTION
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 available 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 cursor location. After successfully burning the hex code into the microcontroller, the message ELECTRONICS FOR YOUAsias Largest Selling Electronics Magazine can be seen scrolling continuously on the LCD module. 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 approximation. The minimum value represents 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 details and operations.

Example 4: Analogue-todigital converter module


The adc_test.c source code is provided to test the working of the analogueto-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 decimal 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 samples 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

Example 5: Timer module


The 8bit_timer.c source code is provided 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 discussed. 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 compare mode. Clock source for the counter 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...

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 conversion completes. In order to enable the ADC interrupt routine, set ADC Interrupt Enable bit in ADCSRA register and Global Interrupt Enable bit

1 2 6 M A R C H 2 0 1 1 E L E C T RO N I C S F O R YO U

W W W. E F Y m AG . CO m