簡體   English   中英

帶有按鈕的簡單直流電機速度控制

[英]Simple DC motor Speed Control with Push Button

我正在STM32L152上進行一些測試,嘗試通過使用按鈕選擇3種不同的速度來控制DC motor speed ,如下所示:

  1. 理想模式下,電動機關閉。
  2. 按下按鈕,電動機以1速度運行
  3. 再次按下按鈕,電動機以2速運行
  4. 再次按下按鈕,電動機停止。

我已經嘗試通過設置CCR1 (TIM4)直接運行電動機,並且它運行良好。 所以我相信唯一的問題是按鈕部分。 這是代碼:

#include <stdio.h>
#include "stm32l1xx.h"                  // Keil::Device:Startup

// initialization of GPIOB, GPIOA & PWM/TIM4
void GPIO_Init()
{
// initialization of GPIOB
    RCC->AHBRSTR |= RCC_AHBRSTR_GPIOBRST; /* Reset GPIOB clock */
    RCC->AHBENR |= RCC_AHBENR_GPIOBEN; /* Enable GPIOB clock*/
    GPIOB->MODER &= ~(0x03 << (2 * 6)); /* Clear bit 12 & 13 Output mode*/
    GPIOB->MODER |= 0x01 << (2 * 6); /* set as Output mode*/
    GPIOB->OSPEEDR &= ~(0x03 << (2 * 6)); /* 40 MHz  speed */
    GPIOB->OSPEEDR |= 0x03 << (2 * 6); /* 40 MHz  speed*/
    GPIOB->PUPDR &= ~(1 << 6); /* NO PULL-UP PULL-DOWN*/
    GPIOB->OTYPER &= ~(1 << 6); /* PUSH-PULL*/
    GPIOB->AFR[0] |= 0x2 << (4 * 6);

// initialization of GPIOA
    RCC->AHBRSTR |= RCC_AHBRSTR_GPIOARST; /* Reset GPIOA clock*/
    RCC->AHBENR |= RCC_AHBENR_GPIOAEN; /* Enable GPIOA clock*/
    GPIOA->MODER &= ~(0x03); /* Clear & set as input*/
    GPIOA->OSPEEDR &= ~(0x03); /* 2 MHz  speed */
    GPIOA->OSPEEDR |= 0x01; /* 2 MHz  speed */
    GPIOA->PUPDR &= ~(0x03); /* No PULL-UP/DOWN */
    GPIOA->OTYPER &= ~(0x1); /* PUSH-PULL */

//initialization of PWM & TIM4 
    RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
    TIM4->PSC = 100;
    TIM4->ARR = 414; // F=2.097MHz , Fck= 2097000/(100+1)= 20.762KHz, Tpwm = 0.02, ARR= (Fck x Tpwm)-1

    TIM4->CCMR1 |= TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2;   // 111: PWM mode 1 
    TIM4->CCMR1 |= TIM_CCMR1_OC1PE;
    TIM4->CR1 |= TIM_CR1_ARPE;
    TIM4->CCER |= TIM_CCER_CC1E;
    TIM4->EGR |= TIM_EGR_UG;
    TIM4->SR &= ~TIM_SR_UIF;
    TIM4->DIER |= TIM_DIER_UIE;
    TIM4->CR1 |= TIM_CR1_CEN;
}

// Delay conf

void setSysTick(void)
{
// ---------- SysTick timer (1ms) -------- //
    if (SysTick_Config(SystemCoreClock / 1000))
    {
        // Capture error
        while (1)
        {
        };
    }
}

volatile uint32_t msTicks;      //counts 1ms timeTicks
void SysTick_Handler(void)
{
    msTicks++;
}

static void Delay(__IO uint32_t dlyTicks)
{
    uint32_t curTicks = msTicks;
    while ((msTicks - curTicks) < dlyTicks);
}

int returnVal = 0;
int updatedpress = 0;

int buttonpress()   // function to check & add, if the pushbutton is pressed
{

    if (GPIOA->IDR & GPIO_IDR_IDR_0)    //condition: if PA0 is set
    {
        Delay(500);   // avoid debouncing
        if (GPIOA->IDR & GPIO_IDR_IDR_0)   //confirm condition: if PA0 is set
        {
            returnVal = 1 + updatedpress;
            while (GPIOA->IDR & GPIO_IDR_IDR_0)
            {
            }
        }

    }

    return returnVal;
}

int main(void)
{

    GPIO_Init();
    setSysTick();

    while (1)
    {

        int buttonpress();

        updatedpress = returnVal;

        if (updatedpress == 1)
            TIM4->CCR1 = 30;

        else if (updatedpress == 2)
            TIM4->CCR1 = 37;

        else if (updatedpress == 3)
            TIM4->CCR1 = 46;

        else if (updatedpress > 3)
            returnVal = 0;

    }

}

當我運行代碼時,實際上沒有任何工作。 我嘗試運行調試器,發現它到達后立即退出了buttonpress函數

 if(GPIOA->IDR & GPIO_IDR_IDR_0)

知道為什么它不能正常工作嗎?

