Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f10x.h"
- void TIM2_IRQHandler(void);
- static void gpio_init() {
- // enable PORTA GPIO0 to alt. function push-pull with max_speed 50MHz
- RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
- RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
- GPIOA->CRL &= ~(GPIO_CRL_CNF0|GPIO_CRL_MODE0);
- GPIOA->CRL |= GPIO_CRL_CNF0_1|GPIO_CRL_MODE0;
- }
- static void pwm_init(uint16_t psc, uint16_t arr, uint16_t ccr) {
- // timer init
- RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
- TIM2->SR = 0;
- TIM2->PSC = psc;
- TIM2->ARR = arr;
- TIM2->CCR1= ccr;
- // set up interrupt
- TIM2->DIER = TIM_DIER_UIE|TIM_DIER_CC1IE;
- NVIC->ISER[0] = NVIC_ISER_SETENA_28;
- // set output to PWM1 mode
- TIM2->CCMR1 = TIM_CCMR1_OC1M_2|TIM_CCMR1_OC1M_1;
- TIM2->CCER = TIM_CCER_CC1E;
- }
- void TIM2_IRQHandler() {
- TIM2->SR ^= TIM_SR_CC1IF|TIM_SR_UIF;
- }
- int main() {
- gpio_init();
- pwm_init(1, 1, 1);
- // enable timer2
- TIM2->CR1 |= TIM_CR1_CEN;
- for (;;);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement