Split from: code for AC current measurement

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please help me!!!
What's wrong this code?? 

    while(1)
    
     {    

    for(count=0;count<=(adc_get_sample-1);count++)
       {
          value=adc_read(1);
          Sum_Ad=(value*5)/1024;
          Sum_Ad =Sum_Ad-5;
          buff[count]=Sum_Ad;
       }

    Volt=buff[0];

   for(count=1;count<=(adc_get_sample-1);count++)
      {
       if(Volt<buff[count])
         {
             Volt = buff[count];
            Volt_prd=Volt*Volt;
            Volt_sum=Volt_sum+Volt_prd;
            values[count]=Volt_sum;
         }
      }
    
    Volt_avr=(values[adc_get_sample-1]/((double)adc_get_sample));
    Volt_sqrt=(double)(sqrt(Volt_avr));
    result=(double)Volt_sqrt;
    
    dtostrf(result,5,1,string);
    sprintf(buffer_Am,"%0.3famps",result);
    _delay_us(10);
    LCD_goto(1,4);
    lCD_puts(buffer_Am);    
     } 

}

Last Edited: Sat. Oct 5, 2019 - 01:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
 while(1)
    
     {    
    for(count=0;count<=(adc_get_sample-1);count++)
       {
          value=adc_read(1);
          Sum_Ad=(value*5)/1024;
          Sum_Ad =Sum_Ad-5;
          buff[count]=Sum_Ad;
       }
    Volt=buff[0];
   for(count=1;count<=(adc_get_sample-1);count++)
      {
       if(Volt<buff[count])
         {
             Volt = buff[count];
            Volt_prd=Volt*Volt;
            Volt_sum=Volt_sum+Volt_prd;
            values[count]=Volt_sum;
         }
      }
    
    Volt_avr=(values[adc_get_sample-1]/((double)adc_get_sample));
    Volt_sqrt=(double)(sqrt(Volt_avr));
    result=(double)Volt_sqrt;
    
    dtostrf(result,5,1,string);
    sprintf(buffer_Am,"%0.3famps",result);
    _delay_us(10);
    LCD_goto(1,4);
    lCD_puts(buffer_Am);    
     } 
}

What's wrong with the code? There's a lot missing.

 

Tell us what the observed problem is and what you've done to narrow down the issue.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why have you hijacked an 8 year old thread

 

EDIT: was originally posted here:  https://www.avrfreaks.net/forum/code-ac-current-measurement

 

sculpter wrote:
What's wrong this code?? 

For a start, you didn't use the proper tool to post it - see Tip #1.

 

But tell us:

 

  1. Does it compile without errors or warnings ?
  2. What do you want it to do ?
  3. What is it actually doing ?
  4. What testing / investigation / debugging have you done to find the problem(s) ?

 

We would need to know what chip it's for, and what (if any) external stuff it relies upon.

 

And what toochain / IDE you're using.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Oct 5, 2019 - 02:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
I'm sorry my friend. I didn't log in properly.I want my code to read the Ac signal with the ADC and then sample it.As a result, I hope to find the RMS value. I use Atmega32 as the controller. I use AVR Studio as compiler

Node: The input signal is 220 volts 50 Hz and ı used CT, 

Last Edited: Sat. Oct 5, 2019 - 11:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


sculpter wrote:
 The input signal is 220 volts 

Remember that 220 V is potentially lethal - this is not suitable as a beginner project!

 

You should only be doing this under competent supervision.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Oct 5, 2019 - 11:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


ı 

 

ı did the simulation with proteus.I took samples looking at the internet. But even if I try to create the code layout, I cannot find the range I want.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sculpter wrote:
if I try to create the code layout, I cannot find the range I want.

What do you mean by that??

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you actually build this circuit?  What, exactly, have you done & what are you doing?  What does proteus have to do with any of this?

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
What does proteus have to do with any of this?

(s)he said (s)he "did the simulation" with it.

 

Whatever that means?!

 

frown

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your U1a +ve input has no DC reference voltage. You could try tying the low side of the current transformer to GND.