Cov txheej txheem:

AVR Microcontroller. Toggle LED's Siv Push Push Switch. Push Button Debouncing: 4 Kauj Ruam
AVR Microcontroller. Toggle LED's Siv Push Push Switch. Push Button Debouncing: 4 Kauj Ruam

Video: AVR Microcontroller. Toggle LED's Siv Push Push Switch. Push Button Debouncing: 4 Kauj Ruam

Video: AVR Microcontroller. Toggle LED's Siv Push Push Switch. Push Button Debouncing: 4 Kauj Ruam
Video: AVR Microcontroller. Toggle LED’s using a Push Button Switch. Push Button Debouncing. 2024, Hlis ntuj nqeg
Anonim
Image
Image

Hauv ntu no, peb yuav kawm Yuav ua li cas txhawm rau program C code rau ATMega328PU txhawm rau hloov qhov xwm txheej ntawm peb tus LED raws li kev nkag los ntawm lub pob hloov. Tsis tas li, peb tau tshawb nrhiav cov kev daws teeb meem ntawm yog 'Hloov Bounce'. Raws li feem ntau, peb yuav sib sau ua ke cov hluav taws xob hluav taws xob ntawm lub hauv paus ntawm AVR ATmega328 txhawm rau txheeb xyuas qhov ua haujlwm ntawm txoj haujlwm program.

Kauj Ruam 1: Sau thiab Tsim AVR Microcontroller Application hauv C Code Siv Kev Txhim Kho Txhim Kho Platform Atmel Studio 7

Sau thiab Tsim AVR Microcontroller Application hauv C Code Siv Kev Txhim Kho Txhim Kho Platform Atmel Studio 7
Sau thiab Tsim AVR Microcontroller Application hauv C Code Siv Kev Txhim Kho Txhim Kho Platform Atmel Studio 7
Sau thiab Tsim AVR Microcontroller Application hauv C Code Siv Kev Txhim Kho Txhim Kho Platform Atmel Studio 7
Sau thiab Tsim AVR Microcontroller Application hauv C Code Siv Kev Txhim Kho Txhim Kho Platform Atmel Studio 7
Sau thiab Tsim AVR Microcontroller Application hauv C Code Siv Kev Txhim Kho Txhim Kho Platform Atmel Studio 7
Sau thiab Tsim AVR Microcontroller Application hauv C Code Siv Kev Txhim Kho Txhim Kho Platform Atmel Studio 7

Yog tias koj tsis muaj Atmel Studio, koj yuav tsum rub tawm thiab teeb tsa nws.

www.microchip.com/mplab/avr-support/atmel-studio-7

Thawj ob peb kab peb muaj qee tus compiler txhais.

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 // lub taub hau kom pab ua haujlwm qeeb hauv qhov haujlwm

Lub tsev qiv ntawv siv/ncua sijhawm.h muaj qee txoj hauv kev rau ncua qeeb. Cov haujlwm peb yuav siv, yog _delay_ms ().

Peb siv lub ntsiab lus tshaj tawm peb lub khawm thiab LED cov chaw nres nkoj thiab tus pin. Kev siv cov lus piav qhia zoo li qhov no tso cai rau peb tsuas yog yuav tsum hloov kho 3 kab ntawv yooj yim yog tias peb txav LED mus rau tus lej I/O sib txawv lossis siv AVR sib txawv.

#define BUTTON1 1 // khawm hloov txuas nrog chaw nres nkoj B tus pin 1

#define LED1 0 // Led1 txuas nrog chaw nres nkoj B tus pin 0 #define LED2 1 // Led2 txuas nrog rau chaw nres nkoj C pin 1 #define LED3 2 // Led3 txuas nrog rau chaw nres nkoj D pin 2

Qhov kawg ob txhais cov ntsiab lus teeb tsa lub sijhawm, hauv millisecond, kom debounce qhov hloov pauv thiab lub sijhawm tos ua ntej tso cai rau lwm tus nias lub pob. Lub sijhawm tshaj tawm yuav tsum tau hloov kho rau lub sijhawm nws yuav siv qhov hloov pauv mus los ntawm digital siab mus rau digital qis tom qab txhua qhov kev sib tw. Kev coj tus cwj pwm yuav txawv ntawm kev hloov mus rau qhov hloov pauv, tab sis 20-30 milliseconds feem ntau txaus.

#define DEBOUNCE_TIME 25 // sijhawm tos thaum "de-bouncing" khawm

#define LOCK_INPUT_TIME 300 // sijhawm tos tom qab nias lub pob

tsis muaj dab tsi init_ports_mcu ()

{

Lub luag haujlwm no yog hu ua ib zaug thaum pib ntawm peb txoj haujlwm los pib ua cov tswv yim tawm tswv yim uas peb yuav siv.

Txog lub pob, peb yuav siv PORT thiab tus lej PIN rau sau thiab nyeem. Nrog AVRs, peb nyeem tus pin siv nws tus lej PINx thiab peb sau rau tus pin siv nws PORTx sau npe. Peb yuav tsum tau sau rau lub pob npe sau npe kom pab rub tawm.

Rau LED peb tsuas yog xav siv PORT sau npe los sau, txawm li cas los xij, peb kuj xav tau cov ntaub ntawv qhia npe (DDR) raws li I/O pins tau teeb tsa raws li kev nkag los ntawm lub neej ntawd.

Ua ntej, peb tab tom teeb tsa LED's I/O pins raws li qhov tsim tawm siv nws cov ntaub ntawv qhia npe.

DDRB = 0xFFu; // Teeb txhua tus pin ntawm PORTB raws li tso tawm.

Tom ntej no, qhia meej meej teeb tsa tus pin tus pin raws li kev nkag.

DDRB & = ~ (1 <

Tom ntej no, PORTB pins tau teeb tsa siab (+5 volt) kom qhib nws. Cov pins tso tawm yog qhov pib siab, thiab txij li thaum peb LED tau txuas ua haujlwm-siab, nws yuav raug qhib tshwj tsis yog peb qhia meej nws tua.

Thiab thaum kawg, peb ua kom lub zog rub sab hauv sab hauv ntawm tus pin nkag peb siv rau peb lub khawm. Qhov no ua tiav yooj yim los ntawm kev xa tawm ib qho mus rau chaw nres nkoj. Thaum teeb tsa ua lub tswv yim, ua li ntawd ua rau muaj peev xwm rub tawm thiab thaum teeb tsa ua qhov tso tawm, ua li ntawd tsuas yog tso tawm qhov hluav taws xob siab.

PORTB = 0xFF; // Teeb txhua tus pin ntawm PORTB li siab. Led yog tig rau, // kuj tseem sab hauv Rub Up resistor ntawm thawj tus pin PORTB tau pab. DDRC = 0xFFu; // Teeb txhua tus pin ntawm PORTC li tso tawm. PORTC = 0x00u; // Teeb txhua tus pin ntawm PORTC qis uas tig nws tawm. DDRD = 0xFFu; // Teeb txhua tus pin ntawm PORTD li tso tawm. PORTD = 0x00u; // Teeb txhua tus pin ntawm PORTD qis uas tig nws tawm. }

unsigned char button_state ()

{

Txoj haujlwm no rov ua tus nqi boolean qhia tias lub pob raug nias lossis tsis raug. Qhov no yog qhov thaiv ntawm cov cai nrog txuas ntxiv ua tiav nyob rau hauv lub voj infinate thiab yog li yog pov npav lub xeev ntawm lub pob. Qhov no tseem yog qhov uas peb debounce qhov hloov pauv.

Tam sim no, nco ntsoov tias thaum peb nias qhov hloov pauv, tus pin nkag nkag tau rub mus rau hauv av. Yog li, peb tab tom tos tus pin mus qis.

/ * lub pob nyem thaum BUTTON1 ntsis meej */

yog (! (PINB & (1 <

Peb ua li ntawd los ntawm kev tshuaj xyuas yog tias qhov me ntsis pom tseeb. Yog tias qhov me ntsis pom tseeb, qhia tias lub pob raug kev nyuaj siab, peb xub ncua sijhawm rau lub sijhawm tau teev tseg los ntawm DEBOUNCE_TIME uas yog 25ms thiab tom qab ntawd xyuas lub xeev lub pob dua. Yog tias lub pob raug kev nyuaj siab tom qab 25ms ces qhov hloov pauv tau txiav txim siab los tawm tsam thiab npaj ua kom muaj kev tshwm sim thiab yog li peb rov qab 1 rau peb qhov kev hu xov tooj. Yog tias lub pob tsis nyuaj siab, peb rov qab 0 rau peb qhov kev hu xov tooj.

_delay_ms (DEBOUNCE_TIME);

yog (! (PINB & (1 <

int main (tsis muaj dab tsi)

{

Peb niaj hnub ua. Lub luag haujlwm tseem ceeb yog qhov tshwj xeeb thiab sib cais los ntawm txhua lwm txoj haujlwm. Txhua qhov program C yuav tsum muaj ib lub ntsiab lus () muaj nuj nqi. qhov tseem ceeb yog qhov AVR pib ua tiav koj cov cai thaum lub zog pib ua ntej, yog li nws yog qhov nkag ntawm qhov haujlwm.

unsigned char n_led = 1; // pib tus lej LED yog tam sim no

Hu ntawm txoj haujlwm los pib I/O pins tau siv:

init_ports_mcu ();

infinite loop qhov twg peb qhov program khiav:

thaum (1)

{

Thaum button_state rov qab ib qho qhia tias lub pob raug nias thiab nthuav tawm, tom qab ntawd hloov pauv qhov xwm txheej tam sim no ntawm LED's nyob rau hauv lem raws li n_led qhov ntsuas.

yog (button_state ()) // Yog tias lub pob nyem, toggle LED lub xeev thiab ncua rau 300ms (#define LOCK_INPUT_TIME)

{hloov (n_led) {rooj plaub 1: PORTB ^= (1 << LED1); PORTC ^= (1 << LED2); tawg;

Cov nqe lus no siv C bitwise operator. Lub sijhawm no nws siv tshwj xeeb LOS tus neeg teb xov tooj. Thaum koj XOR PORT nrog tus nqi me ntsis ntawm qhov koj xav kom hloov pauv, qhov ntawd hloov pauv me ntsis yam tsis ua rau lwm qhov me me.

rooj plaub 2:

PORTC ^= (1 << LED2); PORTD ^= (1 << LED3); tawg; rooj plaub 3: PORTD ^= (1 << LED3); PORTB ^= (1 << LED1); n_led = 0; // rov pib dua LED tus lej tawg; } n_led ++; // tom ntej LED tau qhib _delay_ms (LOCK_INPUT_TIME); }} rov qab (0); }

Yog li tam sim no, thaum koj ua qhov haujlwm no, koj yuav tsum muaj peev xwm nias lub pob-khawm mus rau LED's yog toggling. Vim yog peb ncua sijhawm tau teev tseg los ntawm LOCK_INPUT_TIME, koj tuaj yeem nias thiab tuav lub pob uas yuav ua rau LED tig mus thiab qhib ntawm qhov sib xws (me ntsis ntau dua txhua 275ms).

Kev ua haujlwm tiav

Cov kauj ruam tom ntej yog tsim cov phiaj xwm thiab cov phiaj xwm hex cov ntaub ntawv rau hauv microcontroller siv avrdude program.

Koj tuaj yeem rub tawm cov ntaub ntawv main.c nrog program hauv c code:

Kauj Ruam 2: Hloov HEX Cov Ntaub Ntawv ntawm Qhov Kev Pabcuam Rau Hauv Flash Nco ntawm Chip

Hloov HEX Cov Ntaub Ntawv ntawm Txoj Haujlwm Rau Hauv Flash Nco ntawm Chip
Hloov HEX Cov Ntaub Ntawv ntawm Txoj Haujlwm Rau Hauv Flash Nco ntawm Chip
Hloov HEX Cov Ntaub Ntawv ntawm Txoj Haujlwm Rau Hauv Flash Nco ntawm Chip
Hloov HEX Cov Ntaub Ntawv ntawm Txoj Haujlwm Rau Hauv Flash Nco ntawm Chip

Rub tawm thiab teeb tsa AVRDUDE. Qhov hloov tshiab kawg muaj yog 6.3: Rub tawm cov ntawv zip

Ua ntej, theej hex file of program in to AVRDUDE directory. Hauv kuv qhov xwm txheej nws yog ButtonAVR.hex

Tom qab ntawd, ntaus hauv DOS qhov rai qhib qhov hais kom ua: avrdude –c [lub npe tus programmer] –p m328p –u –U flash: w: [lub npe ntawm koj cov ntaub ntawv hex].

Hauv kuv qhov xwm txheej nws yog: avrdude –c ISPProgv1 mp m328p –u –U flash: w: ButtonAVR.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 3: Kho vajtse Hloov Debouncing

Kho vajtse Hloov Debouncing
Kho vajtse Hloov Debouncing

Ntxiv rau Software hloov debouncing peb tuaj yeem siv kho vajtse hloov debouncing txheej txheem. Lub tswv yim yooj yim tom qab cov txheej txheem no yog siv lub capacitor los lim tawm cov kev hloov pauv sai hauv lub teeb liab.

Dab tsi tus nqi capacitor yuav tsum xaiv? Qhov no yuav kawg nyob ntawm seb lub khawm ua tsis zoo txog qhov teeb meem tshwj xeeb no li cas. Qee lub khawm tuaj yeem tso tawm tus cwj pwm zoo heev, tab sis lwm tus yuav muaj tsawg heev. Tus nqi hluav taws xob qis zoo li 1.0 nanofarads yuav hnov mob sai heev, nrog rau me me lossis tsis muaj kev cuam tshuam rau ntawm kev thawb. Hloov pauv, tus nqi hluav taws xob siab dua xws li 220 nanofarads (uas tseem zoo nkauj me me ntawm cov capacitors) yuav muab kev hloov pauv qeeb los ntawm qhov pib mus rau qhov xaus qhov hluav taws xob (5 volt txog 0 volt). Kev hloov pauv pom nrog 220 nanofarads muaj peev xwm tseem zoo nkauj nrawm hauv qhov kev nkag siab tiag tiag txawm li cas los xij, thiab yog li tuaj yeem siv ntawm cov khawm ua tsis tau zoo.

Kauj Ruam 4: Hluav Taws Xob Hluav Taws Xob

Hluav Taws Xob Hluav Taws Xob
Hluav Taws Xob Hluav Taws Xob
Hluav Taws Xob Hluav Taws Xob
Hluav Taws Xob Hluav Taws Xob
Hluav Taws Xob Hluav Taws Xob
Hluav Taws Xob Hluav Taws Xob

Txuas cov khoom siv raws li daim duab qhia chaw.

Pom zoo: