bembe24:
Im currently creating a project where I can read the current and voltage reading from a appliance.
Below is the code I used to read the parameters:
void rawdata(){
sumI = 0;
sumV = 0;
for (int n=0; n<1000; n++) {
sampleVoltage = analogRead(VoltagePin);
sampleCurrent=analogRead(CurrentPin);
CurrentV = ((sampleCurrent5.0) /1024);//current
VoltageV = ((sampleVoltage5.0)/1024); // voltage
instVoltage = (VoltageV-2.5)*vin;
instCurrent = ((CurrentV-2.5)/0.066);
sumI += instCurrent * instCurrent;
sumV += instVoltage * instVoltage;
}
}
1. How long does arduino take to execute a single line of code? For the analogRead commands, what I have read was I takes 100us to read an analog input? Is there a mathematical way to show to they get the 100us?
2. How long does the arduino take to read the rest of the lines? From the CurrentV computations to the sumV computations. Is there a way to read the time without using the millis() or micros() function? Is there a fixed time for the execution time of a single code?
3. From the sumI and sumV calculations, whats the purpose of multiplying the instCurrent and instVoltage by itself?
bembe24:
VoltageV = ((sampleVoltage*5.0)/1024); // voltage
Why is the divisor 1024 instead of 1023?
- The Full Scale of the ADC is 5V (assuming Vref = AVcc = 5V). It is a 10-bit ADC.
- (a) So, when the input voltage is 0V; the ADC produces 0b00000000 = 0
(b) When the input voltage is 5V; the ADC produces 0b11 1111 1111 = 1024 - 1 = 1023.
(c) When the input voltage is Vin (Sample Voltage); the ADC will produce (5/1023)*Vin.
bembe24:
- How long does arduino take to execute a single line of code?
- Execution Time of an instruction can best be computed from ASM Coding. For example: Reading
Low Byte of ADC -
(a) ASM Code: in r16, ADCL
(b) Binary Code (Mavhine Code): b104 ; 16-bit executable Opcode
(c) Flash Memoy (Code memory) is Word (16-bit) organized.
(d) Atmel says that most of the AVR instructions take 1-CLK period for execution. (I assume that
it includes: Instruction fetching time from flash, internal decoding time, and then actual reading
ADCL data fom prot address 04h.)
(e) In ArduinoUNO ATmega runs at 16 MHz (Period: 0.0625 uS).
(f) So, the execution time of the instruction (a) : 0.0625 uS.
bembe24:
For the analogRead commands, what I have read was I takes 100us to read an analog input? Is there a mathematical way to show to they get the 100us?
4. analogRead(0) function, as I have understood for myself, consists of:
(a) Channel selction via ADMMUX : in r16, ADMUX
andi r16, 0b11110000
out ADMUX, r16
(b) Start the ADC (single conversion): in r16, ADCSRA
ori r16, 0x40
out ADCSRA, r16
(c) Check if conversion complete: LC: in r16, ADCSRA ; by polling ADIF flag bit
rol r16
rol r16
rol r16
rol r16
brcc LC
(d) Clear ADIF flag bit: in r16, ADCSRA
ori r16, 0x10
out ASCSRA
(e) Read ADCL: in r16, ADCL
Read ADCH: in r17, ADCH
5. According to my calculation, the execution time for the function analogRead(0): 17 (number of Single Word instructions in Step-4) x 0.0625 uS = 1.0625 uS!