Guest

Untitled

By: a guest on Feb 20th, 2010  |  syntax: C  |  size: 2.40 KB  |  hits: 619  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. /*
  2.  * David Pello 2010
  3.  * Ladecadence.net
  4.  * This code is under the GNU GPL license.
  5.  *
  6.  */
  7.  
  8. #include <inttypes.h>
  9. #include <avr/io.h>
  10.  
  11. #define F_CPU 8000000UL  // Internal 8 MHz
  12. #include <util/delay.h>
  13.  
  14. #define RED_PIN         0
  15. #define GREEN_PIN       1
  16. #define BLUE_PIN        2
  17.  
  18. #define PPU_CTRL        3
  19. #define CIC_CTRL        4
  20.  
  21. #define RST_IN          5
  22.  
  23. #define RED_ON          PORTB |= (1<<RED_PIN)
  24. #define RED_OFF         PORTB &= ~(1<<RED_PIN)
  25. #define GREEN_ON    PORTB |= (1<<GREEN_PIN)
  26. #define GREEN_OFF   PORTB &= ~(1<<GREEN_PIN)
  27. #define BLUE_ON     PORTB |= (1<<BLUE_PIN)
  28. #define BLUE_OFF    PORTB &= ~(1<<BLUE_PIN)
  29.  
  30. #define COLOR_RED       RED_ON; GREEN_OFF; BLUE_OFF;
  31. #define COLOR_GREEN     RED_OFF; GREEN_ON; BLUE_OFF;
  32. #define COLOR_BLUE      RED_OFF; GREEN_OFF; BLUE_ON;
  33. #define COLOR_PINK      RED_ON; GREEN_OFF; BLUE_ON;
  34.  
  35. #define PPU_PAL         PORTB |= (1<<PPU_CTRL)          // pines PALMODE de la PPU a 1
  36. #define PPU_NTSC    PORTB &= ~(1<<PPU_CTRL)             // pines PALMODE de la PPU a 0
  37.  
  38. #define CIC_ON          PORTB |= (1<<CIC_CTRL)          // pin 4 del CIC a 5V
  39. #define CIC_OFF         PORTB &= ~(1<<CIC_CTRL)         // pin 4 del CIC a 0V
  40.  
  41. #define PAL_CIC_ON              0
  42. #define NTSC_CIC_ON             1
  43. #define PAL_CIC_OFF             2
  44. #define NTSC_CIC_OFF    3
  45.  
  46. // guarda el estado actual
  47. uint8_t estado_actual;
  48.  
  49. uint8_t pulsado;
  50.  
  51. // mira si reset está pulsado
  52. uint8_t comprueba_reset() {
  53.                 uint8_t v1, v2;
  54.                 v1 = (PINB & 0x20) >> 5; // (PINB & b00100000) >> 5
  55.  
  56.                 _delay_ms(10);                  //debounce
  57.                
  58.                 v2 = (PINB & 0x20) >> 5;
  59.                
  60.                 return (v1 && v2);
  61. }
  62.  
  63. void cambia_estado() {
  64.                 estado_actual++;
  65.                 if (estado_actual>3)
  66.                                 estado_actual=0;
  67.  
  68.                 switch (estado_actual) {
  69.                                 case PAL_CIC_ON:
  70.                                                 PPU_PAL;
  71.                                                 CIC_ON;
  72.                                                 COLOR_RED;
  73.                                                 break;
  74.                                 case NTSC_CIC_ON:
  75.                                                 PPU_NTSC;
  76.                                                 CIC_ON;
  77.                                                 COLOR_GREEN;
  78.                                                 break;
  79.                                 case PAL_CIC_OFF:
  80.                                                 PPU_PAL;
  81.                                                 CIC_OFF;
  82.                                                 COLOR_BLUE;
  83.                                                 break;
  84.                                 case NTSC_CIC_OFF:
  85.                                                 PPU_NTSC;
  86.                                                 CIC_OFF;
  87.                                                 COLOR_PINK;
  88.                                                 break;
  89.                 }
  90.  
  91. }
  92.  
  93. int main(void) {
  94.                 // configuramos el puerto
  95.                 DDRB = 0xDF;    // b11011111
  96.  
  97.                 // modo por defecto
  98.                 estado_actual = PAL_CIC_ON;
  99.  
  100.                 PPU_PAL;
  101.                 COLOR_RED;
  102.  
  103.                 pulsado = 0;
  104.                 for(;;) {
  105.                                 if (comprueba_reset() && !pulsado) {
  106.                                                 pulsado = 1;
  107.                                                 _delay_ms(2000);
  108.                                 }
  109.                                 else {
  110.                                                 pulsado = 0;
  111.                                                 _delay_ms(100);
  112.                                 }
  113.  
  114.                                 if (comprueba_reset() && pulsado) {
  115.                                                 cambia_estado();
  116.                                                 pulsado = 0;
  117.                                                 _delay_ms(100);
  118.                                 }
  119.  
  120.                 }
  121.  
  122.                 return 0;
  123. }