Cov txheej txheem:

AVR Assembler Tutorial 3: 9 Cov Kauj Ruam
AVR Assembler Tutorial 3: 9 Cov Kauj Ruam

Video: AVR Assembler Tutorial 3: 9 Cov Kauj Ruam

Video: AVR Assembler Tutorial 3: 9 Cov Kauj Ruam
Video: Lab E: Further Exploration in Mixing C and Assembler on the ATMEGA328 in MPLAB X (3 of 3) 2024, Hlis ntuj nqeg
Anonim
AVR Assembler Tutorial 3
AVR Assembler Tutorial 3

Txais tos rau kev qhia lej 3!

Ua ntej peb pib kuv xav ua lub ntsiab lus tseem ceeb. Tsis txhob ntshai sim nrog cov kab hluav taws xob thiab cov cai uas peb tab tom tsim hauv cov kev qhia no. Hloov cov xov nyob ib puag ncig, ntxiv cov khoom tshiab, tshem tawm cov khoom sib hloov, hloov kab ntawm cov cai, ntxiv kab tshiab, tshem tawm kab, thiab pom dab tsi tshwm sim! Nws nyuaj heev rau ua txhaum ib yam dab tsi thiab yog tias koj ua, leej twg mob siab rau? Tsis muaj ib yam uas peb siv, suav nrog lub microcontroller, yog kim heev thiab nws ib txwm muaj kev kawm los saib seb yam khoom tuaj yeem ua tsis tau li cas. Tsis tsuas yog koj yuav pom dab tsi uas tsis ua tom ntej tab sis, qhov tseem ceeb dua, koj yuav paub vim li cas ho tsis ua. Yog tias koj yog ib yam dab tsi zoo li kuv, thaum koj tseem yog menyuam yaus thiab koj tau txais cov khoom ua si tshiab nws tsis ntev ua ntej koj tau muab nws ua tej daim kom pom dab tsi ua rau nws raug? Qee lub sij hawm cov khoom ua si tau ua puas tas mus li tab sis tsis muaj teeb meem loj. Tso cai rau tus menyuam tshawb xyuas nws qhov kev xav paub txawm tias mus rau cov khoom ua si tawg yog dab tsi hloov nws mus rau tus kws tshawb fawb lossis tus kws tshaj lij siv lub tshuab ntxuav tais diav.

Hnub no peb tab tom yuav xaim hluav taws xob yooj yim heev thiab tom qab ntawd hnyav me ntsis rau hauv txoj kev xav. Thov txim txog qhov no, tab sis peb xav tau cov cuab yeej! Kuv cog lus tias peb yuav ua qhov no hauv kev qhia 4 qhov twg peb yuav ua qee qhov kev tsim hluav taws xob loj dua thiab qhov txiaj ntsig yuav zoo nkauj heev. Txawm li cas los xij, txoj hauv kev koj yuav tsum ua txhua yam ntawm cov kev qhia no yog qeeb heev, xav txog txoj hauv kev. Yog tias koj tsuas yog khawb av, tsim cov kab hluav taws xob, luam tawm thiab muab cov cai, thiab khiav nws tom qab ntawd, paub tseeb, nws yuav ua haujlwm, tab sis koj yuav tsis kawm ib yam. Koj yuav tsum xav txog txhua kab. Ncua. Kev sim. Tsim Yog tias koj ua li ntawd los ntawm qhov kawg ntawm qhov kev qhia 5 koj yuav tsis tsim cov khoom txias thiab tsis xav tau kev qhia ntxiv. Txwv tsis pub koj tsuas yog saib ntau dua li kev kawm thiab tsim.

Txawm li cas los xij, lub tswv yim txaus, cia peb pib!

Hauv qhov kev qhia no koj yuav xav tau:

  1. koj pawg thawj coj saib tsab ntawv
  2. ib LED
  3. txuas xov hlau
  4. resistor ib puag ncig 220 txog 330 ohms
  5. Phau Ntawv Teev Npe Qhia: www.atmel.com/images/atmel-0856-avr-instruction-se…
  6. Cov Ntaub Ntawv: www.atmel.com/images/Atmel-8271-8-bit-AVR-Microco…
  7. sib txawv siv lead ua oscillator (yeem)

Nov yog qhov txuas rau kev sau ua tiav ntawm kev qhia:

Kauj Ruam 1: Txhim Kho Lub Circuit Court

Kev Tsim Cov Circuit
Kev Tsim Cov Circuit

Lub Circuit Court hauv qhov kev qhia no yooj yim heev. Peb yeej tseem yuav mus sau "blink" program yog li txhua yam peb xav tau yog cov hauv qab no.

Nqa tus LED mus rau PD4, tom qab ntawd mus rau 330 ohm resistor, tom qab ntawd mus rau hauv av. i.e. yog

PD4 - LED - R (330) - GND

thiab qhov ntawd yog nws!

Txoj kev xav yuav nyuaj slogging txawm hais tias …

Kauj Ruam 2: Vim Li Cas Peb Xav Tau Cov Lus Qhia thiab M328Pdef.inc Cov Ntaub Ntawv?

Kuv xav tias peb yuav tsum pib los ntawm kev qhia vim li cas cov ntawv suav nrog thiab cov lus pom muaj txiaj ntsig. Tsis muaj leej twg yog qhov tsim nyog thiab koj tuaj yeem sau, sib sau ua ke, thiab xa cov cai ib yam yam tsis muaj lawv thiab nws yuav ua haujlwm zoo kawg nkaus (txawm hais tias tsis suav nrog cov ntawv koj tuaj yeem tau txais qee qhov kev tsis txaus siab los ntawm cov neeg sib dhos - tab sis tsis muaj qhov yuam kev)

Nov yog cov cai peb yuav sau hnub no, tshwj tsis yog tias kuv tau tshem cov lus pom thiab suav nrog cov ntawv:

.device ATmega328P

.org 0x0000 jmp a.org 0x0020 jmp ea: ldi r16, 0x05 tawm 0x25, r16 ldi r16, 0x01 sts 0x6e, r16 sei clr r16 tawm 0x26, r16 sbi 0x0a, 0x04 sbi 0x0b, 0x04 b: sbi 0x0b, 0x04 rcall c cbi 0x0b, 0x04 rcall c rjmp bc: clr r17 d: cpi r17, 0x1e brne d ret e: inc r17 cpi r17, 0x3d brne PC+2 clr r17 reti

zoo nkauj yooj yim txoj cai? Haha. Yog tias koj sib sau ua ke thiab xa cov ntaub ntawv no koj yuav ua rau LED ntsais ntawm tus nqi ntawm 1 ntsais ib pliag nrog ntsais muag ntev 1/2 thib ob thiab ncua ntawm qhov ntsais ntsais ntev 1/2 thib ob.

Txawm li cas los xij, saib cov cai no tsis yooj yim. Yog tias koj yuav tsum sau cov cai zoo li no koj thiab xav hloov kho nws lossis rov kho dua yav tom ntej koj yuav muaj lub sijhawm nyuaj.

Yog li cia peb muab cov lus pom thiab suav nrog cov ntaub ntawv rov qab kom peb tuaj yeem ua qee qhov kev nkag siab ntawm nws.

Kauj Ruam 3: Blink.asm

Nov yog cov cai peb yuav tham txog hnub no:

;************************************

; sau los ntawm: 1o_o7; hnub tim:; Qhov tseeb: 1.0; cov ntaub ntawv khaws tseg raws li: blink.asm; rau AVR: atmega328p; zaus zaus: 16MHz (yeem); ************************************ Txoj haujlwm ua haujlwm: ---------------------; suav tawm vib nas this los ntawm ntsais ib qho LED;; PD4 - LED - R (330 ohm) - GND;; --------------------------------------.nolist.include "./m328Pdef.inc".list; ==============; Kev tshaj tawm:.def temp = r16.def overflows = r17.org 0x0000; nco (PC) qhov chaw rov pib dua tus tswj rjmp Pib dua; jmp nqi 2 cpu mus thiab rjmp nqi tsuas yog 1; yog li tshwj tsis yog koj xav tau dhia ntau dua 8k bytes; koj tsuas xav tau rjmp. Qee qhov microcontrollers yog li ntawd xwb; muaj rjmp thiab tsis jmp.org 0x0020; nco qhov chaw ntawm Timer0 tus tuav haujlwm rjmp overflow_handler; mus ntawm no yog lub timer0 txau cuam tshuam tshwm sim; ============ Pib dua: ldi temp, 0b00000101 tawm TCCR0B, temp; teeb tsa Lub Sijhawm Xaiv Cov Khoom CS00, CS01, CS02 txog 101; qhov no tso Timer Counter0, TCNT0 rau hauv FCPU/1024 hom; yog li nws zuam ntawm CPU zaus/1024 ldi temp, 0b00000001 sts TIMSK0, temp; teeb tsa Timer Overflow Interrupt Enable (TOIE0) me ntsis; ntawm Timer Interrupt Mask Register (TIMSK0) sei; pab kom muaj kev cuam tshuam thoob ntiaj teb - sib npaug rau "sbi SREG, Kuv" clr temp tawm TCNT0, temp; pib ua Timer/Counter rau 0 sbi DDRD, 4; teeb PD4 rau qhov tso tawm; ======================; Lub cev tseem ceeb ntawm txoj haujlwm: ntsais: sbi PORTD, 4; tig LED ntawm PD4 rcall ncua; ncua yuav yog 1/2 thib ob cbi PORTD, 4; tua LED ntawm PD4 rcall ncua; ncua yuav yog 1/2 thib ob rjmp ntsais muag; ntxeev rov qab rau qhov pib ncua: clr hla; teeb hla mus rau 0 sec_count: cpi hla, 30; sib piv cov naj npawb ntawm cov dej ntws thiab 30 brne sec_count; ceg rov qab mus rau sec_count yog tias tsis sib npaug ret; yog tias 30 dhau los tau tshwm sim rov qab mus rau qhov ntsais overhand_handler: inc dhau los; ntxiv 1 rau qhov sib txawv ntawm qhov sib txawv ntawm cpi hla, 61; sib piv nrog 61 brne PC+2; Program Counter + 2 (hla kab tom ntej) yog tias tsis sib npaug clr ntws tawm; yog tias 61 hla dhau tshwm sim rov kho lub txee rau xoom reti; rov qab los ntawm kev cuam tshuam

Raws li koj tuaj yeem pom, kuv cov lus luv luv me ntsis tam sim no. Thaum peb paub dab tsi cov lus txib hauv cov lus qhia teeb tsa peb tsis tas yuav piav qhia qhov ntawd hauv cov lus. Peb tsuas yog yuav tsum piav qhia dab tsi tshwm sim los ntawm qhov pom ntawm qhov haujlwm.

Peb yuav sib tham txog txhua yam ntawm qhov no ua ib qho, tab sis ua ntej peb sim ua kom tau txais kev pom thoob ntiaj teb. Lub cev tseem ceeb ntawm txoj haujlwm yog ua raws li hauv qab no.

Ua ntej peb teeb tsa 4 ntawm PORTD nrog "sbi PORTD, 4" qhov no xa 1 mus rau PD4 uas tso qhov hluav taws xob rau 5V ntawm tus pin ntawd. Qhov no yuav qhib rau LED. Peb mam li dhia mus rau "ncua" subroutine uas suav tawm 1/2 feem thib ob (peb yuav piav qhia nws yuav ua li cas tom qab). Peb mam li rov mus ntsais muag thiab pom me ntsis 4 ntawm PORTD uas teev PD4 rau 0V thiab yog li kaw lub LED. Peb mam li ncua ntxiv rau lwm 1/2 thib ob, thiab tom qab ntawd rov qab mus rau qhov pib ntsais muag dua nrog "rjmp blink".

Koj yuav tsum khiav cov cai no thiab pom tias nws ua li nws yuav tsum tau ua.

Thiab muaj koj muaj nws! Ntawd yog txhua txoj cai no ua rau lub cev. Lub tshuab khoos phis tawj sab hauv ntawm yam microcontroller tab tom ua yog qhov cuam tshuam me ntsis thiab yog vim li cas peb thiaj ua qhov kev qhia no. Yog li cia tham txog txhua ntu nyob rau hauv lem.

Kauj Ruam 4:.org Assembler Directives

Peb twb paub lawm.nolist,.list,.include, thiab.def assembler directives ua los ntawm peb cov kev qhia yav dhau los, yog li ntawd ua ntej peb saib ntawm 4 kab ntawm cov cai uas tuaj tom qab ntawd:

.org 0x0000

jmp Rov pib dua.org 0x0020 jmp overflow_handler

Cov lus.org qhia cov neeg sib sau nyob qhov twg hauv "Program Memory" txhawm rau sau nqe lus tom ntej. Raws li koj qhov program ua tiav, "Program Counter" (tshem tawm raws li PC) muaj qhov chaw nyob ntawm kab tam sim no uas tau ua tiav. Yog li qhov xwm txheej no thaum lub PC nyob ntawm 0x0000 nws yuav pom cov lus txib "jmp Reset" nyob hauv qhov chaw nco ntawd. Yog vim li cas peb xav muab jmp Pib dua hauv qhov chaw ntawd vim tias thaum pib qhov haujlwm, lossis lub nti rov pib dua, PC pib ua txoj cai ntawm qhov chaw no. Yog li, raws li peb pom tau, peb nyuam qhuav hais rau nws tam sim "dhia" mus rau ntu uas sau tias "Pib dua". Vim li cas peb thiaj ua li ntawd? Qhov ntawd txhais tau tias ob kab kawg saum toj no tsuas yog hla dhau xwb! Vim li cas?

Zoo uas yog qhov uas tau txais kev txaus siab. Tam sim no koj yuav tsum tau qhib tus saib pdf nrog daim ntawv qhia tag nrho ATmega328p uas kuv tau taw qhia rau ntawm nplooj ntawv thawj ntawm qhov kev qhia no (yog vim li cas nws thiaj li yog nqe 4 hauv ntu "koj yuav xav tau" ntu). Yog tias koj lub vijtsam me me, lossis koj muaj txoj hauv kev ntau lub qhov rais qhib lawm (zoo li yog qhov xwm txheej nrog kuv) koj tuaj yeem ua qhov kuv ua thiab muab tso rau ntawm Ereader, lossis koj lub xov tooj Android. Koj yuav siv nws txhua lub sijhawm yog tias koj npaj yuav sau cov lej sib dhos. Qhov txias yog tias txhua lub microcontollers tau teeb tsa hauv txoj hauv kev zoo sib xws thiab yog li thaum koj tau siv los nyeem cov ntawv teev npe thiab coding los ntawm lawv koj yuav pom nws yuav luag tsis tseem ceeb los ua ib yam rau qhov sib txawv microcontroller. Yog li peb tau kawm paub yuav siv txhua lub microcontrollers li cas thiab tsis yog atmega328p xwb.

Okay, tig mus rau nplooj ntawv 18 hauv cov ntawv teev tseg thiab saib daim duab 8-2.

Nov yog qhov Program Memory hauv microcontroller tau teeb tsa li cas. Koj tuaj yeem pom tias nws pib nrog chaw nyob 0x0000 thiab tau muab cais ua ob ntu; ib qho kev thov nyem ntu thiab ntu nyem nyem. Yog tias koj hais luv luv rau nplooj 277 lub rooj 27-14 koj yuav pom tias daim ntawv thov nyem ntu yuav siv qhov chaw los ntawm 0x0000 txog 0x37FF thiab ntu nyem nyem yuav siv qhov chaw seem los ntawm 0x3800 txog 0x3FFF.

Qoj 1: Muaj pes tsawg qhov chaw nyob hauv Qhov Kev Pab Cuam nco? I.e. hloov 3FFF rau zauv thiab ntxiv 1 txij li peb pib suav ntawm 0. Txij li txhua qhov chaw nco yog 16 khoom (lossis 2 bytes) dav dab tsi yog tag nrho cov lej ntawm bytes nco? Tam sim no hloov qhov no mus rau kilobytes, nco ntsoov tias muaj 2^10 = 1024 bytes hauv ib kilobyte. Cov khau raj nyem ntu mus los ntawm 0x3800 txog 0x37FF, pes tsawg kilobytes yog qhov no? Muaj pes tsawg kilobytes ntawm kev nco cia rau peb siv los khaws peb cov phiaj xwm? Hauv lwm lo lus, peb qhov haujlwm loj npaum li cas? Thaum kawg, peb tuaj yeem muaj pes tsawg kab ntawm txoj cai?

Yog lawm, tam sim no uas peb paub txhua yam txog lub koom haum ntawm flash program nco, cia peb txuas ntxiv nrog peb kev sib tham ntawm.org nqe lus. Peb pom tias thawj qhov chaw nco 0x0000 muaj peb cov lus qhia kom dhia mus rau peb ntu peb sau npe rov pib dua. Tam sim no peb pom dab tsi ".org 0x0020" nqe lus ua. Nws hais tias peb xav tau cov lus qhia ntawm kab tom ntej no los tso rau ntawm qhov chaw nco 0x0020. Cov lus qhia peb tau tso tseg muaj qhov dhia mus rau ntu hauv peb cov cai uas peb tau sau tias "overflow_handler" … tam sim no vim li cas heck yuav peb xav kom qhov dhia no tso rau ntawm qhov chaw nco 0x0020? Txhawm rau kom paub, peb tig mus rau nplooj 65 hauv cov ntawv teev tseg thiab saib ntawm Rooj 12-6.

Table 12-6 yog lub rooj ntawm "Reset and Interrupt Vectors" thiab nws qhia tau tseeb tias lub PC yuav mus qhov twg thaum nws tau txais "cuam tshuam". Piv txwv li, yog tias koj saib Vector tus lej 1. Tus "qhov chaw" ntawm qhov cuam tshuam yog "RESET" uas tau hais tseg tias "Tus Pin Sab Nraud, Lub Hwj Huam Pib-Pib dua, Xim av tawm dua, thiab Watchdog system rov pib dua" lub ntsiab lus, yog tias muaj cov khoom ntawd tshwm sim rau peb lub microcontroller, lub PC yuav pib ua haujlwm peb qhov program ntawm qhov chaw nco qhov chaw 0x0000. Yuav ua li cas txog peb.org cov lus qhia? Zoo, peb tso cov lus txib ntawm qhov chaw nco 0x0020 thiab yog tias koj saib hauv qab lub rooj koj yuav pom tias yog Timer/Counter0 dhau los tshwm sim (los ntawm TIMER0 OVF) nws yuav ua txhua yam nyob ntawm qhov chaw 0x0020. Yog li thaum twg los xij, lub PC yuav dhia mus rau qhov chaw peb sau tias "overflow_handler". Txias txoj cai? Koj yuav pom hauv ib pliag vim li cas peb thiaj ua qhov no, tab sis ua ntej cia peb ua tiav cov theem no ntawm kev qhia nrog ib sab.

Yog tias peb xav ua kom peb cov cai zoo thiab huv si peb yuav tsum hloov pauv 4 kab uas peb tab tom tham tam sim no nrog cov hauv qab no (saib nplooj 66):

.org 0x0000

rjmp Pib dua; PC = 0x0000 cov reti; PC = 0x0002 reti; PC = 0x0004 reti; PC = 0x0006 reti; PC = 0x0008 reti; PC = 0x000A… reti; PC = 0x001E jmp overflow_handler: PC = 0x0020 reti: PC = 0x0022… reti; PC = 0x0030 reti; PAB = 0x0032

Yog li ntawd yog tias muab qhov cuam tshuam tshwm sim nws tsuas yog "reti" uas txhais tau tias "rov qab los ntawm kev cuam tshuam" thiab tsis muaj dab tsi tshwm sim ntxiv. Tab sis yog tias peb tsis "Pab" cov kev cuam tshuam ntau yam no, tom qab ntawd lawv yuav tsis siv thiab peb tuaj yeem tso cov program program rau hauv cov chaw no. Hauv peb txoj haujlwm tam sim no "blink.asm" peb tsuas yog ua kom lub sijhawm timer0 dhau los cuam tshuam (thiab tau kawg rov pib cuam tshuam uas ib txwm ua haujlwm) thiab yog li peb yuav tsis thab nrog lwm tus.

Peb yuav ua li cas "qhib" lub timer0 txau cuam tshuam? … Uas yog lub ntsiab lus ntawm peb cov kauj ruam tom ntej hauv qhov kev qhia no.

Kauj ruam 5: Timer/Counter 0

Timer/Txee 0
Timer/Txee 0

Ua tibzoo saib daim duab saum toj no. Nov yog qhov kev txiav txim siab txheej txheem ntawm "PC" thaum qee qhov kev cuam tshuam sab nraud "cuam tshuam" qhov ntws ntawm peb cov haujlwm. Thawj qhov nws ua thaum nws tau txais lub teeb liab los ntawm sab nraum qhov cuam tshuam tau tshwm sim yog nws kuaj xyuas seb peb puas tau teeb tsa "cuam tshuam pab" me ntsis rau hom kev cuam tshuam ntawd. Yog tias peb tsis muaj, nws tsuas yog txuas ntxiv ua rau peb txoj kab tom ntej ntawm txoj cai. Yog tias peb tau teeb tsa qhov kev cuam tshuam tshwj xeeb pab me ntsis (yog li muaj 1 hauv qhov chaw me me uas tsis yog 0) nws yuav tshuaj xyuas seb puas yog peb puas tau qhib "thoob ntiaj teb cuam tshuam", yog tsis yog nws yuav rov mus rau kab tom ntej ntawm cov cai thiab txuas ntxiv mus. Yog tias peb tau qhib kev cuam tshuam thoob ntiaj teb ib yam nkaus, tom qab ntawd nws yuav mus rau Qhov Kev Pabcuam Nco qhov chaw ntawm hom kev cuam tshuam ntawd (raws li qhia hauv Table 12-6) thiab ua raws li cov lus txib uas peb tau tso rau ntawd. Yog li cia saib seb peb tau ua tiav txhua qhov no hauv peb cov cai.

Kev rov pib sau cov npe ntawm peb cov cai pib nrog ob kab hauv qab no:

Pib dua:

ldi temp, 0b00000101 tawm TCCR0B, ntsuas

Raws li peb twb paub lawm, qhov no thauj mus rau qhov kub (piv txwv li R16) tus lej tam sim ntawd tom qab, uas yog 0b00000101. Tom qab ntawd nws sau tus lej no tawm mus rau npe hu ua TCCR0B siv qhov "tawm" hais kom ua. Daim ntawv sau npe no yog dab tsi? Zoo, cia peb mus rau nplooj ntawv 614 ntawm cov ntawv teev tseg. Qhov no yog nyob nruab nrab ntawm lub rooj suav nrog txhua qhov kev sau npe. Ntawm qhov chaw nyob 0x25 koj yuav pom TCCR0B. (Tam sim no koj paub qhov twg kab "tawm 0x25, r16" los ntawm hauv kuv qhov tsis tau hais tawm ntawm cov cai). Peb pom los ntawm cov cai ntu saum toj no uas peb tau teeb tsa 0th me ntsis thiab ntu 2 thiab tshem tag nrho cov seem. Los ntawm kev saib ntawm lub rooj koj tuaj yeem pom tias qhov no txhais tau tias peb tau teeb tsa CS00 thiab CS02. Tam sim no cia taub hau mus rau tshooj hauv daim ntawv hu ua "8-ntsis Timer/Counter0 with PWM". Tshwj xeeb, mus rau nplooj ntawv 107 ntawm tshooj ntawd. Koj yuav pom tib qho kev piav qhia ntawm "Timer/Counter Control Register B" (TCCR0B) sau npe uas peb nyuam qhuav pom hauv cov npe sau cov ntsiab lus (yog li peb tuaj yeem tuaj ncaj ntawm no, tab sis kuv xav kom koj pom yuav siv cov lus ntxaws li cas rau yav tom ntej siv). Cov ntawv txuas ntxiv muab cov lus piav qhia ntawm txhua qhov khoom hauv qhov kev tso npe thiab lawv ua dab tsi. Peb yuav hla txhua qhov rau tam sim no thiab tig nplooj ntawv rau Rooj 15-9. Cov lus no qhia txog "Clock Select Bit Description". Tam sim no saib hauv qab lub rooj ntawd kom txog thaum koj pom kab uas sib haum rau cov khoom uas peb nyuam qhuav teev hauv cov npe ntawd. Kab hais tias "clk/1024 (los ntawm prescaler)". Qhov no txhais tau li cas yog tias peb xav Timer/Counter0 (TCNT0) zuam raws tus lej uas yog CPU zaus faib los ntawm 1024. Txij li thaum peb muaj peb lub microcontroller pub los ntawm 16MHz siv lead ua oscillator nws txhais tau tias tus nqi uas peb CPU ua raws cov lus qhia yog 16 lab cov lus qhia ib pliag. Yog li tus nqi uas peb tus lej TCNT0 yuav zuam yog li 16 lab/1024 = 15625 zaug ib pliag (sim nws nrog lub moos sib txawv xaiv cov khoom thiab pom dab tsi tshwm sim - nco peb lub tswv yim?). Cia peb khaws tus lej 15625 nyob tom qab ntawm peb lub siab rau tom qab thiab txav mus rau ob kab tom ntej ntawm cov cai:

ldi kub, 0b00000001

sts TIMSK0, kub

Qhov no teeb tsa 0th ntawm kev sau npe hu ua TIMSK0 thiab tshem tawm tag nrho cov seem. Yog koj saib nplooj 109 hauv cov ntawv koj yuav pom tias TIMSK0 sawv rau "Timer/Counter Interrupt Mask Register 0" thiab peb cov cai tau teeb tsa 0th uas yog lub npe TOIE0 uas sawv rau "Timer/Counter0 Overflow Interrupt Enable" … Muaj! Tam sim no koj pom dab tsi no yog txhua yam hais txog. Tam sim no peb muaj "cuam tshuam pab me ntsis teeb tsa" raws li peb xav tau los ntawm thawj qhov kev txiav txim siab hauv peb daim duab saum toj kawg nkaus. Yog li tam sim no txhua yam peb yuav tsum ua yog pab kom "cuam tshuam thoob ntiaj teb" thiab peb txoj haujlwm yuav tuaj yeem teb rau hom kev cuam tshuam no. Peb yuav ua kom muaj kev cuam tshuam thoob ntiaj teb sai, tab sis ua ntej peb ua qhov ntawd koj yuav tsis meej pem los ntawm qee yam.. vim li cas heck kuv puas tau siv lo lus txib "sts" theej rau hauv TIMSK0 sau npe es tsis siv li ib txwm "tawm"?

Thaum twg koj pom kuv siv cov lus qhia uas koj tsis tau pom ua ntej thawj qhov koj yuav tsum ua yog tig mus rau nplooj ntawv 616 hauv cov ntawv teev npe. Nov yog "Cov Txheej Txheem Qhia Txog Txheej Txheem". Tam sim no nrhiav cov lus qhia "STS" uas yog qhov kuv tau siv. Nws hais tias nws siv tus lej los ntawm tus lej R (peb siv R16) thiab "Khaws ncaj qha rau SRAM" qhov chaw k (hauv peb cov ntaub ntawv muab los ntawm TIMSK0). Yog li vim li cas peb thiaj li yuav tsum tau siv "sts" uas siv 2 teev mus (saib kab ntawv kawg hauv kab lus) khaws hauv TIMSK0 thiab peb tsuas xav tau "tawm", uas siv sijhawm ib teev xwb, khaws hauv TCCR0B ua ntej? Txhawm rau teb lo lus nug no peb yuav tsum rov qab mus rau peb cov ntsiab lus sau npe ntawm nplooj ntawv 614. Koj pom tias TCCR0B rau npe yog nyob ntawm qhov chaw 0x25 tab sis kuj ntawm (0x45) txoj cai? Qhov no txhais tau tias nws yog tus sau npe hauv SRAM, tab sis nws kuj yog qee yam kev sau npe hu ua "chaw nres nkoj" (lossis i/o sau npe). Yog tias koj saib cov lus qhia cov ntsiab lus ntawm ib sab "tawm" cov lus txib koj yuav pom tias nws siv qhov tseem ceeb los ntawm "kev sau npe ua haujlwm" zoo li R16 thiab xa lawv mus rau PORT. Yog li peb tuaj yeem siv "tawm" thaum sau ntawv mus rau TCCR0B thiab txuag peb tus kheej lub moos voj voog. Tab sis tam sim no saib TIMSK0 hauv cov npe sau npe. Koj pom tias nws muaj chaw nyob 0x6e. Qhov no yog sab nraum thaj tsam ntawm cov chaw nres nkoj (uas tsuas yog thawj 0x3F qhov chaw ntawm SRAM) thiab yog li koj yuav tsum rov qab los siv sts hais kom ua thiab siv ob CPU moos mus los ua nws. Thov nyeem Nco Ntsoov 4 qhov kawg ntawm cov lus qhia ntxaws ntawm nplooj ntawv 615 tam sim no. Tseem pom tias txhua yam ntawm peb cov tswv yim tawm tswv yim tawm, zoo li PORTD nyob hauv qab ntawm lub rooj. Piv txwv li, PD4 yog me ntsis 4 ntawm chaw nyob 0x0b (tam sim no koj pom qhov twg tag nrho 0x0b khoom tuaj los ntawm kuv qhov tsis tau hais tawm cov cai!).. zoo, cov lus nug sai: koj puas tau hloov "sts" rau "tawm" thiab pom dab tsi tshwm sim? Nco ntsoov peb lub tswv yim! tawg nws! tsis txhob coj kuv lo lus rau yam.

