atmega8 programiming codevision avr

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

can somebody explain in detail the following code, 

i created a new project in code-vision-avr with atmega8, while creating i enabled adc,

in the workspace i have the below automatically generated lines,

i need a detailed explanation for lines 1 and 6, (comment lines for line number are included by me for your reference). thank you.

 

unsigned int read_adc(unsigned char adc_input)

{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);    //----------------line 1
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);   //-----------------------------------------------------line 2
// Start the AD conversion
ADCSRA|=0x40;  //---------------------------------------------------line 3
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0); //------------------------------------line 4
ADCSRA|=0x10;  //---------------------------------------------------line 5                                                                
return ADCW;      //---------------------------------------------------line 6
}

 

 

 

This topic has a solution.

--Boopathy

call / whatsapp +919715784910

Last Edited: Fri. Nov 27, 2020 - 01:55 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Line1: The ADMUX register contains essentially three things. Top bits set the reference against which the ADC input voltage is measured. There is one bit that can adjust the position of the result to the left or right that sits "in the middle" and then the bottom bits select which of a number of sources are fed into the ADC using a "multiplexer". In your example the left/right adjust bit is not used so ADLAR will be 0 which means it will assume the default of right adjustment. The "(ADC_VREF_TYPE & 0xFF)" is a very curious piece of code. ADMUX is an 8 bit register so the "& 0xFF" would appear to have no effect whatsoever - usually you might use & with some bit pattern smaller than the whole 8 bits to isolate some value into a fixed range but & 0xFF really has no effect. Even if ADC_VREF_TYPE had been defined to more than 8 bits it still wouldn't really have an effect as the write to the 8 bit register would truncate it anyway. The place you might usually us "& 0xmask" in an ADC read routine when setting ADMUX is when you want to limit the chosen input (ie "adc_input" in this example). The MUX bits in ADMUX for mega8 are just the lower 4 bits so you might have (adc_input & 0x0F) to ensure that only the lower 4 bits "get through" but rather curiously that is not done. Presumably it is up to the user to ensure that they never pass an adc_input value to this routine outside the range 0x00..0x0F (0..15)? Finally the "|" in the middle is simply "OR" which joins together two groups of bits. So in the higher bits you have ADC_VREF_TYPE and in the lower bits you have the MUX channel selection. All is then written together in to the ADMUX register.

 

Line6: this is simply returning the result. The function itself has a "unsigned int" (that is uint16_t) return type so it will return 16 bits. Those will be the contents of the ADCH and ADCL registers combined. While it is possible to read ADCH/ADCL separately then combine them together in to the full 16 bits most AVR C compilers define some kind of "psuedo register" that is 16 bits wide and means "ADCH and ADCL together". In Codevision they clearly chose to call this ADCW (meaning ADC Word where "Word" is a fuzzy word in computing terms that in this case means 16 bits). Of course the ADC isn't really 16 bits, it is just 10 bits. That is where the ADLAR bit I mentioned comes into play. If it is not set (default) then the 10 bit reading occupies the lower 10 bits of the 16 bit ADCW. So it will return 0..1023. If the ADLAR bit had been set to Left adjust the result it would occupy the upper 10 of the 16 bits. But you usually only do that when you are only really interested in 8 bit reafdins. In that case you want the upper 8 of the 10 bits. So by moving all ten across the ADCH:ADCL boundary towards the upper end (that is ADCH) then if you just pick up ADCH with ADLAR=1 then you get the top 8 bits as intended.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

My CodeVision v3.42 produces this for a 1MHz ATmega8:

#include <mega8.h>

#include <delay.h>

// Declare your global variables here

// Voltage Reference: AREF pin
#define ADC_VREF_TYPE ((0<<REFS1) | (0<<REFS0) | (0<<ADLAR))

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCW;
}

So it expects the user to pass a valid adc_input to select the correct ADC channel.

You could mask with 0x0F like Cliff has suggested.    But quite honestly it is your responsibility to use the correct channel.    Likewise,   you should ask the CodeWizard to use the appropriate VREF.

 

Modern AVRs have several different VREF types e.g. internal BandGap, ...

Modern AVRs have several different ADC channels e.g. internal temperature sensor, bandgap, ...

 

The mega8 is more simple.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

david.prentice wrote:

ADCSRA|=(1<<ADIF);

Shouldn't that be = not |= ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

That is the Wizard generated code.    It produces a SBI instruction.

 

And like other AVRs some Special Function Registers have a mix of RW and RO  register bits.

ADCSRA has every bit RW.

SPSR, TWCR, TWSR have both RW and RO bits.    Some bits are RW but only respond to writing a 1 e.g. clearing an interrupt flag.

 

So you have to be careful how you write to these registers.  i.e. you must preserve the other bits when clearing the ADIF bit.

On the other hand,  a register like TIFR only has Interrupt Flag bits.    So you probably only want to clear one Flag at a time e.g. with =

As it happens Codevision and GCC will generate SBI instructions for |= whenever possible.   And the AVR hardware only accesses a single bit with SBI.

 

From a semantic point of view.    The C |= operation means Read-Modify-Write the whole variable.    So the SBI instruction is "wrong".

In practice,  both |= and = will work on TIFR

 

As mentioned in numerous threads it is easier to wait for ADSC bit to clear than using the ADIF Flag.

 

David.

Last Edited: Fri. Nov 6, 2020 - 12:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson sir, thank you very much

--Boopathy

call / whatsapp +919715784910

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice sir thank you very much for the discussion

--Boopathy

call / whatsapp +919715784910

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Do Cliff and David receive accreditation when you submit your homework assignment?

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If boopathy  has understood how to use the ADC the Forum has worked very well.

 

He asked specific questions.   Got accurate answers.    And it looks as if he studied them too !

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:
He asked specific questions.

Sure did.

 

david.prentice wrote:
Got accurate answers.

Sure did.

 

david.prentice wrote:
And it looks as if he studied them too !

If you say so.

 

smiley

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

 

 

david.prentice wrote:

And it looks as if he studied them too !

 

If you say so.

 

I am just guessing.    He came back to the Forum 18 days later.    I presume after he has completed his assignment.

 

If he had repeated his original question several times on the original day,   I would have guessed that he had not studied the replies.

If he had stamped his foot and deleted his original question,   I would have been certain that he had not studied the replies.

 

Cliff gave an excellent explanation.    (I only made a pedantic point)

Students don't often come back to their original question.    Especially 18 days later.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi sir  jgmdesign , i asked to explain the code in a certain day, i didnt stop searching with this site, i have some friends who knows something about embedded programming, im not submitting an assignment which has a time limit, im learning for myself, i have seen both  clawson and  david.prentice replies much earlier before i thanked them. but i couldnt understand because im real fresh. so after my friend's arrival, he made me understand a part of these two gentlemen's reply. it took much time because im not into it regularly for now. i would have replied earlier but i couldnt understand their replies easily, i took my friends help to read them.

--Boopathy

call / whatsapp +919715784910

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Thank you for returning and clearing this up.  My apologies.

 

We get a lot of sleeping students around here asking the same question you did.  Hence my skepticism. 

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ha ha,, ok sir

--Boopathy

call / whatsapp +919715784910

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the feedback, boopathy.

 

boopathy wrote:
i couldnt understand their replies easily

If you don't understand a reply, you can always post a follow-up to ask for clarification.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Thanks for the feedback, boopathy.

 

 

boopathy wrote:

i couldnt understand their replies easily

 

If you don't understand a reply, you can always post a follow-up to ask for clarification.

 

ok sir, i will keep that in mind. thank you.

--Boopathy

call / whatsapp +919715784910

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

and don't forget to mark the solution - see Tip #5 in my signature, below.

 

Also see https://www.avrfreaks.net/forum/man-forum-how-reply-post-forum for some tips on how to use the forum's reply features.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

and don't forget to mark the solution - see Tip #5 in my signature, below.

 

Also see https://www.avrfreaks.net/forum/man-forum-how-reply-post-forum for some tips on how to use the forum's reply features.

 

oh the tip is helpful sir

--Boopathy

call / whatsapp +919715784910