Cov txheej txheem:

Yog li, Koj Thauj STM32duino Bootloader hauv Koj "Blue Pill" Yog Li Cas Tam Sim No?: 7 Kauj Ruam
Yog li, Koj Thauj STM32duino Bootloader hauv Koj "Blue Pill" Yog Li Cas Tam Sim No?: 7 Kauj Ruam

Video: Yog li, Koj Thauj STM32duino Bootloader hauv Koj "Blue Pill" Yog Li Cas Tam Sim No?: 7 Kauj Ruam

Video: Yog li, Koj Thauj STM32duino Bootloader hauv Koj
Video: Программирование МК STM32. УРОК 34. HAL. USB. HID 2024, Hlis ntuj nqeg
Anonim
Yog li, Koj Thauj STM32duino Bootloader hauv Koj
Yog li, Koj Thauj STM32duino Bootloader hauv Koj
Yog li, Koj Thauj STM32duino Bootloader hauv Koj
Yog li, Koj Thauj STM32duino Bootloader hauv Koj

Yog tias koj twb tau nyeem kuv cov lus qhia piav qhia yuav ua li cas thauj khoom STM32duino bootloader lossis lwm yam ntaub ntawv zoo sib xws, koj sim thauj cov piv txwv piv txwv thiab…. Tej zaum yuav tsis muaj dab tsi tshwm sim hlo li.

Teeb meem yog, ntau, yog tias tsis yog txhua tus piv txwv rau "Generic" STM32 yuav tsis ua haujlwm tawm ntawm lub thawv. Nws yuav yog qhov hloov pauv me me kom tau txais ua haujlwm hauv koj li STM32 "Blue Pill" board.

Kuv yuav xaiv 4 tus lej piv txwv los piav qhia seb yuav hloov dab tsi thiab vim li cas. Cov lej yog: "BlinkWithoutDelay", "Fading", "Dimmer" thiab "AnalogInSerial".

Nco tseg Kuv tsis tau sau dab tsi. Kuv tsuas yog teeb meem hloov pauv me me hauv cov lej tsim los ntawm:

David A. Mellis thiab hloov kho lig los ntawm Tom Igoe, Marti Bolivar thiab qee kis los ntawm Scott Fitzgerald

Tom Igoe thiab hloov kho lig los ntawm Bryan Newbold

Yog li, Kuv nyiam khaws tus sau npe txawm tias nyob hauv cov lej kuv hloov kho, khaws qhov tsim credit.

Kauj Ruam 1: Pins thiab Pins…. Vim Li Cas Txoj Cai Tsis Ua Haujlwm?

Pins thiab Pins…. Vim Li Cas Txoj Cai Tsis Ua Haujlwm?
Pins thiab Pins…. Vim Li Cas Txoj Cai Tsis Ua Haujlwm?

Cia saib hauv STM32 "Blue Pill" pin tawm. Nco tseg pins tau txheeb xyuas ua PA1 lossis PC2….something zoo li ntawd.

Yog tias koj saib hauv, piv txwv li, "BlinkWithoutDelay" piv txwv tus lej, tus pin tau tshaj tawm tias "33" …. Vim li cas?

Kuv xav tias yog vim Mr. Marti Bolivar xa cov lej no rau MAPLE board.

Kuv xav tias nws tsis yog nws lub hom phiaj cia ua kom haum rau "Blue Pill" boards.

Maple thiab Maple mini board pins yog tus lej tshaj tawm, zoo li Arduino, txawm hais tias lawv siv tus lej zoo li 33, 24 thiab qee qhov zoo li no.

Kuv hais tias code tsis ua haujlwm? Kuv yuam kev. Txoj cai suav sau yam tsis muaj qhov yuam kev thiab xa kom raug mus rau "Blue Pill", yog li, kuv qhov kev xav nws yog qhov ua haujlwm tiag, tab sis siv GPIO cov zis peb tsis xav tau. Tej zaum yuav tsis muaj.

Yog li kev hloov pauv me me yog qhov tsim nyog hauv cov cai rau nws ua haujlwm raws li xav tau.

Kauj Ruam 2: Cia "txhais" Qee Pins …

