Why won't this work?
2004-09-30 by alanganes
Hi all, I'm just a beginner with all of this, so if my code looks sloppy, please go easy on me! I wrote this to make a bar graph type display using an ATtiny15. The idea is to use the ADC to read a dc voltage on pin 5, and compare that (8 bit) value to the expected values for 1V,2V,3V,4V, and 5V, and turn on LEDs (one for eavh voltage level) attached on the remaining portB pins. It compiles OK, but when I try it out it dosen't quite work. as the input voltage rises, no LEDs come on until about 3.2V than the first 3 come on at the same time. As the voltage rises more, the 4V LED comes on at a bit over 4 volts, and the 5V LED comes on at about 5V. As I lower the voltage, the LEDs go off at a bit above the expected voltages, then the 1V LED goes out at about 1.4 V. I am obviously missing something. Any ideas?? Thanks in advance, AL ******CODE FOLLOWS****** AVR LED bargraph program .include "tn15def.inc" ;includes ATtiny15 definition file .def temp = r16 ;define register 16 as TEMP .def ADCRESULT = r17 ;define register 17 to hold ADC value .equ volt1 = 0x33 ;1 volt threshold value(51 bits/volt) .equ volt2 = 0x66 ;2 volt threshold value .equ volt3 = 0x99 ;3 volt threshold value .equ volt4 = 0xCC ;4 volt threshold value .equ volt5 = 0xFF ;5 volt threshold value .org 0x0000 rjmp RESET ;jump to reset over vector space RESET: ldi temp, 0x1F ;Value to set port B pins0,1,2,3,4 out DDRB, temp ;to be outputs, 5 to be ADC input ldi temp, 0x1F ;value to turn LEDs on port B pins 0-4 off. out PORTB, temp ;load value to port B ldi temp, 0x20 ;Value to set ADC to: Vcc reference, out ADMUX, temp ;left adjust result,input to PB5 (AD0) ldi temp, 0x83 ;Value to set up ADC: set to single mode, out ADCSR, temp ;enable ADC, set ADC prescaler to clk/8, ;disable ADC interrupt ldi temp, 0x40 ;value to set the PUD bit in the MCUCR to disable out MCUCR, temp ;pullup resistors on i/o pins, and loadt to MCUCR rjmp MAIN ;jump to main program MAIN: sbi ADCSR,ADSC ;set flag to start AD conversion LOOP: sbis ADCSR,ADIF ;test if conversion is done. if done ;skip next line and continue, else loop ;back and test again rjmp LOOP ;repeat until conversion is done cbi ADCSR,ADIF ;clear "conversion complete" flag in ADCRESULT, ADCH ;load ADC result to register ldi temp, 0x1F ;value to turn LEDs on port B pins 0-4 off. out PORTB, temp ;load value to port B cpi ADCRESULT,volt5 ;check if ADC reading is 5 volt or more brsh VOUT5 ;if 5 volts or greater, jump to subroutine to ;turn on LEDs on port B, pins 0-4 cpi ADCRESULT,volt4 ;check if 4 volts or more, if so, turn on LED brsh VOUT4 ;port B pins 0-3 cpi ADCRESULT,volt3 ;check if 3 volts or more, if so, turn on LED brsh VOUT3 ;on port B pins 0-2 cpi ADCRESULT,volt2 ;check if 2 volts or greater, if so, turn on LED brsh VOUT2 ;on port B pins 0-1 cpi ADCRESULT,volt1 ;check if 1 volt or greater, if so, turn on LED brsh VOUT1 ;on port B pin 0 ldi temp,0x1F ;all above tests are false, turn off all LEDS rjmp MAIN ;return for another sample VOUT5: ldi temp, 0x00 ;value to turn on all LEDs out PORTB, temp ;turn on LEDs rjmp MAIN ;return for another sample VOUT4: ldi temp, 0x10 ;value to turn on LEDs on port B 0-3 out PORTB, temp ;turn on LEDs rjmp MAIN ;return for another sample VOUT3: ldi temp, 0x18 ;value to turn on LEDs on port B 0-2 out PORTB, temp ;turn on LEDs rjmp MAIN ;return for another sample VOUT2: ldi temp, 0x1C ;value to turn on LEDs on port B 0-1 out PORTB, temp ;turn on LEDs rjmp MAIN ;return for another sample VOUT1: ldi temp, 0x1E ;value to turn on LEDs on port B 0 out PORTB, temp ;turn on LEDs rjmp MAIN ;return for another sample