Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. /*
  2. ATtiny25/45/85 Blink 32KHz
  3. Version: 1.0
  4. Author: Alex from Inside Gadgets (http://www.insidegadgets.com)
  5. Created: 2/010/2011
  6. Last Modified: 2/10/2011
  7.  
  8. Blink an LED on the ATtiny25/45/85 using the timer every 1 second with a 32.768 KHz watch crystal then go to IDLE sleep.
  9.  
  10. */
  11.  
  12. #define F_CPU 32768 // 32.768 KHz clock
  13.  
  14. #include <avr/io.h>
  15. #include <avr/interrupt.h>
  16. #include <avr/sleep.h>
  17.  
  18. // ATtiny25/45/85 Pin map
  19. // +-\/-+
  20. // Reset/Ain0 (D 5) PB5 1|o |8 Vcc
  21. // Ain3 (D 3) PB3 2| |7 PB2 (D 2) Ain1
  22. // Ain2 (D 4) PB4 3| |6 PB1 (D 1) pwm1
  23. // GND 4| |5 PB0 (D 0) pwm0 <-- connect resistor and LED here
  24. // +----+
  25.  
  26. #define ledPin PB0
  27.  
  28. #ifndef cbi
  29. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  30. #endif
  31. #ifndef sbi
  32. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  33. #endif
  34.  
  35. #ifndef boolean
  36. typedef uint8_t boolean;
  37. #endif
  38.  
  39. volatile boolean led_state = 0; // LED state on/off variable
  40. volatile int count = 0;
  41. volatile int of_count = 0;
  42.  
  43. int main(void) {
  44.  
  45. setup();
  46.  
  47. DDRB = (1<<ledPin); // Set LED as an output
  48. PORTB |= ((1<<PB2) | (1<<PB1)); // Turn on pull-up resistors on other ports to save power
  49.  
  50. while(1) {
  51.  
  52. if (led_state==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs
  53. led_state=0; // reset flag
  54.  
  55. if (count == of_count){
  56. count = 0;
  57. digitalWrite(ledPin,HIGH);
  58. delayMicroseconds(500);
  59. digitalWrite(ledPin,LOW);
  60. } else {
  61. count++;
  62. }
  63. }
  64. set_sleep_mode(SLEEP_MODE_IDLE); // Set sleep mode as idle
  65. sleep_mode(); // System sleeps here
  66.  
  67. }
  68. }
  69.  
  70. // Timer 1 interrupt
  71. ISR(TIMER1_OVF_vect) {
  72. led_state = 1; // Change led state to be the opposite of what it is
  73. }
  74.  
  75. /*
  76. ATtiny25/45/85 Blink 32KHz
  77. Version: 1.0
  78. Author: Alex from Inside Gadgets (http://www.insidegadgets.com)
  79. Created: 2/010/2011
  80. Last Modified: 2/10/2011
  81.  
  82. Blink an LED on the ATtiny25/45/85 using the timer every 1 second with a 32.768 KHz watch crystal then go to IDLE sleep.
  83.  
  84. */
  85.  
  86.  
  87. // Used from Arduino wiring.c - to setup the ATtiny
  88. void setup(void) {
  89. sei(); // Turn on interrupts
  90.  
  91. // Set prescaler to 128 to give exactly 1 second before an overflow occurs.
  92. // 128 prescaler x 256 timer bits / 32768 clock = 1 second
  93. sbi(TCCR1, CS13);
  94. sbi(TCCR1, CS11);
  95.  
  96. // Enable timer 1 overflow interrupt
  97. sbi(TIMSK, TOIE1);
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement