STM32 based on HAL library running water lamp experiment

STM32basedonHALlibraryrunningwaterlampexperiment

1.SetuptheSTM32developmentenvironment

1.InstallSTM32CubeMX

1.Downloadaddress:STM32CubeMX-STM32Cubeinitializationcodegenerator-STMicroelectronics

2.Runtheinstallationprogramasanadministratorandclicknext:

Edit

3.Click”Iacceptthetermsofthislicenseagreement”,andthenselectNext:

Edit

4.Justcheckthefirstone

Edit

5.Selecttheinstallationlocation.ThedefaultlocationistoinstallintheCdrive(note:donothaveChinesecharactersintheinstallationlocation):

Edit

6.Starttheinstallationdirectly:

Edit

2.InstallHALlibrary

1.OpentheinstalledSTMCubeMX

Edit

2.ClickHELP->Manageembeddedsoftwarepackages:

Edit

3.ChecktheHALlibraryyouwanttoinstallandclick”InstallNow”untiltheinstallationissuccessful.

Edit

2.Newproject

1.ReturntothemaininterfaceofSTMCubeMXandcreateanewproject:

Edit

2.Selectyourownchipinthepartname,clickonthespecificchipinformationintheinformationcolumntoselectit,andclickstartproject:

Edit

3.Clicksystemcore,enterSYS,andselectserialwireunderdebug:

Edit

4.Configuretheclockandentertherccabove.Therearetwoclocks,oneishseandlse.WeneedtousetheGPIOinterface,andtheseinterfacesareallinAPB2:

Edit

Next,observetheclockarchitecture.TheclockoftheAPB2busiscontrolledbyhse.Atthesametime,youmustselecttherightsideofPLLCLKinthisinterface:

Edit

5.SethsetoCrystal/CeramicResonator:

Edit

6.Thenextstepistoclickonthecorrespondingpintosettheoutputregister,whichistheoutputitem.Therearethreeselectedintotal,whicharePA4,PB9,andPC15:

Edit

Edit

7.Clickprojectmanager,configureyourpathandprojectname,andthenchangetheIDEitemtoMDK-ARM:

Edit

8.Enterthecodegenerateinterface,selecttogeneratetheinitialization.c/.hfile,thenclickgeneratecode,selectopenproject,andthengotoKEIL5:
?Edit

keilsimulationdebugging

1Openthe.uvprojxfile(orselectopenprojectinthepreviousstep)

Edit

2.Openthemain.cfileandfindthemainfunctionpart:

Edit

3.Putthefollowingcodeintothemainfunction(replacethecontentinside)

SystemClock_Config();//Systemclockinitialization
MX_GPIO_Init();//gpioinitialization
while(1)
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);//PA4lightsup
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);//PB9lightsout
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);//PC15lightsout
HAL_Delay(1000);//Delay1s
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);//PA4lightsout
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);//PB9lightsup
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_SET);//PC15lightsout
HAL_Delay(1000);//Delay1s
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);//PA4lightsout
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);//PB9lightsout
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_15,GPIO_PIN_RESET);//PC15lightsup
HAL_Delay(1000);//Delay1s
}

3.Circuitconnection

ForUSBtoTTLmoduleandstm32f103c8t6connection:
GND-GND
3v3-3v3
TXD-A10
RXD-A9

4.Burnandrun

5.ObservetheoutputwaveformoftheGPIOport

IntheTargetinterface,selectthecorrectcrystaloscillatorsize.

Edit

Debugpagesettings:

Edit

ClickDebugtoenterthedebugginginterface:

Edit

Selectalogicanalyzer:

Edit

Selectthepintoobserve:

①ClickSetupLogicAnalyzer

Edit

②Addthepinstobeobserved:
?Edit

Relatedsettings

Edit

Runtheprogram:

Edit

Observethewaveform:

Edit

6.Thestm32externalinterruptmodecontrollightturnsonandoff

CubeMXnewproject

1.Peripheralsettings
SettheindicatorLEDpinpb3andsetthepinmodetooutputmodeGPIO_Output
SetthebuttonpinPA2,setthepintotheexternalinterruptfunction,andconnectPA2totheexternalinterruptlineEXIT1toGPIO_EXIT1

Edit

ForthePB3pincorrespondingtotheLED,thedefaultsettingisenough,andthenameissettoLED

Edit

FortheswitchcorrespondingpinPA2,setitstriggermodetorisingedgetrigger.

Edit

Toenablethecorrespondingexternalinterruptline,clickEnabled

Edit

Configureinterruptpriority

Edit

clocksettings

Edit

Justgeneratetheprojectfile

voidHAL_GPIO_EXTI_Callback(uint16_tGPIO_Pin)
{
if(GPIO_Pin==A1_EXTI_Pin)//Determinetheexternalinterruptsource
{
HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);//ToggleLEDstatus
}
}

Edit

Circuitconnection
GPIOdefaultsto3.3Vhighlevel.Whenthebuttonispressed,GPIOchangestolowlevel,andthisprocessisafallingedge.ReleasethebuttonandGPIObecomeshighlevelagain,andthisprocessisarisingedge.Becausetherisingedgeinterruptisset,thatis,whenthebuttonisreleased,thelightwillchange.

GND—G
3v3—-3v3
RXD—-PA9
TXD—-PA10
LEDlonglegs–3V3
LEDshortpin–PB3
PA2–3V3–Lighton
PA2–GND–Lightsoff
Burningprogram

471)]Edit

Circuitconnection
GPIOdefaultsto3.3Vhighlevel.Whenthebuttonispressed,GPIOchangestolowlevel,andthisprocessisafallingedge.Releasethebutton,andGPIObecomeshighlevelagain,andthisprocessisarisingedge.Becausetherisingedgeinterruptisset,thatis,whenthebuttonisreleased,thelightwillchange.

GND—G
3v3—-3v3
RXD—-PA9
TXD—-PA10
LEDlonglegs–3V3
LEDshortpin–PB3
PA2–3V3–Lighton
PA2–GND–Lightsoff
Burningprogram