Advertisement
grist

UV Heartbeat

Dec 27th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. /*
  2.  * LED_Flash.c
  3.  *
  4.  * Created: 12/26/2012 9:21:09 PM
  5.  *  Author: grist.carrigafoyl
  6.  
  7.  
  8.  Simple program to emulate a heartbeat using LEDs.
  9.  PWM output on PB0 (pin 5)
  10.  Input for mode switching on PB3 (pin 2)
  11.  
  12.  
  13.  */
  14.  
  15. #define F_CPU 8000000UL  //  8Mhz
  16.  
  17. #include <avr/io.h>
  18. #include <avr/interrupt.h>
  19. #include <util/delay.h>
  20.  
  21. // Prototypes
  22. int main();
  23. void fullOn();
  24. void heartBeat();
  25. void beatOne();
  26. void beatTwo();
  27.  
  28. int main(void)
  29. {
  30.    
  31.     char button_state;
  32.     // Set PortB to output, except our switch pin (PB3)
  33.     DDRB = 0b11110111;
  34.     // Turn on the pullup resistor
  35.     PORTB = _BV(PB3);
  36.     // Initialise PWM
  37.     TCCR0A = 0b10000011; // Fast PWM 8 bit
  38.     TCCR0B = 0b00000001; // No prescaler
  39.     TCNT0 = 0;  // Reset TCNT0
  40.  
  41.    
  42.     while(1) {
  43.         //fadeUpAndDown(5);
  44.         //fullOn();
  45.         button_state = ~PINB & _BV(PB3);
  46.         if (button_state == 0) {
  47.             heartBeat();       
  48.         } else {
  49.             fullOn();
  50.         }      
  51.     }
  52. }
  53.  
  54.  
  55. void fullOn()
  56. {
  57.     int ms_delay_time = 250;
  58.     OCR0A = 255;
  59.     _delay_ms(ms_delay_time);
  60. }
  61.  
  62. void heartBeat()
  63. {  // Run through a single heartbeat
  64.     // Numbers obtained from http://pastebin.com/FbjcsQM9
  65.  
  66.     int beatOneGap = 150; // ms
  67.     int beatTwoGap = 546; // ms
  68.    
  69.     // Play it
  70.     beatOne();
  71.     _delay_ms(beatOneGap);
  72.     beatTwo();
  73.     _delay_ms(beatTwoGap);
  74.  
  75. }
  76.  
  77. void beatOne()
  78. { // Split these as having both data sets in the same function was hitting memory problems
  79.     char dataOne[] = {
  80.         9, 18, 28, 37, 46, 48,
  81.         51, 55, 60, 64, 85, 107, 95, 83, 71, 102, 133, 163, 194, 224, 255, 255,
  82.         255, 255, 255, 255, 255, 255, 233, 211, 189, 150, 112, 74, 36, 41, 46, 51,
  83.         56, 71, 87, 102, 117, 133, 129, 126, 123, 120, 116, 113, 110, 107, 103, 100,
  84.         97, 92, 87, 82, 77, 71, 66, 61, 56, 51, 51, 51, 51, 51, 51, 48,
  85.         46, 43, 41, 38, 36, 33, 31, 28, 26, 24, 22, 19, 17, 15, 13, 13,
  86.         13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 10, 6, 3, 0, 2,
  87.         4, 7, 9, 11, 13, 16, 18
  88.        
  89.     };
  90.     for (int i=0;i<sizeof(dataOne);i++) {
  91.         OCR0A = dataOne[i];
  92.         _delay_ms(1);
  93.     }
  94. }
  95.  
  96. void beatTwo()
  97. { // Split these as having both data sets in the same function was hitting memory problems
  98.         char dataTwo[] = {
  99.         13, 19, 26,
  100.         32, 38, 64, 89, 115, 143, 171, 199, 213, 227, 241, 255, 255, 255, 255, 255,
  101.         255, 255, 214, 173, 133, 92, 51, 49, 48, 47, 45, 43, 36, 28, 20, 54,
  102.         87, 120, 153, 166, 181, 194, 214, 235, 255, 255, 255, 255, 255, 255, 255, 217,
  103.         179, 140, 124, 108, 92, 77, 69, 61, 54, 46, 40, 33, 27, 20, 18, 13,
  104.         14, 12, 10, 8, 6, 4, 2, 0, 2, 4, 6, 8, 10, 12, 14, 16,
  105.         18, 20, 20, 20, 20, 19, 19, 19, 18, 18, 18, 18, 17, 17, 17, 16,
  106.         16, 16, 16, 15, 15, 15, 14, 14, 13, 13, 13, 12, 12, 11, 11, 11,
  107.         10, 10, 10, 9, 9, 9, 9, 8, 8, 8, 8, 7, 7, 7, 6, 6,
  108.         6, 6, 5, 5, 5, 5, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2,
  109.         1, 1, 1
  110.     };
  111.     for (int i=0;i<sizeof(dataTwo);i++) {
  112.         OCR0A = dataTwo[i];
  113.         _delay_ms(1);
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement