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
justinjohney
PostPosted: Jun 18, 2012 - 01:59 PM
Hangaround


Joined: Jun 04, 2012
Posts: 295
Location: Mumbai,India

im a beginer to avr
what is wrong with this code
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int adc_result=0;
int ch=0;

int main (void)
{
//set portC as input and enable the pull up resistor
DDRC=0x00;
PORTC=0xFF;

//set up port D 0-5  as output& C 6,7 as input
DDRD=0x3F;

// Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);


// Set ADC to Free-Running Mode
ADCSRB &=~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)) ; 
 
// Enable ADC
ADCSRA |= (1 << ADEN); 
// Enable ADC Interrupt
//ADCSRA |= (1 << ADIE); 
   
// Enable Global Interrupts
 //sei(); 


 
   
// Loop Forever
    while(1)

       {
      // Set ADC reference to AREF
      // Left adjust ADC result to allow easy 10 bit reading
      //select adc channel 1
      ADMUX=0x00;

         for(ch=0;ch<3;ch++)
          {
             // Start A2D Conversions
            ADCSRA |= (1 << ADSC);
      
            //Read the adc result
            adc_result=ADCW;
                _delay_ms(10);
         
            if(adc_result>970)
            {
               if(ch==0)
               {
                  PORTD1=1;
                  _delay_ms(50);

                     if(adc_result>970)
                        {
                           PORTB4=1;
                        }
               }

               else if(ch==1)
               {

                  PORTD2=1;
                  _delay_ms(50);
                     if(adc_result>970)
                        {
                           PORTB4=1;
                        }
               }
               else(ch==2)
               {
                  PORTD2=1;
                  _delay_ms(50);
                     if(adc_result>970)
                        {
                           PORTB4=1;
                        }


            }
            ADMUX++;
         }

   }
   
}



 







 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Jun 18, 2012 - 02:25 PM
10k+ Postman


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

Quote:

what is wrong with this code

What AVR model are you working with?
What do you expect to happen?
What is happening?
How are you testing?
What problem(s) are you encountering?

The entire logic of the program is strange. The value is checked for 970--and then again checked. How can it change?

A conversion is started and the result taken--without waiting for the conversion to complete.

In free-running mode, you can easily be grabbing the result from the "previous" channel.
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 18, 2012 - 02:34 PM
10k+ Postman


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

Code:
             // Start A2D Conversions
            ADCSRA |= (1 << ADSC);
     
            //Read the adc result
            adc_result=ADCW;

Did we perhaps forget to do something in the middle here? Contrary to popular belief the result in ADCW will not be available just a few cycles after you set the ADSC bit. Try putting a:
Code:
while(ADCSRA & (1<<ADSC)));

between those lines to sit and wait while ADSC remains high - it will return to 0 and hence end the while() loop when the conversion is complete.
Code:
                  PORTD1=1;

I'd be interested to see the definition of "PORTD1". True, if you use something like Peter Danegger's sbit.h then something like this might be valid syntax but I see no such #include of the necessary .h file.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Jun 18, 2012 - 02:42 PM
10k+ Postman


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

Quote:

Code:
PORTD1=1;

I'd be interested to see the definition of "PORTD1".

lol -- I missed that in my first read-through. Wouldn't you get something like a "not an lvalue" error?


Quote:
Error: C:\AtmelC\TestProgs\M88t.c(15): the expression must be a modifiable lvalue
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 18, 2012 - 05:45 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

clawson wrote:

Code:
                  PORTD1=1;

I'd be interested to see the definition of "PORTD1".

You will find that in .inc files for inclusion in an assembly source though it is equated as a bit number exactly the same as PD1 is. Definitely a non-working piece of code here.

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 18, 2012 - 05:49 PM
10k+ Postman


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

Quote:

You will find that in .inc files for inclusion in an assembly source

Two pints of whatever you are smoking. How are .inc files involved in the use of avr-gcc?

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 18, 2012 - 07:09 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

clawson wrote:
Quote:

You will find that in .inc files for inclusion in an assembly source

Two pints of whatever you are smoking. How are .inc files involved in the use of avr-gcc?

They're not, but since you asked, I pointed out where they "are" defined. Wink

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 18, 2012 - 09:14 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18560
Location: Lund, Sweden

OT:
Quote:
Two pints of whatever you are smoking.

LOL! A really crap day can always be saved by something like this.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
theusch
PostPosted: Jun 18, 2012 - 10:24 PM
10k+ Postman


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

Quote:

OT:
Quote:
Two pints of whatever you are smoking.

LOL! A really crap day can always be saved by something like this.


Try "I'll have what she's having", from "When Harry Met Sally".
http://www.youtube.com/watch?v=F-bsf2x-aeE
Start at about 1:30.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 18, 2012 - 10:30 PM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18560
Location: Lund, Sweden

Quote:
Try "I'll have what she's having", from "When Harry Met Sally".

Seen that a few times, yup.

But this... http://www.youtube.com/watch?v=qJP8_6wKNso (round about 30 seconds). Absolutely priceless!
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
larryvc
PostPosted: Jun 18, 2012 - 10:49 PM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

I just want to know how you two got soooo OT from Johan's previous post. Laughing

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Jun 18, 2012 - 11:15 PM
10k+ Postman


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

It was >>Cliff's fault<< for mixing his metaphors.

No, wait--the root cause was >>you<< -- inserting [the low-level programming technique that shall not be mentioned] into a high-brow C thread.
 
 View user's profile Send private message  
Reply with quote Back to top
justinjohney
PostPosted: Jun 19, 2012 - 05:32 AM
Hangaround


Joined: Jun 04, 2012
Posts: 295
Location: Mumbai,India

theusch wrote

Code:
What AVR model are you working with?

AVR Studio 4, GCC compiler
Code:
What do you expect to happen?

actually i want to convert three analog signal into digtal simultaneously(with max speed) and compare it with threashold and if any one it is greater than the threashold then a led must glow corresponding to that signal.Moreover if the condition stable for a particular time then there must be get an o/p on a port pin at a particular pin.
Code:

What problem(s) are you encountering?


build error,i know the code is not completed



i think now you got it. can you help me?[/code]
 
 View user's profile Send private message  
Reply with quote Back to top
larryvc
PostPosted: Jun 19, 2012 - 06:58 AM
Raving lunatic


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA

Did you read clawson's post two posts ater your first post?

http://www.avrfreaks.net/index.php?name ... 434#966434

What does PORTD1=1; do?

Have you written any code to just blink an LED yet?

_________________
Larry

Those afraid to embrace the future will quickly fade into the past. - larryvc
 
 View user's profile Send private message  
Reply with quote Back to top
mtaschl
PostPosted: Jun 19, 2012 - 08:47 AM
Resident


Joined: Aug 21, 2002
Posts: 895
Location: Austria

justinjohney wrote:
theusch wrote
Quote:
What AVR model are you working with?

AVR Studio 4, GCC compiler

Thanks for telling us the tool-chain, but what does this have to do with the AVR model (=the chip on which your program is intended to run)?

_________________
/Martin.
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Jun 19, 2012 - 09:21 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18560
Location: Lund, Sweden

Quote:
actually i want to convert three analog signal into digtal simultaneously(with max speed) and compare it with threashold and if any one it is greater than the threashold then a led must glow corresponding to that signal.

That rang a bell. So I digged through earlier threads by the OP, and:

http://www.avrfreaks.net/index.php?name ... 571#962571

http://www.avrfreaks.net/index.php?name ... 310#964310

Hey Justin! Please stay in one thread with your work on these ADC-and-threshold questions.

MODERATOR: Perhaps the other two threads should be locked? [done - clawson]
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
bobgardner
PostPosted: Jun 19, 2012 - 12:45 PM
10k+ Postman


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

I asked if the signals were AC or DC. You said the signals were 50Hz AC. I assume you want to read the peak or the average amplitude of these signals, which will require either some digital signal processing to determine the peak or average of a group of samples, or a diode and an RC low pass filter to 'catch' the signal for 20ms so the a/d converter can read it. I felt sort of miffed that you didn't mention that you had read my suggestion and understood it.

_________________
Imagecraft compiler user


Last edited by bobgardner on Jun 19, 2012 - 10:11 PM; edited 1 time in total
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
justinjohney
PostPosted: Jun 19, 2012 - 01:26 PM
Hangaround


Joined: Jun 04, 2012
Posts: 295
Location: Mumbai,India

atmega168
the following code shows error in avr studio, avr gcc compiler.
Code:
#include <avr/io.h>
#include <stdio.h> 
#include <avr/interrupt.h>
#include <util/delay.h>

//system clock @16MHz
#define F_CPU 16000000


//Global variable
int adc_result[2]={0,0,0};
uint8_t ch=0;


/* PORT INIT */
void port_init (void)
{
//set portB.1 as output
DDRB |=(1<<PB1);
PORTB&=~(1<<PB1);

//set portC0,1,2, as input and enable the pull up resistor
DDRC &=~(1<<PC0)|~(1<<PC1)|~(1<<PC2);
PORTC |=(1<<PC0)|(1<<PC1)|(1<<PC2);

//set up port D 0-2  as output& D6,7 as input with pull up resistor
DDRD &=~(1<<PC0)|~(1<<PC1)|~(1<<PC2);
DDRD &=~(1<<PD6)|~(1<<PD7);
PORTD |=(1<<PD6)|(1<<PD6);

}


void init_adc(void)
{
// Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);


// Set ADC to Free-Running Mode
//ADCSRB &=~((1<<ADTS2)|(1<<ADTS1)|(1<<ADTS0)) ;

//select adc channel 0
//ADMUX=ch;

// Enable ADC Interrupt
ADCSRA |= (1 << ADIE); 

// Enable ADC
ADCSRA |= (1 << ADEN); 


// Enable Global Interrupts
sei();

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



/* Interrupt service routines */

ISR(ADC_vect)
{

//delay  for discarding first conversion result
//_delay_ms(10);
//Read the adc result
   switch(ch)
   {
      case 0:
         {
         
         adc_result[0]=ADCW;
         
         if(adc_result[0]>862)
            {

               do
               {
               PORTD|=(1<<PD0);//glow the led corresponding to signal 1
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1<<ADSC);
               adc_result[0]=ADCW;
                  if(adc_result[0]>887)
                  {
                     do
                     {
                     ADCSRA |= (1<<ADSC);
                     adc_result[0]=ADCW;
                     PORTB|=(1<<PB0);//  o/p drive if the state at adc0 exist for a particular time      
                     }while (adc_result[0]<970)
                  }   
               }while(adc_result[0]<862)

            }

         else if (adc_result[0]<754)
            {
            PORTD|=(1<<PD3);//glow the led corresponding to UV indication
            }
         else
            {
               if(ch<3)
               {

                  ch++;
                  ADMUX=ch;
               }
               else
               {
                  ch=0;
                  ADMUX=ch;
               }
            }
         break;
         }

      case 1:
         {
         adc_result[1]=ADCW;
      
         if(adc_result[1]>862)
            {

               do
               {
               PORTD|=(1<<PD1);//glow the led corresponding to signal 2
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1<<ADSC);
               adc_result[0]=ADCW;
                  if(adc_result[0]>887)
                  {
                     do
                     {
                     ADCSRA |= (1<<ADSC);
                     adc_result[0]=ADCW;
                     PORTB|=(1<<PB0);//  o/p drive if the state at adc1 exist for a particular time      
                     }while (adc_result[1]<862)
                  }
               }while(adc_result[1]<862)

            }
         else if(adc_result[1]<754)
            {
            PORTD|=(1<<PD3);//glow the led corresponding to UV indication
            }
         else
            {
               if(ch<2)
               {
                  ch++;
                  ADMUX=ch;
               }
               else
               {
                  ch=0;
                  ADMUX=ch;
               }
            }
         break;
         }

      case 2:
         {
         adc_result[2]=ADCW;
         //Start next  conversion
         ADCSRA |= (1<<ADSC);
         if(adc_result[2]>862)
            {

               do
               {
               PORTD|=(1<<PD2);//glow the led corresponding to signal 3
               //Start next  conversion with same channel
               _delay_ms(3);
               ADCSRA |= (1<<ADSC);
               adc_result[0]=ADCW;
               if(adc_result[2]>887)
                  {
                     do
                     {
                     ADCSRA |= (1<<ADSC);
                     adc_result[0]=ADCW;
                     PORTB|=(1<<PB0);//  o/p drive if the state at adc2 exist for a particular time      
                     }while (adc_result[2]<862)
                  }
               }while(adc_result[2]<862)

            }
         else if(adc_result[2]<754)
            {
            PORTD|=(1<<PD3);//glow the led corresponding to UV indication
            }
         else
            {
               if(ch<3)
               {
                  ch++;
                  ADMUX=ch;
               }
               else
               {
                  ch=0;
                  ADMUX=ch;
               }
            }
         break;
         }
   }
}


void main()
{

   port_init();

   

// Loop Forever
    while(1)

       {
      ADMUX=ch;
      init_adc();
      
      }         
}

 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Jun 19, 2012 - 01:28 PM
10k+ Postman


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

Saying what the error is would help as then we wouldn't have to compile it ourselves (I'm not in a position to do so in my current location).

BTW you do know that warnings/errors include both the filename and the line number (test.c:109 is line 109 of test.c) so you should be able to identify the exact line where the error is located.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
mtaschl
PostPosted: Jun 19, 2012 - 01:40 PM
Resident


Joined: Aug 21, 2002
Posts: 895
Location: Austria

Code:
int adc_result[3]={0,0,0};

instead of
Code:
int adc_result[2]={0,0,0};


Code:
DDRC &=~((1<<PC0)|(1<<PC1)|(1<<PC2));

instead of
Code:
DDRC &=~(1<<PC0)|~(1<<PC1)|~(1<<PC2);

(replace on all occurances).


Code:
// Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);

Are you sure, that you are running on 16 MHz and not on the internal 1 MHz ( 8 MHz / 8 ) as this is default?

Put all stuff from your ISR outside since you do it in polling-mode and all this stuff including the delays should never be done inside an ISR.

I suggest you to read the tutorial on bit-manipulation

_________________
/Martin.
 
 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