Cia
Cia

Nws yog qhov kev coj ua zoo tshaj tawm cov peev txheej uas yooj yim txheeb xyuas lossis txhais lub ntsiab lus hloov pauv lossis tas li. Nws yuav tso cai rau koj nkag siab yooj yim dua thiab daws teeb meem.

Kuv siv tshaj tawm Arduino pins zoo li no:

const int ledPin = 13;

…"

Yog tias koj nyiam kuv, tej zaum koj nug koj tus kheej: "Kuv yuav ua li cas thiaj tuaj yeem tshaj tawm cov koob nrog cov npe zoo li PC13 ???"

Cov lus teb yog: Siv "#define" C nqe lus.

Yog li, raws li kos pinout, PC13 yog tus pin peb muaj ntawm board LED hauv "BluePill". Txhawm rau siv nws, Kuv yuav tshaj tawm zoo li no, tsuas yog tom qab lub tsev qiv ntawv txhais (#suav nrog …) thiab ua ntej txhua yam ntxiv:

#define LedPin PC13

…"

Nco tseg tsis muaj ";" kev tshem tawm kab, NOR "=" txoj haujlwm.

Sib piv ob tus lej. Ib qho yog thawj qhov piv txwv thauj los ntawm IDE. Qhov thib ob yog qhov kuv tau ua qee qhov kho kom ua haujlwm nrog "BluePill".

Kuv pom zoo kom tshaj tawm txhua tus pins koj npaj siab siv hauv cov cai. Txawm tias cov tswv yim siv los ua ADC cov tswv yim (ntau ntxiv txog nws tom qab).

Qhov no yuav ua rau koj lub neej yooj yim.

Kauj Ruam 3: PinMode ()… Koj Yuav Siv Koj Tus Pins Li Cas…

Ua ntej txuas ntxiv, cia nkag siab PinMode () kev ua haujlwm.

Zoo li Arduino, STM32 tus pin muaj ntau txoj haujlwm. Txoj hauv kev yooj yim tshaj plaws los xaiv ib lossis lwm qhov yog siv pinMode () nqe lus.

Arduino tsuas muaj 3 hom muaj, INPUT, OUTPUT, lossis INPUT_PULLUP.

STM32, ntawm qhov tod tes muaj ntau qhov tsw qab ntawm pinMode (). Lawv yog:

OUTPUT -Basic digital output: thaum tus pin siab dua, qhov hluav taws xob tau tuav ntawm +3.3v (Vcc) thiab thaum nws qis, nws raug rub los rau hauv av

OUTPUT_OPEN_DRAIN -Qhov qhib qhov dej ntws, tus pin qhia tias "qis" los ntawm kev lees txais kev ntws tam sim no mus rau hauv av thiab "siab" los ntawm kev muab qhov cuam tshuam ntau ntxiv

INPUT_ANALOG -Qhov no yog hom tshwj xeeb rau thaum tus pin yuav siv rau analog (tsis yog digital) nyeem. Pab kom hloov pauv ADC kom ua tiav ntawm qhov hluav taws xob ntawm tus pin

INPUT_PULLUP -Lub xeev tus pin nyob rau hauv hom no tau tshaj tawm ib yam nkaus li nrog INPUT, tab sis tus pin tus nqi yog maj mam "rub tawm" rau +3.3v

INPUT_PULLDOWN -Lub xeev ntawm tus pin hauv hom no tau tshaj tawm ib yam nkaus li nrog INPUT, tab sis tus pin tus nqi yog maj mam "rub qis" mus rau 0v

INPUT_FLOATING -Sau npe rau INPUT

PWM -This yog hom tshwj xeeb rau thaum tus pin yuav siv rau PWM tso zis (tshwj xeeb ntawm cov zis digital)

PWM_OPEN_DRAIN -Zoo li PWM, tshwj tsis yog tias hloov pauv ntawm LOW thiab HIGH, qhov hluav taws xob ntawm tus pin suav nrog kev hloov pauv ntawm LOW thiab ntab (sib txuas)

(ceeb toom: muab rho tawm los ntawm

Kuv tsuas yog qhib cov kab lus no vim tias thaum koj pib tsim koj tus lej, ua tib zoo siv pinMode () rau qhov koj xav tau.

Kauj ruam 4: AnalogWrite () Versus PwmWrite ()… Analog Output hauv 2 Flavors

AnalogWrite () Versus PwmWrite ()… Analog Output in 2 Flavors
AnalogWrite () Versus PwmWrite ()… Analog Output in 2 Flavors
AnalogWrite () Versus PwmWrite ()… Analog Output hauv 2 Flavors
AnalogWrite () Versus PwmWrite ()… Analog Output hauv 2 Flavors

Ua ntej siv "Blue Pill" GPIO pins nws yog qhov tsim nyog tshaj tawm nws tus cwj pwm, piv txwv li, nws yuav ua haujlwm li cas. Qhov ntawd yog qhov ua haujlwm pinMode () ua dab tsi.

Yog li, cia ua kom pom tseeb tam sim no teeb tsa qhov tso tawm analog li cas. Nws tuaj yeem tshaj tawm xws li hom OUTPUT lossis PWM hom.

Tib txoj kev, Cov txiaj ntsig Analog tuaj yeem ua tus cwj pwm rau GPIO hauv 2 txoj hauv kev: analogWrite () lossis pwmWrite (), TAB SIS, analogWrite () YUAV TSUM tsuas yog ua haujlwm yog pinMode () = OUTPUT. Hauv lwm qhov tes, pwmWrite () yuav tsuas ua haujlwm yog pinMode () = PWM.

Cia ua PA0, piv txwv li: nws yog qhov sib piv/pwm tso tawm tus neeg sib tw.

analogWrite (): qhov no tshaj tawm li no:

….

#define ledPin PA0

tus pinMode (ledPin, OUTPUT);

analogWrite (ledPin, <tus lej>);

……"

qhov twg tus lej yuav tsum nyob nruab nrab ntawm 0 thiab 255, zoo li Arduino. Qhov tseeb, nws yog rov qab sib xws rau Arduino.

pwmWrite (): tshaj tawm li no:

#define ledPin PA0

pinMode (ledPin, PWM);

pwmWrite (ledPin, <tus nab npawb.>);

…."

Qhov twg tus lej yuav tsum nyob nruab nrab ntawm 0 ~ 65535, kev daws teeb meem ntau dua li Arduino.

Hauv cov duab tuaj yeem sib piv ntawm 2 tus lej. Koj kuj tuaj yeem pom tus lej qub.

Kauj Ruam 5: STM32 Kev Sib Txuas Lus

STM32 Kev Sib Txuas Lus
STM32 Kev Sib Txuas Lus

Cia peb pom yuav ua li cas npaj USART cuam tshuam hauv STM32. Yog, kev cuam tshuam hauv ntau ntau ….

"Blue Pill" muaj 3 USART's (RX/ TX 1 ~ 3), thiab, yog tias koj siv lub bootloader tso cai rau koj siv USB, nws tsis txuas nrog ib qho ntawm qhov ntawd.

Nyob ntawm koj tab tom siv lossis tsis siv USB, koj yuav tsum tshaj tawm qhov chaw nres nkoj hauv ib lossis lwm txoj hauv kev hauv koj tus lej.

Case 1: Siv USB:

Txoj kev no, cov duab kos tau rub tawm ncaj qha los ntawm USB. Tsis tas yuav txav BOOT0 jumper mus rau 1 txoj haujlwm thiab rov qab mus rau 0.

Hauv qhov no, txhua lub sijhawm koj tshaj tawm "Serial" tsis muaj qhov ntsuas, txhais tau tias kev sib txuas lus ntawm USB.

Yog li, Serial1, txhais tau tias TX/ RX 1 (Pins PA9 thiab PA10); Serial2, txhais tau tias TX/ RX 2 (pins PA2 thiab PA3) thiab Serial 3 txhais tau tias TX/ RX 3 (Pins PA10 thiab PA11).

Nov yog txoj kev peb tab tom ua haujlwm nrog. Kuv yuav nthuav qhia kev hloov pauv piv txwv rau txoj hauv kev coding no.

Lwm qhov: "Serial USB" tsis tas yuav pib ua ntej. Hauv lwm lo lus, "… Serial.begin (15200);" tsis tsim nyog.

Nws muaj peev xwm hu tau ib qho Serial muaj nuj nqi (Serial.read (), Serial.write (), thiab lwm yam) yam tsis muaj kev pib ua ntej.

Yog tias los ntawm qee qhov laj thawj nws muaj nyob hauv cov cai, tus neeg sau yuav tsis quav ntsej nws.

Case 2: Siv TTL seria rau USB adapter:

Hauv txoj kev no, bootloader tsis txhawb nqa haiv neeg STM32 USB kev sib txuas lus, yog li koj xav tau USB rau txuas txuas txuas rau TX/ RX 1 (tus pin PA9 thiab PA10) txhawm rau rub cov duab kos.

Hauv qhov no, txhua lub sijhawm "Serial" uas tsis muaj qhov ntsuas yog tus lej, txhais tau tias TX/ RX1 (chaw nres nkoj siv los tso cov cai). Yog li, Serial1 hais txog TX/ RX 2 (pins PA2 thiab PA3) thiab Serial2 hais txog TX/ RX 3 (Pins PA10 thiab PA11). Tsis muaj Serial3.

Kauj Ruam 6: Dhau Tus Nqi rau Microcontroller

Dhau tus nqi rau Microcontroller
Dhau tus nqi rau Microcontroller

Dimmer piv txwv yog txoj hauv kev yooj yim los qhia tias yuav kis tus nqi mus rau microcontroller li cas.

Nws xav tias dhau tus nqi ntawm 0 txog 255 los tswj LED ci ci.

Nws yuav tsis ua haujlwm raws li qhov xav tau hauv Blue Pill vim:

  1. Txhawm rau siv pwmWrite () ua haujlwm, pinMode () YUAV TSUM tau tshaj tawm ua PWM hom.
  2. Koj yuav tsis tau txais tag nrho 3 tus lej. Serial.read () muaj nuj nqi ntes cov ntsiab lus tsis yog, uas yog "BYTE". yog tias koj ntaus "100" thiab nias "nkag", tsuas yog zaum kawg "0" yuav raug ntes los ntawm qhov tsis. Thiab nws tus nqi yuav yog "48" (tus lej lej ASCII rau "0"). Yog tias npaj siab yuav muab tus nqi "100", nws yuav tsum tau ntaus "d". Yog li, nws yog qhov tseeb hais tias nws yuav hloov tus lej ASCII cim zauv tus nqi hauv LED ci ci, txoj cai ??…. Txhua yam …
  3. Teeb meem, daim ntawv qhia qhov tseem ceeb ncaj qha los ntawm Serial.read () muaj nuj nqi yog qhov ua kom yuam kev. Nws yuav luag txhua yam tau txais qhov txiaj ntsig tsis tau xav txog. Txoj hauv kev zoo dua yog khaws cov ntsiab lus tsis nyob hauv qhov sib txawv ib ntus thiab THAN daim ntawv qhia nws.

Zoo li kuv piav qhia ua ntej hauv kab lus 2, tus lej kuv qhia hloov pauv yuav tso cai nkag mus rau lub cim ASCII thiab qhov no yuav tswj LED qhov ci ci raws li nws tus lej ASCII tus lej … thiab "}" yog qhov ua tau siab tshaj plaws (tus nqi 126). Lwm tus cim tsis tuaj yeem luam tawm, yog li lub davhlau ya nyob twg yuav tsis nkag siab lossis lawv muaj peev xwm sib xyaw ua cim (xws li "~" yog tus yuam sij tuag hauv kuv cov keyboard thiab yuav tsis ua haujlwm raug). Qhov no txhais tau tias, tus cwj pwm sib xyaw, thaum nkag mus rau lub davhlau ya nyob twg, yuav xa tus cim nws tus kheej thiab lwm yam ntxiv. Feem ntau yog ib qho luam tsis tau. Thiab yog tus lej kawg no yuav raug ntes Ib yam, nco ntsoov koj lub davhlau ya nyob twg, qhov no, yuav tsum tsis txhob xa "Carriage Return" lossis "Line Feed". Koj yuav tsum xyuam xim rau qhov no txhawm rau ua haujlwm kom raug.

Yog tias koj poob nws me ntsis tsis meej pem, nws ua rau tsis zoo ….

Kauj Ruam 7: Thiab Yog Kuv Xav Tau Hom Peb Tus Zauv…. los yog Ntau Ntxiv ??

Thiab Yog tias Kuv Xav Tau Ntaus Peb Tus lej…. los yog Ntau Ntxiv ??
Thiab Yog tias Kuv Xav Tau Ntaus Peb Tus lej…. los yog Ntau Ntxiv ??

Tau txais ntau tus cwj pwm los ntawm kev sib txuas lus tsis yog ib txoj haujlwm yooj yim.

Qhov tsis zoo yog FIFO byte pawg ntawm cov ntawv. Txhua lub sijhawm Serial.read () muaj nuj nqi tau hu, thawj daim ntawv xa tawm raug tshem tawm ntawm pawg thiab khaws cia rau qee qhov chaw lwm qhov. Feem ntau yog tus lej sib txawv hauv cov cai. Nco tseg, vam khom kho vajtse, feem ntau muaj sijhawm tas li yuav ua li cas nkag tsis tuaj yeem khaws cov ntaub ntawv.

Yog tias koj npaj siab yuav nkag mus ntau dua ib tus lej ntawm cov lej, koj yuav tsum "sau" kab ntawv los ntawm tus cwj pwm, vim lawv nkag mus rau hauv UART tsis.

Qhov no txhais tau tias kev caij tsheb kauj vab nyeem txhua qhov tsis sib xws, khaws cia hauv qhov sib txawv ntawm qhov kub, thauj nws hauv thawj qhov chaw ntawm txoj hlua sib dhos, txav mus rau txoj haujlwm tom ntej thiab pib dua, txog thaum … zoo, nyob ntawm daim ntawv thov. Muaj 2 txoj hauv kev los xaus lub voj voog:

  1. Siv qee tus cim "cim kawg", zoo li "rov qab nqa tsheb" lossis "Kab Pub". Sai li "xaus Mark" char pom, lub voj xaus.
  2. Xwb, tus naj npawb ntawm cov cim hauv txoj hlua tuaj yeem txwv, yog li ua, tus naj npawb ntawm kev sib tham sib. Thaum nws mus txog qhov txwv, cia hais tias, 4, tau txais kev ua tiav tas los ntawm nws tus kheej.

Cia saib hauv qhov piv txwv yooj yim yuav ua li cas:

  • Teeb tus cim "kawg", zoo li '\ n' (qhov no txhais tau tias kab pub ASCII char).
  • looping meanwhile Serial.available () muaj tseeb
  • khaws cia Serial.read () ua rau muaj qhov hloov pauv ib ntus. Nco ntsoov: sai li sai tau Serial.read () ua tau "nyeem" tsis zoo, nws yog qhov huv thiab tus yam ntxwv tom ntej muaj nyob hauv nws.
  • nce cov hlua sib txawv nrog cov ntawv no
  • Yog tias daim ntawv kawg yog "kawg" tawm ntawm lub voj.

Feem ntau, niaj hnub kom tau txais cov cim ua ntu zoo li daim duab.

Nws tau ua raws qhov kev hloov pauv dav ntawm Mr. David A. Mellis tus lej qub.

Tsis pub siv thiab sim nws. Nco ntsoov: qhov tseem ceeb YUAV TSUM nkag rau hauv 3 tus lej.

Qhov no nws rau tam sim no. Kuv yuav tsis nthuav kuv tus kheej hauv cov ntsiab lus sib txuas lus txuas ntxiv Nws yog qhov nyuaj dhau los npog ntawm no thiab nws tsim nyog nws tus kheej Intructables.

Kuv vam tias nws yuav pab koj siv piv txwv hauv Blue Pill thiab muab qee qhov kev pom kev rau koj kom raug cai li cas rau pawg tswj hwm me me no.

Pom koj ib puag ncig hauv lwm cov lus qhia.

Pom zoo: