Advertisement
JustinCase2020

Simple K2000

Jan 21st, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. // LED sur pins 0 à 7
  2. // Switch sur pin 8 (Pin8 -> Switch -> Ground)
  3.  
  4. // Le premier nombre est le nombre de frames dans l'animation. 20 frames dans PatternK2000, 10 dans PatternXYZ.
  5. const uint8_t PatternK2000[] PROGMEM  = { 20, 0x01, 0x03, 0x07, 0x0F, 0x1E, 0x3C, 0x78, 0xF0, 0xE0, 0xC0,
  6.                                             0x80, 0xC0, 0xE0, 0xF0, 0x78, 0x3C, 0x1E, 0x0F, 0x07, 0x03 };
  7.                                            
  8. const uint8_t PatternXYZ[] PROGMEM    = { 10, 0x18, 0x3C, 0x66, 0xC3, 0x81, 0x81, 0xC3, 0x66, 0x3C, 0x18 };
  9.  
  10. // Ajouter le nom des Patterns ici et ajuster 'PatternCount' en conséquence.
  11. const uint8_t* const Patterns[] PROGMEM = { PatternK2000, PatternXYZ };
  12. const uint8_t PatternCount              = 2;
  13.  
  14. uint16_t FrameDelay         = 4000;
  15. uint8_t FrameIndex          = 1;
  16. uint8_t* CurrentPattern     = 0;
  17. uint8_t CurrentPatternIndex = 0;
  18.  
  19.  
  20. ISR (TIMER1_COMPA_vect)
  21. {
  22.     OCR1A += FrameDelay;
  23.     PORTD = pgm_read_byte(&CurrentPattern[FrameIndex++]);
  24.     if (FrameIndex > pgm_read_byte(&CurrentPattern[0]))
  25.         FrameIndex = 1;
  26. }
  27.  
  28. ISR (PCINT0_vect)
  29. {
  30.     if ((PINB & 0x01) == 0) // Switch pressed
  31.     {
  32.         CurrentPatternIndex++;
  33.         if (CurrentPatternIndex >= PatternCount)
  34.             CurrentPatternIndex = 0;
  35.  
  36.         CurrentPattern = (uint8_t*)pgm_read_word(&(Patterns[CurrentPatternIndex]));
  37.         FrameIndex = 1;
  38.     }
  39. }
  40.  
  41. void setup()
  42. {
  43.     DDRD = 0xFF;    // Port D0 à D7 en Output (pin 0 à 7)
  44.     PORTD = 0x00;
  45.    
  46.     // TIMER1
  47.     TCCR1A = 0x00;
  48.     TCCR1B = 0x04;
  49.     TIMSK1 = 0x02; // OCR1A Interrupt enabled
  50.  
  51.     // Switch
  52.     PORTB |= 0x01; // Activer pull-up (pin déjà en Input au Power-Up)
  53.     PCICR  = 0x01; // Pin Change Interrupt 0 enabled
  54.     PCMSK0 = 0x01; // B0 interrupt enabled (pin 8)
  55.  
  56.     CurrentPattern = (uint8_t*)pgm_read_word(&(Patterns[CurrentPatternIndex]));
  57.  
  58.     sei();          // Enable interrupts
  59. }
  60.  
  61. void loop()
  62. {
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement