Advertisement
nis

main.c for stm32/example/08-clocking

nis
Mar 17th, 2012
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. /**
  2.  * Main code of firmware
  3.  */
  4. #include "stm32f10x.h"
  5. #include "core_cm3.h"
  6. #include "main.h"
  7. void main();
  8. /* Main function */
  9. void main(void) {
  10.         /* setup i/o port c */
  11.         RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
  12.         LED_PORT->CRH &= ~(GPIO_CRH_CNF8 | GPIO_CRH_CNF9);
  13.         LED_PORT->CRH |= GPIO_CRH_CNF8_1 | GPIO_CRH_CNF9_1;
  14.         LED_PORT->CRH |= GPIO_CRH_MODE8_0 | GPIO_CRH_MODE9_0;
  15.         /* setup i/o port a */
  16.         RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_AFIOEN;
  17.         /* setup AFIO andEXTI */
  18.         AFIO->EXTICR[0] |= AFIO_EXTICR1_EXTI0_PA;
  19.         EXTI->IMR |= (1 << 0);
  20.         EXTI->FTSR |= (1 << 0);
  21.         /* remap TIM3 to pc6..9 pins port C */
  22.         AFIO->MAPR |= AFIO_MAPR_TIM3_REMAP_FULLREMAP;
  23.         /* enable security HSE - CSS */
  24.         RCC->CR |= RCC_CR_CSSON;
  25.         /* setup PLL from HSE */
  26.         /** enable HSE and wait to ready */
  27.         RCC->CR |= RCC_CR_HSEON;
  28.         while ((RCC->CR & RCC_CR_HSERDY) == 0);
  29.         /** setup PLL, enable and wait to ready */
  30.         RCC->CFGR &= ~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL);
  31.         RCC->CFGR |= (RCC_CFGR_PLLSRC_PREDIV1);
  32.         RCC->CR |= RCC_CR_PLLON;
  33.         while ((RCC->CR & RCC_CR_PLLRDY) == 0);
  34.         /** select PLL for system clock and wait to select */
  35.         RCC->CFGR &= ~RCC_CFGR_SW;
  36.         RCC->CFGR |= RCC_CFGR_SW_PLL;
  37.         while ((RCC->CFGR & RCC_CFGR_SWS_PLL) == 0);
  38.         /* setup TIM3 counter */
  39.         RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
  40.         TIM3->PSC = 8000 - 1;
  41.         TIM3->ARR = 1000;
  42.         /* setup TIM3 compare block */
  43.         /** setup CC3..4 as output */
  44.         TIM3->CCMR2 &= ~(TIM_CCMR2_CC3S | TIM_CCMR2_CC4S);
  45.         /** setup CC3..4 to PWM mode 1 */
  46.         TIM3->CCMR2 |= TIM_CCMR2_OC3M_2 | TIM_CCMR2_OC3M_1 | TIM_CCMR2_OC4M_2 | TIM_CCMR2_OC4M_1;
  47.         /** setup value to compare registers 3..4 */
  48.         TIM3->CCR3 = 0;
  49.         TIM3->CCR4 = 500;
  50.         /** setup channels 3..4 to corresponding output pin */
  51.         TIM3->CCER |= TIM_CCER_CC3E | TIM_CCER_CC4E;
  52.         /* enable TIM3 */
  53.         TIM3->CR1 |= TIM_CR1_CEN;
  54.         /* enable EXTI0 interrupt */
  55.         NVIC_EnableIRQ(EXTI0_IRQn);
  56.         /* infinity loop */
  57.         while (1);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement