Cov txheej txheem:

AVR Assembler Tutorial 1: 5 Cov Kauj Ruam
AVR Assembler Tutorial 1: 5 Cov Kauj Ruam

Video: AVR Assembler Tutorial 1: 5 Cov Kauj Ruam

Video: AVR Assembler Tutorial 1: 5 Cov Kauj Ruam
Video: AVR Ассемблер. Урок 1. Вводный. AVR Assembler. Lesson 1. Promo. 2024, Hlis ntuj nqeg
Anonim
AVR Assembler Tutorial 1
AVR Assembler Tutorial 1

Kuv tau txiav txim siab sau cov lus qhia yuav ua li cas sau cov lus sib dhos ua haujlwm rau Atmega328p uas yog microcontroller siv hauv Arduino. Yog tias tib neeg tseem txaus siab kuv tseem yuav tso tawm ib lub lis piam lossis ntau dua kom txog thaum kuv tsis muaj sijhawm so lossis lwm tus neeg tsis nyeem lawv.

Kuv tab tom khiav Arch linux thiab kuv tab tom ua haujlwm ntawm atmega328p-pu teeb tsa ntawm lub khob cij. Koj tuaj yeem ua nws ib yam nkaus li kuv lossis koj tuaj yeem yooj yim ntsaws ib qho arduino rau hauv koj lub computer thiab ua haujlwm ntawm lub microcontroller li ntawd.

Peb yuav sau cov phiaj xwm rau 328p zoo li ib qho uas nyob hauv feem ntau ntawm arduino tab sis koj yuav tsum nco ntsoov tias cov haujlwm zoo ib yam thiab cov txuj ci tseem yuav ua haujlwm rau ib qho ntawm Atmel microcontrollers thiab tom qab ntawd (yog tias muaj kev txaus siab) peb yuav ua haujlwm nrog qee qhov lwm tus thiab. Cov ncauj lus kom ntxaws ntawm microcontroller tuaj yeem nrhiav tau hauv Atmel cov ntaub ntawv txheeb xyuas thiab Phau Ntawv Teev Npe Qhia. Kuv tab tom txuas lawv rau cov lus qhia no.

Nov yog yam koj yuav xav tau:

1. Ib lub khob cij

2. Ib qho Arduino, lossis tsuas yog lub microcontroller

3. Lub khoos phis tawj siv Linux

4. Tus avra assembler siv git: git clone https://github.com/Ro5bert/avra.git lossis yog tias koj siv ubuntu lossis debian raws qhov system tsuas yog ntaus "sudo apt install avra" thiab koj yuav tau txais ob qho avr assembler thiab avrdude. HOWEVER, yog tias koj tau txais qhov hloov tshiab kawg siv github ces koj tseem yuav tau txais txhua yam tsim nyog suav nrog cov ntaub ntawv, hauv lwm lo lus nws twb muaj cov ntaub ntawv m328Pdef.inc thiab tn85def.inc.

5. avrdude

Ua tiav cov txheej txheem ntawm kuv cov AVR sib dhos cov lus qhia tuaj yeem pom ntawm no:

Kauj Ruam 1: Tsim Pawg Ntsuam Xyuas

Tsim Pawg Ntsuam Xyuas
Tsim Pawg Ntsuam Xyuas

Koj tuaj yeem yooj yim siv koj li arduino thiab ua txhua yam hauv cov lus qhia no yog tias koj nyiam. Txawm li cas los xij, txij li peb tab tom tham txog kev sau tus lej hauv cov lus sib dhos peb lub tswv yim yog txhawm rau tshem tawm txhua qhov periferals thiab cuam tshuam ncaj qha nrog microcontroller nws tus kheej. Yog li koj tsis xav tias nws yuav lom zem dua los ua li ntawd?

Rau cov ntawm koj uas pom zoo, koj tuaj yeem rub lub microcontroller tawm ntawm koj arduino thiab tom qab ntawd pib los ntawm kev tsim "Breadboard Arduino" los ntawm kev ua raws cov lus qhia ntawm no:

Hauv daim duab kuv qhia kuv teeb tsa uas suav nrog ob leeg nyob ib leeg Atmega328p's ntawm lub khob cij loj (Kuv xav kom tuaj yeem khaws cov ntawv qhia yav dhau los xaim thiab thauj khoom ntawm ib lub microcontroller thaum ua haujlwm tom ntej). Kuv muaj lub zog teeb tsa kom lub tsheb nqaj hlau saum toj kawg nkaus yog 9V thiab tag nrho lwm qhov yog 5V los ntawm tus tswj hluav taws xob. Kuv kuj siv FT232R pawg thawj coj los tawm tsam cov phiaj xwm. Kuv yuav lawv thiab muab bootloaders rau lawv kuv tus kheej, tab sis yog tias koj nyuam qhuav rub tawm ntawm Arduino ces nws zoo lawm.

Nco ntsoov tias yog koj tab tom sim qhov no nrog ATtiny85 ces koj tsuas tuaj yeem tau txais Sparkfun Tiny Programmer ntawm no: https://www.sparkfun.com/products/11801# thiab tom qab ntawd ntsaws nws rau hauv USB chaw nres nkoj ntawm koj lub computer. Koj yuav tsum tau teeb tsa bootloader ntawm Attiny85 ua ntej thiab txoj hauv kev yooj yim tshaj plaws tsuas yog siv Arduino IDE. Txawm li cas los xij, koj yuav tsum nyem rau ntawm cov ntaub ntawv, thiab nyiam, thiab tom qab ntawd ntxiv qhov Tshiab Boards URL: https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json uas yuav pab koj kom nruab lub bootloader (yog tias koj ATtiny85 tsis tau los nrog ib qho.)

Kauj Ruam 2: Txhim Kho Tus Kheej thiab Avrdude

Tam sim no koj tuaj yeem rub tawm thiab teeb tsa tus sib sau ua ke thiab avrdude los ntawm cov kab ntawv txuas hauv thawj kauj ruam ntawm qhov kev qhia no. Nws zoo li yog tias koj twb tau ua haujlwm nrog Arduino's lawm koj twb tau nruab avrdude.

Tom qab koj tau nruab avra koj yuav pom tias muaj cov kab ntawv subdirectory uas los nrog nws hu ua "cov peev txheej" thiab sab hauv cov npe ntawd yog cov pawg ntawm suav nrog cov ntaub ntawv. Cov no yog txhua yam ntawm microcontrollers uas koj tuaj yeem ua haujlwm nrog avra. Koj yuav pom tam sim ntawd tias tsis muaj ntaub ntawv rau 328p uas peb siv ntawm no. Kuv tau txuas ib qho. Cov ntaub ntawv yuav tsum raug hu ua m328Pdef.inc thiab koj yuav tsum muab tso rau hauv cov npe lossis lwm qhov chaw uas koj nyiam. Peb yuav suav nrog nws hauv peb cov phiaj xwm cov lus sib dhos. Txhua yam no ua yog muab txhua tus npe hauv microcontroller cov npe los ntawm cov ntaub ntawv kom peb tsis tas yuav siv lawv cov npe hexidecimal. Cov saum toj no suav nrog cov ntaub ntawv muaj "pragma directives" txij li nws tau tsim los rau C thiab C ++ programming. Yog tias koj nkees nkees ntawm pom cov neeg sib sau ua paug tawm "tsis quav ntsej pragma cov lus qhia" tsuas yog nkag mus rau hauv cov ntawv thiab rho tawm lossis tawm tswv yim tawm txhua kab pib nrog #pragma

Okay, tam sim no koj tau npaj koj lub microcontroller npaj, koj cov neeg sib sau ua ke, thiab koj tus programmer npaj txhij, peb tuaj yeem sau peb thawj qhov haujlwm.

