C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 4th, 2017, 03:37 AM
zihao zihao is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2017
Posts: 1 zihao User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 50 sec
Reputation Power: 0
General - PWM Generation Troubles

Thanks for reading my post at first.
I would like to ask some help here for the troubles occurred in PWM generation on timer output channel.
I am debugging STM32F103C8T6 and I want to realize to generate PWM with duty cycle=50%,Frequency=50Hz using General Purpose Timer2 in output channel 2 which is at PA1 or pin number 11. My target system is STM32 BluePill development board.
The datasheet of STM32F103C8T6
My code is as following:
Code:
/**************************** Header file *******************************/#include<stm32f10x.h>
/*************************** user defined function **********************/
/**************************************************  **********************
Name        : PWM_Init()
Description : Initializes PWM hardware and associated GPIO ports
              Enables clocks for peripherals and configure modes
Parameters  : None
Return_val  : None
**************************************************  **********************/void PWM_Init(){
/****configure clocks for PORTA & TIMER2********/

  RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;           // enable port a clock
  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;           // enable clock for timer 2
/*****  configure GPIOA mode *******************/

   /* Porta Pin 1 &2 as alternate function output Push pull 50 MHz */
    GPIOA->CRL = 0x000000bb;  
/*******Set timer 2 Period for PWM**************        
  Timer Clock                   = 72MHz = 72000000 Hz
  Time Period Required          = (1/50Hz) = 0.02 Seconds
  Let ARR value be 65535 then,
  Prescalar + 1                 = 72000000/((ARR+1)*50Hz)
  Prescalar                     = 21 (approx)   
***********************************************/

    TIM2->ARR = 65535;                      // Set Auto reload value
    TIM2->PSC = 21;                         // Set Prescalar value


    /* Output Compare Mode, ENABLE Preload,PWM  mode:2*/
    TIM2->CCMR1  = 0x00007800; 

    TIM2->EGR |= TIM_EGR_UG;       // ENABLE update generation

    /*CC2E : channel 2 enabled; polarity : active high*/         

    TIM2->CCER = 0x00000010;  

    TIM2->CR1 |= TIM_CR1_ARPE;      // Auto preload ENABLE
    TIM2->CR1 |= TIM_CR1_CEN;       // ENABLE Timer counter     
}/**************Main Function**********************************/int main()
 {  
    PWM_Init();                                  // initialize PWM

        while(1)
        {
            TIM2->CCR2  = 32768;                 // SET duty cycle to 50%
        }
  }

I configured the GPIOA in Alternate function output Push Pull mode with maximum frequency not exceeding 50 MHz, I connected an LED bulb to the GPIOA pin 1, but the LED went on glowing without reduction in the brightness level. The LED was still glowing even after i performed a test with duty cycle set to 0. On the debugger window I could see exactly all the parameters I configured and the Count register was getting incremented. I cannot find information regarding selection of alternate functions for a GPIO Port.
Please kindly give me some suggestions if there are any corrections that need to be done.
Thanks a lot !

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - PWM Generation Troubles


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap