Slurp

Dependencies:   FastPWM3 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Inverter.cpp Source File

Inverter.cpp

00001 
00002 #include "mbed.h"
00003 #include "hw_pins.h"
00004 //#include "hw_config.h"
00005 #include "structs.h"
00006 #include "FastPWM.h"
00007 
00008 void Init_PWM(GPIOStruct *gpio){
00009     
00010     printf("\nStarting Hardware Function\n\r");
00011     
00012     RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;                        // enable the clock to GPIOC
00013     RCC->APB1ENR |= 0x00000001;                                 // enable TIM2 clock
00014     RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;                         // enable TIM1 clock
00015     
00016     GPIOC->MODER |= (1 << 10);                                  // set pin 5 to be general purpose output for LED
00017 
00018     //gpio->enable = new DigitalOut(ENABLE_PIN);
00019     gpio->pwm_ul = new FastPWM(PIN_AL);
00020     gpio->pwm_vl = new FastPWM(PIN_BL);
00021     gpio->pwm_wl = new FastPWM(PIN_CL);
00022     gpio->pwm_uh = new FastPWM(PIN_AH);
00023     gpio->pwm_vh = new FastPWM(PIN_BH);
00024     gpio->pwm_wh = new FastPWM(PIN_CH);
00025 
00026     gpio->phasing = 1;
00027     
00028     
00029      //ISR Setup     
00030     
00031     NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);                         //Enable TIM1 IRQ
00032     
00033     TIM1->DIER |= TIM_DIER_UIE;                                 // enable update interrupt
00034     TIM1->CR1 = 0x40;                                          // CMS = 10, interrupt only when counting up
00035     TIM1->CR1 |= TIM_CR1_UDIS;  
00036     TIM1->CR1 |= TIM_CR1_ARPE;                                  // autoreload on, 
00037     TIM1->RCR |= 0x001;                                         // update event once per up/down count of tim1 
00038     TIM1->EGR |= TIM_EGR_UG;
00039     
00040     
00041         //PWM Setup
00042     TIM1->DIER |= TIM_DIER_UIE; // enable update interrupt
00043     TIM1->CR1 = 0x40;//CMS = 10, interrupt only when counting up
00044     
00045     TIM1->CR1 |= TIM_CR1_ARPE; // autoreload on, 
00046     TIM1->RCR |= 0x001; // update event once per up/down count of tim1 
00047     TIM1->EGR |= TIM_EGR_UG;
00048  
00049     
00050     //PWM Setup
00051     TIM1->PSC = 0x0; // no prescaler, timer counts up in sync with the peripheral clock
00052     TIM1->ARR = 0x1194; // 20 Khz
00053     //TIM1->BDTR |= TIM_BDTR_MOE;
00054     //TIM1->BDTR |= TIM_BDTR_OSSI;
00055     //TIM1->BDTR |= TIM_BDTR_OSSR;
00056     TIM1->BDTR |= 0xBF;
00057     TIM1->CCER |= TIM_CCER_CC1E | TIM_CCER_CC1NE | TIM_CCER_CC2E | TIM_CCER_CC2NE | TIM_CCER_CC3E | TIM_CCER_CC3NE;
00058     //TIM1->CCER |= ~TIM_CCER_CC1NP; //Interupt when low side is on.
00059     //TIM1->CCER |= TIM_CCER_CC1NP;
00060     TIM1->CR1 |= TIM_CR1_CEN;   
00061     
00062 
00063     }
00064 
00065 void Init_ADC(void){
00066         // ADC Setup
00067      RCC->APB2ENR |= RCC_APB2ENR_ADC2EN;                        // clock for ADC2
00068      RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;                        // clock for ADC1
00069     RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;                        // Enable clock for GPIOC
00070      
00071      ADC->CCR = 0x00000006;                                     // Regular simultaneous mode only
00072      ADC1->CR2 |= ADC_CR2_ADON;//0x00000001;                    // ADC1 ON
00073      ADC1->SQR3 = 0x000000A;                                    // use PC_0 as input  this is the V phase
00074      ADC2->CR2 |= ADC_CR2_ADON;//0x00000001;                    // ADC1 ON
00075      ADC2->SQR3 = 0x0000000B;                                   // use PC_1 as input. This is the U phase
00076      GPIOC->MODER |= 0x0000000f;                                // Alternate function, PC_0, PC_1 are analog inputs 
00077 
00078     }
00079 
00080 void Init_DAC(void){
00081      RCC->APB1ENR |= 0x20000000;                                // Enable clock for DAC
00082      DAC->CR |= 0x00000001;                                     // DAC control reg, both channels ON
00083      GPIOA->MODER |= 0x00000300;                                // PA04 as analog output  
00084     }
00085 
00086 void Init_All_HW(GPIOStruct *gpio){
00087     wait_ms(100);
00088     Init_ADC();
00089     wait(0.1);         
00090     
00091     //Init_DAC();
00092     wait(0.1);
00093     
00094     Init_PWM(gpio);
00095     
00096     }