Advertisement
dlwestab

Prop Circuit for Attiny45

Sep 17th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.63 KB | None | 0 0
  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:  9/17/11
  11. Purpose:  Animated output to LEDs
  12.  
  13. Compiled with AVR-GCC WinAVR
  14.  
  15. Revisions List:
  16. 2/13/11 Initial Draft and test circuit
  17. 2/14/11 Updated Comments, re-arranged structure
  18. 9/17/11 Timer Off Code Added
  19.  
  20. ----------------------------------------------------------------------
  21.     Fuses:
  22. ----------------------------------------------------------------------
  23.  BrownOut Disabled
  24.  CKDIV8
  25.  Int RC Osc 8Mhz + 64ms
  26.  
  27. ----------------------------------------------------------------------
  28.     Inputs:
  29. ----------------------------------------------------------------------
  30.  Power up
  31.  
  32. ----------------------------------------------------------------------
  33.     Ouputs:
  34. ----------------------------------------------------------------------
  35.  pin    label   connections
  36. ----------------------------------------------------------------------
  37.   2     PB3 =   LED
  38.   3     PB4 =   LED
  39.   5     PB0 =   LED
  40.   6     PB1 =   LED
  41.   7     PB2 =   LED
  42.  
  43. ----------------------------------------------------------------------
  44.     Notes:
  45. ----------------------------------------------------------------------
  46.  -Program is free to modify, but please keep my name on the author list.
  47.  
  48. */
  49.  
  50. //--------------------------------------
  51. //          Global Variables           |
  52. //--------------------------------------
  53. // 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
  54. // One CPU Cycle = 1us
  55. #define F_CPU 8000000UL/8
  56.  
  57. // Enumerate up down for use in the fade loops
  58. enum { up, down };
  59.  
  60. //Option to disable the time out turn off
  61. //0 = OFF, 1 = ON
  62. #define TIMER_ENABLE 1
  63.  
  64. //--------------------------------------
  65. //              Includes               |
  66. //--------------------------------------
  67. #include <avr/io.h>
  68. #include <util/delay.h>
  69. #include <avr/sleep.h>
  70. #include <inttypes.h>
  71.  
  72.  
  73. //--------------------------------------
  74. //          Delay Subroutines          |
  75. //--------------------------------------
  76. //These functions are from the delay.h include, the calls to delay functions
  77. //are re-written here to allow for longer waits.
  78. void delay_ms(uint16_t ms) {
  79.   while ( ms )
  80.   {
  81.     _delay_ms(1);
  82.     ms--;
  83.   }
  84. }
  85.  
  86. void delay_us(uint16_t us) {
  87.   while ( us )
  88.   {
  89.     _delay_us(1);
  90.     us--;
  91.   }
  92. }
  93.  
  94. //--------------------------------------
  95. //          Sleep Subroutine           |
  96. //--------------------------------------
  97. void shutdown_and_sleep(void) {
  98.  
  99.     //setup low power sleep mode
  100.     set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  101.  
  102.     //send to sleep
  103.     sleep_mode();
  104.  
  105.     //NOT HIT
  106.     while(1);  
  107.  
  108. }
  109.  
  110.  
  111. //--------------------------------------
  112. //               Main                  |
  113. //--------------------------------------
  114. int main (void)
  115. {
  116.  
  117.   /* ---------------------------------------------------------------- */
  118.   /*                            Initialization                        */
  119.   /* ---------------------------------------------------------------- */
  120.   //Variables used for animations
  121.   int i, count_delay, direction;
  122.   int time_on, time_off, max_value,  min_value, rate_of_change;
  123.   int time_to_off, cycle_count, fade_inc_count;
  124.  
  125.   //Initialize Port B is an output
  126.   DDRB =  0b00011111;
  127.  
  128.   /* ---------------------------------------------------------------- */
  129.   /*                Spin Circle Four Times (approx 1s)                */
  130.   /* ---------------------------------------------------------------- */
  131.   i=0;
  132.   count_delay=50;             //Delay between circle movement is 50ms
  133.   for (i = 0; i < 4; i++)
  134.   {
  135.     PORTB = 0b00000001;       //Turn ON only LED 0 for defined delay (50ms)
  136.     delay_ms(count_delay);
  137.     PORTB = 0b00000010;       //Turn ON only LED 1 for defined delay (50ms)
  138.     delay_ms(count_delay);
  139.     PORTB = 0b00000100;       //Turn ON only LED 2 for defined delay (50ms)
  140.     delay_ms(count_delay);
  141.     PORTB = 0b00001000;       //Turn ON only LED 3 for defined delay (50ms)
  142.     delay_ms(count_delay);
  143.     PORTB = 0b00010000;       //Turn ON only LED 4 for defined delay (50ms)
  144.     delay_ms(count_delay);
  145.   }
  146.  
  147.   /* ---------------------------------------------------------------- */
  148.   /*                     All ON Long (0.8 seconds)                    */
  149.   /* ---------------------------------------------------------------- */
  150.   PORTB = 0b00011111;         //Turn ON all LEDs 01234 for 800ms
  151.   delay_ms(800);
  152.  
  153.   /* ---------------------------------------------------------------- */
  154.   /*                         Blink Blink                              */
  155.   /* ---------------------------------------------------------------- */
  156.   PORTB = 0b00000000;         //Turn OFF all LEDS 01234 for 100ms
  157.   delay_ms(100);
  158.   PORTB = 0b00011111;         //Turn ON  all LEDs 01234 for 10ms
  159.   delay_ms(10);
  160.   PORTB = 0b00000000;         //Turn OFF all LEDs 01234 for 100ms
  161.   delay_ms(100);
  162.   PORTB = 0b00011111;         //Turn ON  all LEDs 01234 for 100ms
  163.   delay_ms(100);
  164.  
  165.   /* ---------------------------------------------------------------- */
  166.   /*              Fade In and Out Continuous (Software PWM)           */
  167.   /* ---------------------------------------------------------------- */
  168.   max_value=500;              //Max of 500us for LEDs ON
  169.   min_value=5;                //Max of   5us for LEDs OFF
  170.   time_on=min_value;          //Set Starting Time ON  to 5us
  171.   time_off=max_value;         //Set Starting Time OFF to 500us
  172.   rate_of_change=2;           //This is the speed that the fade goes between min and max
  173.   direction=down;             //Direction is defined for the direction of the fade (in or out)
  174.  
  175.   time_to_off=5*(54);         //About 5 minutes (54 fades per minute)
  176.   cycle_count=0;              //Use to count the number of times fades to dark
  177.   fade_inc_count=0;           //Used to count iterations of the fade loop
  178.  
  179.   /* Note: The direction is backwards on purpose, causes the light animation to
  180.            look like it hiccuped for the first fade cycle before reaching a steady fade. */
  181.  
  182.   while(1)
  183.   {
  184.     //The ratio of ON time and OFF time determines the brightness of the LEDs (DUTY CYLE)
  185.     //By continuously varying the ON time versus the OFF times we get a fade effect.
  186.     PORTB = 0b00011111;       //Turn ON  all LEDs 01234
  187.     delay_us(time_on);
  188.     PORTB = 0b00000000;       //Turn OFF all LEDs 01234
  189.     delay_us(time_off);
  190.  
  191.     //This is the TimeOut code. Breaks loop.turning off the LEDs.
  192.     if (TIMER_ENABLE)
  193.     {
  194.         fade_inc_count++;
  195.         if (fade_inc_count>(max_value/2))
  196.         {
  197.             fade_inc_count=0;
  198.             cycle_count++;
  199.  
  200.             if (cycle_count > time_to_off)
  201.             {
  202.                 shutdown_and_sleep();
  203.                 //does not return
  204.                 break;
  205.             }
  206.         }
  207.     }
  208.  
  209.     if (direction==up)
  210.     {
  211.         //In the UP direction the Time ON increases while the Time OFF decreases.
  212.         //The result is that the LEDs get brighter
  213.       time_on+=rate_of_change;
  214.       time_off-=rate_of_change;
  215.  
  216.       //When the end of the fade is reached, switch directions
  217.       if (time_on>max_value)
  218.         direction=down;
  219.     }
  220.     else
  221.     {
  222.         //In the DOWN direction the Time ON decreases while the Time OFF increases.
  223.         //The result is that the LEDs get dimmer
  224.       time_on-=rate_of_change;
  225.       time_off+=rate_of_change;
  226.  
  227.       //When the end of the fade is reached, switch directions
  228.       if (time_on<=min_value)
  229.         direction=up;
  230.     }
  231.   }
  232.  
  233.   //this code should not be hit, the following is just in case
  234.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  235.   sleep_mode();
  236.   while(1);  // Ending infinite loop
  237.  
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement