STM32 Keil C + + writing microcontroller program

Source: Internet
Author: User

C + + belongs to the object-oriented programming language, the idea of OOP is needless to say, especially for complex software engineering, using OOP is definitely a multiplier, relative to the traditional C;

Of course, c to write a single-chip computer program is understandable, has continued a tradition, from the beginning of college to work, many people have been using C to do, but since Keil support C + + compilation,

You can use C + + to write your code, you can use the high-level language to structure, clear your program, why not? Haha, personal opinion! Below to get to the chase:

C + + compatible, so STM32 library can be used by C + +, in this case, it is simple! In the establishment of a new STM32 project, it is possible to direct C + + programming;

Using the C + + way to build a good file, I wrote a demo:

. h file--#ifndef __pled__#define__pled__#include"stm32f10x.h"classpled{ Public: PLed (); ~PLed ();  Public:        voidLed_gpio_config (void); voidTurnOn (uint16_t Port,BOOLstatus); Private:};#endif. cpp Files--#include"PLed.h"PLed::P LED () {led_gpio_config ();} PLed::~PLed () {}voidPled::led_gpio_config () {gpio_inittypedef gpio_initstructure;                                                                    Rcc_apb2periphclockcmd (Rcc_apb2periph_gpioe, ENABLE); Gpio_initstructure.gpio_pin= gpio_pin_6|gpio_pin_5; Gpio_initstructure.gpio_mode=gpio_mode_out_pp; Gpio_initstructure.gpio_speed=Gpio_speed_50mhz; Gpio_init (Gpioe,&gpio_initstructure);          Gpio_setbits (gpioe,gpio_pin_5); Gpio_setbits (Gpioe,gpio_pin_6); }voidPled::turnon (uint16_t Port,BOOLstatus) {  if(status) gpio_setbits (Gpioe,port); ElseGpio_resetbits (Gpioe,port);}

Main file--#include"stm32f10x.h"#include"misc.h"#include"stdio.h"#include"PSysTick.h"#include"PLed.h"intMainvoid) {Psystick PST; Pst.    Systick_init ();        PLed PLed;  while(1) {pled. TurnOn (Gpio_pin_5,true); Pled. TurnOn (Gpio_pin_6,false); Pst. Delay ( -); Pled. TurnOn (Gpio_pin_5,false); Pled. TurnOn (Gpio_pin_6,true); Pst. Delay ( -); }}
Systick file #ifndef __pdelay_h#define__pdelay_h#include"stm32f10x.h"classpsystick{ Public: Psystick (); ~Psystick ();  Public:        voidDelay (u32 ntime); voidSystick_init (); Staticu32 Timingdelayx;};#endif#include"PsysTick.h"Psystick::P Systick () {}psystick::~Psystick () {}voidPsystick::D elay (u32 ntime) {Timingdelayx=Ntime;  while(Timingdelayx! =0);}voidPsystick::systick_init () { while(Systick_config (Systemcoreclock/ +)); }u32 Psystick::timingdelayx=0;
Interrupt Function: extern " C " {void systick_handler (void) {    if(psystick::timingdelayx!= 0x00 )    {      Psystick::timingdelayx--;    }     Else         pusart::senddata ('E');}}

The entire project C + + code as above, directly to compile, of course, if your main is still used. C, can also not modify the suffix, directly in the properties of the MAIN.C modified to C++sourcefile can be used in the interrupt file, the interrupt file is also changed to C + + type,

After this compilation, and normal C writing, download files to a single-chip computer can be;

It's almost done! After the problem, continue to study!



STM32 Keil C + + writing microcontroller program

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.