STM32F103C8 Tim4_Pwm Example


7 Gennaio 2019
2
0
1
#1
Codice:
include "stm32f10x.h"                  // Device header

void pwm_init()
{
    /**********************TIM4 CH1 PWM PB6************************** */
    RCC->APB2ENR |=  (1<<3); /* GPIOB Clock enable */
    RCC->APB1ENR |=  ()1<<2); /* TIM4 Clock enable */
    
    GPIOB->CRL   &=  0xF0FFFFFF;/*PB6 clear*/
    GPIOB->CRL   |=  0x0B000000;/* Alternate function output Push-pull Output */
    
    TIM4->ARR     =  49999; // 72000000 /50000 = 1440Hz
    TIM4->PSC     =  0; //Prescaler
    
    TIM4->CCMR1  |=  0x0078; //Output compare 1 preload enable pwm2 mode
    TIM4->CCER   |=  0x0001; //Capture/Compare 1 output enable
    TIM4->EGR    |=  0x0001;//Update generation
    TIM4->CCR1    =  TIM4->ARR/2; //Duty %50
    TIM4->CR1    |=  0x0081;//Auto-reload preload enable Timer enable
    
}

int main()
{
    pwm_init();
    
    while(1)
    {
        
    }
}
 
7 Gennaio 2019
2
0
1
#2
STM32F103C8 hcr-04 ultrasonic sensor example...
Codice:
 #include "stm32f10x.h"                  // Device header

uint16_t distance = 0;

/**************** PA0 pin (TRIG) PA1 pin ,(ECHO)*************** */
void EXTI1_IRQHandler(void)
{
   EXTI->PR |= 0x00000002; /*Pending bit for line 1*/
   if(GPIOA->IDR & 0x0002)
     {
       TIM3->CR1 |= 0x0001; /*counter enable*/
     }
   else
     {                                                                                      
      TIM3->CR1 &= ~(0x0001); /*counter disable*/
      distance = TIM3->CNT/58;
      TIM3->CNT = 0;
   }
}

int main()
{
RCC->APB2ENR  |= 0x00000015; /*GPIOA-AF clock enable*/
GPIOA->CRL     = 0x8A; /*PA0 AF output push -pull PA1 input pull up /pull down */
GPIOC->CRH    |= (0x3 << 20);
GPIOC->CRH    &= (~(0xC << 20));
RCC->APB1ENR  |= 0x00000003; /* TIM2-3 Clock enable */

TIM2->PSC      = 71; // SystemCoreClock/1000000-1; //
TIM2->ARR      = 60000-1; /*Period*/
TIM2->CCR1     = 10;    /*Duty  */
TIM2->CR1     |= 0x0080; /*arpe*/
TIM2->CCMR1   |= 0x0068; /*Bit 3 OC1PE: Output compare 1 preload enable-OC1M: Output compare 1 mode-110: PWM mode 1*/
TIM2->CR1     |= 0x0001; /*counter enable*/
TIM2->CCER    |= 0x0001;/*Bit 0 CC1E: Capture/Compare 1 output enable*/

TIM3->PSC      = 71; //SystemCoreClock/1000000-1; /*Prescaler*/
TIM3->ARR      = 50000;

AFIO->EXTICR[0]= 0x0000; /*0000: PA1 pin*/
EXTI->IMR      = 0x00000002; /*Interrupt mask register PA1*/
EXTI->RTSR     = 0x00000002; /*Rising trigger enabled (for Event and Interrupt) for input line*/
EXTI->FTSR     = 0x00000002; /*Falling trigger enabled (for Event and Interrupt) for input line.*/
//or NVIC_EnableIRQ(EXTI1_IRQn);
NVIC->ISER[0]  = (1<<7);

 
 
 
    while(1)
    {
        if(distance>30){
            GPIOC->ODR |= 0x00002000;
        }
        else{
     
                GPIOC->ODR =0;
        }
 
    }
    }
 

Seguici su Facebook