+ Post New Thread
Results 1 to 11 of 11
  1. #1
    Advanced Member level 4
    Points: 12,143, Level: 26
    Achievements:
    Created Blog entry 7 years registered
    thannara123's Avatar
    Join Date
    Jan 2010
    Location
    Nedumkandam, India, India
    Posts
    1,417
    Helped
    117 / 117
    Points
    12,143
    Level
    26
    Blog Entries
    2

    Atmega8 crashes when ADC value reads at a fast rate

    Hello ,
    I am making an automatic stabilzer by using atmega8 . I am taking AC input and output directly (with very low gain) and connected to ADC and the part of mesuaring and relay section works correctely [not finall].

    But when i varies the voltage speedly the atmega8 is crashes

    FOR MORE plesas see the the working video .

    what will be the problem .

    I am using float to int by typecasting

    PART OF THE CODE

    Code:
     data_value();         input_volt =  volt_read_disply(0);
    	     DELAY_ms(100);
    	     volt =(int)input_volt;       // double to int casting
    	     LCD_GoToXY(0,7);
    	     LCD_DisplayNumber(10,volt,3);
    		 output_volt =  volt_read_disply(1);
    		 DELAY_ms(100);
    		 LCD_GoToXY(1,7);
    		 LCD_DisplayNumber(10,output_volt,3);
    Code:
    double volt_read_disply(int c){   int i,adc_value[40]={0}; int temp =0;
    	
    	adc_init();
    	
    	  
    	  for(i=0; i<40;i++)                        // samples taking from 41 times 9.4milli second taken about on complte half cycle
    	  { DELAY_ms(5);
    		 adc_value[i] = read_adc(c);  // reading voltage 		  	  	  
    	 }
    	  
    	 temp = adc_value[0];	 
    	for(i=0; i<40; i++)
    	{
    		if(temp
    
    REFERANCE : https://www.edaboard.com/showthread....g-up-somewhere
    Last edited by thannara123; 2nd May 2018 at 03:19.
    I Respect You

    •   Alt2nd May 2018, 03:13

      advertising

        
       

  2. #2
    Super Moderator
    Points: 58,794, Level: 59
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    11,981
    Helped
    2776 / 2776
    Points
    58,794
    Level
    59

    Re: Atmega8 crashes when ADC value reads at a fast rate

    Hi,

    I didn't go through your code.
    But you do a time critical control loop. I recommend to avoid float calculations.

    Use 16 bit integers (signed or unsigned ...depending what you need) ..I assume for a voltage stabilizer even 8 bit calculations/resolution could work.


    Additionally I recommend to learn how to use interrupt driven control loops.
    Don't waste 99.9% of your processing power for busy waits like "delay_ms()".
    (An AVR has enough processing power to continously process 1600 samples/s on two channels, squaring, averaging, square root on two channels, with an output data rate of 2.5ms in real time - without missing a single conversion - while it is still able to update the display and communicate via UART)

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. Thank you.


    1 members found this post helpful.

  3. #3
    Super Moderator
    Points: 26,514, Level: 39
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    7,965
    Helped
    995 / 995
    Points
    26,514
    Level
    39
    Blog Entries
    5

    Re: Atmega8 crashes when ADC value reads at a fast rate

    Quote Originally Posted by thannara123 View Post
    Code:
    	  for(i=0; i<40;i++)             	  
             { DELAY_ms(5);
    		 adc_value[i] = read_adc(c);  // reading voltage 		  	  	  
    	 }
    Don't seem clear that you're wasting at least 200ms for every A/D conversion doing nothing?
    What is the usefulness of the loop, considering that only the last value will be computed?
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )


    1 members found this post helpful.

  4. #4
    Advanced Member level 4
    Points: 12,143, Level: 26
    Achievements:
    Created Blog entry 7 years registered
    thannara123's Avatar
    Join Date
    Jan 2010
    Location
    Nedumkandam, India, India
    Posts
    1,417
    Helped
    117 / 117
    Points
    12,143
    Level
    26
    Blog Entries
    2

    Re: Atmega8 crashes when ADC value reads at a fast rate

    Quote Originally Posted by andre_teprom View Post
    Don't seem clear that you're wasting at least 200ms for every A/D conversion doing nothing?
    What is the usefulness of the loop, considering that only the last value will be computed?
    Sorry for late ,
    That delay doing nothing , but without that delay the volatge showing like 229,135 ,220,110 etc shows low value some time.

    May i need to take the avarage value ?

    - - - Updated - - -

    https://youtu.be/6OncbopIX2I
    please see the video.

    - - - Updated - - -

    Quote Originally Posted by KlausST View Post
    Hi,

    I didn't go through your code.
    But you do a time critical control loop. I recommend to avoid float calculations.

    Use 16 bit integers (signed or unsigned ...depending what you need) ..I assume for a voltage stabilizer even 8 bit calculations/resolution could work.


    Additionally I recommend to learn how to use interrupt driven control loops.
    Don't waste 99.9% of your processing power for busy waits like "delay_ms()".
    (An AVR has enough processing power to continously process 1600 samples/s on two channels, squaring, averaging, square root on two channels, with an output data rate of 2.5ms in real time - without missing a single conversion - while it is still able to update the display and communicate via UART)

    Klaus
    changed the double to unsigned 16 bit uint16_t .
    what wrong i did with intrupt driven ?
    may i know the details ?
    Last edited by thannara123; 16th May 2018 at 17:39.
    I Respect You



    •   Alt16th May 2018, 17:31

      advertising

        
       

  5. #5
    Super Moderator
    Points: 26,514, Level: 39
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    7,965
    Helped
    995 / 995
    Points
    26,514
    Level
    39
    Blog Entries
    5

    Re: Atmega8 crashes when ADC value reads at a fast rate

    Quote Originally Posted by thannara123 View Post
    That delay doing nothing , but without that delay the volatge showing like 229,135 ,220,110 etc shows low value some time.
    Seems like you have solved the problem by accident, without properly knowing the reason, and this is a bad practice. From the time you are here, you should have already assimilated some of the tips given on other occasions, and the avoid to use long delays where often mentioned.

    Quote Originally Posted by thannara123 View Post
    May i need to take the avarage value ?
    Not for a voltage stabilizer supposed to act realtime, and using so long delays. Moreover, how to know without knowing the circuit you are using ? Why don't you make simulations in software before testing in real world?
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )


    1 members found this post helpful.

    •   Alt16th May 2018, 19:37

      advertising

        
       

  6. #6
    Super Moderator
    Points: 58,794, Level: 59
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    11,981
    Helped
    2776 / 2776
    Points
    58,794
    Level
    59

    Re: Atmega8 crashes when ADC value reads at a fast rate

    Hi,

    May i need to take the avarage value ?
    I assume you talk about an AC voltage stabilizer.
    Then for sure you know that the AC voltage moves from zero to positiv peak, to zero, to negative peak ...in a loop.
    So a random measurement will give unpredictable results some where between negative peak and positive peak.
    You don't show your schematic, so we just have to guess how you process the analog signal before it goes to the ADC.

    In the end it is urgent that you average the (rectified) signal somehow. You may do this on the analog side or at the digital side.
    I personally like to do it at the digital side. It's reduces part count and cost, it more precise and stable and it is easier to adjust.
    But it needs some clean AD sampling and a bit more software effort.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. Thank you.



  7. #7
    Advanced Member level 4
    Points: 12,143, Level: 26
    Achievements:
    Created Blog entry 7 years registered
    thannara123's Avatar
    Join Date
    Jan 2010
    Location
    Nedumkandam, India, India
    Posts
    1,417
    Helped
    117 / 117
    Points
    12,143
    Level
    26
    Blog Entries
    2

    Re: Atmega8 crashes when ADC value reads at a fast rate

    it is not a satbilizer ,It is a dual Ac voltmeter
    the circuit is given below .
    https://www.edaboard.com/attachment....1&d=1526518958
    I Respect You



  8. #8
    Super Moderator
    Points: 26,514, Level: 39
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    7,965
    Helped
    995 / 995
    Points
    26,514
    Level
    39
    Blog Entries
    5

    Re: Atmega8 crashes when ADC value reads at a fast rate

    Quote Originally Posted by thannara123 View Post
    I am making an automatic stabilzer by using atmega8 . I am taking AC input and output directly (with very low gain) and connected to ADC and the part of mesuaring and relay section works correctely [not finall]
    The original description seemed as an AC stabilzer, as well as the video title refers to a voltage stabilizer.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  9. #9
    Advanced Member level 4
    Points: 12,143, Level: 26
    Achievements:
    Created Blog entry 7 years registered
    thannara123's Avatar
    Join Date
    Jan 2010
    Location
    Nedumkandam, India, India
    Posts
    1,417
    Helped
    117 / 117
    Points
    12,143
    Level
    26
    Blog Entries
    2

    Re: Atmega8 crashes when ADC value reads at a fast rate

    I am trying to deal direct AC to an opamp with a very low gain , And the Ac signal to get corresponding DC signal and passed to the microcontroller.

    - - - Updated - - -

    Quote Originally Posted by andre_teprom View Post
    The original description seemed as an AC stabilzer.
    sorry the same volt meter techniqe is used (the same error shows in both voltmeter and the first post)
    reference : https://www.edaboard.com/showthread....g-up-somewhere
    I Respect You



  10. #10
    Super Moderator
    Points: 26,514, Level: 39
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    7,965
    Helped
    995 / 995
    Points
    26,514
    Level
    39
    Blog Entries
    5

    Re: Atmega8 crashes when ADC value reads at a fast rate

    There is nothing in your code that ensures the reading of the input synchronously to the AC waveform, so that since you are not rectifying the input signal the average of a big ammount of samples is perhaps the most viable solution, but you will have to remove any useless delay or void loop from your program, as well as you will have to deal with a larger response time, assuming the original scenario of the stabilizer that you have suddenly changed.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )


    1 members found this post helpful.

    •   Alt17th May 2018, 02:24

      advertising

        
       

  11. #11
    Super Moderator
    Points: 58,794, Level: 59
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    11,981
    Helped
    2776 / 2776
    Points
    58,794
    Level
    59

    Re: Atmega8 crashes when ADC value reads at a fast rate

    Hi,

    As far as I can see there is no averaging in the analog side.
    This means all the averaging needs to be done by software.
    I assume the voltage at the ADC input is about 1.5V DC when there is no signal input.
    With AC at the input you will see the: divided_AC_voltage + this 1.5V DC at the ADC.
    --> Use a scope (and a DVM) to verify this. Give us your results.

    --> What's your AC frequency range? A rough estimation will do. Is it grid mains?

    --> How often per second do you need a calculated value? (Or calculation interval / regulation interval time)

    --> what resolution of your calculated values do you need?
    --> what precision of your calculated values do you need? (Or: how much noise and ripple can you accept)

    ********
    There are some methods to determine the amplitude*
    1) doing a lot of random samples, find the +/- peak values, multiply them by a constant factor. --> simple math but not the output values will vary a lot.
    2) doing a lot of random samples. Subtract offset, rectifying, averaging. --> simple math. Much better than the "peak" solution, but still there will be some ripple in the output signal
    3) doing a calculated count of perfectly timed samples. Subtract offset, rectifying, averaging. --> simple math. Much better than the "peak" solution. Much better ripple suppression.
    4) doing a calculated count of perfectly timed samples. Subtract offset, squaring, averaging, square root (true RMS) --> needs carefully selected variable sizes, square root takes some processing power. Exact RMS value with low ripple.

    I don't recommend 1) because of the huge uncertainty
    I don't recommend 4) for beginners. (This is how I'd do it, but I'm a perfectionist)

    --> you have to choose a your way.

    Klaus
    Please donīt contact me via PM, because there is no time to respond to them. Thank you.



--[[ ]]--