LED Debugger

This post is useful to them who write AVR applications in assembly language.

Using assembly language for AVR micro controllers needs some sort of hardware debugging. Assembly language programmers use registers to store variables. 32 such registers are available in AVR architecture.

While finding fault in a subroutine, the programmer has to check if certain variables store expected values at the specific time. This can be done using a hardware debugger.

Hardware debugger

This debugger can be used when four AVR port-pins are available. Those pins which are not used in the main application are chosen for debugging purpose.

These pins are connected to four LEDs (with current limiting resistors).
The value of the variable is shown on these four LEDs as a binary number, made up of two nibbles. Upper nibble shown for three seconds and then the lower nibble.
The “ledbg” subroutine is called where ever necessary, to copy the expected variable and then is displayed on LEDs, nibble by nibble.
This is like using a “break-point” in the programme.

ldi r16,0xc3 ;1100 0011b
out ddrd,r16
; sbi DDRD,0 ;portD0 as output LSB
; sbi DDRD,1 ;portD1 as output
; sbi DDRD,6 ;D6 output
; sbi DDRD,7 ;D7 output
;/* sbi DDRB,0 ;B0 output
; sbi DDRB,1 ;B1 output
; sbi DDRB,2 ;B2 output
; sbi DDRB,5 ;B5 (led in arduino) MSB of the led byte */
;/* following routine puts data on the led byte debugger. r18 contains data to be displayed */

ledbg: push r18
andi r18,0xf0 ;high nibble retained
swap r18
sbi portb,led
rcall bitout
rcall onesec
rcall onesec
pop r18
cbi portb,led
andi r18,0x0f ;low-nibble retained
rcall bitout
stop: rjmp stop

bitout: clr r19
bst r18,0
bld r19,0;
bst r18,1
bld r19,1;
bst r18,2
bld r19,6;
bst r18,3
bld r19,7;
out PORTD,r19 ;ls-nibble of led byte complete
rcall mili5 ;r24,r25,r17 involved
;;this debugger uses registers: r16,r18,r19,r24,r25,r27

Using LED debugger

To show a variable in register r0 say, we use the subroutine “ledbg” in the following way:

[code]mov r18,r0 ;copy the value of r0 to r18
rcall ledbg [/code]

Advantages of LED debugger

  1. Very less hardware resources are used.
  2. This is not a complicated subroutine. You can modify it easily.
  3. You can remove it in your final code very quickly.

You can download the ledbg here.

This entry was posted in SciTech. Bookmark the permalink.