This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Hello world simple program to drive DC Motor

Hi

I'm working on a project to drive a DC motor, I really didn't find any example. I would like to know how can I program a pin in Keil, and I would be happy to have a simple example about it.

By the way Im using NRF51 DK.

Bests,

Parents Reply Children
  • nrf_gpio.h defines functions like nrf_gpio_pin_set() so you don't have to write directly to the registers. The blinky example shows how to use this library. Isn't that what you wanted?

    You write: "I really need a simple example which goes directly to gpio.h and shows how to work with this library". That is exactly what the blinky example does.

    If you want to go lower than that, you can have a look into nrf_gpio.h and see how the functions write to the registers on the chip. For example NRF_GPIO->OUT = 0xFF writes 1 to all pins. To see which registers you can write to and what they do, go to the nRF51 Reference Manual section 13.2

  • Thank you for your comment. As an example if I want to describe for you,

        enter code here
        #ifndef F_CPU
    #define F_CPU 8000000UL // 16MHz clock speed
    #endif
    
    #include <avr/io.h>
    #include <util/delay.h>
    
    int main(void)
    {
      DDRC = 0xFF; //PORTB as Output
      while(1)
      {
        //Rotates Motor in Antilockwise
        PORTC = 0x01; //00000001
        _delay_ms(4000);
    
        //Stops Motor
        PORTC = 0x00; //00000000
        _delay_ms(4000);
    
        //Rotates Motor in Clockwise
        PORTC = 0x02; //00000010
        _delay_ms(4000);
    
        //Stops Motor
        PORTC = 0x03; //00000011
        _delay_ms(4000);
      }
    }
    

    this is a simple example which gives me an idea how to control a port on microcontroller directly.My question is that whetehr such a simple senario is available?

  • We do not have such an example. But it would be quite easy to port the code you just wrote, by looking at the nrf_gpio.h file. The equivalent to PORTA/B/C/D on an AVR would be NRF_GPIO->OUT. Just remember that this is 32 bit.

    The equivalent to _delay_ms() is nrf_delay_ms()

Related