Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void system_clk()
- {
- //ENABLE HSE & WAIT FOR HSE TO BECOME READY
- RCC->CR |= RCC_CR_HSEON ; // HSE IS 8MHZ
- while(!(RCC->CR & RCC_CR_HSERDY))
- ;
- //configure flash prefetch and the latency related
- FLASH->ACR |= FLASH_ACR_PRFTBE|FLASH_ACR_LATENCY_1;
- // CONFIGURE PLL AND Buses(AHB,APB1,APB2)
- //PLL SOURCE: HERE HSE IS USED AS SOURCE
- RCC->CFGR |= RCC_CFGR_PLLSRC;
- //HSE DEVIDER FOR PLL(IF HSE IS USED AS SOURCE FOR PLL) : NOT DIVIDING CLOCK
- RCC->CFGR |= RCC_CFGR_PLLXTPRE_HSE ; // SO INPUT TO PLL IS 8MHZ
- //PLL MULTIPLIER: HERE I AM MULTIPLYING HSE OUTPUT WITH 4
- RCC->CFGR |= RCC_CFGR_PLLMULL4; //SO OUTPUT FROM PLL IS 32MHZ
- //BUS CLOCK CONFIGURE(APB1,APB2,AHB): NOT DIVIDING
- RCC->CFGR |= (RCC_CFGR_PPRE1_DIV1 | RCC_CFGR_PPRE2_DIV1 | RCC_CFGR_HPRE_DIV1); // all buses runs at 32MHZ
- //ENABLE THE PLL
- RCC->CR |= RCC_CR_PLLON;
- //WAIT FOR PLL TO SET
- while(!(RCC->CR & RCC_CR_PLLRDY))
- ;
- //ENABLE SYSTEMCLK AND WAIT
- RCC->CFGR |= RCC_CFGR_SW_HSE;
- while(!(RCC->CFGR & RCC_CFGR_SWS_HSE))
- ;
- MCO_pin_conf();
- //CLOCK OUTPUT ON MCO PIN
- RCC->CFGR |= RCC_CFGR_MCO_SYSCLK;
- }
- /* Timer Frequency(TF) = System Clock(Fsysclk)/Prescalor(TIM2PSC)
- *
- * Timer time Period(TTP) = 1/timer frequency(TF)
- *
- * Time delay by timer = Timer Time Period(TTP) * Timer auto reload register(TIM2ARR)
- *
- * System Clock = 32Mhz, TIM2PSC = 65535, So TF = 489hz
- *
- * TTP = 0.002sec
- * TIM2ARR = 500, so timer delay = 0.002*500
- *
- */
- void timer_initialise()
- {
- // Start by making sure the timer's 'counter' is off
- TIM2->CR1 &= ~(TIM_CR1_CEN);
- //RESET THE TIMER2 BUS
- RCC->APB1RSTR |= (RCC_APB1RSTR_TIM2RST);
- RCC->APB1RSTR &= ~(RCC_APB1RSTR_TIM2RST);
- //Enable Timer2 peripheral clock
- RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
- //Timer prescaler value
- TIM2->PSC =65535;
- //Timer Auto reload register value
- TIM2->ARR = 500;
- //Select the UEV event Source
- TIM2->CR1 |= (TIM_CR1_URS) | (~TIM_CR1_UDIS); // Only counter overflow/underflow generates an update
- //Enable the update generation
- TIM2->EGR |= TIM_EGR_UG;
- // Enable the NVIC interrupt for TIM2.
- //NVIC_EnableIRQ(TIM2_IRQn);
- //NVIC_SetPriority(TIM2_IRQn, 0x03);
- //Enable the Update Interrupt
- // TIM2->DIER |= TIM_DIER_UIE;
- }
- void delay()
- {
- TIM2->CR1 |= TIM_CR1_CEN;
- while(!(TIM2->SR & TIM_SR_UIF));
- {
- //Enable the update generation
- TIM2->EGR |= TIM_EGR_UG;
- TIM2->CR1 &= ~TIM_CR1_CEN;
- }
- }
- int main(void)
- {
- system_clk();
- //ENABLE GPIO CLOCK
- Gpio_output_pushpull_conf();
- //Init Timer for time base unit
- timer_initialise();
- while(1)
- {
- GPIOC->ODR ^= (GPIO_ODR_ODR13);
- delay();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement