Close

Very primitive voltage indicator by Attiny13

A project log for Minimalist A-go-go

A project inside Attiny. I will not step out for larger one till I complete it.

kodera2tkodera2t 04/03/2016 at 07:110 Comments

I am still in wonderful Attiny world, where I have learned lots of fundamentals of computer itself. This time I made a very simple and primitive voltage indicator by LEDs utilising A/D converter of ATtiny13.

As it is, ATtiny is 8-pin MCU and VCC, GND and RST ( I am still beginner, and cannot hand it away) total 3-pins are already occupied. So I used the rest 5 pins as, PB3 (pin 2) for A/D input and LEDs are driven by 4-pins by PB0, 1, 2, and 4. I am enjoying assembly language, since I can see and understand very fundamentals and the code is written by AVR assembly as follows.

;
; webADCattiny13.asm
;
; Created: 4/1/2016 12:52:26 PM
; Author : kodera2t
;
.def r_temp1 = R16
.def r_temp2 = R17
.def r_temp3 = R18

.equ lv_1 = 0x2b ; values for voltage comparison
.equ lv_2 = 0x56
.equ lv_3 = 0x81
.equ lv_4 = 0xac
.equ lv_5 = 0xd7
    
setup:                
ldi r_temp1, ((1<<ADEN)+(1<<ADSC)+(1<<ADATE)+(1<<ADIE)+(0<<ADIF)+(1<<ADPS2)+(1<<ADPS1)+(1<<ADPS2))
out ADCSRA,r_temp1 ;starting A/D conversion 
                     ;(free running mode)
;ldi r_temp1, ((0<<REFS0)+(0<<ADLAR)+(1<<MUX1)+(1<<MUX0))
;Vcc as analogue reference (currently commented out)
ldi r_temp1, ((1<<REFS0)+(0<<ADLAR)+(1<<MUX1)+(1<<MUX0))
;internal 1.1V as analogue reference
out ADMUX,r_temp1 ; setting PB3 for A/D input
ldi r_temp1, (1<<DDB4)+(0<<DDB3)+(1<<DDB2)+(1<<DDB1)+(1<<DDB0)
out DDRB, r_temp1 
; PB0, 1, 2, 4 for LED output (PB3 for input)

main: ; main loop start!
in r_temp1,ADCL ;reading lower bit of A/D result
in r_temp2,ADCH ;this program uses lower 8 bits only, therefore the comparing results will be periodical for input voltage variation

cpi r_temp1, lv_1 ;comparing measured result with lv_1=0x2b
brlo s_vchk_1 ; if the measured value is lower than 0x2b, jump to s_vchk_10

cpi r_temp1, lv_2 ;comparing measured result with lv_1=0x56
brlo s_vchk_2

cpi r_temp1, lv_3 ;comparing measured result with lv_1=0x81
brlo s_vchk_3

cpi r_temp1, lv_4 ;comparing measured result with lv_1=0xac
brlo s_vchk_4

cpi r_temp1, lv_5 ;comparing measured result with lv_1=0xd7
brlo s_vchk_5

rjmp main ; closing main loop

s_vchk_1:
ldi r_temp3, 0b10111 ;all LED are off (negative logic)
out PORTB, r_temp3
ret ; return to main loop

s_vchk_2:
ldi r_temp3, 0b10110 ;PB0 is on
out PORTB, r_temp3
ret ; return to main loop


s_vchk_3:
ldi r_temp3, 0b10100 ; PB0, 1 are on
out PORTB, r_temp3
ret ; return to main loop

s_vchk_4:
ldi r_temp3, 0b10000 ; PB0, 1, 2 are on
out PORTB, r_temp3
ret ; return to main loop
	
s_vchk_5:
ldi r_temp3, 0b00000; all LED are on
out PORTB, r_temp3
ret ; return to main loop	
The generated hex file occupies 231 bytes in flash. We still have enough huge room in ATtiny13 !!!!! Actual operation can be found in the following movie... have fun!

Discussions