Cov txheej txheem:

AVR Microcontroller. LEDs Flasher Siv Timer. Timers cuam tshuam. Timer CTC Hom: 6 Kauj Ruam
AVR Microcontroller. LEDs Flasher Siv Timer. Timers cuam tshuam. Timer CTC Hom: 6 Kauj Ruam

Video: AVR Microcontroller. LEDs Flasher Siv Timer. Timers cuam tshuam. Timer CTC Hom: 6 Kauj Ruam

Video: AVR Microcontroller. LEDs Flasher Siv Timer. Timers cuam tshuam. Timer CTC Hom: 6 Kauj Ruam
Video: 8051 microcontroller programming 2024, Hlis ntuj nqeg
Anonim
Image
Image

Nyob zoo sawv daws!

Timers yog lub tswv yim tseem ceeb hauv kev siv hluav taws xob. Txhua qhov khoom siv hluav taws xob ua haujlwm raws lub sijhawm. Lub sijhawm no lub hauv paus pab ua kom txhua txoj haujlwm sib koom ua ke. Txhua lub microcontrollers ua haujlwm ntawm qee lub sijhawm teev ua ntej, lawv txhua tus muaj kev npaj teeb tsa lub sijhawm. AVR khav theeb ntawm kev muaj lub sijhawm uas yog qhov tseeb, raug thiab ntseeg tau. Nws muaj cov yam ntxwv ntau hauv nws, yog li ua rau nws yog lub ncauj lus dav. Qhov zoo tshaj plaws yog lub sijhawm ua haujlwm tsis muaj kev ywj pheej ntawm CPU. Yog li, nws ua haujlwm sib npaug rau CPU thiab tsis muaj CPU kev cuam tshuam, uas ua rau lub sijhawm siv tau raug. Hauv ntu no kuv piav qhia lub hauv paus ntsiab lus ntawm AVR Timers. Kuv tab tom sau txoj haujlwm yooj yim hauv C code los tswj LED flasher, siv timers.

Kauj ruam 1: Kev piav qhia

Nqe Lus Teeb Meem 1: Cia Flash Ua Ntej LED (ntsuab) Txhua 50 Ms
Nqe Lus Teeb Meem 1: Cia Flash Ua Ntej LED (ntsuab) Txhua 50 Ms

Hauv ATMega328 muaj peb hom timers:

Timer/Counter0 (TC0) - yog lub hom phiaj dav dav 8 -ntsis Timer/Counter module, nrog ob qho ywj pheej OutputCompare Units, thiab PWM txhawb nqa;

Timer/Counter1 (TC1) - 16 -ntsis Timer/Counter unit tso cai rau lub sijhawm ua haujlwm tau zoo (kev tswj hwm kev tshwm sim), nthwv dej tsim, thiab ntsuas lub sijhawm ntsuas;

Timer/Counter2 (TC2) -yog lub hom phiaj dav dav, channel, 8 -ntsis Timer/Counter module nrog PWM thiab Kev Ua Haujlwm Asynchronous;

Kauj Ruam 2: Teeb Meem Tshooj 1: Cia Flash Ua Ntej LED (ntsuab) Txhua 50 Ms

Nqe Lus Teeb Meem 1: Cia Flash Ua Ntej LED (ntsuab) Txhua 50 Ms
Nqe Lus Teeb Meem 1: Cia Flash Ua Ntej LED (ntsuab) Txhua 50 Ms
Nqe Lus Teeb Meem 1: Cia Flash Ua Ntej LED (ntsuab) Txhua 50 Ms
Nqe Lus Teeb Meem 1: Cia Flash Ua Ntej LED (ntsuab) Txhua 50 Ms

Txoj Kev:

- siv Timer0 prescaler txhawm rau txo qis teeb liab hluav taws xob kom qis dua los ntawm kev faib ua lej;

- siv kev cuam tshuam txhua lub sijhawm Timer0 dhau los;

Timer0 (8 ntsis) nws suav los ntawm 0 txog 255 tom qab ntawd, lawv dhau los, tus nqi no hloov pauv ntawm txhua lub moos mem tes.

F_CPU = 16MHz: Lub sijhawm moos = 1000ms / 16000000Hz = 0.0000625ms

Timer suav = (Yuav Tsum Ncua Sijhawm / Sijhawm Sijhawm) -1 = (50ms / 0.0000625ms) = 799999

Lub moos twb tau txheeb 799999 zaug los muab ncua ntawm tsuas yog 50 ms!

Peb tuaj yeem siv cov txheej txheem ntawm kev faib zaus yog hu ua prescaling kom txo cov timer. AVR muab cov txiaj ntsig prescaler hauv qab no rau peb xaiv los ntawm: 8, 64, 256 thiab 1024. Saib cov lus piav qhia cov txiaj ntsig ntawm kev siv cov tshuaj sib txawv.

Tus nqi txee yuav tsum yog tus zauv. Cia peb xaiv qhov prescaler 256!

Hauv feem ntau microcontrollers, muaj qee yam hu ua Interrupt. Qhov kev cuam tshuam no tuaj yeem raug rho tawm thaum twg los tau. Tam sim no thaum twg cuam tshuam raug rho tawm haujlwm, AVR nres thiab txuag nws txoj haujlwm tseem ceeb, koom nrog kev cuam tshuam kev hu (los ntawm kev ua haujlwm tshwj xeeb, hu ua Interrupt Service Routine, ISR) thiab thaum nws ua tiav nrog, rov qab mus rau lub luag haujlwm tseem ceeb thiab txuas ntxiv ua nws.

Txij li qhov xav tau ncua (50ms) ntau dua li qhov ua tau ncua qeeb: 4, 096ms = 1000ms / 62500Hz * 256, pom tseeb tias lub sijhawm yuav dhau mus. Thiab thaum twg lub sij hawm dhau mus, kev cuam tshuam raug rho tawm haujlwm.

Muaj pes tsawg zaus uas yuav tsum tau ncua kev cuam tshuam?

50ms / 4.096ms = 3125 /256 = 12.207 Yog tias lub sijhawm tau dhau mus 12 zaug, 12 * 4.096ms = 49.152ms yuav dhau mus. Hauv qhov rov ua dua 13, peb xav tau ncua sijhawm 50ms - 49.152ms = 0.848ms.

Ntawm qhov zaus ntawm 62500Hz (prescaler = 256), txhua tus zuam siv 0.016ms. Yog li kom ua tiav ncua ntawm 0.848ms, nws yuav xav tau 0.848ms / 0.016ms = 53 zuam. Yog li, hauv kev rov ua dua zaum 13, peb tsuas tso cai rau lub sijhawm ntsuas kom suav txog 53, thiab tom qab ntawd rov pib dua.

Pib Timer0/Counter (saib duab):

TCCR0B | = (1 << CS02) // teeb tsa timer nrog prescaler = 256 TCNT0 = 0 // initialize counter TIMSK0 | = (1 << TOIE0) // pab kom cuam tshuam cuam tshuam sei () // qhib kev cuam tshuam thoob ntiaj teb tot_overflow = 0 // initialize overflow counter variable

Kauj Ruam 3: Teeb meem nqe lus 2: Cia Flash Thib Ob LED (xiav) Txhua 1s

Nqe Lus Teeb Meem 2: Cia Flash Thib Ob LED (xiav) Txhua 1s
Nqe Lus Teeb Meem 2: Cia Flash Thib Ob LED (xiav) Txhua 1s
Nqe Lus Teeb Meem 2: Cia Flash Thib Ob LED (xiav) Txhua 1s
Nqe Lus Teeb Meem 2: Cia Flash Thib Ob LED (xiav) Txhua 1s
Nqe Lus Teeb Meem 2: Cia Flash Thib Ob LED (xiav) Txhua 1s
Nqe Lus Teeb Meem 2: Cia Flash Thib Ob LED (xiav) Txhua 1s

Txoj Kev:

- siv Timer1 prescaler txhawm rau txo qis teeb liab hluav taws xob kom qis dua los ntawm kev faib ua lej;

- siv Clear Timer ntawm Kev Sib Piv (CTC) Hom;

- siv cuam tshuam nrog CTC Hom;

Timer1 (16 ntsis) nws suav los ntawm 0 txog 65534 tom qab ntawd, lawv dhau los. Tus nqi no pauv ntawm txhua lub moos mem tes.

F_CPU = 16MHz: Sijhawm Sijhawm = 1000ms / 16000000Hz = 0.0000625msTimer suav = (Yuav Tsum Ncua Sijhawm / Sijhawm Sijhawm) -1 = (1000ms / 0.0000625ms) = 15999999

Lub moos twb tau txheeb xyuas 15999999 zaug los muab ncua ntawm 1s!