Nco tseg: Yog tias koj siv ATtiny85 hloov ntawm ATmega328P ces koj xav tau qhov sib txawv suav nrog cov ntaub ntawv hu ua tn85def.inc. Kuv yuav muab nws txuas ntxiv (nco ntsoov kuv yuav tsum hu nws tn85def.inc.txt yog li Cov Ntawv Qhia yuav tso cai rau kuv upload nws.) XOV XWM, yog tias koj tau txais avra assembler los ntawm github ces koj twb muaj ob qho ntawm cov ntaub ntawv no nrog nws. Yog li kuv pom zoo kom tau nws thiab suav nws tus kheej: git clone

Kauj Ruam 3: Nyob Zoo Ntiaj Teb

Lub hom phiaj ntawm qhov kev qhia ua ntej no yog txhawm rau tsim tus qauv thawj qhov program uas ib tus neeg sau thaum kawm ib yam lus tshiab lossis tshawb nrhiav ib lub tshuab hluav taws xob tshiab. "Nyob zoo ntiaj teb!" Hauv peb qhov xwm txheej peb tsuas xav sau qhov program ua lus sib dhos, sib sau ua ke, thiab xa nws mus rau peb lub microcontroller. Qhov kev zov me nyuam yuav ua rau LED qhib. Ua rau LED "ntsais muag" zoo li lawv ua rau Arduino nyob zoo ib puag ncig lub ntiaj teb txoj haujlwm yog qhov ua haujlwm nyuaj dua hauv cov lus sib dhos thiab yog li peb yuav tsis ua qhov ntawd tseem tsis tau. Peb tab tom yuav sau qhov yooj yim tshaj plaws "cov pob txha liab qab" nrog qhov tsis tseem ceeb tshaj plaws.

Ua ntej txuas lub LED los ntawm PB5 (saib daim duab pinout) uas tseem hu ua Digital Out 13 ntawm arduino, mus rau 220 ohm resistor, tom qab ntawd mus rau GND. I.e.

PB5 - LED - R (220 ohm) - GND

Tam sim no los sau qhov program. Qhib koj cov ntawv nyeem uas koj nyiam tshaj plaws thiab tsim cov ntawv hu ua "hello.asm"

; nyob zoo.asm

; tig ntawm LED uas txuas nrog PB5 (digital tawm 13). suav nrog "./m328Pdef.inc" ldi r16, 0b00100000 tawm DDRB, r16 tawm PortB, r16 Pib: rjmp Pib

Saum toj no yog cov cai. Peb yuav hla nws kab-kab-kab hauv ib feeb, tab sis thawj zaug ua kom ntseeg tau tias peb tuaj yeem ua haujlwm nws ntawm koj lub cuab yeej.

Tom qab koj tau tsim cov ntaub ntawv, tom qab ntawd hauv lub davhlau ya nyob twg koj sib sau ua ke raws li hauv qab no:

avra nyob zoo.asm

qhov no yuav sib sau koj cov cai thiab tsim cov ntawv hu ua hello.hex uas peb tuaj yeem tso nws raws li hauv qab no:

avrdude -p m328p -c stk500v1 -b 57600 -P /dev /ttyUSB0 -U nyem: w: nyob zoo.hex

yog tias koj siv lub breadboard arduino koj yuav tau thawb lub khawm pib dua ntawm lub khob cij arduino ua ntej koj ua raws li cov lus txib saum toj no. Nco ntsoov tias koj kuj tseem yuav tsum tau ntxiv sudo rau pem hauv ntej lossis ua nws raws li hauv paus. Tsis tas li nco ntsoov tias ntawm qee qhov arduino's (zoo li Arduino UNO) koj yuav zaum yuav tsum hloov pauv tus nqi me ntsis rau -b 115200 thiab chaw nres nkoj -P /dev /ttyACM0 (yog tias koj tau txais qhov yuam kev los ntawm avrdude txog qhov tsis raug ntaus cim kos npe tsuas yog ntxiv ib qho) F rau qhov hais kom ua)

Yog tias txhua yam tau ua haujlwm zoo li nws yuav tsum tam sim no koj muaj lub teeb ci LED….. "Nyob Zoo Ntiaj Teb!"

Yog tias koj siv ATtiny85 ces avrdude hais kom ua yuav yog:

avrdude -p attiny85 -c usbtiny -U nyem: w: nyob zoo.hex

Kauj Ruam 4: Hello.asm Kab-los-kab

Txhawm rau ua kom tiav cov lus qhia no peb yuav dhau los ntawm hello.asm program kab-los-kab kom pom nws ua haujlwm li cas.

nyob zoo.asm

; tig ntawm LED uas txuas nrog PB5 (digital tawm 13)

Txhua yam tom qab lub semicolon tsis quav ntsej los ntawm cov sib sau ua ke thiab vim li no thawj ob kab no tsuas yog "cov lus" piav qhia qhov haujlwm ua haujlwm li cas.

.include "./m328Pdef.inc"

Kab ntawv no qhia cov neeg sib sau kom suav nrog cov ntaub ntawv m328Pdef.inc uas koj rub tawm. Tej zaum koj yuav xav muab qhov no tso rau hauv cov npe uas zoo ib yam xws li cov ntaub ntawv thiab tom qab ntawd hloov kab saum toj no los taw tes rau nws.

ldi r16, 0b00100000

ldi sawv rau "thauj tam sim" thiab qhia rau cov neeg sib sau ua haujlwm sau npe, r16 hauv qhov no, thiab thauj tus lej binary rau hauv nws, 0b00100000 hauv qhov no. 0b pem hauv ntej hais tias peb tus lej nyob hauv binary. Yog tias peb xav tau peb tuaj yeem xaiv lwm lub hauv paus, xws li hexidecimal. Hauv qhov xwm txheej ntawd peb tus lej yuav yog 0x20 uas yog hexidecimal rau 0b00100000. Lossis peb tuaj yeem siv 32 uas yog lub hauv paus 10 zauv rau tib tus lej.

Kev qoj ib ce 1: Sim hloov tus lej hauv kab lus saum toj no rau hexidecimal thiab tom qab ntawd los ua lej rau hauv koj tus lej thiab txheeb xyuas tias nws tseem ua haujlwm hauv txhua kis.

Siv binary yog qhov yooj yim tshaj plaws tab sis vim yog txoj kev Ports thiab Cov Ntawv Sau Npe ua haujlwm. Peb yuav tham txog cov chaw nres nkoj thiab sau npe ntawm atmega328p hauv kev nthuav dav ntxiv hauv kev qhia yav tom ntej tab sis tam sim no kuv tsuas yog hais tias peb siv r16 raws li peb "ua haujlwm sau npe" lub ntsiab lus uas peb tsuas yog siv nws ua qhov sib txawv uas peb khaws cia Tus lej "A" sau npe yog txheej ntawm 8 khoom. Lub ntsiab lus 8 qhov chaw uas tuaj yeem yog 0 lossis 1 (`` tawm '' lossis `` rau ''). Thaum peb thauj tus lej binary 0b00100000 rau hauv kev sau npe siv kab ntawv saum toj no peb tau khaws cia tus lej ntawd hauv tus lej r16.

tawm DDRB, r16

Kab ntawv no qhia tus neeg sau kom luam cov ntsiab lus ntawm kev sau npe r16 rau hauv DDRB sau npe. DDRB sawv rau "Cov Ntaub Ntawv Qhia Taw Qhia B" thiab nws teeb tsa "pins" ntawm PortB. Ntawm daim ntawv qhia pinout rau 328p koj tuaj yeem pom tias muaj 8 tus lej cim npe PB0, PB1, …, PB7. Cov pins no sawv cev rau "cov khoom" ntawm "PortB" thiab thaum peb thauj tus lej binary 00100000 rau hauv DDRB rau npe peb hais tias peb xav PB0, PB1, PB2, PB3, PB4, PB6, thiab PB7 teeb tsa INPUT pins txij li thaum lawv muaj 0's hauv lawv, thiab PB5 tau teeb tsa ua tus pin OUTPUT txij li thaum peb tso 1 hauv qhov chaw ntawd.

tawm PortB, r16

Tam sim no peb tau kho cov lus qhia ntawm tus pin peb tam sim no tuaj yeem teeb tsa qhov hluav taws xob ntawm lawv. Cov kab ntawv saum toj no theej tib tus lej binary los ntawm peb daim ntawv sau cia r16 rau PortB. Qhov no teeb tsa txhua tus pin rau 0 volts tshwj tsis yog tus pin PB5 rau HIGH uas yog 5 volts.

Kev tawm dag zog 2: Siv lub ntsuas ntsuas digital, ntsaws cov hlau lead dub rau hauv av (GND) thiab tom qab ntawd ntsuas txhua tus pin PB0 txog PB7 nrog cov hlau lead liab. Puas yog qhov hluav taws xob ntawm txhua tus pin raws nraim qhov sib haum rau muab 0b00100000 hauv PortB? Yog tias muaj ib qho uas tsis yog, vim li cas koj thiaj xav tias yog? (saib daim duab qhia chaw)

Pib:

rjmp Pib

Thaum kawg, thawj kab ntawv saum toj no yog "daim ntawv lo" uas sau ib qho chaw hauv cov cai. Hauv qhov xwm txheej no cim qhov chaw ntawd "Pib". Kab thib ob hais tias "txheeb ze dhia mus rau daim ntawv lo Pib." Cov txiaj ntsig tau yog tias lub khoos phis tawj tau muab tso rau hauv lub voj voos uas tsis muaj qhov kawg uas tsuas yog ua kom rov qab mus rau Start. Peb xav tau qhov no vim tias peb tsis tuaj yeem muaj qhov haujlwm tsuas yog xaus, lossis ntog tawm ntawm lub pob tsuas, txoj haujlwm yuav tsum cia li khiav kom lub teeb nyob twj ywm.

Kev ua 3: Tshem ob kab saum toj no los ntawm koj cov cai kom qhov kev zov me nyuam poob ntawm lub pob tsuas. Ua li cas? Koj yuav tsum pom qee yam uas zoo li ib txwm ua haujlwm "ntsais muag" siv los ntawm Arduino raws li lawv "nyob zoo ntiaj teb!". Vim li cas koj thiaj xav tias nws ua li no? (Xav txog dab tsi yuav tsum tshwm sim thaum txoj haujlwm poob saum lub pob tsuas …)

Kauj Ruam 5: Xaus

Yog tias koj tau txais qhov no deb ces ua kev zoo siab! Tam sim no koj tuaj yeem sau cov lej sib dhos, sib sau ua ke, thiab thauj nws mus rau koj lub microcontroller.

Hauv qhov kev qhia no koj tau kawm paub siv cov lus txib hauv qab no:

ldi hregister, tus lej thauj tus lej (0-255) rau hauv ib nrab sau npe (16-31)

tawm ioregister, sau npe tus lej los ntawm daim ntawv sau npe ua haujlwm rau I/O rau npe

rjmp daim ntawv dhia mus rau kab ntawm txoj haujlwm uas sau los ntawm "daim ntawv lo" (uas tsis tuaj yeem txuas ntxiv ntau dua 204 cov lus qhia tseg - piv txwv li kev txheeb ze dhia)

Tam sim no hais tias cov hauv paus no tawm ntawm txoj hauv kev, peb tuaj yeem txuas ntxiv sau cov lej ntxim nyiam thiab nthuav cov xov tooj cua thiab cov khoom siv yam tsis tas yuav tham txog cov cuab yeej ntawm kev suav sau thiab xa tawm.

Kuv vam tias koj tau nyiam qhov kev qhia pib no. Hauv qhov kev qhia tom ntej no peb yuav ntxiv lwm qhov kev tiv thaiv hluav taws xob (khawm) thiab nthuav peb cov cai kom suav nrog cov chaw nres nkoj nkag thiab kev txiav txim siab.

Pom zoo: