首页 > 代码库 > lpc1788控制步进电机28BYJ-48

lpc1788控制步进电机28BYJ-48

下面直接上代码:

#include "lpc177x_8x.h"#include "lpc177x_8x_clkpwr.h"#include "lpc177x_8x_gpio.h"#include "debug_frmwrk.h"#include "led.h"volatile uint32_t cycleCounter;void DelayMs(uint32_t delay){  uint32_t startTime = cycleCounter;  while ((uint32_t)(cycleCounter - startTime) <= delay);}void SysTick_Handler(void) //SysTick handler sub-routine (1ms) {  cycleCounter++;}uint32_t a[8]={0xFF8FFFFF,0xFFCFFFFF,0xFF4FFFFF,0xFF6FFFFF,0xFF2FFFFF,0xFF3FFFFF,0xFF1FFFFF,0xFF9FFFFF};	int main(void){	SysTick_Config(CLKPWR_GetCLK(CLKPWR_CLKTYPE_CPU) / 1000);	GPIO_Init();//ʹÄÜGPIO¿ÚµÄʱÖÓ	GPIO_SetDir(4,1<<20, GPIO_DIRECTION_OUTPUT);	GPIO_SetDir(4,1<<21, GPIO_DIRECTION_OUTPUT);	GPIO_SetDir(4,1<<22, GPIO_DIRECTION_OUTPUT);	GPIO_SetDir(4,1<<23, GPIO_DIRECTION_OUTPUT);	while(1)	{		 GPIO_SetValue(4, a[0]);		 DelayMs(1);		 GPIO_ClearValue(4,0xFFFFFFFF);		 		 GPIO_SetValue(4, a[1]);                   DelayMs(1);		 GPIO_ClearValue(4,0xFFFFFFFF);				 GPIO_SetValue(4, a[2]);                   DelayMs(1);		 GPIO_ClearValue(4,0xFFFFFFFF);			          GPIO_SetValue(4, a[3]);                   DelayMs(1);		 GPIO_ClearValue(4,0xFFFFFFFF);				 GPIO_SetValue(4, a[4]);                   DelayMs(1);		 GPIO_ClearValue(4,0xFFFFFFFF);				 GPIO_SetValue(4, a[5]);                    DelayMs(1);		 GPIO_ClearValue(4,0xFFFFFFFF);		 		 GPIO_SetValue(4, a[6]);                    DelayMs(1);		 GPIO_ClearValue(4,0xFFFFFFFF);		 		 GPIO_SetValue(4, a[7]);                   DelayMs(1);		 GPIO_ClearValue(4,0xFFFFFFFF);  }}

 其中GPIO_ClearValue(4,0xFFFFFFFF);这句代码非常重要。 因为如果有些位不清零的话,下次setvalue某些位为0是不行的,setvalue只能设置1.

 

lpc1788控制步进电机28BYJ-48