GPIO外設保持復位狀態,應清除復位位:

RCC->AHBRSTR |= RCC_AHBRSTR_GPIOBRST
RCC->AHBRSTR &= ~RCC_AHBRSTR_GPIOBRST

/* ... */

RCC->AHBRSTR |= RCC_AHBRSTR_GPIOARST;
RCC->AHBRSTR &= ~RCC_AHBRSTR_GPIOARST;

好的,它起作用了,在這里進行了更正:

1 / PB6(連接至直流電動機)被錯誤地設置為輸出。 更改為備用功能。

2 /用於比較按鈕按下次數和選擇電動機速度的If條件被轉移到稱為runmotor的單獨功能runmotor

3 /將讀指令if(GPIOA->IDR & 0x0001)移入主功能內的while循環中,以確保連續檢查按鈕狀態。

4 /按照@berendi的建議清除GPIO的復位

這是更新的代碼:

#include <stdio.h>
#include "stm32l1xx.h"                  // Keil::Device:Startup


// initialization of GPIOB, GPIOA & PWM/TIM4
void GPIO_Init(){
    // initialization of GPIOB
    RCC->AHBRSTR |= RCC_AHBRSTR_GPIOBRST;     /* Reset GPIOB clock*/
    RCC->AHBRSTR &= ~RCC_AHBRSTR_GPIOBRST;   /* Clear Reset */
    RCC->AHBENR |= RCC_AHBENR_GPIOBEN;      /* Enable GPIOB clock*/
    GPIOB->MODER   &=   ~(0x03 << (2*6));  /* Clear bit 12 & 13 */
    GPIOB->MODER   |=   0x02 << (2*6);    /* set as Alternate function*/
    GPIOB->OSPEEDR &=   ~(0x03<< (2*6)); /* 40 MHz  speed*/
    GPIOB->OSPEEDR |=   0x03<< (2*6);   /* 40 MHz  speed */
    GPIOB->PUPDR &=         ~(1<<6);   /* NO PULL-UP PULL-DOWN*/
    GPIOB->OTYPER &=        ~(1<<6);  /* PUSH-PULL*/
    GPIOB->AFR[0] |=        0x2 << (4*6);

    // initialization of GPIOA
    RCC->AHBRSTR |= RCC_AHBRSTR_GPIOARST;    /* Reset GPIOA clock */
    RCC->AHBRSTR &= ~RCC_AHBRSTR_GPIOARST;  /* Clear Reset  */
    RCC->AHBENR |= RCC_AHBENR_GPIOAEN;     /* Enable GPIOA clock  */
    GPIOA->MODER   &=   ~(0x03);          /* Clear & set as input */
    GPIOA->OSPEEDR &=   ~(0x03);         /* 2 MHz  speed  */
    GPIOA->OSPEEDR |=   0x01;           /* 2 MHz  speed   */
    GPIOA->PUPDR &=         ~(0x03);   /* reset PULL-DOWN  */
    GPIOA->OTYPER &=        ~(0x1);   /* PUSH-PULL  */

    //initialization of PWM & TIM4 
    RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
    TIM4->PSC = 100; 
    TIM4->ARR = 414; // F=2.097MHz , Fck= 2097000/(100+1)= 20.762KHz, Tpwm = 0.02, ARR= (Fck x Tpwm)-1

    TIM4->CCMR1 |= TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2; // 111: PWM mode 1 
    TIM4->CCMR1 |= TIM_CCMR1_OC1PE;
    TIM4->CR1 |= TIM_CR1_ARPE;
    TIM4->CCER |= TIM_CCER_CC1E;
    TIM4->EGR |= TIM_EGR_UG;
    TIM4->SR &= ~TIM_SR_UIF;
    TIM4->DIER |= TIM_DIER_UIE;
    TIM4->CR1 |= TIM_CR1_CEN;
    }

    void setSysTick(void){
    // ---------- SysTick timer (1ms) -------- //
    if (SysTick_Config(SystemCoreClock / 1000)) {
        // Capture error
        while (1){};
    }
}


    volatile uint32_t msTicks;      //counts 1ms timeTicks
void SysTick_Handler(void) {
    msTicks++;
}

static void Delay(__IO uint32_t dlyTicks){                                              
  uint32_t curTicks = msTicks;
  while ((msTicks - curTicks) < dlyTicks);
}

    int buttonpress=0; 

     static void runmotor(void)
{
    if (buttonpress ==1){
            TIM4->CCR1 = 30;
    return;
        }
         if (buttonpress ==2){
            TIM4->CCR1 = 37;
    return;
         }
         if (buttonpress ==3){
            TIM4->CCR1 = 46;
    return;
         }
        if (buttonpress > 3){
            TIM4->CCR1 = 0;
            buttonpress = 0;
    return;
         }

}


int main(void){

    GPIO_Init();
    setSysTick();

while (1){  

      if(GPIOA->IDR & 0x0001)
    {
        Delay(5);
            if(GPIOA->IDR & 0x0001) 
                buttonpress = buttonpress + 1;
      runmotor();

}   
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM