Advertisement
Ladislav

AVS úkol 3/1

Mar 13th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include <stdio.h>
  3. #include "stm32_eval.h"
  4. #include "serial_driver.h"
  5. #include "string.h"
  6. #include "myUSART.h"
  7.  
  8. /* Private typedef -----------------------------------------------------------*/
  9. /* Private define ------------------------------------------------------------*/
  10. /* Private macro -------------------------------------------------------------*/
  11. /* Private variables ---------------------------------------------------------*/
  12. volatile int16_t lastPos;
  13. /* Private function prototypes -----------------------------------------------*/
  14. /* Private functions ---------------------------------------------------------*/
  15.  
  16. void initEncoder() {
  17.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  18.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  19.    
  20.     GPIO_InitTypeDef gpioInitStruct;
  21.     GPIO_StructInit(&gpioInitStruct);
  22.     gpioInitStruct.GPIO_Mode = GPIO_Mode_AF;
  23.     gpioInitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  24.     gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  25.     gpioInitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
  26.     GPIO_Init(GPIOD, &gpioInitStruct);
  27.    
  28.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
  29.     GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
  30.    
  31.     TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
  32.    
  33.     TIM_Cmd(TIM4, ENABLE);
  34. }
  35.  
  36. void initResetButton() {
  37.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  38.     GPIO_InitTypeDef gpioInitStruct;
  39.     GPIO_StructInit(&gpioInitStruct);
  40.     gpioInitStruct.GPIO_Mode = GPIO_Mode_IN;
  41.     gpioInitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  42.     gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  43.     gpioInitStruct.GPIO_Pin = GPIO_Pin_14;
  44.     GPIO_Init(GPIOC, &gpioInitStruct);
  45. }
  46.  
  47. void SysTick_Handler() {
  48.         char szBuf[10];
  49.         int16_t newPos;
  50.        
  51.         if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_14) == 0) {
  52.             TIM_SetCounter(TIM4, 0);
  53.         }
  54.        
  55.         newPos = (int16_t)TIM_GetCounter(TIM4);
  56.         if (newPos != lastPos) {
  57.             lastPos = newPos;
  58.             sprintf(szBuf, "%d\r\n", newPos);
  59.             putString(szBuf);
  60.         }
  61. }
  62.  
  63. /**
  64.  * Main program.
  65.  * @return Nothing.
  66.  */
  67. int main(void) {
  68.     initUSART();
  69.     initEncoder();
  70.     initResetButton();
  71.     SysTick_Config(SystemCoreClock / 100);
  72.  
  73.     putString("Init complete.\r\n");
  74.     for (;;);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement