I Have made this code to be able to read the grids frequency, ( some files are not required as you can run code without however i left in to see the process)

Dependencies:   mbed Servo LM75B C12832_lcd

Committer:
qosay147
Date:
Tue May 28 11:43:01 2019 +0000
Revision:
0:c56707c70c71
Reading frequency from mains at a very fast rate and displaying on PC.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
qosay147 0:c56707c70c71 1 //This code is for 8Vpp or amplitude of 4V when connected to mbed
qosay147 0:c56707c70c71 2
qosay147 0:c56707c70c71 3 #include "mbed.h"
qosay147 0:c56707c70c71 4 #include "C12832_lcd.h"
qosay147 0:c56707c70c71 5 #include <complex>
qosay147 0:c56707c70c71 6 #include "Servo.h"
qosay147 0:c56707c70c71 7
qosay147 0:c56707c70c71 8 C12832_LCD lcd;
qosay147 0:c56707c70c71 9 AnalogIn VIN(A2);
qosay147 0:c56707c70c71 10 Servo s1(p22);
qosay147 0:c56707c70c71 11 Servo s2(p23);
qosay147 0:c56707c70c71 12 Serial pc(USBTX, USBRX); // see how to use this to print off to PC
qosay147 0:c56707c70c71 13
qosay147 0:c56707c70c71 14 float y;
qosay147 0:c56707c70c71 15 float f;
qosay147 0:c56707c70c71 16 int counter = 1;
qosay147 0:c56707c70c71 17 Timer t;
qosay147 0:c56707c70c71 18
qosay147 0:c56707c70c71 19 int main()
qosay147 0:c56707c70c71 20 { //open1
qosay147 0:c56707c70c71 21 lcd.printf("STARTING! \n") ;
qosay147 0:c56707c70c71 22 while(1) //Repeat process loop
qosay147 0:c56707c70c71 23 { //open2
qosay147 0:c56707c70c71 24 counter = 1; // resets counter back to 1 every time frequency has been read
qosay147 0:c56707c70c71 25 t.reset(); // reset timer every time the frequency has been read out
qosay147 0:c56707c70c71 26 while (counter < 22) // while loop counts for 21 hits on peak point
qosay147 0:c56707c70c71 27 { //open3
qosay147 0:c56707c70c71 28 float V = 3.2 * VIN.read(); // Converting the Voltage input to a higher decimal for more accurate readings. Check if needed by dividing the next if statement by 3.2...
qosay147 0:c56707c70c71 29 if( V >= 0.9&& V <= 1.1) // Checks when the voltage on the sinusoidal input is at a given range to measure time taken for certain amount of hitpoints to calculate frequency...
qosay147 0:c56707c70c71 30 { //open4
qosay147 0:c56707c70c71 31 lcd.printf("V = %2.4f Counter is : %d \n", V, counter) ; // just a checking for myself to ensure counter is working, Will be taken out because unnecessary reading...
qosay147 0:c56707c70c71 32 if (counter == 1) //when counter is equal to 1 and the previous if statement is correct then will start the timer once, until counter is < 21
qosay147 0:c56707c70c71 33 { //open5
qosay147 0:c56707c70c71 34 t.start();
qosay147 0:c56707c70c71 35 } //close5
qosay147 0:c56707c70c71 36 counter++; // adds to counter 1, everytime the while loop has been gone through
qosay147 0:c56707c70c71 37 } //close4
qosay147 0:c56707c70c71 38 } //close3
qosay147 0:c56707c70c71 39 t.stop(); // Timer stops when the counter loop is finished
qosay147 0:c56707c70c71 40 y = t.read()/10.01 ; // averaging the time it has taken to take
qosay147 0:c56707c70c71 41 f = ((1/y)+0.013);
qosay147 0:c56707c70c71 42 lcd.cls();
qosay147 0:c56707c70c71 43 //lcd.printf("time taken is : %3.4f sec \n", t.read()) ;
qosay147 0:c56707c70c71 44 lcd.printf(" %3.3f Hz \n", f) ; // print frequency reading to 3 decimal places
qosay147 0:c56707c70c71 45
qosay147 0:c56707c70c71 46 s1 = ((1*f)- 49.5) ;
qosay147 0:c56707c70c71 47 s2 = ((1*f)- 49.45);
qosay147 0:c56707c70c71 48 //s2 = 0.5;
qosay147 0:c56707c70c71 49 // No need for this when printing off on computer screen
qosay147 0:c56707c70c71 50 pc.printf("$%d %d;", f, y); // SEE HOW TO USE THIS TO PRINT OFF FREQUENCY READING TO PC
qosay147 0:c56707c70c71 51 wait_ms(2000);
qosay147 0:c56707c70c71 52 } //close2
qosay147 0:c56707c70c71 53 } //close1