We Recommend

Mastering Regular Expressions Mastering Regular Expressions
As this book shows, a command of regular expressions is an invaluable skill. Regular expressions allow you to code complex and subtle text processing that you never imagined could be automated. Regular expressions can save you time and aggravation. They can be used to craft elegant solutions to a wide range of problems. Once you've mastered regular expressions, they'll become an invaluable part of your toolkit. You will wonder how you ever got by without them.


Posted By

ausrobotics on 03/29/09


Tagged

day heartbeat ausrobotics valentines


Versions (?)


Heartbeat


Published in: C 


The code for my heartbeat electronic sculpture.

Built using AVR Studio 4 for the ATiny8.

  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3.  
  4. // Some macros that make the code more readable
  5. #define output_low(port,pin) port &= ~(1<<pin)
  6. #define output_high(port,pin) port |= (1<<pin)
  7. #define set_input(portdir,pin) portdir &= ~(1<<pin)
  8. #define set_output(portdir,pin) portdir |= (1<<pin)
  9.  
  10. #define LON 1
  11. #define LOF 0
  12.  
  13. #define LED_COUNT 12
  14.  
  15. uint8_t led_table[] =
  16. {
  17. LOF, PB0, PB1,
  18. LOF, PB1, PB0,
  19. LOF, PB0, PB2,
  20. LOF, PB2, PB0,
  21. LOF, PB0, PB4,
  22. LOF, PB4, PB0,
  23. LOF, PB1, PB2,
  24. LOF, PB2, PB1,
  25. LOF, PB1, PB4,
  26. LOF, PB4, PB1,
  27. LOF, PB2, PB4,
  28. LOF, PB4, PB2
  29. };
  30.  
  31. void clear_leds()
  32. {
  33. set_input(DDRB, PB0);
  34. set_input(DDRB, PB1);
  35. set_input(DDRB, PB2);
  36. set_input(DDRB, PB4);
  37. }
  38.  
  39. void set_led(uint8_t index, uint8_t state)
  40. {
  41. uint8_t i = (index * 3);
  42.  
  43. uint8_t s = led_table[i];
  44. uint8_t p1 = led_table[i + 1];
  45. uint8_t p2 = led_table[i + 2];
  46.  
  47. set_output(DDRB, p1);
  48. set_output(DDRB, p2);
  49.  
  50. if(state == LON)
  51. {
  52. output_low(PORTB, p1);
  53. output_high(PORTB, p2);
  54. }
  55. else
  56. {
  57. output_low(PORTB, p1);
  58. output_low(PORTB, p2);
  59. }
  60. }
  61.  
  62. void delay_10us(uint8_t us)
  63. {
  64. uint8_t delay_count = F_CPU / 2000000;
  65. volatile uint8_t i;
  66.  
  67. while (us != 0) {
  68. for (i=0; i != delay_count; i++);
  69. us--;
  70. }
  71. }
  72.  
  73. void do_pwm_fade(uint8_t index, uint8_t start_duty, uint8_t end_duty, uint8_t rate)
  74. {
  75. uint8_t duty;
  76. uint8_t j;
  77.  
  78. duty = start_duty;
  79. while (duty != end_duty)
  80. {
  81. for (j = 0; j < rate; j++)
  82. {
  83. set_led(index, LON);
  84. delay_10us(duty);
  85. set_led(index, LOF);
  86. delay_10us(255-duty);
  87. }
  88.  
  89. if (start_duty < end_duty)
  90. duty++;
  91. else
  92. duty--;
  93. }
  94. }
  95.  
  96. int main(void)
  97. {
  98. // initialize the direction of the B port to be outputs
  99. // on the 3 pins that have LEDs connected
  100.  
  101. while(1)
  102. {
  103. for(int i=0; i < LED_COUNT; i++)
  104. {
  105. clear_leds();
  106. do_pwm_fade(i, 0, 255, 1);
  107. do_pwm_fade(i, 255, 0, 1);
  108. }
  109. }
  110. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: aisheteru_143 on June 2, 2009

include

include

// Some macros that make the code more readable

define output_low(port,pin) port &= ~(1

Posted By: aisheteru_143 on June 2, 2009

include

include

// Some macros that make the code more readable

define output_low(port,pin) port &= ~(1

You need to login to post a comment.