Discussion in "AVR Discussion Forum" started by    sammy mutua    May 11, 2009.
Mon May 11 2009, 08:06 pm
#1
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.
Tue May 12 2009, 02:19 pm
#2
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]
Tue May 12 2009, 02:58 pm
#3
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?
Tue May 12 2009, 07:52 pm
#4
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 like this.
Wed May 13 2009, 06:54 pm
#5
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.
Thu May 14 2009, 02:06 pm
#6
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 like this.
Thu May 14 2009, 06:02 pm
#7
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?
Thu May 14 2009, 06:17 pm
#8
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.
Thu May 14 2009, 06:43 pm
#9
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.
Thu May 14 2009, 07:03 pm
#10
out of range.. hmm.. strange..

what have u defined as rmp and tmp?

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am
Anthonyvab
Wed Apr 17 2024, 08:48 am
RobertCix
Wed Apr 17 2024, 06:46 am
Astorne
Tue Apr 16 2024, 08:52 pm