Controlling PWM of LED through direct access of TIM2 timer's registers.

Dependencies:   mbed

Revision:
9:e65dd1ad537e
Parent:
8:6464500cc838
Child:
10:f80370dd55f8
--- a/main.cpp	Fri Aug 02 12:18:17 2019 +0000
+++ b/main.cpp	Fri Aug 02 12:22:17 2019 +0000
@@ -7,13 +7,7 @@
 {
     // Led is at PA5.
     // -
-    // Set PA5 as output.
-    // -
-    // Port Mode Register.
-    // -
-//    GPIOA->MODER &= ~GPIO_MODER_MODER5;
-//    GPIOA->MODER |= 1 << GPIO_MODER_MODER5_Pos;
-    // Set PA5 as AF1.
+    // Set PA5 as AF1 (TIM2_CH1).
     // -
 ///*
     GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL5;
@@ -25,16 +19,6 @@
     cout << "OSPEED : 0x" << (GPIOA->OSPEEDR & GPIO_OSPEEDER_OSPEEDR5) << endl;
     cout << "MODER  : 0x" << (GPIOA->MODER & GPIO_MODER_MODER5) << endl;
 //*/
-/*
-    GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL0;
-    GPIOA->AFR[0] |= 1 << GPIO_AFRL_AFRL0_Pos;
-    GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR0;
-    GPIOA->MODER &= ~GPIO_MODER_MODER0;
-    GPIOA->MODER |= 2 << GPIO_MODER_MODER0_Pos;
-    cout << "AFR    : 0x" << hex << (GPIOA->AFR[0] & GPIO_AFRL_AFRL0) << endl;
-    cout << "OSPEED : 0x" << (GPIOA->OSPEEDR & GPIO_OSPEEDER_OSPEEDR0) << endl;
-    cout << "MODER  : 0x" << (GPIOA->MODER & GPIO_MODER_MODER0) << endl;
-*/
 }
 
 void inline reset_timer ()
@@ -56,12 +40,10 @@
 
 void inline timer_enable_pwm ()
 {
-    TIM2->CCR1 = TIM2->ARR * 0.9;
-//    TIM2->CCMR1 = (0b111 << TIM_CCMR1_OC1M_Pos) | TIM_CCMR1_OC1PE | TIM_CCMR1_OC1FE;
-    TIM2->CCMR1 = (0b111 << TIM_CCMR1_OC1M_Pos) | TIM_CCMR1_OC1FE;
-    TIM2->CCER &= ~TIM_CCER_CC1NP;
+    TIM2->CCR1 = TIM2->ARR * 0.6;
+    TIM2->CCMR1 = (0b111 << TIM_CCMR1_OC1M_Pos) | TIM_CCMR1_OC1PE | TIM_CCMR1_OC1FE;
+//    TIM2->CCER &= ~TIM_CCER_CC1NP;
     TIM2->CCER |= TIM_CCER_CC1E;
-    cout << "CCMR1 : 0x" << hex << TIM2->CCMR1 << endl;
 }
 
 void inline timer_downscale_by (const unsigned short& v)