Home - Search - Members
Full Version: ADC converter for atmega168 using assembly language
sammy mutua
May 11 2009, 10:36 AM
can somebody help me with a program code in assembly language for ADC conversion using atmega168 controller, i will appreciate, thank you. have already done it in c language but i want to do it in assembly language.

Ajay
May 12 2009, 4:49 AM
if you've done it in C then assembly wont be difficult. I just want you to go through chapter 21 of datasheet of ATmega168.

It has got all the information of registers you are going to set for ADC conversions.
Write something, will help you as you go on.

[Topic moved to AVR Discussion forum]

sammy mutua
May 12 2009, 5:28 AM
thank you very much Ajay for the reply, i am going to read the data sheet and mean while i have also found the following code for at tiny 45 adc conversion,
ad_init:
ldi rmp, 0b00000000
out ADMUX, rmp
ldi rmp, 0b11001111
out ADCSRA, rmp

ad_init_done:
ret

ad:
in tmp, ADCSRA
ldi rmp, 1<<ADSC
or tmp, rmp
out ADCSRA, tmp


ad_loop:
in rmp, ADCSRA
andi rmp, 1<<ADIF
ldi tmp, 1<<ADIF
cpse rmp, tmp
breq ad_loop

ad_done:
ret

will the code for atmega168 ressemble this one or what changes need to be adjusted in the code?

Ajay
May 12 2009, 10:22 AM
AVR has mostly same set of registers in use. you can find codes online but its always good to practice on your own, its one time investment that will help u for lifetime
sammy mutua
May 13 2009, 9:24 AM
dear Ajay,
Thank you very much for the reply, i have read the data sheet and using the example for attiny 45 i have followed up to enabling the conversion and indicated my understanding using the comments as follows.



ad_init:
ldi rmp, 0b00000000 ;selecting the reference voltage, the right adjustment of the
out ADMUX, rmp ; result and the input channels i.e. ADC0
ldi rmp, 0b11001111 ; select to enable the ADC, to start conversion, for auto
out ADCSRA, rmp ;triggering, for complete conversion interrupt through SREG i-bit
; and for prescaler factor i.e. 128



However, i am not able to understand the rest of the code for conversion specifically where we have 1<<ADSC please help.
Ajay
May 14 2009, 4:36 AM
in that code, he is using interrupts for ADC, where as you wont need it as far as i know. so change the value being loaded to ADCSRA register.

by default ADCSRB register is 0x00 so your ADC will be running in free running mode.

now once the ADC is enabled, you need to wait for ADIF flag to go high which indicates the conversion is complete and then read the ADC registers for converted value.

you can also set DIDR0 register to disable Digital input buffer which reduces the power loss.

Dont bother about what he/she wrote in attiny code, coz i told you every write for his own requirements
sammy mutua
May 14 2009, 8:32 AM
Thank you for the help. i also noticed that with studio 416 which am using, it is not possible to load registers such as ADCSRA, ADMUX even through other registers otherwise they require you deal with their internal bit registers as follows:
setting the ADMUX register;
cbi REFS1,sensor ;selecting the reference voltage
cbi REFS0,sensor ;ie AREF in this case
cbi ADLAR,sensor ;for right adjusting the result
cbi MUX3,sensor ;selecting the input channel i.e. ADC0
cbi MUX2,sensor
cbi MUX1,sensor
cbi MUX0,sensor
and setting ADCSRA register;
sbi ADEN, sensor ; select to enable the ADC
sbi ADSC,sensor ;to start conversion
cbi ADATE,sensor;for auto triggering
cbi ADIF, sensor ; for conversion complete interrupt
sbi ADIE,sensor ; through SREG i-bit
sbi ADPS2,sensor ; prescaler factor i.e. 128
sbi ADPS1,sensor
sbi ADPS0,sensor

Is there any other method of doing it?
Ajay
May 14 2009, 8:47 AM
well whats the prob in the prev code? i dont find any..

use r16 for reading and writing.. make sure you have defined rmp and tmp before writing that code. you can take a look at AVR tutorial once to know what basic stuff is needed before dealing with AVR.
sammy mutua
May 14 2009, 9:13 AM
i checked the tutorial and ofcourse have defined the two registers but the instructions out ADMUX, rmp and in tmp, ADCSRA indicates an error -register out of range ie 0*7a. so i tried setting each register in the two registers and it indicated no error.
Ajay
May 14 2009, 9:33 AM
out of range.. hmm.. strange..

what have u defined as rmp and tmp?
sammy mutua
May 14 2009, 9:42 AM
have defined r16 as rmp and r17 as tmp .
Ajay
May 14 2009, 9:50 AM
hmm.. try using r15 instead of r17 then try..
sammy mutua
May 15 2009, 4:21 AM
hi, the problem is still the same even when i use the higher registers.
sammy mutua
May 17 2009, 10:44 AM
any body with a working adc assembly code for atmega 168 please help. :mad
Ajay
May 18 2009, 6:07 AM
well ok i will help you writing it.
Lets setup ADCMUX, i am assuming Aref is AVcc with cap at Aref pin.
so REFS1:REFS0 = 01
assuming channel to be 0
so Mux3:Mux0 = 0

so ADMUX will be 0b01000000 = 0x40

now setup ADCSRA reg.
here we will enable ADC, Disable Auto Trigger, No ADC Interrupts, ADC clock pre-scalar to 0.
so ADCSRA = 0b10000000 = 0x80

Now ADC is enabled and setup is complete..

we will start a conversion and read it.

to start a conversion, Make ADSC bit 1.
monitor this bit coz it will remain high till the conversion is in process. as soon as it goes low, conversion completes.

once the conversion is compelete, you can read ADCL and ADCH register. remember that we have used ADLAR = 0 in ADCSRA register. so ADCH will contain higher two bits and lower 8 bits will be stored in ADCL.

Can you try writing a code now?
sammy mutua
May 25 2009, 8:32 AM
hi, i also found out that the ATtiny45 code i had posted above could work with ATmega168 but only if the instructions IN and OUT are replaced with LDS and STS instructions, this is because in ATmega168 the ADC registers are above 0x70 but the instructions IN and OUT can only access up to 0x3F in i/o space.

this is the code;

.include "m168def.inc"
.def rmp = R16
.def tmp = R17

ldi rmp, 0b00000000
sts ADMUX, rmp
ldi rmp, 0b11001111
sts ADCSRA, rmp

ad:
lds tmp, ADCSRA
ldi rmp, 1<<ADSC
or tmp, rmp
sts ADCSRA, tmp
ad_loop:
lds rmp, ADCSRA
andi rmp, 1<<ADIF
ldi tmp, 1<<ADIF
cpse rmp, tmp
breq ad_loop
ret


thank you all for your help.
Ajay
May 27 2009, 6:52 AM
cool thank you too.. came to know something new...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Rickey's World © 2003 - 2007