Okay, ua ntej peb txav mus, tig mus rau nplooj ntawv 19 hauv cov ntawv teev tseg ib pliag. Koj pom daim duab ntawm cov ntaub ntawv nco (SRAM). Thawj 32 qhov sau npe hauv SRAM (los ntawm 0x0000 txog 0x001F) yog "lub hom phiaj kev ua haujlwm tso npe" R0 txog R31 uas peb siv txhua lub sijhawm ua qhov hloov pauv hauv peb cov cai. Cov npe 64 tom ntej yog I/O cov chaw nres nkoj mus txog 0x005f (piv txwv li cov uas peb tau tham txog uas muaj cov chaw nyob tsis nyob ib sab ntawm lawv hauv cov npe sau npe uas peb tuaj yeem siv "tawm" hais kom ua "sts") Thaum kawg ntu txuas ntxiv ntawm SRAM muaj tag nrho lwm cov npe hauv cov ntsiab lus rooj mus rau qhov chaw 0x00FF, thiab kawg qhov seem yog sab hauv SRAM. Tam sim no nrawm, cia peb tig mus rau nplooj ntawv 12 ib pliag. Muaj koj pom lub rooj ntawm "kev ua haujlwm dav dav rau npe" uas peb ib txwm siv los ua peb cov kev hloov pauv. Koj pom kab tuab ntawm cov lej R0 txog R15 thiab tom qab ntawd R16 txog R31? Kab ntawv ntawd yog vim li cas peb ib txwm siv R16 ua qhov tsawg tshaj plaws thiab kuv yuav nkag mus rau hauv nws me ntsis ntxiv hauv qhov kev qhia tom ntej uas peb tseem yuav xav tau peb 16-ntsis qhov chaw nyob tsis raug npe, X, Y, thiab Z. Kuv yuav tsis nkag mus rau qhov uas tseem tsis tau tab sis txij li thaum peb tsis xav tau tam sim no thiab peb tau bogged cia txaus ntawm no.

Tig rov qab ib nplooj ntawv mus rau nplooj 11 ntawm cov ntawv teev npe. Koj yuav pom daim duab ntawm SREG sau npe rau sab xis saum toj? Koj pom qhov me ntsis 7 ntawm cov npe ntawd hu ua "Kuv". Tam sim no mus rau nplooj ntawv thiab nyeem cov lus piav qhia ntawm Bit 7…. yaj! Nws yog Lub Ntiaj Teb cuam tshuam Pab Tau me ntsis. Qhov ntawd yog yam peb yuav tsum tau teeb tsa txhawm rau dhau los ntawm qhov kev txiav txim siab zaum thib ob hauv peb daim duab saum toj no thiab tso cai rau lub sijhawm/txee hla dhau cuam tshuam hauv peb txoj haujlwm. Yog li kab kab tom ntej ntawm peb txoj haujlwm yuav tsum nyeem:

SIB SREG, i

uas teev me ntsis hu ua "Kuv" hauv SREG sau npe. Txawm li cas los xij, ntau dua qhov no peb tau siv cov lus qhia

sei

es tsis txhob Qhov no tau teeb tsa ntau zaus hauv cov program uas lawv nyuam qhuav ua txoj hauv kev yooj yim los ua nws.

Okay! Tam sim no peb tau txais cov cuam tshuam cuam tshuam npaj mus, yog li peb "jmp overflow_handler" yuav ua tiav thaum twg ib qho tshwm sim.

Ua ntej peb txav mus los, ua tibzoo saib ntawm SREG rau npe (Cov Ntawv Sau Npe) vim tias nws yog ib qho tseem ceeb heev. Nyeem dab tsi ntawm txhua tus chij sawv cev. Tshwj xeeb, ntau cov lus qhia uas peb siv yuav teeb tsa thiab txheeb xyuas cov chij no txhua lub sijhawm. Piv txwv li, tom qab ntawd peb yuav siv qhov hais kom ua "CPI" uas txhais tau tias "sib piv tam sim". Ua tibzoo saib cov lus qhia ntxaws rau cov lus qhia no thiab pom tias muaj pes tsawg tus chij nws teeb tsa hauv kab "chij". Cov no yog txhua tus chij hauv SREG thiab peb cov cai yuav teeb tsa lawv thiab tshuaj xyuas lawv tas li. Koj yuav pom cov piv txwv sai. Thaum kawg qhov kawg ntawm ntu ntu ntawm cov cai no yog:

clr pwm

tawm TCNT0, temp sbi DDRD, 4

Kab kawg ntawm no yog qhov pom tseeb. Nws tsuas yog teeb tsa ntu 4 ntawm Kev Qhia Cov Ntaub Ntawv rau PortD ua rau PD4 yog OUTPUT.

Thawj qhov teeb tsa qhov sib txawv ntawm qhov sov mus rau xoom thiab tom qab ntawv theej tawm mus rau TCNT0 sau npe. TCNT0 yog peb Timer/Counter0. Qhov no teev nws rau xoom. Sai li lub PC ua txoj kab no lub sijhawm 0 yuav pib ntawm xoom thiab suav ntawm tus nqi ntawm 15625 zaug txhua ob. Qhov teeb meem yog qhov no: TCNT0 yog "8-ntsis" sau npe txoj cai? Yog li tus lej loj tshaj plaws uas tus lej 8-ntsis tuaj yeem tuav tau yog dab tsi? Zoo 0b11111111 yog nws. Nov yog tus lej 0xFF. Uas yog 255. Yog li koj pom dab tsi tshwm sim? Lub sijhawm ntsuas tau zipping nrog nce 15625 zaug ib pliag thiab txhua lub sijhawm nws nce mus txog 255 nws "dhau mus" thiab rov qab mus rau 0 dua. Nyob rau tib lub sijhawm nws rov qab mus rau xoom nws xa tawm Timer Overflow Interrupt teeb liab. Lub PC tau txais qhov no thiab koj paub tias nws ua dab tsi tam sim no? Yog Nws mus rau qhov program Memory qhov chaw 0x0020 thiab ua raws cov lus qhia nws pom nyob ntawd.

Zoo kawg! Yog tias koj tseem nyob nrog kuv ces koj yog ib tus neeg tsis paub qab hau superhero! Cia peb mus ntxiv …

Kauj Ruam 6: Tus Kws Tuav Pov Hwm

Yog li cia peb xav tias timer/counter0 sau npe nyuam qhuav dhau los. Peb tam sim no paub tias qhov program tau txais lub teeb liab cuam tshuam thiab ua haujlwm 0x0020 uas qhia qhov Program Counter, PC kom dhia mus rau daim ntawv lo "overflow_handler" cov hauv qab no yog cov cai peb sau tom qab daim ntawv lo:

txug_handler:

inc hla dhau cpi hla, 61 brne PC+2 clr hla dhau reti

Thawj qhov nws ua yog nce qhov sib txawv "hla" (uas yog peb lub npe rau lub hom phiaj ua haujlwm sau npe R17) tom qab ntawd nws "sib piv" cov ntsiab lus ntawm kev hla nrog tus lej 61. Txoj kev uas qhia cpi ua haujlwm yog tias nws yooj yim rho tawm ob tus lej thiab yog qhov txiaj ntsig yog xoom nws teeb tsa tus chij Z hauv SREG sau npe (Kuv tau hais rau koj tias peb yuav pom qhov sau npe no txhua lub sijhawm). Yog tias ob tus lej sib npaug ces tus chij Z yuav yog 1, yog tias ob tus lej tsis sib npaug ces nws yuav yog 0.

Kab tom ntej hais tias "brne PC+2" uas txhais tau tias "ceg yog tsis sib npaug". Qhov tseem ceeb, nws txheeb xyuas tus chij Z hauv SREG thiab yog tias nws TSIS YOG ib qho (piv txwv li ob tus lej tsis sib npaug, yog tias lawv sib npaug, tus chij xoom yuav raug teeb tsa) PC ceg rau PC+2, lub ntsiab lus nws hla mus ntxiv kab thiab mus ncaj nraim rau "reti" uas rov los ntawm qhov cuam tshuam mus rau qhov chaw twg nws nyob hauv cov cai thaum qhov cuam tshuam tuaj txog. Yog tias cov lus qhia brne pom 1 nyob rau hauv xoom tus chij me ntsis nws yuav tsis ceg thiab hloov nws tsuas yog txuas ntxiv mus rau kab tom ntej uas yuav clr hla dhau rov pib dua nws mus 0.

Qhov txiaj ntsig net ntawm txhua qhov no yog dab tsi?

Zoo peb pom tias txhua lub sijhawm muaj lub sijhawm ntsuas hla tus neeg ua haujlwm no nce tus nqi "hla" los ntawm ib qho. Yog li qhov sib txawv "hla" yog suav tus naj npawb ntawm cov dej ntws raws li lawv tshwm sim. Thaum twg tus lej nce mus txog 61 peb rov pib dua nws rau xoom.

Tam sim no vim li cas hauv ntiaj teb peb yuav ua li ntawd?

Cia saib. Nco qab tias peb lub moos nrawm rau peb CPU yog 16MHz thiab peb "prescaled" nws siv TCCR0B kom lub sijhawm tsuas suav nrog ntawm tus lej ntawm 15625 suav rau ob txoj cai? Thiab txhua lub sijhawm timer nce mus txog qhov suav ntawm 255 nws dhau los. Yog li ntawd txhais tau tias nws hla 15625/256 = 61.04 zaug ib pliag. Peb tab tom taug qab tus naj npawb ntawm kev hla nrog peb qhov sib txawv "overflows" thiab peb tab tom sib piv tus lej ntawd nrog 61. Yog li peb pom tias "hla" yuav sib npaug 61 ib zaug txhua ob! Yog li peb tus neeg lis haujlwm yuav rov pib "hla" mus rau xoom ib zaug txhua ob. Yog li yog tias peb tsuas yog saib xyuas qhov sib txawv "hla" thiab sau tseg txhua lub sijhawm nws rov pib dua rau xoom peb yuav suav qhov thib ob-thib-ob hauv lub sijhawm tiag tiag (Nco ntsoov tias hauv qhov kev qhia tom ntej peb yuav qhia yuav ua li cas kom tau qhov tseeb dua qeeb hauv milliseconds tib txoj kev uas Arduino "ncua" kev ua haujlwm niaj hnub).

Tam sim no peb tau "daws" lub sijhawm ua haujlwm cuam tshuam. Xyuas kom koj nkag siab tias qhov no ua haujlwm li cas thiab tom qab ntawd mus rau kauj ruam tom ntej uas peb siv qhov tseeb no.

Kauj Ruam 7: Ncua Sijhawm

Tam sim no peb tau pom tias peb lub sijhawm teev sijhawm cuam tshuam cuam tshuam tus tuav "overflow_handler" niaj hnub yuav teeb tsa qhov sib txawv "hla" mus rau xoom ib zaug txhua ob peb tuaj yeem siv qhov tseeb no los tsim "ncua sijhawm" subroutine.

Ua tibzoo saib cov cai hauv qab no los ntawm hauv qab peb ncua: daim ntawv lo

ncua:

clr hla dhau sec_count: cpi hla, 30 brne sec_count ret

Peb yuav hu rau subroutine txhua lub sijhawm peb xav tau ncua hauv peb txoj haujlwm. Txoj hauv kev nws ua haujlwm yog nws xub teeb tsa qhov sib txawv "hla" mus rau xoom. Tom qab ntawd nws nkag mus rau thaj tsam uas sau tias "sec_count" thiab sib piv cov dej ntws nrog 30, yog tias lawv tsis sib npaug nws ceg rov qab mus rau daim ntawv lo sec_count thiab sib piv dua, thiab ntxiv, thiab lwm yam txog thaum lawv sib npaug thaum kawg (nco ntsoov tias tag nrho lub sijhawm no mus ntawm peb lub sijhawm cuam tshuam cuam tshuam tus neeg tuav haujlwm txuas ntxiv ua kom muaj qhov sib txawv dhau mus thiab yog li nws tau hloov pauv txhua lub sijhawm peb mus nyob ib puag ncig ntawm no. Thaum cov dej ntws dhau los thaum kawg sib npaug 30 nws tau tawm ntawm lub voj thiab rov qab mus rau qhov twg peb hu qeeb: los ntawm. Net qhov tshwm sim yog ncua ntawm 1/2 thib ob

Nqe Lus 2: Hloov cov txheej txheem overflow_handler rau cov hauv qab no:

txug_handler:

inc dhau los reti

thiab khiav qhov program. Puas muaj dab tsi txawv? Vim li cas lossis vim li cas tsis?

Kauj ruam 8: Blink

Thaum kawg cia saib lub ntsais ntsais li niaj zaus:

ntsais muag:

sbi PORTD, 4 rcall ncua cbi PORTD, 4 rcall ncua rjmp ntsais muag

Ua ntej peb qhib PD4, tom qab ntawd peb hu rau peb ncua sijhawm subroutine. Peb siv rcall kom thaum lub PC tau txais cov lus "ret" nws yuav rov qab los rau kab tom qab rcall. Tom qab ntawv ncua sijhawm ncua sijhawm 30 suav nyob rau hauv qhov sib txawv ntawm qhov sib txawv raws li peb tau pom thiab qhov no yuav luag zoo li 1/2 thib ob, tom qab ntawd peb tua PD4, ncua ntxiv 1/2 thib ob, thiab rov qab mus rau qhov pib dua.

Cov txiaj ntsig tau yog qhov ntsais ntsais LED!

Kuv xav tias tam sim no koj yuav pom zoo tias "ntsais muag" tej zaum tsis yog qhov zoo tshaj plaws "nyob zoo ntiaj teb" program hauv cov lus sib dhos.

Qoj 3: Hloov ntau yam tsis nyob hauv qhov kev zov me nyuam kom LED ntsais ntawm qhov sib txawv xws li thib ob lossis 4 zaug ib pliag, thiab lwm yam Kev Ua Si 4: Hloov nws kom LED qhib thiab tawm rau ntau lub sijhawm. Piv txwv li rau 1/4 thib ob thiab tom qab ntawd kaw rau 2 vib nas this lossis qee yam zoo li ntawd. Lub ntsiab lus twg nws dhau los ua qhov sib txawv ntawm peb "hello.asm" program los ntawm kev qhia 1? Qoj 6 (tsis teb los tau): Yog tias koj muaj qhov sib txawv siv lead ua oscillator, zoo li 4 MHz lossis 13.5 MHz lossis ib qho twg, hloov koj li 16 MHz oscillator ntawm koj daim ntawv qhia zaub mov rau tus tshiab thiab saib yuav ua li cas cuam tshuam rau qhov ntsais ntawm tus LED. Tam sim no koj yuav tsum muaj peev xwm mus dhau qhov kev suav meej thiab twv seb yuav cuam tshuam tus nqi li cas.

Kauj Ruam 9: Xaus

Rau cov neeg ntawm koj tuag-nyuaj uas ua rau nws nyob deb, Zoo siab!

Kuv paub tias nws nyuaj heev slogging thaum koj nyeem ntawv ntau dua thiab saib zoo dua li koj tab tom thaiv thiab sim tab sis kuv vam tias koj tau kawm txog yam tseem ceeb hauv qab no:

  1. Yuav ua li cas Program Memory ua haujlwm
  2. SRAM ua haujlwm li cas
  3. Yuav ua li cas mus saib cov npe
  4. Yuav ua li cas saib cov lus qhia thiab paub tias lawv ua dab tsi
  5. Yuav ua li cas siv kev cuam tshuam
  6. CP ua raws txoj cai li cas, SREG ua haujlwm li cas, thiab tshwm sim dab tsi thaum cuam tshuam
  7. Yuav ua li cas loops thiab dhia thiab thaws ib ncig ntawm txoj cai
  8. Nws tseem ceeb npaum li cas los nyeem cov ntawv teev npe!
  9. Yuav ua li cas ib zaug koj paub yuav ua li cas txhua qhov no rau Atmega328p microcontroller nws yuav yog tus txheeb ze ncuav mog qab zib taug kev kom kawm paub cov tswj hwm tshiab uas koj nyiam.
  10. Yuav hloov CPU lub sijhawm li cas thiab siv nws li ncua sijhawm.

Tam sim no peb muaj ntau txoj kev xav tawm ntawm txoj hauv kev peb tuaj yeem sau cov cai zoo dua thiab tswj hwm yam nyuaj. Yog li ntawv qhia ntxiv peb yuav ua qhov ntawd nkaus xwb. Peb yuav tsim qhov nyuaj dua, nthuav dua, ua Circuit thiab tswj nws hauv kev lom zem.

Qoj 7: "Ua txhaum" cov cai hauv ntau txoj hauv kev thiab pom tias muaj dab tsi tshwm sim! Kev xav paub menyuam! Lwm tus neeg tuaj yeem ntxuav cov tais diav kom raug? Qoj 8: Sib sau ua ke cov cai siv "-l" xaiv los tsim cov npe khoom. I.e. "avra -l blink.lst blink.asm" thiab saib ntawm daim ntawv teev npe. Credit ntxiv: Tus lej tsis tau hais tawm uas kuv tau muab rau thaum pib thiab cov cai hais tawm uas peb tham tom qab sib txawv! Muaj ib kab ntawm cov cai uas txawv. Koj puas pom nws? Vim li cas qhov ntawd tsis txawv?

Vam tias koj muaj kev lom zem! Ntsib lwm zaus …

Pom zoo: