Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 5:4e5c644d5cc3
- Parent:
- 4:3aedc9246ae4
- Child:
- 6:f7028034aabb
--- a/main.cpp Wed Nov 14 15:18:12 2018 +0000
+++ b/main.cpp Thu Nov 15 08:43:32 2018 +0000
@@ -12,6 +12,7 @@
void Ph4(void);
void Ph41 (void);
void GetChar (void);
+void RPM (void);
Serial pc(USBTX, USBRX); // tx, rx - set up the Terraterm input from mbed
@@ -37,11 +38,12 @@
int TimePerClick = 0;
int TimePerRev = 0;
int RPS = 0;
-int RPM = 0;
+int rpm = 0;
char c;
-float x=0.05, y=0.02, z=1; //x=time of square wave when 1 phase energised, y=time of square wave when 2 phases energised
+float x=0.05; //x=time of square wave when 1 phase energised,
+float y=0.02; //y=time of square wave when 2 phases energised
int main(void)
{
@@ -51,75 +53,76 @@
wait(0.1);
t.start();
-while(wheel.getRevolutions()==0)
+ while(wheel.getRevolutions()==0)
{
- switch(StateA)
+ switch(StateA)
{
- case 0:Ph1();pc.printf("Ph1= %i\n\r", wheel.getPulses());break;
- case 1:Ph12();pc.printf("Ph12= %i\n\r", wheel.getPulses());break;
- case 2:Ph2();pc.printf("Ph2= %i\n\r", wheel.getPulses());break;
- case 3:Ph23();pc.printf("Ph23= %i\n\r", wheel.getPulses());break;
- case 4:Ph3();pc.printf("Ph3= %i\n\r", wheel.getPulses());break;
- case 5:Ph34();pc.printf("Ph34= %i\n\r", wheel.getPulses());break;
- case 6:Ph4();pc.printf("Ph4= %i\n\r", wheel.getPulses());break;
- case 7:Ph41();pc.printf("Ph41= %i\n\r", wheel.getPulses());break;
- default:break;
+ case 0:Ph1();pc.printf("Ph1= %i\n\r", wheel.getPulses());break;
+ case 1:Ph12();pc.printf("Ph12= %i\n\r", wheel.getPulses());break;
+ case 2:Ph2();pc.printf("Ph2= %i\n\r", wheel.getPulses());break;
+ case 3:Ph23();pc.printf("Ph23= %i\n\r", wheel.getPulses());break;
+ case 4:Ph3();pc.printf("Ph3= %i\n\r", wheel.getPulses());break;
+ case 5:Ph34();pc.printf("Ph34= %i\n\r", wheel.getPulses());break;
+ case 6:Ph4();pc.printf("Ph4= %i\n\r", wheel.getPulses());break;
+ case 7:Ph41();pc.printf("Ph41= %i\n\r", wheel.getPulses());break;
+ default:break;
}
if(wheel.getYay()==1)
{
- StateA++;
- wheel.ResetYay();
- if (StateA>7)
+ StateA++;
+ wheel.ResetYay();
+ if (StateA>7)
{
- StateA=0;
+ StateA=0;
}
}
-}
+ }
- while(1)
- {
+ while(1)
+ {
while((led1 == 0) && (led2 == 0))
{
GetChar();
+ StateB = (wheel.getPulses())%16;
+ Phase1 = 0;
+ Phase2 = 0;
+ Phase3 = 0;
+ Phase4 = 0;
+ pc.printf("StateA= %i\r", StateA);
+ //pc.printf("0 StateB= %i, Pulses= %i, Revs= %i\r", StateB,wheel.getPulses(),wheel.getRevolutions());
}
while((wheel.getRevolutions()>0) && (wheel.getPulses()>0) && (led1==1))
{
GetChar();
StateB = (wheel.getPulses())%16;
+ pc.printf("StateA= %i\r", StateA);
//pc.printf("1 StateB= %i, Pulses= %i, Revs= %i\r", StateB,wheel.getPulses(),wheel.getRevolutions());
switch(StateB)
{
- case 0:Ph3();break;
- case 1:Ph3();break;
- case 2:Ph34();break;
- case 3:Ph34();break;
- case 4:Ph4();break;
- case 5:Ph4();break;
- case 6:Ph41();break;
- case 7:Ph41();break;
- case 8:Ph1();break;
- case 9:Ph1();break;
- case 10:Ph12();break;
- case 11:Ph12();break;
- case 12:Ph2();break;
- case 13:Ph2();break;
- case 14:Ph23();break;
- case 15:Ph23();break;
- default:break;
+ case 0:Ph3();break;
+ case 1:Ph3();break;
+ case 2:Ph34();break;
+ case 3:Ph34();break;
+ case 4:Ph4();break;
+ case 5:Ph4();break;
+ case 6:Ph41();break;
+ case 7:Ph41();break;
+ case 8:Ph1();break;
+ case 9:Ph1();break;
+ case 10:Ph12();break;
+ case 11:Ph12();break;
+ case 12:Ph2();break;
+ case 13:Ph2();break;
+ case 14:Ph23();break;
+ case 15:Ph23();break;
+ default:break;
}
if(wheel.getYay()==1)
{
- wheel.ResetYay();
- TimePerClick = (t.read_us());
- t.reset();
- TimePerRev = TimePerClick * 400;
- TimePerRev = TimePerRev / 1000;
- RPS = 10000000 / TimePerRev;
- RPM = (RPS * 60)/10000;
- pc.printf("RPM = %d\n\r", RPM);
+ RPM();
}
}
@@ -127,45 +130,108 @@
{
GetChar();
StateB = (800+wheel.getPulses())%16;
+ pc.printf("StateA= %i\r", StateA);
//pc.printf("2 StateB= %i, Pulses= %i, Revs= %i\r", StateB,wheel.getPulses(),wheel.getRevolutions());
switch(StateB)
{
- case 0:Ph3();break;
- case 1:Ph3();break;
- case 2:Ph34();break;
- case 3:Ph34();break;
- case 4:Ph4();break;
- case 5:Ph4();break;
- case 6:Ph41();break;
- case 7:Ph41();break;
- case 8:Ph1();break;
- case 9:Ph1();break;
- case 10:Ph12();break;
- case 11:Ph12();break;
- case 12:Ph2();break;
- case 13:Ph2();break;
- case 14:Ph23();break;
- case 15:Ph23();break;
- default:break;
+ case 0:Ph3();break;
+ case 1:Ph3();break;
+ case 2:Ph34();break;
+ case 3:Ph34();break;
+ case 4:Ph4();break;
+ case 5:Ph4();break;
+ case 6:Ph41();break;
+ case 7:Ph41();break;
+ case 8:Ph1();break;
+ case 9:Ph1();break;
+ case 10:Ph12();break;
+ case 11:Ph12();break;
+ case 12:Ph2();break;
+ case 13:Ph2();break;
+ case 14:Ph23();break;
+ case 15:Ph23();break;
+ default:break;
}
- if(wheel.getYay()==1)
+ if(wheel.getYay()==1)
+ {
+ RPM();
+ }
+ }
+ while((wheel.getRevolutions()>0) && (wheel.getPulses()>0) && (led2==1))
{
- wheel.ResetYay();
- TimePerClick = (t.read_us());
- t.reset();
- TimePerRev = TimePerClick * 400;
- TimePerRev = TimePerRev / 1000;
- RPS = 10000000 / TimePerRev;
- RPM = (RPS * 60)/10000;
- pc.printf("RPM = %d\n\r", RPM);
- }
+ GetChar();
+ StateB = (800+wheel.getPulses())%16;
+ pc.printf("StateA= %i\r", StateA);
+ //pc.printf("StateA= %i\n\r", StateA);
+ //pc.printf("3 StateB= %i, Pulses= %i, Revs= %i \r", StateB,wheel.getPulses(),wheel.getRevolutions());
+ switch(StateB)
+ {
+ case 15:Ph34();break;
+ case 14:Ph3();break;
+ case 13:Ph3();break;
+ case 12:Ph23();break;
+ case 11:Ph23();break;
+ case 10:Ph2();break;
+ case 9:Ph2();break;
+ case 8:Ph12();break;
+ case 7:Ph12();break;
+ case 6:Ph1();break;
+ case 5:Ph1();break;
+ case 4:Ph41();break;
+ case 3:Ph41();break;
+ case 2:Ph4();break;
+ case 1:Ph4();break;
+ case 0:Ph34();break;
+ default:break;
+ }
+ if (StateB<0)
+ {
+ StateB = 800+wheel.getPulses();
+ }
+ if(wheel.getYay()==1)
+ {
+ RPM();
+ }
}
-
+ while((wheel.getRevolutions()>0) && (wheel.getPulses()<1) && (led2==1))
+ {
+ GetChar();
+ StateB = (800+wheel.getPulses())%16;
+ pc.printf("StateA= %i\r", StateA);
+ //pc.printf("4 StateB= %i, Pulses= %i, Revs= %i \r", StateB,wheel.getPulses(),wheel.getRevolutions());
+ switch(StateB)
+ {
+ case 15:Ph34();break;
+ case 14:Ph3();break;
+ case 13:Ph3();break;
+ case 12:Ph23();break;
+ case 11:Ph23();break;
+ case 10:Ph2();break;
+ case 9:Ph2();break;
+ case 8:Ph12();break;
+ case 7:Ph12();break;
+ case 6:Ph1();break;
+ case 5:Ph1();break;
+ case 4:Ph41();break;
+ case 3:Ph41();break;
+ case 2:Ph4();break;
+ case 1:Ph4();break;
+ case 0:Ph34();break;
+ default:break;
+ }
+ if (StateB<0)
+ {
+ StateB = 800+wheel.getPulses();
+ }
+ if(wheel.getYay()==1)
+ {
+ RPM();
+ }
+ }
}
}
-
void StepCW(void) //Square wave switching
{
Ph1();
@@ -273,4 +339,16 @@
led2 = !led2 ;
}
}
+}
+
+void RPM (void)
+{
+ wheel.ResetYay();
+ TimePerClick = (t.read_us());
+ t.reset();
+ TimePerRev = TimePerClick * 400;
+ TimePerRev = TimePerRev / 1000;
+ RPS = 10000000 / TimePerRev;
+ rpm = (RPS * 60)/10000;
+ //pc.printf("rpm = %d\n\r", rpm);
}
\ No newline at end of file