Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ATtiny25/45/85 Blink 32KHz
- Version: 1.0
- Author: Alex from Inside Gadgets (http://www.insidegadgets.com)
- Created: 2/010/2011
- Last Modified: 2/10/2011
- 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.
- */
- #define F_CPU 32768 // 32.768 KHz clock
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <avr/sleep.h>
- // ATtiny25/45/85 Pin map
- // +-\/-+
- // Reset/Ain0 (D 5) PB5 1|o |8 Vcc
- // Ain3 (D 3) PB3 2| |7 PB2 (D 2) Ain1
- // Ain2 (D 4) PB4 3| |6 PB1 (D 1) pwm1
- // GND 4| |5 PB0 (D 0) pwm0 <-- connect resistor and LED here
- // +----+
- #define ledPin PB0
- #ifndef cbi
- #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
- #endif
- #ifndef sbi
- #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
- #endif
- #ifndef boolean
- typedef uint8_t boolean;
- #endif
- volatile boolean led_state = 0; // LED state on/off variable
- volatile int count = 0;
- volatile int of_count = 0;
- int main(void) {
- setup();
- DDRB = (1<<ledPin); // Set LED as an output
- PORTB |= ((1<<PB2) | (1<<PB1)); // Turn on pull-up resistors on other ports to save power
- while(1) {
- if (led_state==1) { // wait for timed out watchdog / flag is set when a watchdog timeout occurs
- led_state=0; // reset flag
- if (count == of_count){
- count = 0;
- digitalWrite(ledPin,HIGH);
- delayMicroseconds(500);
- digitalWrite(ledPin,LOW);
- } else {
- count++;
- }
- }
- set_sleep_mode(SLEEP_MODE_IDLE); // Set sleep mode as idle
- sleep_mode(); // System sleeps here
- }
- }
- // Timer 1 interrupt
- ISR(TIMER1_OVF_vect) {
- led_state = 1; // Change led state to be the opposite of what it is
- }
- /*
- ATtiny25/45/85 Blink 32KHz
- Version: 1.0
- Author: Alex from Inside Gadgets (http://www.insidegadgets.com)
- Created: 2/010/2011
- Last Modified: 2/10/2011
- 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.
- */
- // Used from Arduino wiring.c - to setup the ATtiny
- void setup(void) {
- sei(); // Turn on interrupts
- // Set prescaler to 128 to give exactly 1 second before an overflow occurs.
- // 128 prescaler x 256 timer bits / 32768 clock = 1 second
- sbi(TCCR1, CS13);
- sbi(TCCR1, CS11);
- // Enable timer 1 overflow interrupt
- sbi(TIMSK, TOIE1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement