Advertisement
igendel

Untitled

Dec 27th, 2017
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. /*
  2.     Rudolph the red nosed reindeer breathing LED
  3.     Atmel Studio 7 C Code for the PCBWay Christmas 2017 surprise kit
  4.     (ATtiny85, 128KHz internal oscillator)
  5.     Share and enjoy!
  6. */
  7.  
  8. // This must be at most 254
  9. #define PWM_STEPS 64U
  10.  
  11. #define F_CPU 32000UL
  12.  
  13. #include <avr/io.h>
  14. #include <util/delay.h>
  15.  
  16. void setup(void) {
  17.    
  18.     // Rudolph's nose is on PB0
  19.     DDRB = 1;
  20.     // Set clock prescaler 1:4
  21.     // Unlock...
  22.     CLKPR = (1 << CLKPCE);
  23.     // Set
  24.     CLKPR = 2;
  25.    
  26. }
  27.  
  28. void pwmCycle(uint8_t onTime) {
  29.    
  30.     uint8_t offTime = PWM_STEPS - onTime;
  31.     while (offTime--) {
  32.         PORTB &= ~1;
  33.     }
  34.     while (onTime--) {
  35.         PORTB |= 1;
  36.     }
  37.    
  38. }
  39.  
  40.  
  41. int main(void)
  42. {
  43.     uint8_t x = 0;
  44.  
  45.     setup();
  46.    
  47.     while (1) {
  48.        
  49.         for (x = 0; x < PWM_STEPS; ++x) {
  50.             pwmCycle(x);
  51.         }
  52.  
  53.         for (--x; x < PWM_STEPS; --x) {
  54.             pwmCycle(x);
  55.         }
  56.        
  57.         _delay_ms(1000);
  58.        
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement