daily pastebin goal
34%
SHARE
TWEET

Prop Circuit for Attiny45

dlwestab Mar 21st, 2011 360 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Copyright 2011 Dustin L. Westaby
  3.  
  4. ----------------------------------------------------------------------
  5.     Prop Circuit for Attiny45
  6. ----------------------------------------------------------------------
  7. Title:      cortana.c
  8. Author:     Dustin Westaby
  9. Date Created:   2/13/11
  10. Last Modified:  3/21/11
  11. Purpose:  Animated output to LEDs
  12.  
  13. Circuit Schematic: http://www.flickr.com/photos/dwest2/5448858395/
  14.  
  15. Compiled with AVR-GCC WinAVR
  16.  
  17. Revisions List:
  18. 2/13/11 Initial Draft and test circuit
  19. 2/14/11 Updated Comments, re-arranged structure
  20. 3/21/11 Added link to circuit schematic
  21.  
  22. ----------------------------------------------------------------------
  23.     Fuses:
  24. ----------------------------------------------------------------------
  25.  BrownOut Disabled
  26.  CKDIV8 (leave default if set)
  27.  Int RC Osc 8Mhz + 64ms
  28.  
  29. ----------------------------------------------------------------------
  30.     Inputs:
  31. ----------------------------------------------------------------------
  32.  Power up
  33.  
  34. ----------------------------------------------------------------------
  35.     Ouputs:
  36. ----------------------------------------------------------------------
  37.  pin    label   connections
  38. ----------------------------------------------------------------------
  39.   2     PB3 =   LED
  40.   3     PB4 =   LED
  41.   5     PB0 =   LED
  42.   6     PB1 =   LED
  43.   7     PB2 =   LED
  44.  
  45. ----------------------------------------------------------------------
  46.     Notes:
  47. ----------------------------------------------------------------------
  48.  -Program is free to modify, but please keep my name on the author list.
  49.  
  50. */
  51.  
  52. //--------------------------------------
  53. //          Global Variables           |
  54. //--------------------------------------
  55. // 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
  56. // One CPU Cycle = 1us
  57. #define F_CPU 8000000UL/8
  58.  
  59. // Enumerate up down for use in the fade loops
  60. enum { up, down };
  61.  
  62. //--------------------------------------
  63. //              Includes               |
  64. //--------------------------------------
  65. #include <avr/io.h>
  66. #include <util/delay.h>
  67. //#include <avr/sleep.h>
  68. #include <inttypes.h>
  69.  
  70.  
  71. //--------------------------------------
  72. //          Delay Subroutines          |
  73. //--------------------------------------
  74. //These functions are from the delay.h include, the calls to delay functions
  75. //are re-written here to allow for longer waits.
  76. void delay_ms(uint16_t ms) {
  77.   while ( ms )
  78.   {
  79.     _delay_ms(1);
  80.     ms--;
  81.   }
  82. }
  83.  
  84. void delay_us(uint16_t us) {
  85.   while ( us )
  86.   {
  87.     _delay_us(1);
  88.     us--;
  89.   }
  90. }
  91.  
  92. //--------------------------------------
  93. //               Main                  |
  94. //--------------------------------------
  95. int main (void)
  96. {
  97.  
  98.   /* ---------------------------------------------------------------- */
  99.   /*                            Initialization                        */
  100.   /* ---------------------------------------------------------------- */
  101.   //Variables used for animations
  102.   int i, count_delay, direction;
  103.   int time_on, time_off, max_value,  min_value, rate_of_change;
  104.  
  105.   //Initialize Port B is an output
  106.   DDRB =  0b00011111;
  107.  
  108.   /* ---------------------------------------------------------------- */
  109.   /*                Spin Circle Four Times (approx 1s)                */
  110.   /* ---------------------------------------------------------------- */
  111.   i=0;
  112.   count_delay=50;             //Delay between circle movement is 50ms
  113.   for (i = 0; i < 4; i++)
  114.   {
  115.     PORTB = 0b00000001;       //Turn ON only LED 0 for defined delay (50ms)
  116.     delay_ms(count_delay);
  117.     PORTB = 0b00000010;       //Turn ON only LED 1 for defined delay (50ms)
  118.     delay_ms(count_delay);
  119.     PORTB = 0b00000100;       //Turn ON only LED 2 for defined delay (50ms)
  120.     delay_ms(count_delay);
  121.     PORTB = 0b00001000;       //Turn ON only LED 3 for defined delay (50ms)
  122.     delay_ms(count_delay);
  123.     PORTB = 0b00010000;       //Turn ON only LED 4 for defined delay (50ms)
  124.     delay_ms(count_delay);
  125.   }
  126.  
  127.   /* ---------------------------------------------------------------- */
  128.   /*                     All ON Long (0.8 seconds)                    */
  129.   /* ---------------------------------------------------------------- */
  130.   PORTB = 0b00011111;         //Turn ON all LEDs 01234 for 800ms
  131.   delay_ms(800);
  132.  
  133.   /* ---------------------------------------------------------------- */
  134.   /*                         Blink Blink                              */
  135.   /* ---------------------------------------------------------------- */
  136.   PORTB = 0b00000000;         //Turn OFF all LEDS 01234 for 100ms
  137.   delay_ms(100);
  138.   PORTB = 0b00011111;         //Turn ON  all LEDs 01234 for 10ms
  139.   delay_ms(10);
  140.   PORTB = 0b00000000;         //Turn OFF all LEDs 01234 for 100ms
  141.   delay_ms(100);
  142.   PORTB = 0b00011111;         //Turn ON  all LEDs 01234 for 100ms
  143.   delay_ms(100);
  144.  
  145.   /* ---------------------------------------------------------------- */
  146.   /*              Fade In and Out Continuous (Software PWM)           */
  147.   /* ---------------------------------------------------------------- */
  148.   max_value=500;              //Max of 500us for LEDs ON
  149.   min_value=5;                //Max of   5us for LEDs OFF
  150.   time_on=min_value;          //Set Starting Time ON  to 5us
  151.   time_off=max_value;         //Set Starting Time OFF to 500us
  152.   rate_of_change=2;           //This is the speed that the fade goes between min and max
  153.   direction=down;             //Direction is defined for the direction of the fade (in or out)
  154.  
  155.   /* Note: The direction is backwards on purpose, causes the light animation to
  156.            look like it hiccuped for the first fade cycle before reaching a steady fade. */
  157.  
  158.   while(1)
  159.   {
  160.         //The ratio of ON time and OFF time determines the brightness of the LEDs (DUTY CYLE)
  161.         //By continuously varying the ON time versus the OFF times we get a fade effect.
  162.     PORTB = 0b00011111;       //Turn ON  all LEDs 01234
  163.     delay_us(time_on);
  164.     PORTB = 0b00000000;       //Turn OFF all LEDs 01234
  165.     delay_us(time_off);
  166.  
  167.     if (direction==up)
  168.     {
  169.         //In the UP direction the Time ON increases while the Time OFF decreases.
  170.         //The result is that the LEDs get brighter
  171.       time_on+=rate_of_change;
  172.       time_off-=rate_of_change;
  173.  
  174.       //When the end of the fade is reached, switch directions
  175.       if (time_on>max_value)
  176.         direction=down;
  177.     }
  178.     else
  179.     {
  180.         //In the DOWN direction the Time ON decreases while the Time OFF increases.
  181.         //The result is that the LEDs get dimmer
  182.       time_on-=rate_of_change;
  183.       time_off+=rate_of_change;
  184.  
  185.       //When the end of the fade is reached, switch directions
  186.       if (time_on<=min_value)
  187.         direction=up;
  188.     }
  189.   }
  190.  
  191.   while(1);                 // Ending infinite loop (just in case)
  192.  
  193. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top