STM32 Learning (1) Encapsulating peripherals into an Arduino-style class library

Source: Internet
Author: User

Recently suddenly want to study STM32, see almost all of the tutorials are used in C-Series microcontroller program, so that has been studying C 艹 I write very not adapt to, today studied for a noon, successfully a single LED driver package into an Arduino-style class library, the use is very convenient, the method is as follows.


Before you begin, it is recommended that you follow the methods described in this article in the STM32 template creation method to put the basic libraries needed for the project into a folder, which makes it easier to add. C and. h files.


1. In order to encapsulate the driver into a class, first define the driver code header file Led.h.

#ifndef led_h#define led_h#include "Stm32f10x_gpio.h" class led{    enum State{high, low};p ublic:    LED (Gpio_ typedef* Gpiox, uint16_t gpio_pin_x, state s = high);    Virtual ~led ();    void Open ();    void Close ();    inline bool IsOpen () const {return IsOpen;} Private:    gpio_typedef* gpiox;    uint16_t gpio_pin_x;    bool IsOpen;};    #endif

The class name is led, the three parameters of the constructor are in turn the IO port line where the small bulb is located, the IO number of the small bulb and the initial state of the IO port (the small bulb has a high level at one end, and the IO port is high by default). Add a public-lit led and a method declaration to turn off the LEDs, and an inline function to return the status of the current small bulb.


2. Next define the implementation of this class, create a new. cpp file, and note that the. c cannot be compiled otherwise.

#include "led.h" led::led (gpio_typedef* gpiox, uint16_t gpio_pin_x, state s) {IsOpen =false;    This->gpiox = Gpiox;        this->gpio_pin_x = gpio_pin_x;    Set the port clock if (Gpiox = = Gpioa) {rcc_apb2periphclockcmd (Rcc_apb2periph_gpioa, ENABLE);    } else if (Gpiox = = Gpiob) {rcc_apb2periphclockcmd (RCC_APB2PERIPH_GPIOB, ENABLE);    } else if (Gpiox = = gpioc) {rcc_apb2periphclockcmd (RCC_APB2PERIPH_GPIOC, ENABLE);    } else if (Gpiox = = gpiod) {rcc_apb2periphclockcmd (rcc_apb2periph_gpiod, ENABLE);    } else if (Gpiox = = Gpioe) {rcc_apb2periphclockcmd (Rcc_apb2periph_gpioe, ENABLE);    } else if (Gpiox = = gpiof) {rcc_apb2periphclockcmd (rcc_apb2periph_gpiof, ENABLE);    } else if (Gpiox = = Gpiog) {rcc_apb2periphclockcmd (Rcc_apb2periph_gpiog, ENABLE);    } gpio_inittypedef Init; Configure IO port init.    Gpio_pin = gpio_pin_x; Init.    Gpio_mode = gpio_mode_out_pp; InchIt.    Gpio_speed = Gpio_speed_50mhz;    Initialize IO port gpio_init (gpiox, &init);    if (s = = high) {gpio_setbits (Gpiox, gpio_pin_x);    } else {gpio_resetbits (gpiox, gpio_pin_x);    }}led::~led () {}void led::open () {gpio_resetbits (Gpiox, gpio_pin_x); IsOpen = true;}    void Led::close () {gpio_setbits (Gpiox, gpio_pin_x); IsOpen = false;}

First, the corresponding port clock is set according to the function parameter, then the IO port is configured normally. Finally, the Resetbits () and setbits () functions are written separately in the open and close functions. This enables a simple package that is driven.


#include "stm32f10x.h" #include "led.h" #include "delay.h" int main () {    delayer mydelay;        Led myled (Gpioa, gpio_pin_2);        while (1)    {        myled.open ();        Mydelay.delay (+);        Myled.close ();        Mydelay.delay (+);    }
    return 0;}

When used, instantiate the class directly and invoke the appropriate public method.

If you want to implement control of multiple GPIO ports, you can instantiate multiple objects, or overload the constructors of classes.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

STM32 Learning (1) Encapsulating peripherals into an Arduino-style class library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.