STM32用IO口控制步進電機的簡單程式

來源:互聯網
上載者:User

練習IO口庫函數操作。

//相序uint16_t phasecw[4] ={0x2000,0x0001,0x0004,0x0008};// D-C-B-Auint16_t phaseccw[4]={0x0008,0x0004,0x0001,0x2000};// A-B-C-D

 

//步進電機相關IO口初始化// IN4:PC13// IN3:PC0// IN2:PC2// IN1:PC3void Moto_Init(void){GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//GPIO CLOCK ENABLEGPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽輸出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz速率GPIO_Init(GPIOC, &GPIO_InitStructure);GPIO_ResetBits(GPIOC,GPIO_Pin_13);//輸出低電平GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;GPIO_Init(GPIOC, &GPIO_InitStructure);GPIO_ResetBits(GPIOC,GPIO_Pin_0);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;GPIO_Init(GPIOC, &GPIO_InitStructure);GPIO_ResetBits(GPIOC,GPIO_Pin_2);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;GPIO_Init(GPIOC, &GPIO_InitStructure);GPIO_ResetBits(GPIOC,GPIO_Pin_3);}

 

//電機正轉void Motorcw(void){uint8_t i;for(i=0;i<4;i++)  {  GPIO_Write(GPIOC,phasecw[i]);   delay_ms(4);}}//電機反轉void Motorccw(void){uint8_t i; for(i=0;i<4;i++)  {  GPIO_Write(GPIOC,phaseccw[i]);   delay_ms(4);   }}//點擊停止void MotorStop(void){ GPIO_Write(GPIOC,0x0000);}

記得使能IO口時鐘。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.