Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file GPIO/GPIO_Toggle/main.c
- * @author P. Szulim
- * @version V1.0.0
- * @date 10-Listopad-2015
- * @brief Program changes periodicaly brightnes of LED lighting
- ******************************************************************************
- *
- ******************************************************************************
- */
- /* Header files -------------------------------------------------------------------*/
- #include "stm32f30x.h"
- #include "stm32f30x_gpio.h"
- #include "stm32f30x_rcc.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- void Delay(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- int main(void)
- {
- /* Configuration structure definition */
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- /* GPIOE Periph clock enable */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
- /* Alternative configuration for pin PE9 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_14;
- /* alternative mode for pin */
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- /* Alternative function for pin - TIM1 take control of this pin */
- /* For more information go to the DS */
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_2);
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_2);
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_2);
- GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_2);
- /************* Timer configuration ******************/
- /* TIM1 clock activation */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
- /* Main counter activation */
- TIM_TimeBaseStructure.TIM_Prescaler = 720; //720 means, that clock signal will be divided 720 times.
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //counter count up
- TIM_TimeBaseStructure.TIM_Period = 1000; //counter count from 0 to 1000
- TIM_TimeBaseStructure.TIM_ClockDivision = 0; //additional clock divider disabled
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //additional mode disabled
- /* Initialisation of timer */
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- /* Timer channel struct initiation */
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM mode for this channel
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //channel act on external pin
- TIM_OCInitStructure.TIM_Pulse = 0; //PWM duty equal 0%.
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //additional function
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;//additional function
- /* Configure channel 1 */
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
- /* Run timer*/
- TIM_Cmd(TIM1, ENABLE);
- /* activate channel output */
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
- /* program changes led brightnes */
- while (1){
- /* set PWM on 10% (100/1000)*/
- TIM_SetCompare1(TIM1,100);
- Delay();
- /* set PWM duty on 20% */
- TIM_SetCompare1(TIM1,200);
- Delay();
- /* set PWM duty on 30% */
- TIM_SetCompare1(TIM1,300);
- Delay();
- /* set PWM duty on 40% */
- TIM_SetCompare1(TIM1,400);
- Delay();
- /* set PWM duty on 50% */
- TIM_SetCompare1(TIM1,500);
- Delay();
- /* set PWM duty on 60% */
- TIM_SetCompare1(TIM1,600);
- Delay();
- /* set PWM duty on 70% */
- TIM_SetCompare1(TIM1,700);
- Delay();
- /* set PWM duty on 80% */
- TIM_SetCompare1(TIM1,800);
- Delay();
- /* set PWM duty on 90% */
- TIM_SetCompare1(TIM1,900);
- Delay();
- /* set PWM duty on 100% - direct acces to CCR register */
- TIM1->CCR1=1000;
- Delay();
- }
- }
- /**
- * @brief Delay function.
- * @param None
- * @retval None
- */
- void Delay(void){
- int i;
- for(i=0;i<0x7FFFF;i++);
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement