Last active
April 9, 2020 20:25
-
-
Save saffetblt/9b5e46388331fb8d0a9d4d15066fce0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stm32f4xx.h" | |
int period; | |
float freq; | |
int main(){ | |
int last_value = 0; | |
int current_value = 0; | |
//Configure PA5 as alternate function mode | |
RCC->AHB1ENR |= 0x1; //Enable GPIOA | |
GPIOA->MODER |= (2<<10); //Set PA5 to alternate function mode | |
GPIOA->AFR[0] |= (1<<20); //Set PA5 to TIM2 Ch1 | |
//TIM2 Ch1 PA5 output compare @1Hz | |
RCC->APB1ENR |= 0x1; //Enable TIM2 Clock Bus | |
TIM2->PSC = 1600-1; //16.000.000 dive by 1.600 = 10.000 (Prescaler Register) | |
TIM2->ARR = 10000-1; //10.000 dive by 10.000 = 1Hz (Auto Reload Register) | |
TIM2->CCMR1 |= 0x30; //Set output to toggle on match (Capture/Compare Mode Register) | |
TIM2->CCER |= 1; //Enable CH1 compare mode (Capture/Compare Enable Register) | |
TIM2->CCR1 = 0; //Set match mode (Capture/Compare Register 1) | |
TIM2->CNT = 0; //Clear counter (Counter) | |
TIM2->CR1 |=1; //Enable TIM2 counter (Control Registe 1) | |
//Configure PA6 as alternate function mode | |
RCC->AHB1ENR |=0x1; //Enable GPIOA | |
GPIOA->MODER |= (2<<12); //Set PA6 to alternate function mode | |
GPIOA->AFR[0]|= (2<<24); //Set PA6 to TIM3 Ch1 | |
//TIM3 Ch1 PA6 input capture | |
RCC->APB1ENR |= 0x2; //Enable TIM3 Clock Bus | |
TIM3->PSC = 16000-1; //1kHz | |
TIM3->CCMR1 = 0x41; //Set input capture | |
TIM3->CCER = 0xB; //Capture Enable, Falling edge, Capture polarity | |
TIM3->CR1 = 1; //Enable TIM3 counter (Control Registe 1) | |
while(1){ | |
while(!(TIM3->SR & 2)); //Wait until edge is captured | |
current_value = TIM3->CCR1; | |
period = current_value - last_value; | |
last_value = current_value; | |
freq = 1000/period; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment