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