Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1.  
  2. /*
  3. Copyright 2011 Dustin L. Westaby
  4.  
  5. ----------------------------------------------------------------------
  6.         Prop Circuit for Attiny45        
  7. ----------------------------------------------------------------------
  8. Title:      cortana.c
  9. Author:     Dustin Westaby
  10. Date Created:   2/13/11
  11. Last Modified:  2/13/11
  12. Purpose:  Animated output to LEDs
  13.  
  14. Compiled with AVR-GCC WinAVR
  15.  
  16. Revisions List:
  17. 2/13/11 Initial Draft and test circuit
  18.  
  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.  -
  47.  
  48. */
  49.  
  50. //--------------------------------------
  51. //          Global Variables           |
  52. //--------------------------------------
  53. #define F_CPU 8000000UL/8  /* 8 MHz Internal Oscillator DIV8 */
  54.  
  55. enum { up, down };
  56.  
  57. //--------------------------------------
  58. //              Includes               |
  59. //--------------------------------------
  60. #include <avr/io.h>
  61. #include <util/delay.h>
  62. //#include <avr/sleep.h>
  63. #include <inttypes.h>
  64.  
  65.  
  66. //--------------------------------------
  67. //          Delay Subroutines          |
  68. //--------------------------------------
  69. void delay_ms(uint16_t ms) {
  70.     while ( ms )
  71.     {
  72.         _delay_ms(1);
  73.         ms--;
  74.     }
  75. }
  76.  
  77. void delay_us(uint16_t us) {
  78.     while ( us )
  79.     {
  80.         _delay_us(1);
  81.         us--;
  82.     }
  83. }
  84.  
  85. //--------------------------------------
  86. //               Main                  |
  87. //--------------------------------------
  88. int main (void)
  89. {
  90.    
  91.     //variables used for animations
  92.     int i;
  93.     i=0;
  94.  
  95.     int count_delay;
  96.     count_delay=50;
  97.  
  98.     int duty_cycle;
  99.     duty_cycle=1000;
  100.    
  101.     int direction;
  102.     direction=up;
  103.    
  104.     //port B is an output
  105.     DDRB =  0b00011111;
  106.  
  107.     //Spin Circle
  108.     count_delay=50;
  109.     for (i = 0; i < 4; i++)
  110.     {
  111.         PORTB = 0b00000001;
  112.         delay_ms(count_delay);
  113.         PORTB = 0b00000010;
  114.         delay_ms(count_delay);
  115.         PORTB = 0b00000100;
  116.         delay_ms(count_delay);
  117.         PORTB = 0b00001000;
  118.         delay_ms(count_delay);
  119.         PORTB = 0b00010000;
  120.         delay_ms(count_delay);
  121.     }
  122.  
  123.     //ALL ON
  124.     PORTB = 0b00011111;
  125.     delay_ms(800);
  126.  
  127.     //Blink Blink
  128.     PORTB = 0b00000000;
  129.     delay_ms(100);
  130.     PORTB = 0b00011111;
  131.     delay_ms(10);
  132.     PORTB = 0b00000000;
  133.     delay_ms(100);
  134.     PORTB = 0b00011111;
  135.     delay_ms(100);
  136.  
  137.  
  138.  
  139.     //starts brigth then dims (software PWM)
  140.     int time_on, time_off, max_value,  min_value, rate_of_change;
  141.     max_value=500;
  142.     min_value=5;
  143.     time_on=min_value;
  144.     time_off=max_value;
  145.     rate_of_change=2;
  146.     direction=down;
  147.     while(1)
  148.     {
  149.         PORTB = 0b00011111;
  150.         delay_us(time_on);
  151.         PORTB = 0b00000000;
  152.         delay_us(time_off);
  153.  
  154.         if (direction==up)
  155.         {
  156.             time_on+=rate_of_change;
  157.             time_off-=rate_of_change;
  158.             if (time_on>max_value)
  159.                 direction=down;
  160.         }
  161.         else
  162.         {
  163.             time_on-=rate_of_change;
  164.             time_off+=rate_of_change;
  165.             if (time_on<=min_value)
  166.                 direction=up;
  167.         }
  168.        
  169.     }
  170.  
  171.    
  172.     while(1);                                   // Ending infinite loop
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement