Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include <stdio.h>
- #include "stm32_eval.h"
- #include "serial_driver.h"
- #include "string.h"
- #include "myUSART.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- volatile int16_t lastPos;
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- void initEncoder() {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
- GPIO_InitTypeDef gpioInitStruct;
- GPIO_StructInit(&gpioInitStruct);
- gpioInitStruct.GPIO_Mode = GPIO_Mode_AF;
- gpioInitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
- gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- gpioInitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
- GPIO_Init(GPIOD, &gpioInitStruct);
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
- TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI1, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
- TIM_Cmd(TIM4, ENABLE);
- }
- void initResetButton() {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
- GPIO_InitTypeDef gpioInitStruct;
- GPIO_StructInit(&gpioInitStruct);
- gpioInitStruct.GPIO_Mode = GPIO_Mode_IN;
- gpioInitStruct.GPIO_PuPd = GPIO_PuPd_UP;
- gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- gpioInitStruct.GPIO_Pin = GPIO_Pin_14;
- GPIO_Init(GPIOC, &gpioInitStruct);
- }
- void SysTick_Handler() {
- char szBuf[10];
- int16_t newPos;
- if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_14) == 0) {
- TIM_SetCounter(TIM4, 0);
- }
- newPos = (int16_t)TIM_GetCounter(TIM4);
- if (newPos != lastPos) {
- lastPos = newPos;
- sprintf(szBuf, "%d\r\n", newPos);
- putString(szBuf);
- }
- }
- /**
- * Main program.
- * @return Nothing.
- */
- int main(void) {
- initUSART();
- initEncoder();
- initResetButton();
- SysTick_Config(SystemCoreClock / 100);
- putString("Init complete.\r\n");
- for (;;);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement