Water lamp of stm32f103

Source: Internet
Author: User

I plan to review the hardware resources related to stm32 over the past few days. I want to start from the very beginning and get familiar with what I learned. Learning stm32 initially started from the flow lamp, and today it starts to light up the flow lamp.

First of all, you must understand the hardware principle of the Water lamp. The following is a schematic diagram:


It is controlled by eight pins. Therefore, you must initialize eight pins first.

// Initialize pe8... pe15 is the output port. and enable the clock // led Io of these eight ports to initialize void led_init (void) {gpio_inittypedef gpio_initstructure; rcc_apb2periphclockcmd (rcc_apb2periph_gpioe, enable ); // enable PE port clock frequency = hour | gpio_pin_10 | gpio_pin_11 | gpio_pin_12 | gpio_pin_13 | gpio_pin_14 | gpio_pin_15; // led1 --> pe.8... led8 --> pe.15 port configuration middleware = gpio_mode_out_pp; // push-pull output middleware = gpio_speed_50mhz; // The IO port speed is 50mhzgpio_init (gpioe, & gpio_initstructure ); // initialize the gpioegpio_setbits (gpioe, oe | gpio_pin_9 | gpio_pin_10 | gpio_pin_11 | gpio_pin_12 | gpio_pin_13 | gpio_pin_14 | gpio_pin_15) according to the set parameters. // pe.8 low output}
In this way, the initialization of the flux lamp pin is realized, and the remaining operations only need to be completed by calling the corresponding operation in the main function.

In order to make the program more perfect, the initialization of the buzzer is added, and the water lamp is on and the buzzer is on.

<Span style = "font-size: 14px;"> <strong> // initialize pb5 as the output port. and enable the clock of this port // The buzzer to initialize void beep_init (void) {gpio_inittypedef gpio_initstructure; rcc_apb2periphclockcmd (enabled, enable); // enable the gpiob port clock timeout = gpio_pin; // beep --> port pb.5 is configured with export = gpio_mode_out_pp; // The export speed = gpio_speed_50mhz; // The speed is 50mhzgpio_init (gpiob, & gpio_initstructure ); // initialize gpiob.5gpio _ resetbits (gpiob, gpio_pin_5) according to the parameter; // output 0 and close the buzzer output} </strong> </span>


There is also a key step in the flow lamp operation. To achieve precise latency, you need to write your own latency function. Latency is subtle and has a millisecond latency. You can write the latency as needed.

<Span style = "font-size: 14px;"> // initialize the latency function. // the clock of the clock specified by systick is fixed to 1/8 of the hclk clock. // sysclk: system clock void delay_init () {javasick_clksourceconfig (javasick_clksource_hclk_div8); // select the external clock hclk/8fac_us = systemcoreclock/8000000; // set the system clock's 1/8 fac_ms = (1000) fac_us; // for non-UCOS, it indicates the number of clock clocks required by each Ms} </span>


Millisecond function Initialization

// Latency NMS // note the range of NMS // The maximum latency is: // NMS <= 0xffffff * 8*1000/sysclk // The unit of sysclk is Hz, and the unit of NMS is Ms // for 72 m, NMs <= 1864 void delay_ms (NMS) {u32 temp; cmdick-> load = (u32) NMS * fac_ms; // time loading (when ick-> load is 24bit) Running ick-> val = 0x00; // clear the counter running ick-> CTRL | = export ick_ctrl_enable_msk; // start the countdown do {temp = running ick-> CTRL;} while (temp & 0x01 &&! (Temp & (1 <16); // The wait time reaches ick-> CTRL & = ~ Systick_ctrl_enable_msk; // close the counter running ick-> val = 0x00; // clear the counter}


Call and initialize the main function

Main function call

# Include "stm32f10x. H "# include" LED. H "# include" beep. H "# include" delay. H "# include" sys. H "int main (void) {delay_init (); // delay function initialization led_init (); // led port initialization beep_init (); // initialize the buzzer port while (1) {led1 = ledon; led2 = ledoff; led3 = ledoff; led4 = ledoff; led5 = ledoff; led6 = ledoff; led7 = ledoff; led8 = ledoff; beep = 1; delay_ms (100); led1 = ledoff; led2 = ledon; led3 = ledoff; led4 = ledoff; beep = beepoff; led5 = ledoff; led6 = ledoff; led7 = ledoff; led8 = ledoff; delay_ms (100); led1 = ledoff; led2 = ledoff; led3 = ledon; led4 = ledoff; led5 = ledoff; led6 = ledoff; led7 = ledoff; led8 = ledoff; delay_ms (100); led1 = ledoff; led2 = ledoff; led3 = ledoff; led4 = ledon; led5 = ledoff; led6 = ledoff; led7 = ledoff; led8 = ledoff; delay_ms (100); led1 = ledoff; led2 = ledoff; led3 = ledoff; led4 = ledoff; led5 = ledon; led6 = ledoff; led7 = ledoff; led8 = ledoff; delay_ms (100); led1 = ledoff; led2 = ledoff; led3 = ledoff; led4 = ledoff; led5 = ledoff; led6 = ledon; led7 = ledoff; led8 = ledoff; delay_ms (100); led1 = ledoff; led2 = ledoff; led3 = ledoff; led4 = ledoff; led5 = ledoff; led6 = ledoff; led7 = ledon; led8 = ledoff; delay_ms (100); led1 = ledoff; led2 = ledoff; led3 = ledoff; led4 = ledoff; led5 = ledoff; led6 = ledoff; led7 = ledoff; led8 = ledon; delay_ms (100 );}}

The main function is actually the most simple program to understand, it is also a program that many beginners can easily write. The principle is to turn each lamp on in sequence, and add a delay function between each lamp to achieve the effect of running water. The specific delay depends on your personal interests and how fast it will change in the delay function.

Here is the final result of the experiment. It looks nice. (When doing things in the future, you can add the water lamp, which looks nice, for example, to a car or a four-axis aircraft. This looks cool !)

Everything has to begin with the foundation. A small lab will do a bigger project in the future.







Water lamp of stm32f103

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.