/*
* David Pello 2010
* Ladecadence.net
* This code is under the GNU GPL license.
*
*/
#include <inttypes.h>
#include <avr/io.h>
#define F_CPU 8000000UL // Internal 8 MHz
#include <util/delay.h>
#define RED_PIN 0
#define GREEN_PIN 1
#define BLUE_PIN 2
#define PPU_CTRL 3
#define CIC_CTRL 4
#define RST_IN 5
#define RED_ON PORTB |= (1<<RED_PIN)
#define RED_OFF PORTB &= ~(1<<RED_PIN)
#define GREEN_ON PORTB |= (1<<GREEN_PIN)
#define GREEN_OFF PORTB &= ~(1<<GREEN_PIN)
#define BLUE_ON PORTB |= (1<<BLUE_PIN)
#define BLUE_OFF PORTB &= ~(1<<BLUE_PIN)
#define COLOR_RED RED_ON; GREEN_OFF; BLUE_OFF;
#define COLOR_GREEN RED_OFF; GREEN_ON; BLUE_OFF;
#define COLOR_BLUE RED_OFF; GREEN_OFF; BLUE_ON;
#define COLOR_PINK RED_ON; GREEN_OFF; BLUE_ON;
#define PPU_PAL PORTB |= (1<<PPU_CTRL) // pines PALMODE de la PPU a 1
#define PPU_NTSC PORTB &= ~(1<<PPU_CTRL) // pines PALMODE de la PPU a 0
#define CIC_ON PORTB |= (1<<CIC_CTRL) // pin 4 del CIC a 5V
#define CIC_OFF PORTB &= ~(1<<CIC_CTRL) // pin 4 del CIC a 0V
#define PAL_CIC_ON 0
#define NTSC_CIC_ON 1
#define PAL_CIC_OFF 2
#define NTSC_CIC_OFF 3
// guarda el estado actual
uint8_t estado_actual;
uint8_t pulsado;
// mira si reset está pulsado
uint8_t comprueba_reset() {
uint8_t v1, v2;
v1 = (PINB & 0x20) >> 5; // (PINB & b00100000) >> 5
_delay_ms(10); //debounce
v2 = (PINB & 0x20) >> 5;
return (v1 && v2);
}
void cambia_estado() {
estado_actual++;
if (estado_actual>3)
estado_actual=0;
switch (estado_actual) {
case PAL_CIC_ON:
PPU_PAL;
CIC_ON;
COLOR_RED;
break;
case NTSC_CIC_ON:
PPU_NTSC;
CIC_ON;
COLOR_GREEN;
break;
case PAL_CIC_OFF:
PPU_PAL;
CIC_OFF;
COLOR_BLUE;
break;
case NTSC_CIC_OFF:
PPU_NTSC;
CIC_OFF;
COLOR_PINK;
break;
}
}
int main(void) {
// configuramos el puerto
DDRB = 0xDF; // b11011111
// modo por defecto
estado_actual = PAL_CIC_ON;
PPU_PAL;
COLOR_RED;
pulsado = 0;
for(;;) {
if (comprueba_reset() && !pulsado) {
pulsado = 1;
_delay_ms(2000);
}
else {
pulsado = 0;
_delay_ms(100);
}
if (comprueba_reset() && pulsado) {
cambia_estado();
pulsado = 0;
_delay_ms(100);
}
}
return 0;
}