Professional Documents
Culture Documents
NO USAR! DDRB = DDRB | B00000011; DDRB |= B00000011; Como DDB0 = 0, DDB1 = 1, DDB2 = 2, etc. DDRB = DDRB | ((1<<DDB0) | (1<<DDB1)); DDRB |= (1<<DDB0) | (1<<DDB1); SET PORTB1 y PORTB0 se setean. PORTB = B00000011; // !NO USAR! PORTB = PORTB | B00000011; PORTB |= B00000011; Como PORTB0 = 0, PORTB1 = 1, PORTB2 = 2, etc. PORTB = PORTB | ((1<<PORTB0) | (1<<PORTB1)); PORTB |= (1<<PORTB0) | (1<<PORTB1); SET PORTB1 y PORTB0 se limpian PORTB = B00000000; // !NO USAR! PORTB = PORTB & ~B00000011; PORTB &= ~B00000011; Como PORTB0 = 0, PORTB1 = 1, PORTB2 = 2, etc. PORTB = PORTB & ~ ((1<<PORTB0) | (1<<PORTB1)); PORTB &= ~ ((1<<PORTB0) | (1<<PORTB1)); CLEAR PORTB1 y PORTB0 se conmutan PORTB = PORTB ^ B00000011; PORTB ^= B00000011; Como PORTB0 = 0, PORTB1 = 1, PORTB2 = 2, etc. PORTB = PORTB ^ ((1<<PORTB0) | (1<<PORTB1)); PORTB ^= (1<<PORTB0) | (1<<PORTB1); TOGGLE
Verdad si PINB0 est en alto: if (PINB & (1<<PINB0)) { }
2
Verdad si PINB0 est en bajo: if ((PINB & (1<<PINB0))) { }
void setup() { // MCUCR &= ~(1<<PUD); // Se habilita PULLUP Global DDRD &= ~ (1<<DDD2); // PORTD2 entrada: DDD2=0 // PORTD |= (1<<PORTD2); // Se habilita PULLUP de PORTD2. (Se escribe 1 en PORTD2) DDRB |= (1<<DDB0) | (1<<DDB1); // PORTB0 y PORTB1 salidas: DDB0=1 y DDB1=1 SREG |= (1<<7); // Habilitador Global de Int. (I=1). Tb. sei(); EIMSK |= (1<<INT0); // Habilitador de Int. externa 0: INT0=1 // EICRA |= (1<<ISC01) | (1<<ISC00); // Evento en flaco de subida: RISING EICRA |= 1<<ISC01; // Evento en flaco de bajada: FALLING EICRA &= ~ (1<<ISC00); // Evento en flaco de bajada: FALLING } void loop(void) { PORTB ^= (1<<PORTB0); delay(1000); } ISR(INT0_vect) { PORTB ^= (1<<PORTB1); }
3
Sin las lneas comentadas: void setup() { DDRD &= ~ (1<<DDD2); // DDRB |= (1<<DDB0) | (1<<DDB1); SREG |= (1<<7); // EIMSK |= (1<<INT0); // EICRA |= 1<<ISC01; // EICRA &= ~ (1<<ISC00); // } void loop(void) { PORTB ^= (1<<PORTB0); delay(1000); } ISR(INT0_vect) { PORTB ^= (1<<PORTB1); }
PORTD2 entrada: DDD2=0 // PORTB0 y PORTB1 salidas: DDB0=1 y DDB1=1 Habilitador Global de Int. (I=1). Tb. sei(); Habilitador de Int. externa 0: INT0=1 Evento en flaco de bajada: FALLING Evento en flaco de bajada: FALLING
int sw = 2; // interrupcin externa 0 int led_loop = 8; int led_isr = 9; void setup() { pinMode(sw2,INPUT); pinMode(led_loop,OUTPUT); pinMode(led_isr,OUTPUT); attachInterrupt(0,isr,RISING); }
5
Hacemos una comparacin entre los bytes de memoria de programa usados con ambos mtodos:
1596 = (
2,08)
766
El mismo programa, pero la conmutacin se realiza escribiendo en PINB: El registro PINB sirve para leer el estado de un pin (ya sea que est configurado como entrada o como salida). Cuando se escribe un 1 se conmuta el pin correspondiente.
void setup() { DDRD &= ~ (1<<DDD2); // PORTD2 entrada: DDD2=0 DDRB |= (1<<DDB0) | (1<<DDB1); // PORTB0 y PORTB1 salidas: DDB0=1 y DDB1=1 SREG |= (1<<7); // Habilitador Global de Int. (I=1). Tb. sei(); EIMSK |= (1<<INT0); // Habilitador de Int. externa 0: INT0=1 EICRA |= 1<<ISC01; EICRA &= ~ (1<<ISC00); } void loop(void) { PINB |= (1<<PINB0); delay(1000); } ISR(INT0_vect) { PINB |= (1<<PINB1); }
// Conmuta PORTB0!
// Conmuta PORTB1!
6
#include <Bounce.h> #define led 8 // PORTB0 #define pulsador 2 // PORTD2 Bounce rebote_pulsador = Bounce(pulsador,20); void setup() { DDRB |= (1<<DDB0); DDRD &= ~ (1<<DDD2); MCUCR &= ~(1<<PUD); PORTD |= (1<<PORTD2); PORTB &= ~(1<<PORTB0); }
// // tiempo de 20 ms
// // // // //
PORTB0 salida: DDB0=1 PORTD2 entrada: DDD2=0 Se habilita PULLUP Global Se habilita PULLUP de PORTD2. (Se escribe 1 en PORTD2) Valor inicial de PORTB0=1
librera
BOUNCE
consulta
de
estado
de
la
entrada
con
#include <Bounce.h> #define led 8 // PORTB0 #define pulsador 2 // PORTD2 Bounce rebote_pulsador = Bounce(pulsador,20); void setup() { DDRB |= (1<<DDB0); DDRD &= ~ (1<<DDD2); MCUCR &= ~(1<<PUD); PORTD |= (1<<PORTD2); PORTB &= ~(1<<PORTB0); }
// // tiempo de 20 ms
// // // // //
PORTB0 salida: DDB0=1 PORTD2 entrada: DDD2=0 Se habilita PULLUP Global Se habilita PULLUP de PORTD2. (Se escribe 1 en PORTD2) Valor inicial de PORTB0=1
void loop() { if (rebote_pulsador.update() ) { if (rebote_pulsador.read() == LOW) { PINB |= (1<<PINB0); // Tambin: PORTB ^= (1<<PORTB0); } } }
7
#include <Bounce.h> #define led 8 // PORTB0 #define pulsador 2 // PORTD2 Bounce rebote_pulsador = Bounce(pulsador,20); void setup() { { DDRB |= (1<<DDB0); DDRD &= ~ (1<<DDD2); MCUCR &= ~(1<<PUD); PORTD |= (1<<PORTD2); PORTB &= ~(1<<PORTB0); }}
// // tiempo de 20 ms
// // // // //
PORTB0 salida: DDB0=1 PORTD2 entrada: DDD2=0 Se habilita PULLUP Global Se habilita PULLUP de PORTD2. (Se escribe 1 en PORTD2) Valor inicial de PORTB0=1
void loop() { if (rebote_pulsador.update()) { if (rebote_pulsador.read() == { if (PINB & (1<<PINB0)) // PORTB &= ~(1<<PORTB0); // if (!(PINB & (1<<PINB0)))// PORTB |= (1<<PORTB0); // } } }