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
tsibos22
PostPosted: May 22, 2011 - 03:43 PM
Rookie


Joined: Nov 16, 2010
Posts: 24


Dear freaks,
I work in a project using and the 8 adc chanels in atmega16( avr studio gcc) and i want t read and the 8 chanels and to send them to the serial port. The problem is when i try to change analog chanel and i choose the one who i had used before the atmega read always the same chanel.
For example
Code:

adc0=0
adc1=10
adc2=20
adc3=30
First time read adc0 and sends 0
Second time reads adc1 sends 10
Third time reads adc2 sends 20
Fourth time reads adc3 sends 30
Fifth time reads adc0 sends 30
Six time reads adc1 sends 30

And the problem stops when ia reset the atmega16
The code
Code:

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)


   //adc~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void InitADC()
{

ADMUX=(0<<REFS0)|(0<<REFS1);                       
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(0<<ADPS0);
}

uint16_t ReadADC(uint8_t ch)
{
   //Select ADC Channel ch must be 0-7
   ch=ch&0b00000111;
   ADMUX|=ch;

   //Start Single conversion
   ADCSRA|=(1<<ADSC);

   //Wait for conversion to complete
   while(!(ADCSRA & (1<<ADIF)));

 

   ADCSRA|=(1<<ADIF);

   return(ADC);
   
}


//-----------------------MAIN
int main (void)
{
//serialINSTAL
 UCSRB |= (1 << RXEN) | (1 << TXEN);   
   UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);

   UBRRL = BAUD_PRESCALE;
   UBRRH = (BAUD_PRESCALE >> 8);
//ENDserialINSTAL


uint16_t adc_result;
InitADC();
 
char a;


begin:

while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until read serial
a=UDR;


if(a==((char) '1')){
adc_result=ReadADC(0);
while ((UCSRA & (1 << UDRE)) == 0) {};
UDR=adc_result;
goto begin;
}


if(a==((char) '2')){
adc_result=ReadADC(1);                 
while ((UCSRA & (1 << UDRE)) == 0) {};
UDR=adc_result;
goto begin;
}

if(a==((char) '3')){
adc_result=ReadADC(2);                 
while ((UCSRA & (1 << UDRE)) == 0) {};
UDR=adc_result;
goto begin;
}

if(a==((char) '4')){
adc_result=ReadADC(3);                 
while ((UCSRA & (1 << UDRE)) == 0) {};
UDR=adc_result;
goto begin;
}

if(a==((char) '5')){
adc_result=ReadADC(4);                   
while ((UCSRA & (1 << UDRE)) == 0) {};
UDR=adc_result;
goto begin;
}

if(a==((char) '6')){
adc_result=ReadADC(5);                 
while ((UCSRA & (1 << UDRE)) == 0) {};
UDR=adc_result;
goto begin;
}

if(a==((char) '7')){
adc_result=ReadADC(6);                   
while ((UCSRA & (1 << UDRE)) == 0) {};
UDR=adc_result;
goto begin;
}

if(a==((char) '8')){
adc_result=ReadADC(7);                   
while ((UCSRA & (1 << UDRE)) == 0) {};
UDR=adc_result;
goto begin;
}

goto begin;
}



I use 8Mhz external clock and the pin AREF is connected to VCC pin(5volts)
My analog inputs are potensiomters in series with resistors
Does anyone knows what is happeneing?
Thaks!
 
 View user's profile Send private message  
Reply with quote Back to top
Visovian
PostPosted: May 22, 2011 - 04:10 PM
Posting Freak


Joined: Aug 07, 2007
Posts: 1475
Location: Czech

Quote:
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX|=ch;

If you want to change the adc channel, you have to
Code:
first clear the old channel
ADMUX &= 0b11111000

then OR the new one
ADMUX |= ch;

PS
Another way
Code:
// define reference
#define REF (1<<REFS0)|(1<<REFS1)

// set/change channel
ADMUX = REF | channel
 
 View user's profile Send private message  
Reply with quote Back to top
tsibos22
PostPosted: May 22, 2011 - 04:39 PM
Rookie


Joined: Nov 16, 2010
Posts: 24


Visovian wrote:
Quote:
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX|=ch;

If you want to change the adc channel, you have to
Code:
first clear the old channel
ADMUX &= 0b11111000

then OR the new one
ADMUX |= ch;

PS
Another way
Code:
// define reference
#define REF (1<<REFS0)|(1<<REFS1)

// set/change channel
ADMUX = REF | channel


Dear Visovian,
Thanks for response and for your solution Very Happy
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: May 22, 2011 - 07:07 PM
10k+ Postman


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

Once again, a Gem of General AVR C Advice will be missed by all users of the Other Compilers. Hey Eivind! Lets change to vBulletin so we can rate the threads and tag the good replies.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
theusch
PostPosted: May 22, 2011 - 07:14 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 25912
Location: Wisconsin USA

Quote:

Once again, a Gem of General AVR C Advice will be missed by all users of the Other Compilers.

Make up your mind, Bob. Or post your rules for being politically-correct in choosing which Forum. I remember you complaining about fairly generic questions in the main Forum after it was seen that the poster used gcc. "You've got your own Forum for that compiler brand" or something like that.

I can't play the game if I don't know the rules.

Lee
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: May 22, 2011 - 07:33 PM
10k+ Postman


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

I guess just post a sticky thread in the AVR forum that says "If you are trying to learn General C, you better read the GCC forum too, because that's what most people use, so that's where the most advice is dished out". Otherwise, does vBulletin let you add tags to posts? Then you could search for [mega32] [adc] [c source] [cv] or [mega168] [twi] [gcc]

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
clawson
PostPosted: May 22, 2011 - 08:08 PM
10k+ Postman


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

Quote:

I guess just post a sticky thread in the AVR forum that says

Yeah, stickies telling new people where to post have been proven to work SO well - not!

You have to accept that newbies ARE going to post in the wrong place. What you can do is contact a moderator and ask them to move any such thread.

Should I move this one - even though it is "[solved]"?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: May 22, 2011 - 08:17 PM
10k+ Postman


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

I don't REALLY care. I'm just input overloaded. The list of stuff I Don't Know Anything About keeps getting bigger. I barely know c on one compiler. I need to learn about ARMs, TFT lcds, decomposing program into tasks, learn some modern languages like c#. And I have 30+ years of experience in embedded microcontrollers. It must be real hard for the new guys to get going. Tell em to read both forums I guess.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
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