Cov txheej txheem:

AVR Microcontroller. Mem tes dav tes taws los piav. Сontroller ntawm DC Tsav thiab LED Teeb Pom Kev Zoo: 6 Cov Kauj Ruam
AVR Microcontroller. Mem tes dav tes taws los piav. Сontroller ntawm DC Tsav thiab LED Teeb Pom Kev Zoo: 6 Cov Kauj Ruam

Video: AVR Microcontroller. Mem tes dav tes taws los piav. Сontroller ntawm DC Tsav thiab LED Teeb Pom Kev Zoo: 6 Cov Kauj Ruam

Video: AVR Microcontroller. Mem tes dav tes taws los piav. Сontroller ntawm DC Tsav thiab LED Teeb Pom Kev Zoo: 6 Cov Kauj Ruam
Video: Proteus | AVRDUDE | Отладка и прошивка микроконтроллеров AVR | Микроконтроллеры с нуля #5 2024, Kaum ib hlis
Anonim
Image
Image

Nyob zoo sawv daws!

Pulse Width Modulation (PWM) yog txheej txheem ntau heev hauv kev sib tham hauv xov tooj thiab tswj lub zog. nws feem ntau yog siv los tswj lub zog xa mus rau cov khoom siv hluav taws xob, txawm tias nws yog lub cev muaj zog, LED, hais lus, thiab lwm yam. Nws yog qhov pib txheej txheem thev naus laus zis, uas qhov dav ntawm cov khoom siv pulse tau hloov pauv raws li cov lus sib txuas lus.

Peb ua hluav taws xob hluav taws xob yooj yim los tswj kev sib hloov ntawm DC lub cev muaj zog nyob ntawm qhov pom kev. Peb yuav siv Lub Teeb Ntsuas Tus Kheej thiab AVR microcontroller nta xws li Analog rau Digital Hloov pauv los ntsuas lub teeb siv. Tsis tas li peb yuav siv Dual H-Choj Tsav Tsav Tsav Tsheb-L298N. Nws feem ntau yog siv hauv kev tswj lub cav nrawm thiab taw qhia, tab sis tuaj yeem siv rau lwm qhov haujlwm xws li tsav lub ci ci ntawm qee qhov haujlwm teeb pom kev zoo. Tsis tas li, ntxiv ib lub pob rau peb lub vojvoog kom hloov qhov kev taw qhia ntawm kev sib hloov ntawm lub cav.

Kauj ruam 1: Kev piav qhia

Nqe lus piav qhia
Nqe lus piav qhia
Nqe lus piav qhia
Nqe lus piav qhia

Txhua tus thiab txhua lub cev hauv ntiaj teb no muaj qee qhov inertia. Lub cav tig thaum twg nws tau qhib. Sai li nws tau muab tua, nws yuav zoo li tso tseg. Tab sis nws tsis nres tam sim, nws siv sijhawm me ntsis. Tab sis ua ntej nws nres kiag li, nws tau rov qhib dua! Yog li nws pib txav mus. Tab sis txawm tam sim no, nws siv sijhawm qee lub sijhawm kom mus txog nws qhov nrawm. Tab sis ua ntej nws tshwm sim, nws tau raug tua, thiab lwm yam. Yog li, tag nrho cov txiaj ntsig ntawm qhov kev nqis tes ua no yog lub cav tig tsis tu ncua, tab sis ntawm qhov nrawm dua.

Pulse Width Modulation (PWM) yog qhov piv txwv tsis ntev los no hloov pauv cov txheej txheem rau muab cov khoom siv hluav taws xob nruab nrab nruab nrab ntawm kev ua tiav thiab ua tiav theem. Feem ntau, digital pulses muaj tib lub sijhawm thiab tawm sijhawm, tab sis qee qhov xwm txheej peb xav tau digital pulse kom muaj ntau/tsawg dua sijhawm/sijhawm tawm. Hauv PWM cov txheej txheem, peb tsim cov digital pulses nrog qhov tsis sib xws ntawm lub xeev thiab tawm mus kom tau txais qhov ntsuas qhov nruab nrab qhov tseem ceeb.

Lub voj voog ua haujlwm tau txhais los ntawm qhov feem pua ntawm lub sijhawm hluav taws xob siab hauv kev ua tiav digital pulse. Nws tuaj yeem suav los ntawm:

% ntawm Duty cycle = T rau /T (lub sijhawm) x 100

Cia peb coj ib nqe lus hais txog teeb meem. Peb yuav tsum tsim kom muaj 50 Hz PWM teeb liab muaj 45% kev ua haujlwm.

Zaus = 50 Hz

Lub sijhawm, T = T (rau) + T (tawm) = 1/50 = 0.02 s = 20 ms

Duty Cycle = 45%

Yog li, daws raws li kab zauv uas tau hais los saud, peb tau txais

T (ntawm) = 9 ms

T (tawm) = 11 ms

Kauj Ruam 2: AVR Timers - PWM Hom

AVR Timers - PWM Mode
AVR Timers - PWM Mode
AVR Timers - PWM Mode
AVR Timers - PWM Mode

Kev ua PWM, AVR muaj cov khoom sib cais! Los ntawm kev siv qhov no, CPU qhia kho vajtse los tsim PWM ntawm lub luag haujlwm tshwj xeeb. ATmega328 muaj 6 PWM tso tawm, 2 nyob ntawm timer/counter0 (8bit), 2 nyob ntawm timer/counter1 (16bit), thiab 2 nyob ntawm timer/counter2 (8bit). Timer/Counter0 yog qhov yooj yim PWM ntaus ntawv ntawm ATmega328. Timer/Counter0 muaj peev xwm khiav ntawm 3 hom:

  • Fast PWM
  • Theem thiab Zaus Kho PWM
  • Phase Corrected PWM

txhua hom no tuaj yeem thim rov qab lossis tsis thim rov qab.

Pib Timer0 hauv PWM hom:

TCCR0A | = (1 << WGM00) | (1 << WGM01) - set WGM: Fast PWM

TCCR0A | = (1 << COM0A1) | (1 << COM0B1) - teeb tsa kev sib piv hom A, B

TCCR0B | = (1 << CS02) - teeb tsa lub sijhawm nrog prescaler = 256

Kauj Ruam 3: Kev Ntsuas Kev Nyuaj Siab - ADC & LDR

Kev Ntsuas Kev Nyuaj Siab - ADC & LDR
Kev Ntsuas Kev Nyuaj Siab - ADC & LDR
Kev Ntsuas Kev Nyuaj Siab - ADC & LDR
Kev Ntsuas Kev Nyuaj Siab - ADC & LDR
Kev Ntsuas Kev Nyuaj Siab - ADC & LDR
Kev Ntsuas Kev Nyuaj Siab - ADC & LDR

Lub Teeb Ntsuas Tus Kws Tiv Thaiv (LDR) yog tus hloov pauv uas hloov nws qhov kev tiv thaiv thaum lub teeb ntog ntawm nws qhov kev hloov pauv.

LDRs yog tsim los ntawm cov khoom siv hluav taws xob los pab kom lawv muaj lawv lub zog ua kom pom kev zoo. Cov LDRs lossis PHOTO RESISTORS ua haujlwm ntawm lub hauv paus ntsiab lus ntawm "Photo Conductivity". Tam sim no dab tsi lub hauv paus ntsiab lus hais tias yog thaum twg lub teeb poob rau ntawm LDR (qhov no) qhov kev coj ua ntawm lub hauv paus nce lossis hauv lwm lo lus qhov kev tiv thaiv ntawm LDR txo qis thaum lub teeb poob rau ntawm LDR. Cov cuab yeej ntawm qhov txo qis hauv kev tiv thaiv rau LDR tau ua tiav vim tias nws yog cov cuab yeej ntawm cov khoom siv hluav taws xob siv rau saum npoo. LDR feem ntau siv los tshuaj xyuas pom lub teeb lossis ntsuas ntsuas qhov pom kev.

Rau kev xa cov ntaub ntawv txuas mus ntxiv sab nraud (cov ntaub ntawv sib piv) mus rau hauv cov lej/kev suav, peb yuav tsum hloov lawv mus rau hauv cov lej (cov lej). Hom kev hloov pauv no tau ua los ntawm Analog rau Digital Converter (ADC). Tus txheej txheem ntawm kev hloov pauv tus nqi sib piv rau tus nqi digital yog lub npe hu ua Analog to Digital Conversion. Hauv ntej, Analog cov cim qhia yog lub ntiaj teb tiag qhia nyob ib puag ncig peb zoo li lub suab thiab lub teeb.

Cov cim qhia yog cov sib piv sib npaug hauv cov lej lossis cov lej uas tau nkag siab zoo los ntawm cov tshuab zoo li microcontrollers. ADC yog ib qho khoom siv uas ntsuas cov cim sib piv thiab tsim cov digital sib npaug ntawm tib lub teeb liab. AVR microcontrollers muaj inbuilt ADC qhov chaw los hloov cov hluav taws xob sib piv rau hauv cov lej. AVR hloov nws mus rau 10-tus lej ntawm thaj tsam 0 txog 1023.

Peb siv analog rau digital hloov pauv ntawm qib hluav taws xob los ntawm kev faib hluav taws xob nrog LDR txhawm rau ntsuas lub teeb ci.

Pib ADC:

TADCSRA | = (1 << ADEN) - Pab kom ADC

ADCSRA | = (1 << ADPS2) | (1 << ADPS1) | (1ADPS0) - teeb tsa ADC prescaler = 128

ADMUX = (1 << REFS0) - teeb tsa qhov ntsuas hluav taws xob = AVCC; - teeb tsa Cov Tswv Yim Channel = ADC0

Saib cov vis dis aus nrog cov lus piav qhia ntxaws txog ADC AVR microcontroller: AVR Microcontroller. Kev Ntsuas Teeb Pom Kev. ADC & LDR

Kauj Ruam 4: Tus Tswj DC Tsav & Dual H-Choj Tsav Tsav Tsav-L298N

Xws li DC Tsav & Dual H-Choj Tsav Tsav Tsav Tsheb-L298N
Xws li DC Tsav & Dual H-Choj Tsav Tsav Tsav Tsheb-L298N
Xws li DC Tsav & Dual H-Choj Tsav Tsav Tsav Tsheb-L298N
Xws li DC Tsav & Dual H-Choj Tsav Tsav Tsav Tsheb-L298N
Xws li DC Tsav & Dual H-Choj Tsav Tsav Tsav Tsheb-L298N
Xws li DC Tsav & Dual H-Choj Tsav Tsav Tsav Tsheb-L298N

Peb siv DC lub tshuab tsav tsheb vim tias microcontrollers tsis muaj peev xwm xa tam sim no tsis ntau dua 100 milliamps feem ntau. Cov microcontrollers ntse tab sis tsis muaj zog; cov qauv no yuav ntxiv qee cov leeg rau microcontrollers los tsav lub zog DC lub cev muaj zog. Nws tuaj yeem tswj 2 DC lub cev muaj zog ib txhij txog 2 amps txhua lossis ib lub cav tsav. Peb tuaj yeem tswj qhov nrawm siv PWM thiab tseem nws qhov kev taw qhia ntawm cov motors. Tsis tas li, Nws siv rau kev tsav lub ci ci ntawm daim kab xev LED.

Piav qhia:

OUT1 thiab OUT2 chaw nres nkoj, uas yog rau txuas DC lub cev muaj zog. OUT3 thiab OUT4 rau txuas LED kab xev.

ENA thiab ENB tuaj yeem pab tus pin: los ntawm kev txuas ENA mus rau qhov siab (+5V) nws ua rau qhov chaw nres nkoj OUT1 thiab OUT2.

Yog tias koj txuas tus ENA tus pin mus rau qis (GND), nws ua tsis taus OUT1 thiab OUT2. Ib yam li ntawd, rau ENB thiab OUT3 thiab OUT4.

IN1 txog IN4 yog cov pins nkag uas yuav txuas nrog AVR.

Yog IN1-siab (+5V), IN2-low (GND), OUT1 tig siab thiab OUT2 tig qis, yog li peb tuaj yeem tsav lub cav.

Yog tias IN3-siab (+5V), IN4-low (GND), OUT4 tig siab thiab OUT3 tig qis, yog li LED kab xev lub teeb tau qhib.

Yog tias koj xav thim rov qab qhov kev taw qhia ntawm lub cav tsuas yog thim rov qab IN1 thiab IN2 polarity, zoo ib yam rau IN3 thiab IN4.

Los ntawm kev thov PWM teeb liab rau ENA thiab ENB koj tuaj yeem tswj qhov nrawm ntawm lub cav ntawm ob qhov chaw nres nkoj sib txawv.

Pawg thawj coj tuaj yeem lees txais los ntawm 7V txog 12V npe.

Jumpers: Muaj peb tus pin jumper; Jumper 1: Yog tias koj lub cev xav tau ntau dua 12V cov khoom koj yuav tsum txuas lub Jumper 1 thiab siv qhov xav tau qhov siab (max 35V) ntawm 12V davhlau ya nyob twg. Nqa lwm 5V cov khoom siv thiab cov tswv yim ntawm 5V lub davhlau ya nyob twg. Yog lawm, koj yuav tsum tau nkag 5V yog tias koj xav tau thov ntau dua 12V (thaum Jumper 1 raug tshem tawm).

5V cov tswv yim yog rau kev ua haujlwm kom raug ntawm IC, txij li tshem lub jumper yuav ua rau tsis ua haujlwm 5V tus tswj hwm thiab tiv thaiv los ntawm kev nkag siab ntau dua los ntawm 12V davhlau ya nyob twg.

Lub davhlau ya nyob twg 5V ua haujlwm raws li qhov tso tawm yog tias koj cov khoom siv nyob nruab nrab ntawm 7V txog 12V thiab ua raws li cov tswv yim yog tias koj thov ntau dua 12V thiab jumper raug tshem tawm.

Jumper 2 thiab Jumper 3: Yog tias koj tshem tawm ob lub dhia no koj yuav tsum tau nkag mus qhib thiab kaw lub teeb liab los ntawm lub microcontroller, feem ntau ntawm cov neeg siv xav tshem ob lub dhia thiab siv lub teeb liab los ntawm microcontroller.

Yog tias koj khaws ob tus neeg dhia tawm OUT1 rau OUT4 yuav raug qhib ib txwm. Nco ntsoov ENA jumper rau OUT1 thiab OUT2. ENB jumper rau OUT3 thiab OUT4.

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

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

#ifndef F_CPU #txhais F_CPU 16000000UL // qhia tswj cov zaus siv (16 MHz AVR ATMega328P) #endif

#suav nrog // lub taub hau kom pab cov ntaub ntawv ntws tswj tus pin. Txhais tus pin, chaw nres nkoj, thiab lwm yam. #Suav nrog // lub taub hau kom pab ua haujlwm qeeb hauv qhov haujlwm

#define BUTTON1 2 // khawm hloov txuas mus rau chaw nres nkoj B tus pin 2 #define DEBOUNCE_TIME 25 // lub sijhawm tos thaum "de-bouncing" khawm #define LOCK_INPUT_TIME 300 // sijhawm tos tom qab khawm

// Timer0, PWM Initialization void timer0_init () {// teeb tsa timer OC0A, OC0B tus pin hauv hom toggle thiab CTC hom TCCR0A | = (1 << COM0A1) | (1 << COM0B1) | (1 << WGM00) | (1 << WGM01); // teeb tsa timer nrog prescaler = 256 TCCR0B | = (1 << CS02); // initialize counter TCNT0 = 0; // initialize sib piv tus nqi OCR0A = 0; }

// ADC Initialization void ADC_init () {// Enable ADC, sampling freq = osc_freq/128 set prescaler to max value, 128 ADCSRA | = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);

ADMUX = (1 << REFS0); // Xaiv Voltage Siv (AVCC)

// Khawm hloov xwm txheej tsis tau kos npe char button_state () {

/ * lub pob nyem thaum BUTTON1 ntsis meej */

yog (! (PINB & (1 <

{

_delay_ms (DEBOUNCE_TIME);

yog (! (PINB & (1 <

}

rov 0;

}

// Ports Initialization void port_init () {DDRB = 0b00011011; // PB0-IN1, PB1-IN2, PB3-IN3, PB4-IN4, PB2-BUTTON SWITCH DIRECT PORTB = 0b00010110;

DDRD = 0b01100000; // PD5-ENB (OC0B), PD6-ENA (OC0A) PORTD = 0b00000000;

DDRC = 0b00000000; // PC0-ADC PORTC = 0b00000000; // Teeb txhua tus pin ntawm PORTC qis uas tig nws tawm. }

// Qhov haujlwm no nyeem tus nqi ntawm analog rau digital hloov pauv. uint16_t get_LightLevel () {_delay_ms (10); // Tos qee lub sijhawm rau cov channel kom tau txais kev xaiv ADCSRA | = (1 << ADSC); // Pib ADC hloov dua siab tshiab los ntawm teeb tsa ADSC me ntsis. Sau 1 rau ADSC

thaum (ADCSRA & (1 << ADSC)); // Tos kom hloov dua siab tshiab kom tiav

// ADSC dhau 0 rov mus txog thaum ntawd, khiav voj tsis tu ncua _delay_ms (10); rov qab (ADC); // Rov qab 10-ntsis tshwm sim

}

// Qhov haujlwm no Rov qhia cov lej los ntawm ib qho (0-1023) mus rau lwm (0-100). uint32_t daim ntawv qhia (uint32_t x, uint32_t in_min, uint32_t hauv_max, uint32_t tawm_min, uint32_t tawm_max) {rov qab (x - in_min) * (tawm_max - tawm_min) / (in_max - hauv_min) + tawm_min; }

int main (tsis muaj dab tsi)

{uint16_t i1 = 0;

port_init (); cov.

timer0_init (); ADC_init (); // initialization ADC

thaum (1)

{i1 = daim ntawv qhia (get_LightLevel (), 0, 1023, 0, 100);

OCR0A = i1; // Teeb tsa cov txiaj ntsig sib piv sau npe channel A OCR0B = 100-i1; // Teeb tsa cov txiaj ntsig sib piv sau npe channel B (thim rov qab)

yog (button_state ()) // Yog tias lub pob nyem, toggle LED lub xeev thiab ncua rau 300ms (#define LOCK_INPUT_TIME) {PORTB ^= (1 << 0); // toggling lub xeev tam sim no ntawm tus pin IN1. PORTB ^= (1 << 1); // toggling lub xeev tam sim no ntawm tus pin IN2. Rov qab cov lus taw qhia ntawm lub cav

PORTB ^= (1 << 3); // toggling lub xeev tam sim no ntawm tus pin IN3. PORTB ^= (1 << 4); // toggling lub xeev tam sim no ntawm tus pin IN4. LED daim kab xev yog tua/tawm. _delay_ms (LOCK_INPUT_TIME); }}; rov qab (0); }

Kev ua haujlwm tiav Tom ntej no, tsim thiab suav cov phiaj xwm phiaj xwm rau hauv cov ntaub ntawv hex.

Kev xa cov ntaub ntawv HEX rau hauv lub microcontroller flash nco: ntaus hauv DOS qhov rai 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 qhov xwm txheej nws yog:

avrdude ISc ISPProgv1 mp m328p –u –U flash: w: PWM.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 burning: Microcontroller flash memory burning…

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

Kauj Ruam 6: Cov Hluav Taws Xob Hluav Taws Xob

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

Txuas cov khoom siv raws li daim duab qhia chaw.

Pom zoo: