Guest User

Untitled

a guest
Aug 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #include <__cross_studio_io.h>
  2. #include "stm32f4xx.h"
  3. #include "system_stm32f4xx.h"
  4. #include <math.h>
  5.  
  6. void TIM2_IRQHandler(void)
  7. {
  8.   debug_printf("test!\n");
  9. }
  10.  
  11. int BIT(int input)
  12. {
  13.   return pow(2, input);;
  14. }
  15.  
  16. void main(void)
  17. {
  18.   RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;    // enable TIM2 clock
  19.   RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;   // enable GPIOA
  20.  
  21.   TIM2->CCER = 0;
  22.   TIM2->CR1 = 0;
  23.   TIM2->CCMR1 = 0;
  24.   TIM2->CCMR2 = 0;
  25.  
  26.   // CH1 is connected to TI1 input
  27.   TIM2->CR2 = BIT(7) ;
  28.  
  29.   // set CCMR1 CC1S to 01 (set active input TI1)
  30.   /// 01: CC1 channel is configured as input, IC1 is mapped on TI1.
  31.   TIM2->CCMR1 &= ~BIT(1);
  32.   TIM2->CCMR1 |= BIT(0);
  33.  
  34.   // set CCMR1 CC2S to 10 (set active input TI1)
  35.   /// 10: CC2 channel is configured as input, IC2 is mapped on TI1
  36.   TIM2->CCMR1 |= BIT(9);
  37.   TIM2->CCMR1 &= ~BIT(8);
  38.  
  39.   // select the active polarity on TI1FP1. write CC1P to 0 (active on rising edge)
  40.   ///0: non-inverted: capture is done on a rising edge of IC1.
  41.   TIM2->CCER &= ~BIT(5);
  42.  
  43.   // select the active polarity on TI1FP2. write CC2P to 1 (active on falling edge)
  44.   ///1: inverted: capture is done on a falling edge of IC1
  45.   TIM2->CCER |= BIT(1) ;
  46.  
  47.   //select valid trigger input: write TS to 101 in SMCR (Slave Mode Config Register). meaning we should reset when TI1 triggers
  48.   /// 101: Filtered Timer Input 1 (TI1FP1)
  49.   TIM2->SMCR |= BIT(6) | BIT(4) ;
  50.   TIM2->SMCR &= ~BIT(5);
  51.  
  52.   // write SMS to 100 in SMCR: slave controller in reset mode
  53.   ///100: Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter and generates an update of the registers
  54.   TIM2->SMCR &= ~BIT(0);
  55.   TIM2->SMCR &= ~BIT(1) ;
  56.   TIM2->SMCR |= BIT(2);
  57.  
  58.   //enable CC modules
  59.   NVIC->ISER[0] |= 1 << TIM2_IRQn;
  60.   TIM2->CCER |= (BIT(0) | BIT(4));
  61.   TIM2->CR1 |= 1; // enable TIM2
  62.   TIM2->DIER= BIT(2) | BIT(1) | BIT(0);
  63.   TIM2->EGR = BIT(2);
  64.  
  65.   debug_printf("hello world\n");
  66.  
  67.   while (1)
  68.   {
  69.   debug_printf("R: %d\n", TIM2->CCR1);
  70.   }
  71.  
  72.   debug_exit(0);
  73. }
Add Comment
Please, Sign In to add comment