go_away

Author Topic: error programming adc  (Read 856 times)

0 Members and 1 Guest are viewing this topic.

Offline ram aravindTopic starter

  • Jr. Member
  • **
  • Posts: 11
  • Helpful? 0
error programming adc
« on: July 17, 2012, 10:59:21 AM »
i need to do this:
whenever i press button ,power goes to d0 input pin and i must carry the task given in if loop:

#include <avr/io.h>
#include <avr/interrupt.h>

int main (void)
{
   PORTD &= ~(1 << 0); //set port d0 as input
   DDRE |= (1 << 2); // Set LED1 as output
   DDRG |= (1 << 0); // Set LED2 as output
   ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescaler to 128 - 125KHz sample rate @ 16MHz

   ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
   ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading

   // No MUX values needed to be changed to use ADC0
 
   ADCSRA |= (1 << ADFR);  // Set ADC to Free-Running Mode

  if(PIND==0b00000001)// D0 gets 5v when button is pressed
   {
    ADCSRA |= (1 << ADEN);  // Enable ADC

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

   ADCSRA |= (1 << ADSC);  // Start A2D Conversions
  }
   for(;;)  // Loop Forever
   {
   }
}

ISR(ADC_vect)
{
   if(ADCH < 128)
   {
      PORTE |= (1 << 2); // Turn on LED1
      PORTG &= ~(1 << 0); // Turn off LED2
   }
     
   else
   {
      PORTE &= ~(1 << 2); // Turn off LED1
      PORTG |= (1 << 0); // Turn on LED2
   }
}
but its showing error that ::
error - static declaration of vector_4 follows non static declaration.
plz help me in this..
« Last Edit: July 17, 2012, 11:03:23 AM by ram aravind »

Offline adanvasco

  • Full Member
  • ***
  • Posts: 107
  • Helpful? 6
  • Necessity is the mother of invention.
Re: error programming adc
« Reply #1 on: July 25, 2012, 05:37:52 PM »
Why don't you use a while(1) loop instead of a for loop?
Knowledge does not weigh.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 109
Re: error programming adc
« Reply #2 on: July 25, 2012, 08:21:14 PM »
Are you doing this in an 'h' or 'c' file?

What chip are you using?
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline greywanderer012345

  • Robot Overlord
  • ****
  • Posts: 133
  • Helpful? 2
Re: error programming adc
« Reply #3 on: August 23, 2012, 11:56:31 AM »
Quote
if(PIND==0b00000001)// D0 gets 5v when button is pressed
   {
    ADCSRA |= (1 << ADEN);  // Enable ADC

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

   ADCSRA |= (1 << ADSC);  // Start A2D Conversions
  }

This needs to be in your interrupt or your infinite loop. As written, you are only polling the button as soon as the program starts.

This wont fix your current problem, but I'd hate for you to bump into another headache as soon as you solve this one.

 


Get Your Ad Here