STM32basedonHALlibraryrunningwaterlampexperiment
1.SetuptheSTM32developmentenvironment
1.InstallSTM32CubeMX
1.Downloadaddress:STM32CubeMX-STM32Cubeinitializationcodegenerator-STMicroelectronics
2.Runtheinstallationprogramasanadministratorandclicknext:
3.Click”Iacceptthetermsofthislicenseagreement”,andthenselectNext:
4.Justcheckthefirstone
5.Selecttheinstallationlocation.ThedefaultlocationistoinstallintheCdrive(note:donothaveChinesecharactersintheinstallationlocation):
6.Starttheinstallationdirectly:
2.InstallHALlibrary
1.OpentheinstalledSTMCubeMX
2.ClickHELP->Manageembeddedsoftwarepackages:
3.ChecktheHALlibraryyouwanttoinstallandclick”InstallNow”untiltheinstallationissuccessful.
2.Newproject
1.ReturntothemaininterfaceofSTMCubeMXandcreateanewproject:
2.Selectyourownchipinthepartname,clickonthespecificchipinformationintheinformationcolumntoselectit,andclickstartproject:
3.Clicksystemcore,enterSYS,andselectserialwireunderdebug:
4.Configuretheclockandentertherccabove.Therearetwoclocks,oneishseandlse.WeneedtousetheGPIOinterface,andtheseinterfacesareallinAPB2:
Next,observetheclockarchitecture.TheclockoftheAPB2busiscontrolledbyhse.Atthesametime,youmustselecttherightsideofPLLCLKinthisinterface:
5.SethsetoCrystal/CeramicResonator:
6.Thenextstepistoclickonthecorrespondingpintosettheoutputregister,whichistheoutputitem.Therearethreeselectedintotal,whicharePA4,PB9,andPC15:
7.Clickprojectmanager,configureyourpathandprojectname,andthenchangetheIDEitemtoMDK-ARM:
8.Enterthecodegenerateinterface,selecttogeneratetheinitialization.c/.hfile,thenclickgeneratecode,selectopenproject,andthengotoKEIL5:
keilsimulationdebugging
1Openthe.uvprojxfile(orselectopenprojectinthepreviousstep)
2.Openthemain.cfileandfindthemainfunctionpart:
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.
Debugpagesettings:
ClickDebugtoenterthedebugginginterface:
Selectalogicanalyzer:
Selectthepintoobserve:
①ClickSetupLogicAnalyzer
②Addthepinstobeobserved:
Relatedsettings
Runtheprogram:
Observethewaveform:
6.Thestm32externalinterruptmodecontrollightturnsonandoff
CubeMXnewproject
1.Peripheralsettings
SettheindicatorLEDpinpb3andsetthepinmodetooutputmodeGPIO_Output
SetthebuttonpinPA2,setthepintotheexternalinterruptfunction,andconnectPA2totheexternalinterruptlineEXIT1toGPIO_EXIT1
ForthePB3pincorrespondingtotheLED,thedefaultsettingisenough,andthenameissettoLED
FortheswitchcorrespondingpinPA2,setitstriggermodetorisingedgetrigger.
Toenablethecorrespondingexternalinterruptline,clickEnabled
Configureinterruptpriority
clocksettings
Justgeneratetheprojectfile
voidHAL_GPIO_EXTI_Callback(uint16_tGPIO_Pin) { if(GPIO_Pin==A1_EXTI_Pin)//Determinetheexternalinterruptsource { HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);//ToggleLEDstatus } }
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