ADC converter for atmega168 using assembly language
Discussion in "AVR Discussion Forum" started by sammy mutua May 11, 2009.
Fri May 15 2009, 01:51 pm
hi, the problem is still the same even when i use the higher registers.
Sun May 17 2009, 08:14 pm
any body with a working adc assembly code for atmega 168 please help. :mad
Mon May 18 2009, 03:37 pm
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?
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 like this.
Mon May 25 2009, 06:02 pm
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.
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_bhargav like this.
Powered by e107 Forum System