2011

Dependencies:   mbed FastPWM

Revision:
241:c53d4c26b04a
Parent:
240:3ffd78cbd029
--- a/INIT_HW/INIT_HW.cpp	Wed Jul 20 07:50:48 2022 +0000
+++ b/INIT_HW/INIT_HW.cpp	Tue Jul 26 06:58:25 2022 +0000
@@ -139,50 +139,85 @@
 
 void Init_TMR3()
 {
+    ///////////////Trigger by TIMER 1/////////////////////////////////
+    
     RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;                         // enable TIM3 clock
     NVIC_EnableIRQ(TIM3_IRQn);                                  //Enable TIM3 IRQ
 
     TIM3->DIER |= TIM_DIER_UIE;                                 // enable update interrupt
-    TIM3->CR1 = 0x40;                                           // CMS = 10, interrupt only when counting up // Center-aligned mode
+    TIM3->CR1 = 0x10;
     TIM3->CR1 |= TIM_CR1_UDIS;
-    TIM3->RCR |= 0x001;                                         // update event once per up/down count of TIM3
     TIM3->EGR |= TIM_EGR_UG;
 
-    TIM3->PSC = 0x00;                                            // no prescaler, timer counts up in sync with the peripheral clock
-    TIM3->ARR = TMR3_COUNT-1;                                          // set auto reload, 5 khz
+    TIM3->PSC = 1-1;                                           // 10kHz
+    TIM3->ARR = 1;
     TIM3->CNT = 0;
     TIM3->SR = 0;
-    TIM3->CCER |= ~(TIM_CCER_CC1NP);                            // Interupt when low side is on.
-    TIM3->CR1 |= TIM_CR1_CEN;                                   // enable TIM4
+    TIM3->SMCR = 0x07;                                         //External clock mode
+    TIM3->CR1 |= TIM_CR1_CEN;                                   // enable TIM3
+    
+//    RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;                         // enable TIM3 clock
+//    NVIC_EnableIRQ(TIM3_IRQn);                                  //Enable TIM3 IRQ
+//
+//    TIM3->DIER |= TIM_DIER_UIE;                                 // enable update interrupt
+//    TIM3->CR1 = 0x40;                                           // CMS = 10, interrupt only when counting up // Center-aligned mode
+//    TIM3->CR1 |= TIM_CR1_UDIS;
+//    TIM3->RCR |= 0x001;                                         // update event once per up/down count of TIM3
+//    TIM3->EGR |= TIM_EGR_UG;
+//
+//    TIM3->PSC = 0x00;                                            // no prescaler, timer counts up in sync with the peripheral clock
+//    TIM3->ARR = TMR3_COUNT-1;                                          // set auto reload, 5 khz
+//    TIM3->CNT = 0;
+//    TIM3->SR = 0;
+//    TIM3->CCER |= ~(TIM_CCER_CC1NP);                            // Interupt when low side is on.
+//    TIM3->CR1 |= TIM_CR1_CEN;                                   // enable TIM4
 }
 
 void Init_TMR2()
 {
+    ///////////////Trigger by TIMER 1/////////////////////////////////
+    
     RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;                         // enable TIM2 clock
-
-    //ISR Setup
     NVIC_EnableIRQ(TIM2_IRQn);                                  //Enable TIM2 IRQ
 
     TIM2->DIER |= TIM_DIER_UIE;                                 // enable update interrupt
-    TIM2->CR1 = 0x40;                                           // CMS = 10, interrupt only when counting up // Center-aligned mode
+    TIM2->CR1 = 0x10;
     TIM2->CR1 |= TIM_CR1_UDIS;
-    TIM2->RCR |= 0x001;                                         // update event once per up/down count of TIM2
     TIM2->EGR |= TIM_EGR_UG;
 
-    TIM2->PSC = 0x00;                                            // no prescaler, timer counts up in sync with the peripheral clock
-    TIM2->ARR = TMR2_COUNT-1;                                          // set auto reload, 5 khz
+    TIM2->PSC = 2-1;                                           // 20 prescaler, 5kHz
+    TIM2->ARR = 1;
     TIM2->CNT = 0;
     TIM2->SR = 0;
+    TIM2->SMCR = 0x07;                                         //External clock mode
     TIM2->CR1 |= TIM_CR1_CEN;                                   // enable TIM2
+
+
+    ////////////////Trigger by Internal Clock/////////////////////////////////
+//    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;                         // enable TIM2 clock
+//    //ISR Setup
+//    NVIC_EnableIRQ(TIM2_IRQn);                                  //Enable TIM2 IRQ
+//    TIM2->DIER |= TIM_DIER_UIE;                                 // enable update interrupt
+//    TIM2->CR1 = 0x40;                                           // CMS = 10, interrupt only when counting up // Center-aligned mode
+//    TIM2->CR1 |= TIM_CR1_UDIS;
+//    TIM2->RCR |= 0x001;                                         // update event once per up/down count of TIM2
+//    TIM2->EGR |= TIM_EGR_UG;
+//    TIM2->PSC = 0x00;                                            // no prescaler, timer counts up in sync with the peripheral clock
+//    TIM2->ARR = TMR2_COUNT-1;                                          // set auto reload, 5 khz
+//    TIM2->CNT = 0;
+//    TIM2->SR = 0;
+//    TIM2->CR1 |= TIM_CR1_CEN;                                   // enable TIM2
 }
 
 
 void Init_TMR1()
 {
-    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;                         // enable TIM4 clock
+    ///////////////////PWM output by L6205D/////////////////////////////////
+    
+    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;                         // enable TIM1 clock
     FastPWM pwm_v(PIN_H1);
     FastPWM pwm_w(PIN_L1);
-    
+
     TIM1->CR2 |= 0x20;                                          //MMS = 010
     TIM1->DIER |= TIM_DIER_UIE;                                 // enable update interrupt
     TIM1->CR1 = 0x40;                                           // CMS = 10, interrupt only when counting up // Center-aligned mode
@@ -196,9 +231,9 @@
     TIM1->CNT = 0;
     TIM1->SR = 0;
     TIM1->CR1 |= TIM_CR1_CEN;                                   // enable TIM1
-    
 
 
+    ///////////////////PWM output by IRSM045MA/////////////////////////////////
 //    RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;                         // enable TIM1 clock
 //
 //    FastPWM pwm_H1(PIN_H1);
@@ -245,7 +280,8 @@
     TIM4->CR1 |= TIM_CR1_UDIS;
     TIM4->EGR |= TIM_EGR_UG;
 
-    TIM4->PSC = 10-1;                                            // 10 prescaler, timer counts up in sync with the peripheral clock
+//    TIM4->PSC = 10-1;                                           // 10 prescaler, 1kHz
+    TIM4->PSC = 20-1;                                           // 20 prescaler, 500Hz
     TIM4->ARR = 1;
     TIM4->CNT = 0;
     TIM4->SR = 0;