Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
anastasi.fr
PostPosted: Jun 09, 2012 - 08:58 PM
Newbie


Joined: Mar 26, 2012
Posts: 2


Hello to everybody...
i need to use an adc converter to convert a analog input in a digital output and to put it in a register named vreg.

I tried to do this:

ldi temp, $C0
out ADCSR,temp
in vreg, ADCH
out PortB, vreg


where is my error???

Could someone help me?
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Jun 09, 2012 - 09:59 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 21262
Location: Orlando Florida

Welcome to the avrfreaks forum. Looks like you are one of the last programmers in the world that prefers to use assembly language. I assume you have the datasheet for the avr you want to write a program for. There are little examples of how to init the a/d converter and read it using assembly language. Please forgive for suggesting that reading the manufacturer's datasheet might actually convey information about how the computer operates. I assume you will be using the atmel assembler, which is in the AtmelStudio software. AVRfreaks has many many projects here, and with a little searching and downloading, you can find a project written in assembler that inits and reads the a/d converter.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
smkipus
PostPosted: Jun 09, 2012 - 10:18 PM
Hangaround


Joined: Jun 13, 2009
Posts: 307
Location: Ann Arbor, MI

Since there is an ADC high register, you probably need to move the ADC low register into the high register for starters. The high register is where you read the upper nibble of the 10 bit conversion, or left shift the least significant bits into it from the lower byte. My Mega48/88 series has copy and paste assembler code to initialize and read.

Make sure you have 8 bit or 10 bit mode set
Make sure you have the clock sampling time set
Make sure you set the ADMUX
Make sure your AREF and ADC Power pins are hooked up
Make sure your Data direction port is set on the port you are outputting to all 1s
Make sure your processor simply works by putting anything out of Port B for starters, not just ADC values, can you print out 01010101 from port B?
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
ka7ehk
PostPosted: Jun 10, 2012 - 02:28 AM
10k+ Postman


Joined: Nov 22, 2002
Posts: 12049
Location: Tangent, OR, USA

You have to initialize the ADC, then start a conversion, then wait for it to complete. Just reading the register will get you nothing of value.

If you only want 8 bits of resolution, choose "left justified" configuration and read only ADCH.

Jim

_________________
Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA

"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
 
 View user's profile Send private message  
Reply with quote Back to top
JimK
PostPosted: Jun 10, 2012 - 05:28 AM
Hangaround


Joined: Jun 17, 2005
Posts: 472
Location: New Zealand

bobgardner wrote:
.. Looks like you are one of the last programmers in the world that prefers to use assembly language.

Au contraire ... I'd say he's not one of the first not to use non-assembly language.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
smkipus
PostPosted: Jun 10, 2012 - 05:56 AM
Hangaround


Joined: Jun 13, 2009
Posts: 307
Location: Ann Arbor, MI

Do people write in assembly when they need a program that does more than 2 or 3 tasks? Like if you want to make a device which reads 2 gyros and 3 accelerometers and you want it to navigate a maze?
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
Kartman
PostPosted: Jun 10, 2012 - 08:40 AM
Raving lunatic


Joined: Dec 30, 2004
Posts: 8776
Location: Melbourne,Australia

People used to write accounting systems in assembly. My first programs were written in machine code as the assembler ran on a mainframe. i didn't have a mainframe. I can tell you after many years of writing assembler that I much prefer to use C.
 
 View user's profile Send private message  
Reply with quote Back to top
chartman
PostPosted: Jun 10, 2012 - 09:06 AM
Resident


Joined: May 01, 2003
Posts: 577


I use assembly all the time...I just can't get to grips with C... I can just about 'get' BASIC...
I'll post some simple test code for my recent ADC tests later..in assembler Smile
Sad, isn't it, but at 60+ my brain just doesn't switch on to anything 'high level'. Sad
 
 View user's profile Send private message  
Reply with quote Back to top
david.prentice
PostPosted: Jun 10, 2012 - 09:35 AM
10k+ Postman


Joined: Feb 12, 2005
Posts: 16313
Location: Wormshill, England

Somehow people think that ASM must be easier to learn.
Yes, the syntax is simpler.

The program design and housekeeping is a lot more complex.

When I started, home computers had interpreted Basic. If you programmed SOME functions in assembler, you could get several 1000 times speed improvement.

Nowadays, you have compilers that are pretty clever. Yes, you can possibly make a 2 times speed improvement. Mostly it will be 1.2 times or less.

So I can't see any great satisfaction for spending a lot more effort to get 1.2 times improvement.

But if ASM is what you want to do, go for it.

David.
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
chartman
PostPosted: Jun 10, 2012 - 10:19 AM
Resident


Joined: May 01, 2003
Posts: 577


It's not what I WANT to do..... I appreciate all the arguments re speed/improvement and I could happily live with the marginally less efficient code of C and undoubtedly quicker time to get a program going.... If you want to pop over to Fleet and tutor me, I'd be more than happy. Maybe when I retire, I'll go back to colledge and take a course on C... Then I'd probably be stuck switching from the generics to the specifics of C for AVR... I did consider MicroE Basic but that might just confuse me even more....
Annyway, for the OP here is a bit of test code I did to monitor battery volts and then RSSI on my 433 receiver. The Analogue input is connected to PC5.No cap on AREF, nothing special on AVCC and readings were +/-1 LSB using the top 8 bits( = 3 bits @10 full bits) Plenty accurate enough for what I want.Power is a 12V N cell and I want to detect LoBatt = 6V ish..
For RSSI, I want to distinguish between 'noise' on Rxd and 'a carrier'. For this I just test the 8 bits against a preset 'threshold'.It provides the fastest response to a signal in a polled receiver- 3mS on,150mS off and avoids using a transistor to do the job, as per my post in the wireless section.
Setup and subroutines are not included, just the core of the test software. The OP will have to work out the varios bits that are used in the ADC control registers...


Code:

;ADC TEST- USES rssi AS INPUT TO PC5
;COMMS AT 9600
;USES MEGA48P
;*******************************************************************
CBI PORTD,5      ;GROUND THE BATTERY RESISTOR

RCALL DELAY      ;5mS- ALLOW THE 2N2 CAP TO CHARGE

LDI RTEMP,0XE5
STS ADMUX,RTEMP

LDI RTEMP,0X00
STS ADCSRB,RTEMP

LDI RTEMP,0X20
STS DIDR0,RTEMP
;******************
ADC1:
LDI RTEMP,0XC3   ;START ADC
STS ADCSRA,RTEMP

ADCRDY:      ;WAIT TIL COMPLETE
LDS RTEMP,ADCSRA
SBRC RTEMP,6
RJMP ADCRDY

LDS RTEMP,ADCH   ;GET HIGH BYTE,8 BITS

STS UDR0,RTEMP   ;SEND VALUE TO UART
SBI PORTD,5      ;OPEN THE BATT RESISTOR FROM GND
RCALL DELAY      ;WAIT 5mS
CBI PORTD,5      ;GROUND THE BATTERY RESISTOR
RCALL DELAY
RJMP ADC1      ;LOOP
 
 View user's profile Send private message  
Reply with quote Back to top
chartman
PostPosted: Jun 10, 2012 - 10:30 AM
Resident


Joined: May 01, 2003
Posts: 577


A mention about the battery detect circuit.. I use a 2M2 from the battery and 180K to a port pin. Across the 180K is a 2N2 cap. This allows the ADC cap (14pf?) to suck charge from the 2N2.The junction of the 2M2 and 180K goes to the ADC input pin ( remember to turn off the digital I/P from this pin!)although I'm going to use ADC 6 and 7 eventually....
When the port pin connected to the 'bottom' of the 180K is set low' it takes 5mS for the ADC input to rise from ground to a stable voltage and then you can read it.
When the port pin is set high, the port pin is crowbarred at Vcc + a bit and the ADC reads 0xFF. Current into the protection diode is ~12/2.4Mohm ~5uA.
That might not suit everyone.. but for me it's OK as the average receiver current is 400uA
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 10, 2012 - 11:32 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62314
Location: (using avr-gcc in) Finchingfield, Essex, England

Www.avrbeginners.net seem to have a good section on using ADC in Asm.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits