Advertisement
wariat

PoliceBike.c

Feb 26th, 2016
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.22 KB | None | 0 0
  1. /*
  2.  * Policyjna Lampa Rowerowa Hackera
  3.  * PLRH 1.0
  4.  *
  5.  * copyleft 2012 Jakub Klawiter
  6.  * GNU GPL v 3.0 or later blah, blah, blah
  7.  * http://www.gnu.org/licenses/gpl-3.0.html
  8.  *
  9.  * ATtiny85 @ 1MHz
  10.  * lfuse: 0x62 hfuse: 0xDD efuse: 0x01
  11.  */
  12.  
  13. #define F_CPU 1000000UL // Define the speed the clock is running at.
  14.                         // Used for the delay.h functions
  15.  
  16. #include <avr/io.h>
  17. #include <avr/sleep.h>
  18. #include <util/delay.h>
  19.  
  20. // Outputs
  21. #define blueLED     PB0
  22. #define redLED      PB1
  23. #define whiteLED    PB2
  24. #define speaker     PB4
  25.  
  26. // Inputs
  27. #define reedSwitch  PB3
  28.  
  29. // Config
  30. #define lowFreq    145   // 145 -  440Hz
  31. #define highFreq    45   //  45 - 1400Hz
  32. #define stepsUp    100
  33. #define stepsDown  200
  34. #define multiUp     ( lowFreq - highFreq ) / stepsUp
  35. #define multiDown   ( lowFreq - highFreq ) / stepsDown
  36. #define sleep       10
  37.  
  38.  
  39. char blinkCounter = 0;
  40. int  stopCounter  = 0;
  41.  
  42. char reset_cntr __attribute__ ((section (".noinit")));
  43. char mode       __attribute__ ((section (".noinit")));  // biała lampa / policja
  44.  
  45.  
  46. // Prototypes
  47. void Delay_ms       ( int cnt );
  48. void init_io        ( void );
  49.  
  50. void ResetHandler   ( void );
  51. void PoliceLamp     ( void );
  52. void WhiteLamp      ( void );
  53. void Sound          ( char );
  54. void Blink          ( void );
  55. void PowerOff       ( void );
  56.  
  57.  
  58.  
  59. // Functions
  60. void Delay_ms( int cnt )
  61. {
  62.   while( cnt-- > 0 ) _delay_ms( 1 );
  63. }
  64.  
  65. void init_io( void )
  66. {
  67.  
  68.   if ( mode )
  69.   { // Police Lamp
  70.     DDRB   = ( 1 << redLED ) | ( 1 << blueLED ) | ( 1 << speaker ); // Set pins as outputs
  71.    
  72.     // TCCR1: Timer/Counter1 Control Register
  73.     // CTC1 PWM1A COM1A1 COM1A0 CS13 CS12 CS11 CS10
  74.  
  75.     // Prescale Select
  76.     TCCR1 = ( 0 << CS13   ) | ( 1 << CS12    ) | ( 0 << CS11    ) | ( 1 << CS10 );
  77.  
  78.     //General Timer/Counter Control Register
  79.     GTCCR  = ( 1 << PWM1B  ) | ( 1 << COM0B1 ) | ( 0 << COM0B0 );
  80.  
  81. //    OCR1A = 100; // wtf
  82.     OCR1B = 5; // szerokość impulsu
  83.     OCR1C = 0; // czestotliwosc
  84.   }
  85.   else
  86.   { // White Lamp
  87.     DDRB = ( 1 << whiteLED ); // Set pins as outputs
  88.   }
  89.  
  90.   PORTB = ( 1 << reedSwitch ); // włącza PullUp na WEJŚCIU
  91. }
  92.  
  93. void ResetHandler( void )
  94. {
  95. // MCUSR: 0 0 0 0 WDRF BORF EXTRF PORF
  96.   if ( MCUSR & 0x1 ) // PORF
  97.   {
  98.     reset_cntr = 0;
  99.     mode       = 0;  
  100.     MCUSR      = 0;
  101.   }
  102.   if ( MCUSR & 0x2 ) // EXTRF
  103.   {
  104.     reset_cntr++;
  105.     MCUSR      = 0;
  106.   }
  107.  
  108.   /* // BORF nie działa w moim attiny :/
  109.   if (MCUSR & 0x3 ) // BORF
  110.   {
  111.     MCUSR      = 0;
  112.     PowerOff();
  113.   }
  114.   */
  115.  
  116.   if ( reset_cntr >= 2 )
  117.   {
  118.     mode       = ~mode;
  119.     reset_cntr = 0;
  120.   }
  121. }
  122.  
  123. void Sound ( char i )
  124. {
  125.   OCR1C = i;
  126.   Delay_ms( sleep );
  127. }
  128.  
  129.  
  130.  
  131. void Blink( void )
  132. {
  133.   blinkCounter++;
  134.  
  135.   if ( blinkCounter % 6 == 0 ) PORTB &= ~( ( 1 << redLED  ) |
  136.                                            ( 1 << blueLED ) );
  137.   if ( blinkCounter % 6 == 3 )
  138.   {
  139.     if ( blinkCounter < 60 )   PORTB |=    ( 1 << redLED  );
  140.     else                       PORTB |=    ( 1 << blueLED );
  141.   }
  142.   if ( blinkCounter > 120 ) blinkCounter = 0;
  143. }
  144.  
  145.  
  146.  
  147. void PoliceLamp ( void )
  148. {
  149.  
  150.   while ( stopCounter++ < 10 ) // was 2
  151.   {
  152.     for (int i = stepsUp; i >= 1; i-- )
  153.     {
  154.       Sound( i * multiUp + highFreq );
  155.       Blink();
  156.       if ( ( PINB & ( 1 << reedSwitch ) ) == 0 ) stopCounter = 0;
  157.     }
  158.    
  159.     for (int i = 1; i <= stepsDown; i++ )
  160.     {
  161.       Sound( i * multiDown + highFreq );
  162.       Blink();
  163.       if ( ( PINB & ( 1 << reedSwitch ) ) == 0 ) stopCounter = 0;
  164.     }
  165.     reset_cntr = 0;
  166.   }
  167.   Sound( 0 );
  168. }
  169.  
  170.  
  171. void WhiteLamp( void )
  172. {
  173.  
  174.   while ( stopCounter++ < 10 ) // was 2
  175.   {
  176.     for ( int i = 1; i < 300; i++ )
  177.     {
  178.       PORTB |=  ( 1 << whiteLED );
  179.       Delay_ms( 5 );
  180.      
  181.       //PORTB &= ~( 1 << whiteLED );
  182.       Delay_ms( 3 );
  183.  
  184.       if ( ( PINB & ( 1 << reedSwitch ) ) == 0 ) stopCounter = 0;
  185.     }
  186.     reset_cntr = 0;
  187.   }
  188.  
  189. }
  190.  
  191. void PowerOff ( void )
  192. {
  193.   PORTB = 0;
  194.   DDRB =  0; // ważne dla oszczędzenia prądu!
  195.  
  196.   set_sleep_mode( SLEEP_MODE_PWR_DOWN );
  197.   sleep_mode(  );
  198. }
  199.  
  200. //Main Function
  201. int main( void )
  202. {
  203.   ResetHandler(  );
  204.  
  205.   init_io(  );
  206.  
  207.   if ( mode ) PoliceLamp(  );
  208.   else        WhiteLamp (  );
  209.  
  210.   PowerOff(  );
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement