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

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Ladon
Date:
Fri Aug 02 12:31:03 2019 +0000
Parent:
9:e65dd1ad537e
Child:
11:42d5cbbd0f3e
Commit message:
3rd version.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Aug 02 12:22:17 2019 +0000
+++ b/main.cpp	Fri Aug 02 12:31:03 2019 +0000
@@ -3,22 +3,17 @@
 
 using namespace std;
 
-void led_init ()
+void inline led_init ()
 {
     // Led is at PA5.
     // -
     // Set PA5 as AF1 (TIM2_CH1).
     // -
-///*
     GPIOA->AFR[0] &= ~GPIO_AFRL_AFRL5;
     GPIOA->AFR[0] |= 1 << GPIO_AFRL_AFRL5_Pos;
     GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR5;
     GPIOA->MODER &= ~GPIO_MODER_MODER5;
     GPIOA->MODER |= 2 << GPIO_MODER_MODER5_Pos;
-    cout << "AFR    : 0x" << hex << (GPIOA->AFR[0] & GPIO_AFRL_AFRL5) << endl;
-    cout << "OSPEED : 0x" << (GPIOA->OSPEEDR & GPIO_OSPEEDER_OSPEEDR5) << endl;
-    cout << "MODER  : 0x" << (GPIOA->MODER & GPIO_MODER_MODER5) << endl;
-//*/
 }
 
 void inline reset_timer ()
@@ -42,8 +37,7 @@
 {
     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;
+    TIM2->CCER = TIM_CCER_CC1E;
 }
 
 void inline timer_downscale_by (const unsigned short& v)
@@ -73,7 +67,7 @@
     //     Update Interrupt Enable.
     // -
 //    TIM2->DIER = TIM_DIER_UIE | TIM_DIER_CC1IE;
-    TIM2->DIER |= TIM_DIER_UIE;
+    TIM2->DIER = TIM_DIER_UIE;
 }
 
 void inline timer_enable ()
@@ -94,13 +88,6 @@
     TIM2->SR = 0;
 }
 
-void led_toggle ()
-{
-    // Output Data Register.
-    // -
-    GPIOA->ODR ^= GPIO_ODR_5;
-}
-
 /* 
     // I was unable to use the following handler :
     // -
@@ -122,7 +109,7 @@
 //    led_toggle();
 }
 
-inline void interrupt_enable ()
+void inline interrupt_enable ()
 {
 //    NVIC_SetPriority(TIM2_IRQn, 250);
     NVIC_SetVector(TIM2_IRQn, reinterpret_cast<uint32_t>(interrupt_handler));