473,411 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Measuring ADC samples and store in digital - how to calculate?

365 100+
I am reading an ADC Input by Microcontroller form AC realy, I have to store them to represent sign wave. when I read peak value, which is active High, I am reading 12 bit ADC value is 4090 (actually 4096, but some drop is there), sometimes I get 4086. the low I gets zero and middle I gets 2044 or 2040. Here

active high: 4094
middle: 2048.
active low : 0

I have make middle as centre point and I have to store 2048 as zero and active low as negative 1 and active high as 4094. how can I do in C language to store them, do I have to divide or modulus?. I am taking 1000 samples per Second, using all these samples, these samples I have to store between -1 and +1, can somebody through how can I store (when I read I get svalues between 0 and 4094 and I have to store them between -1 and +1). appreciated.
Nov 10 '09 #1
6 3355
mac11
256 100+
Are you just trying to figure out how to map the values (0 ... 4095) to (-1 ... +1)?

When converting ADC readings I like to think of it as chopping the measured range into pieces. The number of pieces is defined by the number of bits in your ADC. So here, you have 12 bits, which is 4096 small steps.

The thing you're chopping is the input voltage. You say you want your numbers to range from -1 to +1, so that means you're dividing 2 by 4096 (the 2 comes from -1 - 1).

So, each of the bits in the ADC number (svalue) represents 2/4096 volts (a tiny little step in voltage). Thinking about it this way should make more sense.

ADC reading of 1 (I mean svalue = 1) equates to 1 * (2 / 4096) = some tiny voltage (one little step in voltage)
ADC reading of 2 gives you 2 *(2 / 4096), which is double what the above value is (two steps in voltage)
ADC reading of N = N * 2 / 4096

But these aren't really the right range, these values go from 0 to 2 not -1 to +1 like you wanted. So you have to offset the result by subtracting one. So you end up with;

result to store = (svalue * 2 / 4096) - 1

This whole thing assumes your input scales linearly. And it's actually not really telling you voltage, just splitting your ADC svalue equally between -1 and +1.

If you wanted to interpret your ADC svalue as an actual voltage (which you didn't ask for, but I'm writing here...) you'd have to factor in your reference voltage and resistor divider factor on the ADC input line.

If you're just measuring some arbitrary sine wave and just want to store values as -1 to +1 it should be fine. If you're measuring the output of some device and trying to equate that to some real world measurement (speed, temperature, distance, etc) it might not be accurate.
Nov 10 '09 #2
tvnaidu
365 100+
Thanks for the e-mail.

This is to measure arbitrary since wave only, I think I have to declare all those variables as float instead integer since integer gets truncated and I will get zero when I do (svalue * 2 / 4096), if I declare variable "adc_samp1" below as float and then I can have a value between -1 and +1.

int svalue;
float adc_samp1;

svalue = read ADC Input //this value is between 0 and 4096

adc_samp1 = (svalue * 2 / 4096) -1; //this should be between -1 and +1.

that wy I can store all those sampled values in an float array to my final sinusoidal curve.
Nov 10 '09 #3
tvnaidu
365 100+
I am reading 24 ADC inputs with 1ms Timer, but it is taking more time to read, anybody have idea about ADC?
Nov 13 '09 #4
tvnaidu
365 100+
I have all are floating point vars. it take 8.3us for each FP (+,*)?. I have 48 of those plus 8 more total additions in addition to select GPIO for each LED and reading each sample (56 * 8.3us = 464.8 us, almost half of 1ms). if I comment this below code, I have print for half-second at the end, I can see my print coming very faster, if I enable below code, print is very very slow. this is my separate task, the first flag (get_curr_read_flag = TRUE) set by PIT 1ms timer, here it loks for that flag TRUE, then it reset to FALSE, then read one set of ADC samples and do math and store them in accumulators, then I have local counter here for 500 for Half-sec (measure_power_factor), when this reaches 500 (SAMPLES/2), I reset this to zero and do power calculations. this is my code. I have print all the way below for half-sec, if I comment all these FP (+,*), I get that orint very quickly, if I enable these, it is very slow, I gets print for every 5 Seconds.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #define SAMPLES 1000
  4.  
  5. TK_ENTRY(tk_adc_readings)
  6. {
  7.  int err;
  8.  int temp;
  9. int i;
  10.  
  11. float curr_buf1 =0;
  12. float curr_buf2 =0;
  13. float curr_buf3 =0;
  14. float curr_buf4 =0;
  15. float curr_buf5 =0;
  16. float curr_buf6 =0;
  17. float curr_buf7 =0;
  18. float curr_buf8 =0;
  19. float curr_buf9 =0;
  20. float curr_buf10 =0;
  21. float curr_buf11 =0;
  22. float curr_buf12 =0;
  23. float curr_buf13 =0;
  24. float curr_buf14 =0;
  25. float curr_buf15 =0;
  26. float curr_buf16 =0;
  27. float curr_buf17 =0;
  28. float curr_buf18 =0;
  29. float curr_buf19 =0;
  30. float curr_buf20 =0;
  31. float curr_buf21 =0;
  32. float curr_buf22 =0;
  33. float curr_buf23 =0;
  34. float curr_buf24 =0;
  35. float Board_Total =0;
  36.  
  37.  
  38.  
  39.  while (!net_ready)
  40.   TK_SLEEP(1);
  41.  
  42.  
  43.  
  44.  
  45.  err = eg_init();
  46.  
  47.  if( err == SUCCESS )
  48.  {
  49.   exit_hook(eg_cleanup);
  50.  }
  51.  else
  52.  {
  53.   dtrap();         // eg_init()  shouldn't ever fail
  54.  }
  55.  
  56.  for (;;)
  57.  {
  58.  
  59.    //this get_curr_read_flag seyt by PIT 1ms timer, reset here  
  60.  
  61. if (get_curr_read_flag == TRUE) {
  62.    get_curr_read_flag = FALSE;
  63.  
  64.    //comment this temporarily - added this function code below
  65.    //get_readings();
  66.  
  67.    VOLT_READ1;
  68.    for(i=0; i < 0x22; i++)
  69.    volt_buf1 = (int)((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 407 ) / 2048;
  70.    volt1_accu += volt_buf1 * volt_buf1;
  71.  
  72.  
  73.  
  74.    CURR_READ1;
  75.    for(i=0; i < 0x22; i++)
  76.    curr_buf1 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  77.    curr1_accu += curr_buf1 * curr_buf1;
  78.    power1_accu += volt_buf1 * curr_buf1;
  79.  
  80.  
  81.  
  82.    CURR_READ2;
  83.    for(i=0; i < 0x22; i++)
  84.    curr_buf2 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  85.     curr2_accu += curr_buf2 * curr_buf2;
  86.    power2_accu += volt_buf1 * curr_buf2;
  87.  
  88.  
  89.  
  90.     CURR_READ3;
  91.    for(i=0; i < 0x22; i++)
  92.    curr_buf3 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  93.     curr3_accu += curr_buf3 * curr_buf3;
  94.    power3_accu += volt_buf1 * curr_buf3;
  95.  
  96.  
  97.  
  98.      CURR_READ4;
  99.    for(i=0; i < 0x22; i++)
  100.    curr_buf4 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  101.       curr4_accu += curr_buf4 * curr_buf4;
  102.    power4_accu += volt_buf1 * curr_buf4;
  103.  
  104.  
  105.  
  106.        CURR_READ5;
  107.    for(i=0; i < 0x22; i++)
  108.    curr_buf5 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  109.        curr5_accu += curr_buf5 * curr_buf5;
  110.    power5_accu += volt_buf1 * curr_buf5;
  111.  
  112.  
  113.  
  114.        CURR_READ6;
  115.    for(i=0; i < 0x22; i++)
  116.    curr_buf6 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  117.        curr6_accu += curr_buf6 * curr_buf6;
  118.    power6_accu += volt_buf1 * curr_buf6;
  119.  
  120.  
  121.  
  122.        CURR_READ7;
  123.    for(i=0; i < 0x22; i++)
  124.    curr_buf7 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  125.        curr7_accu += curr_buf7 * curr_buf7;
  126.    power7_accu += volt_buf1 * curr_buf7;
  127.  
  128.  
  129.  
  130.        CURR_READ8;
  131.    for(i=0; i < 0x22; i++)
  132.    curr_buf8 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  133.        curr8_accu += curr_buf8 * curr_buf8;
  134.    power8_accu += volt_buf1 * curr_buf8;
  135.  
  136.  
  137.  
  138.    curr_18_BT = curr_buf1+curr_buf2 +curr_buf3+ curr_buf4 + curr_buf5 + curr_buf6 + curr_buf7  + curr_buf8;
  139.    curr_18_AT += curr_18_BT * curr_18_BT;
  140.  
  141.  
  142.    VOLT_READ2;
  143.    for(i=0; i < 0x22; i++)
  144.    volt_buf2 = (int)((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 407 ) / 2048;
  145.        volt2_accu += volt_buf2 * volt_buf2;
  146.  
  147.  
  148.  
  149.        CURR_READ9;
  150.    for(i=0; i < 0x22; i++)
  151.    curr_buf9 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  152.        curr9_accu += curr_buf9 * curr_buf9;
  153.    power9_accu += volt_buf2 * curr_buf9;
  154.  
  155.  
  156.  
  157.        CURR_READ10;
  158.    for(i=0; i < 0x22; i++)
  159.    curr_buf10 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  160.        curr10_accu += curr_buf10 * curr_buf10;
  161.    power10_accu += volt_buf2 * curr_buf10;
  162.  
  163.  
  164.  
  165.     CURR_READ11;
  166.    for(i=0; i < 0x22; i++)
  167.    curr_buf11 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  168.        curr11_accu += curr_buf11 * curr_buf11;
  169.    power11_accu += volt_buf2 * curr_buf11;
  170.  
  171.  
  172.  
  173.        CURR_READ12;
  174.    for(i=0; i < 0x22; i++)
  175.    curr_buf12 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  176.        curr12_accu += curr_buf12 * curr_buf12;
  177.    power12_accu += volt_buf2 * curr_buf12;
  178.  
  179.        CURR_READ13;
  180.    for(i=0; i < 0x22; i++)
  181.    curr_buf13 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  182.        curr13_accu += curr_buf13 * curr_buf13;
  183.    power13_accu += volt_buf2 * curr_buf13;
  184.  
  185.  
  186.  
  187.        CURR_READ14;
  188.    for(i=0; i < 0x22; i++)
  189.    curr_buf14 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  190.        curr14_accu += curr_buf14 * curr_buf14;
  191.    power14_accu += volt_buf2 * curr_buf14;
  192.  
  193.  
  194.  
  195.        CURR_READ15;
  196.    for(i=0; i < 0x22; i++)
  197.    curr_buf15 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  198.        curr15_accu += curr_buf15 * curr_buf15;
  199.    power15_accu += volt_buf2 * curr_buf15;
  200.  
  201.  
  202.  
  203.        CURR_READ16;
  204.    for(i=0; i < 0x22; i++)
  205.    curr_buf16 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  206.        curr16_accu += curr_buf16 * curr_buf16;
  207.    power16_accu += volt_buf2 * curr_buf16;
  208.  
  209.  
  210.  
  211.    curr_9_16_BT = curr_buf9+curr_buf10 +curr_buf11+ curr_buf12 + curr_buf13 + curr_buf14 + curr_buf15  + curr_buf16;
  212.    curr_9_16_AT += curr_9_16_BT * curr_9_16_BT;
  213.  
  214.  
  215.    VOLT_READ3;
  216.    for(i=0; i < 0x22; i++)
  217.    volt_buf3 =(int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 407 ) / 2048;
  218.       volt3_accu += volt_buf3 * volt_buf3;
  219.  
  220.  
  221.  
  222.       CURR_READ17;
  223.    for(i=0; i < 0x22; i++)
  224.    curr_buf17 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  225.       curr17_accu += curr_buf17 * curr_buf17;
  226.    power17_accu += volt_buf3 * curr_buf17;
  227.  
  228.  
  229.  
  230.       CURR_READ18;
  231.    for(i=0; i < 0x22; i++)
  232.    curr_buf18 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  233.       curr18_accu += curr_buf18 * curr_buf18;
  234.    power18_accu += volt_buf3 * curr_buf18;
  235.  
  236.  
  237.  
  238.       CURR_READ19;
  239.    for(i=0; i < 0x22; i++)
  240.    curr_buf19 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  241.       curr19_accu += curr_buf19 * curr_buf19;
  242.    power19_accu += volt_buf3 * curr_buf19;
  243.  
  244.  
  245.  
  246.       CURR_READ20;
  247.    for(i=0; i < 0x22; i++)
  248.    curr_buf20 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  249.        curr20_accu += curr_buf20 * curr_buf20;
  250.    power20_accu += volt_buf3 * curr_buf20;
  251.  
  252.  
  253.  
  254.       CURR_READ21;
  255.    for(i=0; i < 0x22; i++)
  256.    curr_buf21 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  257.       curr21_accu += curr_buf21 * curr_buf21;
  258.    power21_accu += volt_buf3 * curr_buf21;
  259.  
  260.  
  261.  
  262.       CURR_READ22;
  263.    for(i=0; i < 0x22; i++)
  264.    curr_buf22 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  265.       curr22_accu += curr_buf22 * curr_buf22;
  266.    power22_accu += volt_buf3 * curr_buf22;
  267.  
  268.  
  269.  
  270.       CURR_READ23;
  271.    for(i=0; i < 0x22; i++)
  272.    curr_buf23 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  273.       curr23_accu += curr_buf23 * curr_buf23;
  274.    power23_accu += volt_buf3 * curr_buf23;
  275.  
  276.  
  277.  
  278.       CURR_READ24;
  279.    for(i=0; i < 0x22; i++)
  280.    curr_buf24 = (int) ((((MCF_ADC_ADRSLT7&0x7FF8)>>3) - 2048) * 100) / 2048;   
  281.       curr24_accu += curr_buf24 * curr_buf24;
  282.    power24_accu += volt_buf3 * curr_buf24;
  283.  
  284.  
  285.  
  286.    curr_17_24_BT = curr_buf17+curr_buf18+curr_buf19+curr_buf20+curr_buf21+curr_buf22+curr_buf23+curr_buf24;
  287.    Board_Total = curr_18_BT + curr_9_16_BT + curr_17_24_BT;
  288.    Board_Total_AT += Board_Total * Board_Total;
  289.    curr_17_24_AT += curr_17_24_BT * curr_17_24_BT;
  290.  
  291.  
  292.               measure_power_factor ++;
  293.   }
  294.  
  295.  
  296.  
  297.    //for every Half-second calculate powers and reset this flag
  298.  
  299.  
  300.          if (measure_power_factor == (SAMPLES/2))  {
  301.          //calculate RMS currents and voltages and powers
  302.   //calc_powers();
  303.              measure_power_factor = 0;
  304.          printf("********* Half-second-print ****************************\n");
  305.  
  306.  
  307.         }
  308.  
  309.  
  310.  
  311.  
  312.   //eg_loop();             // will block if anything needs to be done
  313.  
  314.   tk_yield();             // give up CPU in case it didn't block
  315.  
  316.   eg_wakes++;
  317.  
  318.   if (net_system_exit)
  319.    break;
  320.  }
  321.  
  322.  
  323.  
  324.  TK_RETURN_OK();
  325. }
  326.  
Nov 14 '09 #5
tvnaidu
365 100+
floating point variable here slowing down my CPU, since my CPU clock is running at 60MHz. I changed all float variables to integer, I can see lot of difference.
Nov 15 '09 #6
tvnaidu
365 100+
I found the issue, it is in for loop. sorry for posting this. I don't know may be I can remove that code?.
Nov 15 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jayne Wolps | last post by:
Hello I wonder if anyone can help. I would like to know how certain sites: http://aboutbritain.com/ArundelCastle.htm, and http://travel.knowhere.co.uk/place/+bristol-0/ manage to put approx...
7
by: Guangxi Wu | last post by:
Hi all, Happy New Year. I am using SignedXML and an X509 certificate to digitally sign a SOAP message body and put the signature in the SOAP header for a B2B business application. Can you...
3
by: Xavier | last post by:
hello, i have a kind of workflow like: enter data in a form aprove data by person 1 aprove data by person 2 data are saved in a sqldatabase
8
by: HalcyonWild | last post by:
Hi, I installed the free version(command line only) of the digital mars c++ compiler. It said it features a garbage collection mechanism, but there was no documentation. I figured out that...
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
1
by: almurph | last post by:
Hi, How do you measure the degree of sorted/unsortedness of an array in VB.NET? Are there any well known algorithms out there for doing this? Any help/assistance/code-samples/suggestions much...
0
by: tsalikivenu | last post by:
Hi i have problem with loading digital certificates.i am currently using asp.net 2003.i have installed wse2.0 here is my code..i want it to load digital certficates to list box..it is...
1
by: PeterAlt | last post by:
I have a Yahoo store. In order for me to access via javascript or HTML (on the client side) any information stored in Yahoo's product fields, I have to use something that looks like this... ...
0
by: shwetamodi | last post by:
Hello everyone, I am working on an ASP.Net Project and i have used digital signature in my project and it is working perfectly fine in my system but when i run it on IIS server then my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.