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.
Dependencies: mbed CANBuffer Watchdog MODSERIAL mbed-rtos xbeeRelay IAP
Fork of SystemManagement by
Get_IMD/Get_IMD.h@8:ecf68db484af, 2014-10-10 (annotated)
- Committer:
- martydd3
- Date:
- Fri Oct 10 21:46:52 2014 +0000
- Revision:
- 8:ecf68db484af
- Parent:
- 0:e516fcccccda
Added IMD monitoring code based on Kiran's calculations
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martydd3 | 0:e516fcccccda | 1 | #include "mbed.h" |
| martydd3 | 0:e516fcccccda | 2 | #include "string.h" |
| martydd3 | 8:ecf68db484af | 3 | |
| martydd3 | 0:e516fcccccda | 4 | DigitalOut myled(LED1); |
| martydd3 | 0:e516fcccccda | 5 | |
| martydd3 | 0:e516fcccccda | 6 | //Serial pc(USBTX,USBRX); |
| martydd3 | 0:e516fcccccda | 7 | |
| martydd3 | 0:e516fcccccda | 8 | #ifndef GET_IMD_H |
| martydd3 | 8:ecf68db484af | 9 | #define GET_IMD_H |
| martydd3 | 8:ecf68db484af | 10 | |
| martydd3 | 0:e516fcccccda | 11 | typedef struct |
| martydd3 | 0:e516fcccccda | 12 | { |
| martydd3 | 0:e516fcccccda | 13 | float Frequency; |
| martydd3 | 0:e516fcccccda | 14 | float Duty_Cycle; |
| martydd3 | 0:e516fcccccda | 15 | char State[50]; |
| martydd3 | 0:e516fcccccda | 16 | char Encoded_Status; |
| martydd3 | 0:e516fcccccda | 17 | }IMD_Measurement_Output; |
| martydd3 | 0:e516fcccccda | 18 | |
| martydd3 | 0:e516fcccccda | 19 | bool FirstFE=true; |
| martydd3 | 0:e516fcccccda | 20 | bool FirstRE=true; |
| martydd3 | 0:e516fcccccda | 21 | uint32_t ON_Period =0; |
| martydd3 | 0:e516fcccccda | 22 | uint32_t INT0_RE,INT0_FE; |
| martydd3 | 0:e516fcccccda | 23 | uint32_t CAP0RE=0, CAP0FE=1, CRO_I=4; |
| martydd3 | 0:e516fcccccda | 24 | IMD_Measurement_Output Result; |
| martydd3 | 0:e516fcccccda | 25 | |
| martydd3 | 0:e516fcccccda | 26 | extern "C" void TIMER0_IRQHandler(void) |
| martydd3 | 0:e516fcccccda | 27 | { |
| martydd3 | 0:e516fcccccda | 28 | INT0_RE=LPC_TIM0->CCR; |
| martydd3 | 0:e516fcccccda | 29 | INT0_FE=LPC_TIM0->CCR; |
| martydd3 | 0:e516fcccccda | 30 | |
| martydd3 | 0:e516fcccccda | 31 | |
| martydd3 | 0:e516fcccccda | 32 | if(((INT0_FE & (1 << CAP0FE)) >> CAP0FE)) |
| martydd3 | 0:e516fcccccda | 33 | { |
| martydd3 | 0:e516fcccccda | 34 | if(!FirstFE) |
| martydd3 | 0:e516fcccccda | 35 | { |
| martydd3 | 0:e516fcccccda | 36 | //pc.printf("LPC_TIM1->TC in FE:%d\n\r",LPC_TIM1->TC); |
| martydd3 | 0:e516fcccccda | 37 | ON_Period = LPC_TIM1->TC; |
| martydd3 | 0:e516fcccccda | 38 | //pc.printf("On Period:%d\n\r",ON_Period); |
| martydd3 | 0:e516fcccccda | 39 | LPC_TIM0->CCR |= ((1<<0) | (1<<2)); //Copy TC to CRO on Rising Edge AND Generate Interrupt |
| martydd3 | 0:e516fcccccda | 40 | LPC_TIM0->CCR &= ~(1<<1); //Disable Falling Edge |
| martydd3 | 0:e516fcccccda | 41 | } |
| martydd3 | 0:e516fcccccda | 42 | else |
| martydd3 | 0:e516fcccccda | 43 | FirstFE=false; |
| martydd3 | 0:e516fcccccda | 44 | } |
| martydd3 | 0:e516fcccccda | 45 | else if((INT0_RE & (1 << CAP0RE))>> CAP0RE) |
| martydd3 | 0:e516fcccccda | 46 | { |
| martydd3 | 0:e516fcccccda | 47 | //pc.printf("On Period In RE:%d\n\r",ON_Period); |
| martydd3 | 0:e516fcccccda | 48 | if(!FirstRE) |
| martydd3 | 0:e516fcccccda | 49 | { |
| martydd3 | 0:e516fcccccda | 50 | //pc.printf("LPC_TIM1->TC in RE:%d\n\r",LPC_TIM1->TC); |
| martydd3 | 0:e516fcccccda | 51 | //pc.printf("On Period In RE:%d\n\r",ON_Period); |
| martydd3 | 0:e516fcccccda | 52 | Result.Frequency=((1/(float)LPC_TIM1->TC)*1000); |
| martydd3 | 0:e516fcccccda | 53 | Result.Duty_Cycle=((ON_Period/(float)LPC_TIM1->TC)*100); |
| martydd3 | 0:e516fcccccda | 54 | LPC_TIM0->CCR |= ((1<<1) | (1<<2)); //Copy TC to CRO on Falling Edge AND Generate Interrupt |
| martydd3 | 0:e516fcccccda | 55 | LPC_TIM0->CCR &= ~(1<<0); //Disable Rising Edge |
| martydd3 | 0:e516fcccccda | 56 | LPC_TIM1->TCR |= (1<<1); //Reset Timer1 |
| martydd3 | 0:e516fcccccda | 57 | LPC_TIM1->TCR &= ~(1<<1); //Restart Timer1 |
| martydd3 | 0:e516fcccccda | 58 | } |
| martydd3 | 0:e516fcccccda | 59 | else |
| martydd3 | 0:e516fcccccda | 60 | FirstRE=false; |
| martydd3 | 0:e516fcccccda | 61 | } |
| martydd3 | 0:e516fcccccda | 62 | LPC_TIM0->IR |= (1<<CRO_I); //Reset Counter0 Interrupt |
| martydd3 | 0:e516fcccccda | 63 | return; |
| martydd3 | 0:e516fcccccda | 64 | } |
| martydd3 | 0:e516fcccccda | 65 | |
| martydd3 | 0:e516fcccccda | 66 | void init() |
| martydd3 | 0:e516fcccccda | 67 | { |
| martydd3 | 0:e516fcccccda | 68 | Result.Frequency=0; |
| martydd3 | 0:e516fcccccda | 69 | Result.Duty_Cycle=0; |
| martydd3 | 0:e516fcccccda | 70 | strcpy(Result.State,""); |
| martydd3 | 0:e516fcccccda | 71 | //Set pin as capture mode |
| martydd3 | 0:e516fcccccda | 72 | //Initialize Counter2 |
| martydd3 | 0:e516fcccccda | 73 | LPC_PINCON->PINSEL3 |= ((1<<21) | (1<<20)); //Set Pin1.26 as CAP0.0 |
| martydd3 | 0:e516fcccccda | 74 | LPC_SC->PCONP |= (1<<1); //PowerOn Timer/Counter0. |
| martydd3 | 0:e516fcccccda | 75 | LPC_TIM0->CCR |= ((1<<1) | (1<<2)); //Copy TC to CRO on Falling Edge AND Generate Interrupt |
| martydd3 | 0:e516fcccccda | 76 | NVIC_SetPriority(TIMER0_IRQn,255); |
| martydd3 | 0:e516fcccccda | 77 | NVIC_EnableIRQ(TIMER0_IRQn); //Enable TIMER0 IRQ |
| martydd3 | 0:e516fcccccda | 78 | |
| martydd3 | 0:e516fcccccda | 79 | //Initiallize Timer1 |
| martydd3 | 0:e516fcccccda | 80 | LPC_SC->PCONP |= (1<<2); //PoewerOn Timer/Counter1 |
| martydd3 | 0:e516fcccccda | 81 | LPC_SC->PCLKSEL0 |= ((1<<4) | (1<<5)); //Prescale Timer1 CCLK/8 |
| martydd3 | 0:e516fcccccda | 82 | LPC_TIM1->CTCR &= ~((1<<1) | (1<<0)); //Set Timer/Counter1 as as Timer mode |
| martydd3 | 0:e516fcccccda | 83 | LPC_TIM1->PR = 11985; //Timeout every 1msec(Timer Resolution) |
| martydd3 | 0:e516fcccccda | 84 | LPC_TIM1->TCR |= (1<<1); //Reset Timer1 |
| martydd3 | 0:e516fcccccda | 85 | LPC_TIM1->TCR |= (1<<0); //Enable Timer1 |
| martydd3 | 0:e516fcccccda | 86 | //LPC_TIM1->TCR &= ~(1<<1); //Restart Timer1 |
| martydd3 | 0:e516fcccccda | 87 | FirstRE=false; |
| martydd3 | 0:e516fcccccda | 88 | FirstFE=false; |
| martydd3 | 0:e516fcccccda | 89 | } |
| martydd3 | 0:e516fcccccda | 90 | |
| martydd3 | 0:e516fcccccda | 91 | void Disable() |
| martydd3 | 0:e516fcccccda | 92 | { |
| martydd3 | 0:e516fcccccda | 93 | LPC_TIM1->TCR |= (1<<1); //Reset Timer1 |
| martydd3 | 0:e516fcccccda | 94 | LPC_TIM0->IR |= (1<<4); //Reset Counter0 Interrupt |
| martydd3 | 0:e516fcccccda | 95 | NVIC_DisableIRQ(TIMER0_IRQn); |
| martydd3 | 0:e516fcccccda | 96 | } |
| martydd3 | 0:e516fcccccda | 97 | |
| martydd3 | 0:e516fcccccda | 98 | IMD_Measurement_Output Get_Measurement() |
| martydd3 | 0:e516fcccccda | 99 | { |
| martydd3 | 0:e516fcccccda | 100 | init(); |
| martydd3 | 0:e516fcccccda | 101 | wait_ms(300); |
| martydd3 | 0:e516fcccccda | 102 | Disable(); |
| martydd3 | 0:e516fcccccda | 103 | if(Result.Frequency >=5 && Result.Frequency <15){ |
| martydd3 | 0:e516fcccccda | 104 | strcpy(Result.State, "Normal condition"); |
| martydd3 | 0:e516fcccccda | 105 | Result.Encoded_Status='1'; |
| martydd3 | 0:e516fcccccda | 106 | } |
| martydd3 | 0:e516fcccccda | 107 | else if(Result.Frequency >=15 && Result.Frequency <25){ |
| martydd3 | 0:e516fcccccda | 108 | strcpy(Result.State, "underVoltage condition"); |
| martydd3 | 0:e516fcccccda | 109 | Result.Encoded_Status='2'; |
| martydd3 | 0:e516fcccccda | 110 | } |
| martydd3 | 0:e516fcccccda | 111 | else if(Result.Frequency >=25 && Result.Frequency <35) |
| martydd3 | 0:e516fcccccda | 112 | { |
| martydd3 | 0:e516fcccccda | 113 | if(Result.Duty_Cycle <=15){ |
| martydd3 | 0:e516fcccccda | 114 | strcpy(Result.State, "Insulation measurement:good"); |
| martydd3 | 0:e516fcccccda | 115 | Result.Encoded_Status='3'; |
| martydd3 | 0:e516fcccccda | 116 | } |
| martydd3 | 0:e516fcccccda | 117 | else if(Result.Duty_Cycle > 85 && Result.Duty_Cycle < 100){ |
| martydd3 | 0:e516fcccccda | 118 | strcpy(Result.State, "Insulation measurement:bad"); |
| martydd3 | 0:e516fcccccda | 119 | Result.Encoded_Status='4'; |
| martydd3 | 0:e516fcccccda | 120 | } |
| martydd3 | 0:e516fcccccda | 121 | } |
| martydd3 | 0:e516fcccccda | 122 | else if(Result.Frequency >=35 && Result.Frequency <45){ |
| martydd3 | 0:e516fcccccda | 123 | strcpy(Result.State, "Device error"); |
| martydd3 | 0:e516fcccccda | 124 | Result.Encoded_Status='5'; |
| martydd3 | 0:e516fcccccda | 125 | } |
| martydd3 | 0:e516fcccccda | 126 | else if(Result.Frequency >=45 && Result.Frequency <55){ |
| martydd3 | 0:e516fcccccda | 127 | strcpy(Result.State, "Connection fault earth"); |
| martydd3 | 0:e516fcccccda | 128 | Result.Encoded_Status='6'; |
| martydd3 | 0:e516fcccccda | 129 | } |
| martydd3 | 0:e516fcccccda | 130 | return Result; |
| martydd3 | 0:e516fcccccda | 131 | } |
| martydd3 | 0:e516fcccccda | 132 | |
| martydd3 | 0:e516fcccccda | 133 | #endif/* GET_IMD_H */ |
