Martin Deng / Mbed 2 deprecated SystemManagement

Dependencies:   CANBuffer mbed SystemManagement mbed-rtos

Dependents:   SystemManagement

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Get_IMD.h Source File

Get_IMD.h

00001 #include "mbed.h"
00002 #include "string.h"
00003 /*
00004 #include "Get_IMD.h"
00005 DigitalOut myled(LED1);
00006 
00007 int main() 
00008 {
00009     IMD_Measurement_Output IMD_Result;
00010     while(1) 
00011     {
00012         IMD_Result=Get_Measurement();
00013         pc.printf("Frequency:%f\n\r",IMD_Result.Frequency);
00014         pc.printf("Duty Cycle:%f\n\r",IMD_Result.Duty_Cycle);
00015         pc.printf("State:%s\n\n\r",IMD_Result.State);
00016     }
00017 }
00018 */
00019 
00020 //Serial pc(USBTX,USBRX);
00021 
00022 #ifndef GET_IMD_H
00023     #define GET_IMD_H  
00024     typedef struct
00025     {
00026         float Frequency;
00027         float Duty_Cycle;
00028         char State[50];
00029         char Encoded_Status;
00030     }IMD_Measurement_Output;
00031     
00032     bool FirstFE=true;
00033     bool FirstRE=true;
00034     uint32_t ON_Period =0;
00035     uint32_t INT0_RE,INT0_FE;
00036     uint32_t CAP0RE=0, CAP0FE=1, CRO_I=4;
00037     IMD_Measurement_Output Result;
00038     
00039     extern "C" void TIMER0_IRQHandler(void)
00040     {
00041         INT0_RE=LPC_TIM0->CCR;
00042         INT0_FE=LPC_TIM0->CCR;
00043         
00044         
00045         if(((INT0_FE & (1 << CAP0FE)) >> CAP0FE))
00046         {
00047             if(!FirstFE)
00048             {
00049                 //pc.printf("LPC_TIM1->TC in FE:%d\n\r",LPC_TIM1->TC);                
00050                 ON_Period = LPC_TIM1->TC;
00051                 //pc.printf("On Period:%d\n\r",ON_Period);
00052                 LPC_TIM0->CCR   |=  ((1<<0) | (1<<2));      //Copy TC to CRO on Rising Edge AND Generate Interrupt                
00053                 LPC_TIM0->CCR   &=  ~(1<<1);     //Disable Falling Edge
00054             }
00055             else
00056                 FirstFE=false;                            
00057         }
00058         else if((INT0_RE & (1 << CAP0RE))>> CAP0RE)
00059         {
00060             //pc.printf("On Period In RE:%d\n\r",ON_Period);
00061             if(!FirstRE)
00062             {
00063                 //pc.printf("LPC_TIM1->TC in RE:%d\n\r",LPC_TIM1->TC);
00064                 //pc.printf("On Period In RE:%d\n\r",ON_Period);
00065                 Result.Frequency=((1/(float)LPC_TIM1->TC)*1000);
00066                 Result.Duty_Cycle=((ON_Period/(float)LPC_TIM1->TC)*100);
00067                 LPC_TIM0->CCR   |=  ((1<<1) | (1<<2));      //Copy TC to CRO on Falling Edge AND Generate Interrupt                             
00068                 LPC_TIM0->CCR   &=  ~(1<<0);                //Disable Rising Edge
00069                 LPC_TIM1->TCR   |=  (1<<1);                 //Reset Timer1
00070                 LPC_TIM1->TCR   &=  ~(1<<1);                 //Restart Timer1
00071             }
00072             else
00073                 FirstRE=false;    
00074         }
00075         LPC_TIM0->IR |= (1<<CRO_I);                         //Reset Counter0 Interrupt
00076         return;  
00077     }
00078     
00079     void init()
00080     {
00081         Result.Frequency=0;
00082         Result.Duty_Cycle=0;
00083         strcpy(Result.State,"");
00084         //Set pin as capture mode
00085         //Initialize Counter2
00086         LPC_PINCON->PINSEL3 |=  ((1<<21) | (1<<20));          //Set Pin1.26 as CAP0.0 
00087         LPC_SC->PCONP       |=  (1<<1);                    //PowerOn Timer/Counter0.
00088         LPC_TIM0->CCR       |=  ((1<<1) | (1<<2));          //Copy TC to CRO on Falling Edge AND Generate Interrupt
00089         NVIC_SetPriority(TIMER0_IRQn,255);
00090         NVIC_EnableIRQ(TIMER0_IRQn);                        //Enable TIMER0 IRQ      
00091         
00092         //Initiallize Timer1
00093         LPC_SC->PCONP       |=  (1<<2);                     //PoewerOn Timer/Counter1
00094         LPC_SC->PCLKSEL0    |=  ((1<<4) | (1<<5));          //Prescale Timer1 CCLK/8  
00095         LPC_TIM1->CTCR      &=  ~((1<<1) | (1<<0));         //Set Timer/Counter1 as as Timer mode
00096         LPC_TIM1->PR        =   11985;                      //Timeout every 1msec(Timer Resolution)
00097         LPC_TIM1->TCR       |=  (1<<1);                     //Reset Timer1
00098         LPC_TIM1->TCR       |=  (1<<0);                     //Enable Timer1
00099         //LPC_TIM1->TCR       &=  ~(1<<1);                 //Restart Timer1              
00100         FirstRE=false;
00101         FirstFE=false;
00102     }    
00103     
00104     void Disable()
00105     {
00106         LPC_TIM1->TCR   |=  (1<<1);                     //Reset Timer1
00107         LPC_TIM0->IR    |=  (1<<4);                     //Reset Counter0 Interrupt
00108         NVIC_DisableIRQ(TIMER0_IRQn);
00109     }
00110     
00111     IMD_Measurement_Output Get_Measurement()
00112     {
00113         init();
00114         wait_ms(300);
00115         Disable();
00116         if(Result.Frequency >=5 && Result.Frequency <15){
00117                 strcpy(Result.State, "Normal condition");
00118                 Result.Encoded_Status='1';
00119             }
00120         else if(Result.Frequency >=15 && Result.Frequency <25){
00121                 strcpy(Result.State, "underVoltage condition");
00122                 Result.Encoded_Status='2';
00123             }
00124         else if(Result.Frequency >=25 && Result.Frequency <35)
00125                 {
00126                     if(Result.Duty_Cycle <=15){
00127                         strcpy(Result.State, "Insulation measurement:good");
00128                         Result.Encoded_Status='3';
00129                     }
00130                     else if(Result.Duty_Cycle > 85 && Result.Duty_Cycle < 100){
00131                         strcpy(Result.State, "Insulation measurement:bad");
00132                         Result.Encoded_Status='4';
00133                     }    
00134                 }
00135         else if(Result.Frequency >=35 && Result.Frequency <45){
00136                 strcpy(Result.State, "Device error");
00137                 Result.Encoded_Status='5';
00138             }
00139         else if(Result.Frequency >=45 && Result.Frequency <55){
00140                 strcpy(Result.State, "Connection fault earth");    
00141                 Result.Encoded_Status='6';
00142             }                                            
00143         return Result; 
00144     }
00145     
00146     #endif/* GET_IMD_H */