Friday, April 19, 2024

Part 2 of 3: Using AVR Microcontroller for Projects

After the interrupt processing instructions, the various subroutines are entered. Then comes the main program. In the main program, the first thing to write is the stack initialisation instructions. Here, the stack pointer is set to the highest end of the internal RAM, for which a temporary register is used, and the high and low addresses are written to the stack pointer using an instruction at the Ext_Int0 vector address (0x001) such as:
ldi temp,low(RAMEND)
out spl,temp
ldi temp,high(RAMEND)
outsph,temp

Register operations. Each register is assigned a data memory address, mapping it directly into the first 32 locations of the data space. Register pairs R26-R27, R28-R29 and R30-R31 serve as 16-bit registers, which are used for indirect addressing of the data memory space. These three 16-bit registers are known as ‘X’ (R27:R26), ‘Y’ (R29:R28) and ‘Z’ (R31:R30) registers, respectively. The last 16 registers in the register file (R16 through R31) cannot be used with the first 16 registers (R0 through R15).

The operating instructions for registers have direct and single-cycle access to the registers. The following instructions—constant arithmetic instructions—use the second half of the registers in the register file and cannot be used with the first half:

sbci, subi, cpi, andi, ori and ldi

The following general instructions that use two registers or only a single register can use the entire register file:

- Advertisement -

Sbc, sub, cp and & or

Embedded control functions and their applications
Here we’ll use the following four functions of ATMega8535 for typical control applications:
1. Timers; two 8-bit and one 16-bit with add-on features
2. Pulse-width modulated output
3. Analogue-to-digital converter
4. Serial RS-232 interface

Timers and their applications. Both timer 0 and timer 1 are 8-bit timers, while timer 1 is a 16-bit timer. The clock inputs to the timers can have a variety of selections. The CPU clock itself, divided by a prescaling divider with divisors of 8, 64, 256 and 1024, can be chosen. Further, it can also count an externally applied clock at T1 pin (for timer 1).

- Advertisement -

We shall use these timers for developing a real-time clock with time display on the LCD (see Fig. 1). For the purpose, the registers to be used in timer 0 are:
1. TCCR0: Timer counter control register 0
2. TIMSK: Timer interrupt mask register

TCCR0 register bits. Fig. 13 shows the bit details for TCCR0 register. Bits WGM01, COM00, COM01, WGM00 and FOC0 (bits 3 through 7) of TCCRO register are used with the timer-based comparators for waveform and pulsewidth-modulated output generation. Since these bits are not required for the normal timing operation of the timer, they have not been used here.

The timer clock is selected by using the remaining three bits (CS00, CS01 and CS02). We will set these bits to ‘0,’ ‘1’ and ‘1,’ respectively, for dividing the 1MHz default internal clock of ATmega8535 k (with no external crystal) by ‘64.’ This division gives 65 microseconds per clock. Then we accumulate the counts for getting one second and divide it by ‘60’ to get minutes and again by ‘60’ to get hours, which are counted up to ‘12’ and the process is repeated.

TIMSK register bits. Fig. 14 shows the bit details for TIMSK register. Bit 0 refers to ‘timer-overflow interrupt enable.’ It must be set to enable the interrupt action on overflow. The TIMO_OVF interrupt ($0009 address) is used to direct a vector at this address to the respective interrupt service routine, where we will perform the relevant action that is needed upon timer-0 overflowing, i.e., when the number in its TCNT0 register (timer counter 0) crosses ‘255’ (decimal). So in the software program for real-time clock, we initialise TIMSK to ‘01.’

Software program for real-time clock (8535clk.asm). The 8535clk.asm program with suitable explanations and comments is given at the end of this article. The assembled hex file ‘8535clk.hex’ is included in this month’s EFY-CD, which can be straightaway programmed in the ATmega8535 IC. The programmed IC can be fixed to the RTC circuit board to show real-time clock on the LCD.

Download source code: click here

SHARE YOUR THOUGHTS & COMMENTS

Unique DIY Projects

Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components

Calculators