#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Need tons of help asap!!s.o.s


    Hello guys,
    im an Industrial Informatics student and im working on a project concerning an automatic moving photovoltaic. I use light sensors for motion. Automatically it has to find the less sensor value and moves to that position .. Im using 20 sensors.. I have difficulty in reading the sensors as they use Adc...
    Using ATmega 16A
    Atmel Studio 6.0

    I really need ur help guys if its possible!!Thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,893
    Rep Power
    481
    Do you have a cute little robot with wheels and motors and cpu and beeping and blinking lights (I hope not) and stuff? Would an ATmega forum be more useful to you?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    nop , just a piece of wood/steel..no wheels & stuff. Just a photovoltaic with sensors / stepper motors etc. AVR Forum didnt help cuz they dont say anything about programming!
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    The first thing I would suggest is you look through the atmel application notes and review any of them which mention ADC, especially any which focus on basic operation.

    > Im using 20 sensors.. I have difficulty in reading the sensors as they use Adc...
    Start small and simple.
    Using just one sensor, write a program that turns on a LED when the sensor is exposed to light, and turns off the LED when there is no (or less) light on the sensor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Is your problem with the ADCs? Just what is the problem? Do you not know how to interface with them? The data sheets should tell you that. Do you not know what to do with the digital value you receive from them? Well, what's the conversion function? Just exactly what is the problem that you're having?

    In one product I worked on (computerized greenhouse controls), we had sensors that were hooked up to ADCs which were read by microprocessors (more primitive than 8051s, so that was purely in assembly) and then those values were transmitted back to a PC which converted the readings into temperature, humidity, light level, whatever. In most cases, it was a simple linear formula of 0 to 5 volts corresponding to digital values from 0 to 255 which in turn corresponded to readings from some minimum to some maximum. In the case of one humidity sensor, the curve was not linear so we had to use a look-up table. What is the conversion formula for your sensors? What is keeping you from simply applying that formula?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    @salem
    Im gonna post the program in a while
    @dwise1_aol
    Its my first time "playing" with ADCs , reading sensors and etc, so yea basically i dont know how to interface with them. Is it possible to add u on skype?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    #include <avr/io.h>
    #include <stdio.h>
    #include <avr/interrupt.h>
    #include <util/delay.h>



    void adc_init(void);
    uint16_t adc_read(uint8_t ADC_channel);

    int main(void){

    adc_init();

    while(1){
    int i;
    uint16_t min;
    int imin;
    uint16_t pinakas[8];
    DDRC = 0xFF;
    PORTC = 0X00;

    for (i=0; i<8; i++)
    {
    pinakas[i]=adc_read(i);
    }

    min=pinakas[0];
    imin=0;

    for(i=0;i<8;i++)
    {
    if(min>pinakas[i])
    {
    min=pinakas[i];
    imin=i;
    }
    }


    switch (imin)
    {
    case 1:
    if (imin=1)
    {
    PORTC =0b00000001;
    }
    break;
    case 2:
    if (imin=2)
    {
    PORTC =0b00000010;
    }
    break;
    case 3:
    if (imin=3)
    {
    PORTC =0b00000011;
    }
    break;
    case 4:
    if (imin=4)
    {
    PORTC =0b00000100;
    }
    break;
    case 5:
    if (imin=5)
    {
    PORTC =0b00000101;
    }
    break;
    case 6:
    if (imin=6)
    {
    PORTC =0b00000110;
    }
    break;
    case 7:
    if (imin=7)
    {
    PORTC =0b00000111;
    }
    break;
    case 8:
    if (imin=8)
    {
    PORTC =0b00001000;
    }
    break;
    default:
    {
    PORTC =0b11111111;
    }
    }
    }
    return 0;
    }

    void adc_init(){
    ADCSRA |=(1<<ADPS2)| (1<<ADPS1)|(1<<ADPS0); // Prescaler 8 (4 MHz/8=125KHz).
    ADMUX |= (1<<REFS0); // AVCC with external capacitor at AREF pin.
    ADMUX |= (1 << ADLAR);
    ADCSRA |= (1<<ADEN); // Enable ADC.
    ADCSRA |= (1<<ADSC); // Start ADC conversion
    }

    uint16_t adc_read(uint8_t ADC_channel){
    int16_t adc_val;
    ADMUX = (ADMUX & 0xF0) | (ADC_channel & 0x0F);
    ADCSRA |= (1<<ADSC);
    while( ADCSRA & (1<<ADSC) );// wait until ADC conversion is complete
    adc_val = ADC;
    return adc_val;
    }

    I dont understand whats the prob...the 3rd led lights up only
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    Buenos Aires, Argentina
    Posts
    64
    Rep Power
    2
    Originally Posted by RoN#32

    while(1){
    int i;
    uint16_t min;
    int imin;
    uint16_t pinakas[8];
    DDRC = 0xFF;
    PORTC = 0X00;

    for (i=0; i<8; i++)
    {
    pinakas[i]=adc_read(i);
    }

    min=pinakas[0];
    imin=0;

    for(i=0;i<8;i++)
    {
    if(min>pinakas[i])
    {
    min=pinakas[i];
    imin=i;
    }
    }


    switch (imin)
    {
    case 1:
    if (imin=1)
    {
    PORTC =0b00000001;
    }
    break;
    case 2:
    if (imin=2)
    {
    PORTC =0b00000010;
    }
    break;
    case 3:
    if (imin=3)
    {
    PORTC =0b00000011;
    }
    break;
    case 4:
    if (imin=4)
    {
    PORTC =0b00000100;
    }
    break;
    case 5:
    if (imin=5)
    {
    PORTC =0b00000101;
    }
    break;
    case 6:
    if (imin=6)
    {
    PORTC =0b00000110;
    }
    break;
    case 7:
    if (imin=7)
    {
    PORTC =0b00000111;
    }
    break;
    case 8:
    if (imin=8)
    {
    PORTC =0b00001000;
    }
    break;
    default:
    {
    PORTC =0b11111111;
    }
    }
    }
    return 0;
    }
    I just wonder if that infinite loop would have something to do.
    Maybe I'm wrong, but doesn't it avoids the program to return 0?

    As I said, maybe I'm wrong.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    3
    DP
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    3
    The infinite loop just causes the CPU to loop through the exact same code indefinitely until the power is shut off. That is a normal technique for embedded code.

    Is there a way you can tell if adc_init() has properly initialized the ADC's ?

    And what about adc_read(i) ? Is there a way you can either print out a value or illuminate an LED when it returns some value but extinguish the LED when it returns some other value? That would give you an indication of whether or not it is properly reading an analog voltage that you could manually change for testing.

    Comments on this post

    • MauroEldritch agrees : Thanks for explaining it to me. +rep.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    Its running for 2 sensors , but not for more...Cant understand whats going on...Can someone help me a bit?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    3
    Google "AVR AT Mega16A enable ADC" for lots of tutorials on using the ADCs in this CPU. You probably forgot something. I don't know what it is. Trying out the tutorials might help you figure out the missing link. If the ADCs are interrupt-driven, it is also possible something in the code masks off all the ADC interrupts except for the two that are working. Investigate that possibility.

    P.S. You need to figure out whether the ADC interrupts are hardware interrupts or software interrupts. If they are software interrupts, then it is a distinct possibility that some ADC interrupts are disabled. If they are hardware interrupts, then you will need to investigate further.
    Last edited by EEmaestro; October 29th, 2012 at 11:21 AM.

IMN logo majestic logo threadwatch logo seochat tools logo