The AVR 8535 microcontroller and its new version ATmega8535 are versatile, high-performance but low-cost chips. This article series covers typical applications of this processor illustrating its power and cost-effectiveness in an embedded system.
The AVR family comprises several chips, all with almost the same instruction set. Of them, the 90S8515, 90S8535 and ATmega8535 chips are low-cost and readily available with the complete set of port pins. The ATmega8535-16 is more powerful and available for around Rs 250. Capable of running at 16 MHz and achieving almost 16 million instructions per second (MIPS), it is one of the fastest devices available in the market today.
Using ATmega8535, you can build a microcontroller-based project with following features:
1. Four ports, of which one of them has eight analogue-to-digital converter (ADC) channels
2. ADC conversion time is as little as 60 microseconds. Imagine adding an external ADC to 8051 or any other microcontroller chip—that would have taken the cost to over four digits. And mind you, it is a 10-bit ADC, not just 8-bit.
3. If an 8MHz crystal is connected, each instruction executes in 1/8th of a microsecond. The 89C51 at 12MHz clock had its internal division by twelve, so it ran at just one microsecond. Thus, ATmega8535 chip is eight times faster with an 8MHz crystal. However, you can also use a higher-frequency crystal. The chip is basically a RISC processor that executes most instructions in one clock cycle itself.
4. The chip has RS-232 transmit and receive terminals much like the 8051 family, but it can support even higher baud rates.
5. It has quite a few internal registers, RAM, EEPROM and CODE memory (flash memory in excess of 4kB).
6. The instruction set is versatile, complete with several arithmetic, logic and transfer instructions and related jump instructions, etc.
7. An analogue comparator pin, which can compare an external analogue voltage and take control action.
8. Reset is possible through the software, and a watchdog is provided. Power-down or sleep modes are available.
9. An additional serial interface, known as the SPI bus, with three wires: data (2) and clock (1). These pins can be used for programming or loading the code from a PC through the printer port or serial port. For programming the internal flash memory locations, just 5V supply is enough.
10. Two PWM output pins, which are useful for power control applications.
11. Several timers as in other members of the 8051 family, but with much better time resolution.
12. Additional features like input capture and output compare.
Here, we shall delve into the chip’s operations with typical programs and circuits. All the development tools including ‘C’ compiler are available for free from the Internet.
The features of ATmega8535 make it the right candidate for various embedded control applications. Even a digital filter can be implemented on the device, provided you are fully conversant with its hardware and software features. You can download the databook of ATmega8535 from the ‘ATMEL.com’ Website to understand its features and work out simple applications.
The sample programs given here can be used to yield a powerful controller for many applications like a filter or motor controller.
Programming the chip
The AVR source code file with ‘.asm’ extension can be written using either the EDIT, Wordpad or notebook programs.
As with all microprocessor or microcontroller programs, for the source code, one has to enter the program by mnemonics and assembler directives and then convert the same into a code list for the program. (Directives are assembler commands used to control the input, output and data allocation of the assembler. These are, however, not translated into op-codes directly.) This is done using the cross-assembler software ‘avrasm.exe.’
To describe the modus of writing of an Assembly language program, a simple program (LED.ASM) for AVR processors is given below:
This program helps you understand:
1. Access to the output port (here port B, where LEDs are connected)
2. The different parts of a typical assembler program
3. Different conventions like use of semicolon, upper-/lower-case letters, etc
Explanatory notes for LED.ASM
1. In Assembly language, all the text on a line after a semicolon (;) is treated by the cross-assembler as comments and it does not use it for code formation.
2. Including the m8535def.inc processor-specific file in Assembly program means all the I/O register names, I/O register bit names, etc appearing in the datasheet can be used. Failure to include this file may result in a number of error messages. Ensure that this file is placed in the same directory as your source code file (LED.asm in this case). Else, give complete path for the m8535def.inc file.
3. Following conventions havebeen used in the program:
(a) Words in upper-case letters are used for command directive words of the Assembly language or predefined ports of the processor.
(b) Mnemonic words are written in lower case.
4. LIST directive turns on the listing output if it had been previously turned off. Similarly, NOLIST directive, if used, will turn off the listing output.
5. DEF directive is used to define a text-substitution label for a string. A label/name is easy to remember. Here, register R16 is replaced with ‘mp’ name. Thus whenever ‘mp’ is encountered in the source code, it will be automatically replaced with ‘R16.’
6. ‘.org $0000’ defines the reset address. When power is swit
ched on, the program starts from this location. A restart from the reset address can be activated by resetting the respective hardware pin of the chip (pin 9) or upon watchdog timer reaching its zero count. A relative jump command (rjump) at this reset location directs the program execution to label (main)—as long as the label is within 2k locations from the reset address (0000). Incidentally, ‘rjmp main’ is the first code-generating instruction.
7. It is essential to set up the stack pointer before being able to call any subroutine, since stack is required for saving the return address, where the next program execution is to start from. The program lines starting with ‘ldi R16,low(RAMEND)’ and ending with ‘out SPH, R16’ do just that.
8. The ‘ldi mp, 0b11111111’ and ‘out DDRB, mp’ lines set port-B pins as the output. The first line, interpreted as ‘load immediate (ldi) into register ‘mp’’, loads binary value ‘11111111’ into the ‘mp’ register. The second line transfers the contents of ‘mp’ (11111111) to the data direction register of port B (DDRB). DDRB is already defined in the m8535def.inc file. (If you want to set port-B pins as input, load binary ‘00000000’ into ‘mp’ and output it to DDRB.) Incidentally, ‘0b’ precedes a binary number. Similarly ‘0x’ precedes a hex number. Numbers without these prefixes denote decimal numbers by default. Hence you may replace ‘0b11111111’ with either ‘0xFF’ or simply ‘255’ to achieve the same results.
9. The rest of the program starting at label ‘loop:’ and ending with ‘rjmp loop’ achieves switching on and off of the LEDs with a delay. The delay subroutine starting at label ‘delay:’ and ending with return instruction ‘ret’ is called from within the loop.
Initially, ‘mp’ is loaded with hex value ‘00’ and output through port-B pins, making them low. Since the cathodes of all the eight LEDs are connected to these port pins via current-limiting resistors, the LEDs light up. Thereafter, the delay subroutine (Rcall delay) is called and ‘mp’ is loaded with hex value ‘FF’ and transferred to the port-B output to turn off the LEDs. The loop is repeated as long as the power is switched on.
10. The internal R-C clock of ATmega8535 is 1 MHz by default. In the absence of ‘Rcall delay’ instruction, each of ‘ldi’ and ‘out’ instructions requires 1000 ns, while ‘rjmp’ instruction requires 2000 ns. Thus loop execution would take 4000 ns. This amounts to LED switching rate of 250 kHz.
Introduction of delay between switching on and off reduces this frequency to around 0.5 Hz by decrementing registers ‘r19’ and ‘r17’ from ‘255’ to ‘0,’ thereby making the elapsed time slower by 256×256 (which works out to around 0.5Hz rate).
After assembling the LED.asm source file, the program will have eight words. The LED.LST file stores the result of the assembly process in the form of a listing.
Once a program has been written using any editor, wordpad or notepad, it is assembled using the avrasm.exe AVR assembler, which is included in this month’s EFY-CD. Of course, the AVRSTUDIO 4.0 integrated development environment (IDE) is more versatile and user-friendly software for development, but the avrasm.exe assembler is simpler and direct.
Simply typing ‘avrasm -i LED.asm LED.lst LED.hex’ under the DOS prompt makes the cross-assembler generate code for the LED.hex file and also provide a text file giving both the code and the program together in LED.lst. Thus, you get the LED.lst listing file and the LED.hex Intel hex code file.
Download PCB and component layout PDFs: click here
Alternatively, you can prepare a batch file as follows:
Upon DOS prompt, enter ‘copy con avr.bat.’ In the following line, type ‘Avrasm -i %1.asm %1.lst %1.hex.’ Pressing ‘F6’ key in the following line displays ‘Control-Z.’ Now pressing the ‘Enter’ key displays “1 file copied.”
Now the avr.bat file has been prepared. This simple batch file is invoked to assemble this (or any) program by typing ‘Avr LED’ upon the DOS prompt and pressing the ‘Enter’ key.
This assembles the program, and forms both the list file (that contains the code-cum-Assembly listing) and the hex file (the actual Intel-format hex file for use by the programmer).
Likewise, any other assembly program ‘xxx.asm’ can be coded into the hex file by simply typing ‘avr xxx’ on DOS prompt. ‘xxx’ denotes the name of the program. The ‘.asm’ is not to be typed.
In our LED.asm program, we have included the m8535def.inc file. This file is required along with the avrasm.exe cross-assembler. For other AVR processors like 90S8515, 90S8535 and at-Tiny 26, the files to be included are 8515def.inc, 8535def.inc and tn26def.inc, respectively.
The next task is to burn the code into the chip. Note that a chip previously programmed or erased is automatically erased when a new program is burnt into it using the device programmer as described below.
The AVR device programmer
The AT-PROG programmer software is used for programming ATmega8535. This menu-drive programming software is simple to use and invoked from command prompt.
The software uses a simple pod connected to the printer port of a computer. The circuit of the pod (shown in Fig. 3) is very simple. It just connects the IC to be programmed to the pins of the PC’s printer port.
This circuit is assembled on a small PCB with a D25 male-female plug at one end. The IC base is a 40-pin zero-insertion-force socket (ZIF). This enables easy insertion and removal of the IC to be programmed.
The AT_PROG.exe is a simple programming software that can be run under DOS prompt by typing AT-PROG. The files At-prog-hlp.htm, At-prog.exe, At-prog.cfg and At-prog.ini should be placed in one directory before running the AT-PROG. These files have been included in this month’s EFY-CD as part of this article.
The menu-driven window of the AT-PROG programmer has the following menu items:
1. File menu. This menu is used to select or open the LED.hex file, or whatever, which is to be programmed into the device.
Pull down the menu by clicking it. Under ‘Open’ option, enter the file name as ‘LED.hex’ and press ‘Enter.’ The IC to be programmed is selected from the AVR-Programmer window by clicking the edge of the small rectangular window and choosing the IC as shown in Fig. 4. Now connect the printer-port connector to the programming pod, whose circuit is shown in Fig. 3.
2. Write menu. On clicking the ‘Write’ menu, the ‘Activity’ window at the bottom whitens and shows ‘Connecting’ (refer Fig. 5). Then, the data is transferred to the IC and verified after programming, showing ‘ok’ in the same window.
3. Check menu. This menu is used to find out whether the IC is inserted in the socket and whether the connector connections are okay. It will indicate an error if the IC is not there or not responding.
In this mode of programming, the serial-peripheral interface (SPI) of the AVR chip is used. This interface has three wire connections:
(i) Master output and slave input (MOSI)
(ii) Master input and slave output (MISO)
(iii) Serial clock (SCLK)
Using these wires, the SPI interface does the serial transfer of data (i.e., our program codes) into the chip, which is configured as a slave. The data and clock are connected via MOSI and SCLK pins of the chip, respectively. Upon reception of each byte, the chip acknowledges it by sending a byte (53hex).
In ‘Check’ mode, the IC is enquired about its name by the computer (Master), which it replies with its signature code embedded in the chip memory by the manufacturer. Each IC has its specific signature code. Thus, by noting the code itself, what IC is being programmed will be known to the computer. So the small window under the device-select rectangular window can be clicked to show ‘autodetect’ the IC.
4. Options menu. In this menu, the speed of the clock used for transferring data from the computer can be selected as ‘slow,’ ‘normal’ or ‘fast.’ With present high-speed PCs, choose ‘normal’ or ‘slow.’ In the same menu, the ‘read signature bytes’ option is to be enabled and it is so by default.
5. Port menu. The port menu, which is next to the file menu, is useful if a different printer port is available. The program automatically selects the available printer port.
When the ‘Activity’ window shows ‘ok’ after clicking the ‘Write’ menu, remove the programmed chip from the programmer circuit board and fix it onto the target circuit for the LED.asm program (shown in Fig. 2). Now apply 5V and press the switch connected to Reset pin, if needed. (The circuit resets at power-on.) The LEDs start blinking fast and the waveform can be observed on the CRO for any of the pins at the output to the LEDs. It will be around 600 Hz.
Message display on the LCD module
Method I. Given below is the source code for message display on the LCD module along with suitable comments wherever needed.
This program displays ‘Electronics ForU’ on the LCD module (Fig. 6). The message may be displayed on the LCD in a single or two rows depending on the LCD module. In some LCD modules, the first eight characters are written consecutively, while for display of the next eight characters, the program needs to restart the cursor at address $C0. But Hitachi-make single-row types do not need to restart the cursor’s address after the eighth entry; the characters can be written consecutively up to ‘16,’ i.e., in a single row.
The program is named as ‘LCD_CHAR.asm’ and assembled into the ‘.hex’ file by typing ‘avr lcd_char’ and invoking the cross-assembler AVR. Now the lcd_char.hex file is generated. The AT-PROG programmer burns this code into the flash memory of the ATmega8535.
Note that while assembling this program using ‘avr lcd_char’ command, the definition file for IC ATmega8535 (m8535def.inc) should be in the same directory.
Method II. This message display program uses look-up table. In the message display program described in Method I, ‘Call lcdwr’ instruction was written for each character. Here, instead, if we enter all the bytes for ‘Electronics ForU’ in a table, they can be picked up one by one until the end and shown on the LCD screen. For the purpose, there is an instruction called load program memory (LPM).
The table, as also the name, is stored in the program memory. Here is the program along with necessary comments.
The actual-size PCB for programming and LCD message display are given in Figs 7 and 9, while their component layouts are shown in Figs 8 and 10, respectively.