Peb tuaj yeem siv cov txheej txheem ntawm kev faib zaus yog hu ua prescaling kom txo cov timer. AVR muab cov txiaj ntsig prescaler hauv qab no rau peb xaiv los ntawm: 8, 64, 256 thiab 1024. Saib cov lus piav qhia cov txiaj ntsig ntawm kev siv cov tshuaj sib txawv. Tus nqi txee yuav tsum yog tus zauv. Cia peb xaiv qhov prescaler 256!

Hauv Ntsuas timer ntawm Kev Sib Piv (CTC) hom, OCR1A lossis ICR1 rau npe tau siv los tswj lub rooj daws teeb meem. Hauv CTC hom lub txee raug tshem tawm mus rau xoom thaum tus nqi txee (TCNT1) sib xws nrog OCR1A lossis ICR1. OCR1A lossis ICR1 piav qhia tus nqi zoo tshaj plaws rau lub txee, yog li nws kuj tseem daws teeb meem. Hom no tso cai rau kev tswj hwm ntau dua ntawm kev sib piv sib tw tso zis nws kuj yooj yim rau kev suav ntawm cov xwm txheej sab nraud. Peb yuav tsum qhia AVR kom rov pib Timer1/Counter sai li sai tau thaum nws tus nqi nce mus txog tus nqi 62500, yog li kom ua tiav ncua sijhawm 1s.

Pib Pib Timer1/Counter (saib duab):

TCCR1B | = (1 << WGM12) | (1 << CS12) // teeb tsa timer nrog prescaler = 256 thiab CTC hom TCNT1 = 0 // initialize counter TIMSK1 | = (1 << OCIE1A) // qhib sib piv cuam tshuam cuam tshuam OCR1A = 62500 // pib sib piv tus nqi

Kauj Ruam 4: Teeb meem nqe lus 3: Cia Flash Thib Peb LED (liab) Txhua 16ms

Nqe Lus Teeb Meem 3: Cia Peb Thib Peb LED (liab) Txhua 16ms
Nqe Lus Teeb Meem 3: Cia Peb Thib Peb LED (liab) Txhua 16ms
Nqe Lus Teeb Meem 3: Cia Peb Thib Peb LED (liab) Txhua 16ms
Nqe Lus Teeb Meem 3: Cia Peb Thib Peb LED (liab) Txhua 16ms
Nqe Lus Teeb Meem 3: Cia Peb Thib Peb LED (liab) Txhua 16ms
Nqe Lus Teeb Meem 3: Cia Peb Thib Peb LED (liab) Txhua 16ms
Nqe Lus Teeb Meem 3: Cia Peb Thib Peb LED (liab) Txhua 16ms
Nqe Lus Teeb Meem 3: Cia Peb Thib Peb LED (liab) Txhua 16ms

Txoj Kev:

- siv Timer2 prescaler txhawm rau txo qis teeb liab hluav taws xob kom qis dua los ntawm kev faib ua lej;

- siv Clear Timer ntawm Kev Sib Piv (CTC) Hom;

- siv Hardware CTC Mode yam tsis muaj kev cuam tshuam;

Timer2 (8 ntsis) nws suav los ntawm 0 txog 255 tom qab ntawd, lawv dhau los. Tus nqi no pauv ntawm txhua lub moos mem tes.

F_CPU = 16MHz: Lub sijhawm moos = 1000ms / 16000000Hz = 0.0000625ms

Timer suav = (Yuav Tsum Ncua Sijhawm / Sijhawm Sijhawm) -1 = (16ms / 0.0000625ms) = 255999

Lub moos twb tau kos 255999 zaug los muab ncua ntawm 16ms!

Saib cov lus piav qhia cov txiaj ntsig ntawm kev siv cov tshuaj sib txawv. Tus nqi txee yuav tsum yog tus zauv. Cia peb xaiv qhov prescaler 1024!

Hauv CTC hom lub txee raug tshem tawm mus rau xoom thaum tus nqi txee (TCNT2) sib xws nrog OCR2A lossis ICR2. Tus pin PB3 kuj tseem yog Cov Khoom Sib Piv Tus pin ntawm TIMER2 - OC2A (saib daim duab).

Timer/Counter2 Tswj Sau Npe A - TCCR2A Ntsis 7: 6 - COM2A1: 0 - Sib Piv Cov Qauv Tawm Rau Sib Piv Chav A. OC2A tus pin tau hloov pauv tau. Tsis tas yuav txheeb xyuas tus chij me ntsis, tsis tas yuav koom nrog ib qho kev cuam tshuam.

Pib Timer2/Counter

TCCR2A | = (1 << COM2A0) | (1 << WGM21) // teeb tsa timer OC2A tus pin hauv hom toggle thiab CTC hom TCCR2B | = (1 << CS22) | (1 << CS21) | (1 << CS20) // teeb tsa timer nrog prescaler = 1024 TCNT2 = 0 // initialize counter OCR2A = 250 // initialize sib piv tus nqi

Kauj Ruam 5: Sau Txoj Cai rau Txoj Haujlwm hauv C. Xa Cov Ntaub Ntawv HEX Rau Hauv Microcontroller Flash Memory

Sau Txoj Cai rau Txoj Haujlwm hauv C. Tso HEX Cov Ntaub Ntawv Rau Hauv Microcontroller Flash Memory
Sau Txoj Cai rau Txoj Haujlwm hauv C. Tso HEX Cov Ntaub Ntawv Rau Hauv Microcontroller Flash Memory
Sau Txoj Cai rau Txoj Haujlwm hauv C. Tso HEX Cov Ntaub Ntawv Rau Hauv Microcontroller Flash Memory
Sau Txoj Cai rau Txoj Haujlwm hauv C. Tso HEX Cov Ntaub Ntawv Rau Hauv Microcontroller Flash Memory

Sau thiab tsim AVR microcontroller thov hauv C Code siv Kev Txhim Kho Kev Txhim Kho - Atmel Studio.

F_CPU piav qhia lub moos zaus hauv Hertz thiab ib txwm muaj nyob hauv cov haujlwm uas siv avr-libc lub tsev qiv ntawv. Hauv qhov no nws tau siv los ntawm kev ncua sijhawm los txiav txim siab yuav suav li cas ncua sijhawm.

#ifndef F_CPU

#define F_CPU 16000000UL // qhia cov tswj hwm zaus (16 MHz AVR ATMega328P) #endif

#suav nrog // lub taub hau kom pab tau cov ntaub ntawv ntws tswj tus pin. Txhais pins, chaw nres nkoj, thiab lwm yam.

Thawj qhov suav nrog cov ntaub ntawv yog ib feem ntawm avr-libc thiab yuav raug siv zoo nkauj npaum li txhua qhov haujlwm AVR uas koj ua haujlwm. io.h yuav txiav txim siab CPU koj tab tom siv (uas yog vim li cas koj thiaj hais qhia ib feem thaum suav ua ke) thiab nyob rau hauv suav nrog qhov tsim nyog IO txhais lub ntsiab lus rau lub nti uas peb siv. Nws yooj yim txhais qhov txuas ntxiv rau txhua tus koj tus pin, chaw nres nkoj, cov ntawv tshwj xeeb, thiab lwm yam.

#suav nrog // header kom pab cuam tshuam

volatile uint8_t tot_overflow; // thoob ntiaj teb sib txawv los suav cov lej dhau los

Txoj hauv kev ntawm Cov Lus Qhia Teeb Meem: Flash Ua Ntej (Ntsuab) LED txhua 50 ms

- siv Timer0 prescaler txhawm rau txo qis teeb liab hluav taws xob kom qis dua los ntawm kev faib ua lej;

- siv kev cuam tshuam txhua lub sijhawm Timer0 dhau los;

void timer0_init () // initialize timer0, cuam tshuam thiab hloov pauv

{TCCR0B | = (1 << CS02); // teeb tsa lub sijhawm nrog prescaler = 256 TCNT0 = 0; // initialize counter TIMSK0 | = (1 << TOIE0); // pab kom hla dhau nterrupt sei (); // pab kom thoob ntiaj teb cuam tshuam tot_overflow = 0; // initialize overflow counter variable}

Txoj hauv kev ntawm teeb meem Cov Lus Qhia: Flash Thib Ob LED (xiav) txhua 1s

- siv Timer1 prescaler txhawm rau txo qis teeb liab hluav taws xob kom qis dua los ntawm kev faib ua lej;

- siv Clear Timer ntawm Kev Sib Piv (CTC) Hom;

- siv cuam tshuam nrog CTC Hom;

void timer1_init () // initialize timer1, cuam tshuam thiab hloov pauv {TCCR1B | = (1 << WGM12) | (1 << CS12); // teeb tsa lub sijhawm nrog prescaler = 256 thiab CTC hom TCNT1 = 0; // initialize counter OCR1A = 62500; // initialize sib piv tus nqi TIMSK1 | = (1 << OCIE1A); // pab sib piv cuam tshuam}

Txoj hauv kev ntawm teeb meem Cov Lus Qhia: Flash thib peb LED (liab) txhua 16ms

- siv Timer2 prescaler txhawm rau txo qis teeb liab hluav taws xob kom qis dua los ntawm kev faib ua lej;

- siv Clear Timer ntawm Kev Sib Piv (CTC) Hom;

- siv Hardware CTC Mode yam tsis muaj kev cuam tshuam;

void timer2_init () // initialize timer2 {TCCR2A | = (1 << COM2A0) | (1 << WGM21); // teeb tsa timer OC2A tus pin hauv hom toggle thiab CTC hom TCCR2B | = (1 << CS22) | (1 << CS21) | (1 << CS20); // teeb tsa lub sijhawm nrog prescaler = 1024 TCNT2 = 0; // initialize counter OCR2A = 250; // initialize sib piv tus nqi}

TIMER0 hla kev cuam tshuam cov kev pabcuam niaj hnub hu ua thaum twg TCNT0 ntws dhau:

ISR (TIMER0_OVF_vect)

{tot_overflow ++; // khaws cov lej ntawm cov dej ntws dhau}

Qhov ISR no raug rho tawm thaum twg muaj kev sib tw tshwm sim li no, toggle coj ntawm no nws tus kheej:

ISR (TIMER1_COMPA_vect) {PORTC ^= (1 << 1); // toggle coj ntawm no}

int main (tsis muaj dab tsi)

{DDRB | = (1 << 0); // txuas 1 (ntsuab) coj mus rau tus pin PB0 DDRC | = (1 << 1); // txuas 2 (xiav) coj mus rau tus pin PC1 DDRB | = (1 << 3); // txuas 3 (liab) coj mus rau tus pin PB3 (OC2A) timer0_init (); // initialize timer0 timer1_init (); // initialize timer1 timer2_init (); // initialize timer2 thaum (1) // loop ib si {

Yog tias Timer0 tau dhau 12 zaug, 12 * 4.096ms = 49.152ms yuav dhau mus. Hauv qhov rov ua dua 13, peb xav tau ncua sijhawm 50ms - 49.152ms = 0.848ms. Yog li, hauv kev rov ua dua zaum 13, peb tsuas tso cai rau lub sijhawm ntsuas kom suav txog 53, thiab tom qab ntawd rov pib dua.

yog (tot_overflow> = 12) // kos yog tias tsis yog. ntawm overflows = 12 NCO TSEG: '> =' yog siv

{yog (TCNT0> = 53) // kos yog tias lub sijhawm timer nce mus txog 53 {PORTB ^= (1 << 0); // toggles tus coj TCNT0 = 0; // rov pib dua counter tot_overflow = 0; // rov pib dua cov khoom txee}}}}

Kev xa cov ntaub ntawv HEX rau hauv microcontroller flash nco:

ntaus hauv DOS tam sim qhov rai qhov hais kom ua:

avrdude –c [lub npe programmer] –p m328p –u flashU flash: w: [lub npe ntawm koj cov ntaub ntawv hex] Hauv kuv cov ntaub ntawv nws yog: avrdude –c ISPProgv1 –p m328p –u –U flash: w: Timers.hex

Cov lus txib no sau cov ntawv hex rau lub microcontroller lub cim xeeb. Saib cov vis dis aus nrog cov lus piav qhia ntxaws txog lub microcontroller flash memory hlawv:

Microcontroller flash nco hlawv…

Ok! Tam sim no, lub microcontroller ua haujlwm raws li cov lus qhia ntawm peb txoj haujlwm. Cia peb kuaj nws!

Kauj Ruam 6: Ua Cov Hluav Taws Xob Hluav Taws Xob

Ua Cov Hluav Taws Xob Hluav Taws Xob
Ua Cov Hluav Taws Xob Hluav Taws Xob
Ua Cov Hluav Taws Xob Hluav Taws Xob
Ua Cov Hluav Taws Xob Hluav Taws Xob
Ua Cov Hluav Taws Xob Hluav Taws Xob
Ua Cov Hluav Taws Xob Hluav Taws Xob

Txuas cov khoom siv raws li daim duab qhia chaw.

Pom zoo: