STM32F103+ULN2003驱动步进电机的最简单例子,4相8拍
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "main.h"
- #include "stm32f1xx_hal.h"
- TIM_HandleTypeDef htim1;
- void LcdDelay(uint32_t time)
- {
- while(time--);
- }
- void _NOP_(void)
- {
- uint32_t i = 100;
- while(i > 0)
- {
- i--;
- }
- }
- void Delay(__IO uint32_t nCount)
- {
- for(; nCount != 0; nCount--);
- }
- void delay_us(__IO uint32_t nTime)
- {
- int old_val, new_val, val;
- if(nTime > 900)
- {
- for(old_val = 0; old_val < nTime/900; old_val++)
- {
- delay_us(900);
- }
- nTime = nTime%900;
- }
- old_val = SysTick->VAL;
- new_val = old_val - 72*nTime;
- if(new_val >= 0)
- {
- do
- {
- val = SysTick->VAL;
- }
- while((val < old_val)&&(val >= new_val));
- }
- else
- {
- new_val +=72*1000;
- do
- {
- val = SysTick->VAL;
- }
- while((val <= old_val)||(val > new_val));
- }
- }
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /* Initializes the CPU, AHB and APB busses clocks */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
-
- /* Initializes the CPU, AHB and APB busses clocks */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
- /* Configure the Systick interrupt time */
- //HAL_SYSTICK_Config( HAL_RCC_GetHCLKFreq()/1000 );
- SysTick_Config(HAL_RCC_GetHCLKFreq()/1000);
- // SysTick_Config(72000000/1000);
- /* Configure the Systick */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_SET);
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
- HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
- GPIO_InitStruct.Pin = GPIO_PIN_All;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- }
- /* TIM1 init function */
- static void MX_TIM1_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig;
- TIM_MasterConfigTypeDef sMasterConfig;
- htim1.Instance = TIM1;
- htim1.Init.Prescaler = 0;
- htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim1.Init.Period = 0;
- htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim1.Init.RepetitionCounter = 0;
- HAL_TIM_Base_Init(&htim1);
-
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig);
-
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
- }
- #define Delay_ms(x) Delay_us(1000*x) //单位ms
- #define uchar unsigned char
- #define uint unsigned int
- uchar fan[] = {0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //正向
- uchar zheng[] = {0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //反向
- int main(void)
- {
- uint i;
-
- HAL_Init();
-
- SystemClock_Config();
-
- MX_GPIO_Init();
-
- while (1)
- {
- for(i=0;i<8;i++)
- {
- //GPIOC->ODR=zheng[i];
- GPIOC->ODR=fan[i];
- HAL_Delay(100);
- }
-
- }
- }
复制代码
所有资料51hei提供下载:
stm32f103_stepper.7z
(1.17 MB, 下载次数: 213)
|