Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

ATmega328 opamp code

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Robotics
View previous topic :: View next topic  
Author Message
jcd06

Bascom Member



Joined: 19 May 2014
Posts: 2

blank.gif
PostPosted: Tue May 20, 2014 2:10 pm    Post subject: ATmega328 opamp code Reply with quote

Hello people,

Microcontrollers are very new to me.
I'm currently following the series of articles in Elektor called "Microcontroller Bootcamp" so I'm using an Arduino Uno board with ATmega328 chip.
Elektor has chosen Bascom AVR as programming language instead of the Arduino IDE.
This is completely in line with my objective as I would like to learn to use and program microcontrollers and not only Arduino.
For the time being I can make a LED blink and use a digital input to control a digital output Smile)
Great, but I'm a bit stuck now waiting for the third article...

One of the practical applications that I could imagine and for which I would like to use the Arduino/Bascom, makes use of opamps when it were built with classic components.
Is there a way to program the ATmega so it behaves like an opamp or a comparator?
As I'm totally new I can only imagine: two inputs being converted to digital (ADC) and then subtracted mathematically (in binary) and the result converted back to analog (DAC).
Perhaps there is another way to achieve this.
Can you show me the way to some example of code doing this?
Or do you have any other comment in this regard?

Thank you very much,

Jean

_________________
The amateur is a fool. Ignorant that something is impossible, he will succeed realizing it.
Paul Godley 2ZE
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5922
Location: Holland

blank.gif
PostPosted: Tue May 20, 2014 7:58 pm    Post subject: Reply with quote

most AVR chips already have an analog comparator. but the output is not routed to a pin. you can only check a bit in a register to see the output which is digital 0 or 1. So you can not use it as a normal OPAMP.
If you want to make an opamp you would need an D/A converter. some chips have both a/d and d/a but with a resistor network you can also create a D/A converter. then you can make a digital amplifier.
Maybe there are users that have a similar interest and can share some samples and ideas.

_________________
Mark
Back to top
View user's profile Visit poster's website
jcd06

Bascom Member



Joined: 19 May 2014
Posts: 2

blank.gif
PostPosted: Wed May 21, 2014 8:13 am    Post subject: Reply with quote

Dear Mark,

Thank you for your reply.
I noticed the Analog Comparator chapter 22 in the datasheet but could not interpret its meaning.
Also it is clear that there is no D/A converter chapter.
Is it correct that to make a D/A converter with a resistor network one will need to use as many digital outputs as D/A bits?
This would use up a lot of the chips hardware...

Best regards,

Jean

_________________
The amateur is a fool. Ignorant that something is impossible, he will succeed realizing it.
Paul Godley 2ZE
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5922
Location: Holland

blank.gif
PostPosted: Wed May 21, 2014 9:02 am    Post subject: Reply with quote

yes for 8 bit resolution you need 8 bits of a port. but you can also add D/A using an external chip.
Or choose a chip that has D/A as well.

analog comp : you need to enable it : ACSR.7=1 'enable
now all you need to do is watch bit 5
do
if acsr.5=0 then

else

end if
loop

it only compare voltage on ain0 and ain1 pins of the micro. and sets bit 5 according to the voltage. so usage as opamp is limited.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Robotics All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum