Advertisement
Guest User

pastebinpastebin

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. /**
  2. ******************************************************************************
  3. * @file GPIO/GPIO_Toggle/main.c
  4. * @author P. Szulim
  5. * @version V1.0.0
  6. * @date 10-Listopad-2015
  7. * @brief Program changes periodicaly brightnes of LED lighting
  8. ******************************************************************************
  9. *
  10. ******************************************************************************
  11. */
  12.  
  13.  
  14. /* Header files -------------------------------------------------------------------*/
  15. #include "stm32f30x.h"
  16. #include "stm32f30x_gpio.h"
  17. #include "stm32f30x_rcc.h"
  18.  
  19. /* Private typedef -----------------------------------------------------------*/
  20. /* Private define ------------------------------------------------------------*/
  21. /* Private macro -------------------------------------------------------------*/
  22. /* Private variables ---------------------------------------------------------*/
  23. /* Private function prototypes -----------------------------------------------*/
  24. void Delay(void);
  25. /* Private functions ---------------------------------------------------------*/
  26.  
  27. /**
  28. * @brief Main program.
  29. * @param None
  30. * @retval None
  31. */
  32. int main(void)
  33. {
  34. /* Configuration structure definition */
  35. GPIO_InitTypeDef GPIO_InitStructure;
  36. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  37. TIM_OCInitTypeDef TIM_OCInitStructure;
  38.  
  39. /* GPIOE Periph clock enable */
  40. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);
  41.  
  42. /* Alternative configuration for pin PE9 */
  43. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_14;
  44. /* alternative mode for pin */
  45. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  46. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  47. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  48. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  49. GPIO_Init(GPIOE, &GPIO_InitStructure);
  50.  
  51. /* Alternative function for pin - TIM1 take control of this pin */
  52. /* For more information go to the DS */
  53. GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_2);
  54. GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_2);
  55. GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_2);
  56. GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_2);
  57. /************* Timer configuration ******************/
  58.  
  59. /* TIM1 clock activation */
  60. RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
  61.  
  62. /* Main counter activation */
  63. TIM_TimeBaseStructure.TIM_Prescaler = 720; //720 means, that clock signal will be divided 720 times.
  64. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //counter count up
  65. TIM_TimeBaseStructure.TIM_Period = 1000; //counter count from 0 to 1000
  66. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //additional clock divider disabled
  67. TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //additional mode disabled
  68. /* Initialisation of timer */
  69. TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  70.  
  71. /* Timer channel struct initiation */
  72. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM mode for this channel
  73. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //channel act on external pin
  74. TIM_OCInitStructure.TIM_Pulse = 0; //PWM duty equal 0%.
  75. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //additional function
  76. TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;//additional function
  77. /* Configure channel 1 */
  78. TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  79. /* Run timer*/
  80. TIM_Cmd(TIM1, ENABLE);
  81. /* activate channel output */
  82. TIM_CtrlPWMOutputs(TIM1, ENABLE);
  83.  
  84. /* program changes led brightnes */
  85. while (1){
  86. /* set PWM on 10% (100/1000)*/
  87. TIM_SetCompare1(TIM1,100);
  88. Delay();
  89. /* set PWM duty on 20% */
  90. TIM_SetCompare1(TIM1,200);
  91. Delay();
  92. /* set PWM duty on 30% */
  93. TIM_SetCompare1(TIM1,300);
  94. Delay();
  95. /* set PWM duty on 40% */
  96. TIM_SetCompare1(TIM1,400);
  97. Delay();
  98. /* set PWM duty on 50% */
  99. TIM_SetCompare1(TIM1,500);
  100. Delay();
  101. /* set PWM duty on 60% */
  102. TIM_SetCompare1(TIM1,600);
  103. Delay();
  104. /* set PWM duty on 70% */
  105. TIM_SetCompare1(TIM1,700);
  106. Delay();
  107. /* set PWM duty on 80% */
  108. TIM_SetCompare1(TIM1,800);
  109. Delay();
  110. /* set PWM duty on 90% */
  111. TIM_SetCompare1(TIM1,900);
  112. Delay();
  113. /* set PWM duty on 100% - direct acces to CCR register */
  114. TIM1->CCR1=1000;
  115. Delay();
  116. }
  117. }
  118.  
  119.  
  120.  
  121. /**
  122. * @brief Delay function.
  123. * @param None
  124. * @retval None
  125. */
  126. void Delay(void){
  127. int i;
  128. for(i=0;i<0x7FFFF;i++);
  129. }
  130.  
  131.  
  132.  
  133. #ifdef USE_FULL_ASSERT
  134.  
  135. /**
  136. * @brief Reports the name of the source file and the source line number
  137. * where the assert_param error has occurred.
  138. * @param file: pointer to the source file name
  139. * @param line: assert_param error line source number
  140. * @retval None
  141. */
  142. void assert_failed(uint8_t* file, uint32_t line)
  143. {
  144. /* User can add his own implementation to report the file name and line number,
  145. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  146.  
  147. /* Infinite loop */
  148. while (1)
  149. {
  150. }
  151. }
  152. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement