Rotork Research Team / Mbed 2 deprecated TFM_Encoder

Dependencies:   mbed QEI

main.cpp

Committer:
simontruelove
Date:
2018-10-24
Revision:
1:0191658b6ff4
Parent:
0:634dd505dace
Child:
2:3f95c82c26bb

File content as of revision 1:0191658b6ff4:

#include "mbed.h"
void ReportA(void); //These voids are written after the main. They must be listed here too (functional prototypes).
void ReportB(void);
void ReportX(void);
void Startup(void);
void StepCW(void);
void Ph1(void);
void Ph2(void);
void Ph3(void);
void Ph4(void);

Serial pc(USBTX, USBRX); // tx, rx - set up the Terraterm input from mbed

DigitalOut      Phase1                 (p21);         //Pin and LED set up                   
DigitalOut      Phase2                 (p22);
DigitalOut      Phase3                 (p23);
DigitalOut      Phase4                 (p24);

InterruptIn      ChannelA                (p5);
InterruptIn      ChannelB                (p6);
InterruptIn      Index                   (p8);

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

int CountA=0;   //On start up the integer will be zero
int CountB=0;
int CountX=0;

float x=0.04, y=0.01;   //x=time of square wave when 1 phase energised, y=time of square wave when 2 phases energised

int main(void) 
{  
    ChannelA.rise(&ReportA);  // Interrupt - When channel A goes from 0 to 1 it is reported       
    ChannelB.rise(&ReportB);  // Interrupt - When channel B goes from 0 to 1 it is reported
    Index.rise(&ReportX);  // Interrupt - When Index goes from 0 to 1 it is reported             
    pc.baud(230400);        //Set fastest baud rate
    Startup();
   
    while(CountX==3)
    {
        wait(0.5);
        pc.printf("A = %d\n\r",CountA);
        pc.printf("B = %d\n\r",CountB);
        wait(0.5);
        Ph1();
        wait(0.5);
        pc.printf("A = %d\n\r",CountA);
        pc.printf("B = %d\n\r",CountB);
        wait(0.5);
        Ph2();
        wait(0.5);
        pc.printf("A = %d\n\r",CountA);
        pc.printf("B = %d\n\r",CountB);
        wait(0.5);
        Ph3();
        wait(0.5);
        pc.printf("A = %d\n\r",CountA);
        pc.printf("B = %d\n\r",CountB);
        wait(0.5);
        Ph4();
    }        
}
     

void Startup(void)
{
    while(CountX==0)
    {
        StepCW();    
    }
}




void ReportA(void)                      
{ 
CountA++;
led1 = !led1;                               //Counts A
//pc.printf("A = %d\n\r",CountA);       //Prints cumulative counts to Terraterm
}


void ReportB(void)                      
{
CountB++;
led2 = !led2;                               //Counts B
//pc.printf("B = %d\n\r",CountB);       //Prints cumulative counts to Terraterm

}
    
    
void ReportX(void) 
{
        CountX++;
        pc.printf("X = %d\n\r",CountX);   //Prints cumulative counts to Terraterm
        led4 = !led4;
        CountA = CountB = 0;
    }

void StepCW(void)                           //Square wave switching
{
        Ph1();
          
        Phase1 = Phase2 = 1;
        Phase3 = Phase4 = 0;
        wait(y);
    
        Ph2();
     
        Phase2 = Phase3 = 1;
        Phase1 = Phase4 = 0;
        wait(y);
    
        Ph3();
        
        Phase3 = Phase4 = 1;
        Phase1 = Phase2 = 0;
        wait(y);
    
        Ph4();
    
        Phase4 = Phase1 = 1;
        Phase2 = Phase3 = 0;
        wait(y);   
}

void Ph1(void)
{
    Phase1 = 1;                         
    Phase2 = Phase3 = Phase4 = 0;
    wait(x);
}

void Ph2(void)
{
        Phase2 = 1;
        Phase1 = Phase3 = Phase4 = 0;
        wait(x);
}

void Ph3(void)
{
        Phase3 = 1;
        Phase1 = Phase2 = Phase4 = 0;
        wait(x);
}

void Ph4(void)
{
        Phase4 = 1;
        Phase1 = Phase2 = Phase3 = 0;
        wait(x);
}