Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright 2011 Dustin L. Westaby
- ----------------------------------------------------------------------
- Prop Circuit for Attiny45
- ----------------------------------------------------------------------
- Title: cortana.c
- Author: Dustin Westaby
- Date Created: 2/13/11
- Last Modified: 9/17/11
- Purpose: Animated output to LEDs
- Compiled with AVR-GCC WinAVR
- Revisions List:
- 2/13/11 Initial Draft and test circuit
- 2/14/11 Updated Comments, re-arranged structure
- 9/17/11 Timer Off Code Added
- ----------------------------------------------------------------------
- Fuses:
- ----------------------------------------------------------------------
- BrownOut Disabled
- CKDIV8
- Int RC Osc 8Mhz + 64ms
- ----------------------------------------------------------------------
- Inputs:
- ----------------------------------------------------------------------
- Power up
- ----------------------------------------------------------------------
- Ouputs:
- ----------------------------------------------------------------------
- pin label connections
- ----------------------------------------------------------------------
- 2 PB3 = LED
- 3 PB4 = LED
- 5 PB0 = LED
- 6 PB1 = LED
- 7 PB2 = LED
- ----------------------------------------------------------------------
- Notes:
- ----------------------------------------------------------------------
- -Program is free to modify, but please keep my name on the author list.
- */
- //--------------------------------------
- // Global Variables |
- //--------------------------------------
- // 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
- // One CPU Cycle = 1us
- #define F_CPU 8000000UL/8
- // Enumerate up down for use in the fade loops
- enum { up, down };
- //Option to disable the time out turn off
- //0 = OFF, 1 = ON
- #define TIMER_ENABLE 1
- //--------------------------------------
- // Includes |
- //--------------------------------------
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/sleep.h>
- #include <inttypes.h>
- //--------------------------------------
- // Delay Subroutines |
- //--------------------------------------
- //These functions are from the delay.h include, the calls to delay functions
- //are re-written here to allow for longer waits.
- void delay_ms(uint16_t ms) {
- while ( ms )
- {
- _delay_ms(1);
- ms--;
- }
- }
- void delay_us(uint16_t us) {
- while ( us )
- {
- _delay_us(1);
- us--;
- }
- }
- //--------------------------------------
- // Sleep Subroutine |
- //--------------------------------------
- void shutdown_and_sleep(void) {
- //setup low power sleep mode
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- //send to sleep
- sleep_mode();
- //NOT HIT
- while(1);
- }
- //--------------------------------------
- // Main |
- //--------------------------------------
- int main (void)
- {
- /* ---------------------------------------------------------------- */
- /* Initialization */
- /* ---------------------------------------------------------------- */
- //Variables used for animations
- int i, count_delay, direction;
- int time_on, time_off, max_value, min_value, rate_of_change;
- int time_to_off, cycle_count, fade_inc_count;
- //Initialize Port B is an output
- DDRB = 0b00011111;
- /* ---------------------------------------------------------------- */
- /* Spin Circle Four Times (approx 1s) */
- /* ---------------------------------------------------------------- */
- i=0;
- count_delay=50; //Delay between circle movement is 50ms
- for (i = 0; i < 4; i++)
- {
- PORTB = 0b00000001; //Turn ON only LED 0 for defined delay (50ms)
- delay_ms(count_delay);
- PORTB = 0b00000010; //Turn ON only LED 1 for defined delay (50ms)
- delay_ms(count_delay);
- PORTB = 0b00000100; //Turn ON only LED 2 for defined delay (50ms)
- delay_ms(count_delay);
- PORTB = 0b00001000; //Turn ON only LED 3 for defined delay (50ms)
- delay_ms(count_delay);
- PORTB = 0b00010000; //Turn ON only LED 4 for defined delay (50ms)
- delay_ms(count_delay);
- }
- /* ---------------------------------------------------------------- */
- /* All ON Long (0.8 seconds) */
- /* ---------------------------------------------------------------- */
- PORTB = 0b00011111; //Turn ON all LEDs 01234 for 800ms
- delay_ms(800);
- /* ---------------------------------------------------------------- */
- /* Blink Blink */
- /* ---------------------------------------------------------------- */
- PORTB = 0b00000000; //Turn OFF all LEDS 01234 for 100ms
- delay_ms(100);
- PORTB = 0b00011111; //Turn ON all LEDs 01234 for 10ms
- delay_ms(10);
- PORTB = 0b00000000; //Turn OFF all LEDs 01234 for 100ms
- delay_ms(100);
- PORTB = 0b00011111; //Turn ON all LEDs 01234 for 100ms
- delay_ms(100);
- /* ---------------------------------------------------------------- */
- /* Fade In and Out Continuous (Software PWM) */
- /* ---------------------------------------------------------------- */
- max_value=500; //Max of 500us for LEDs ON
- min_value=5; //Max of 5us for LEDs OFF
- time_on=min_value; //Set Starting Time ON to 5us
- time_off=max_value; //Set Starting Time OFF to 500us
- rate_of_change=2; //This is the speed that the fade goes between min and max
- direction=down; //Direction is defined for the direction of the fade (in or out)
- time_to_off=5*(54); //About 5 minutes (54 fades per minute)
- cycle_count=0; //Use to count the number of times fades to dark
- fade_inc_count=0; //Used to count iterations of the fade loop
- /* Note: The direction is backwards on purpose, causes the light animation to
- look like it hiccuped for the first fade cycle before reaching a steady fade. */
- while(1)
- {
- //The ratio of ON time and OFF time determines the brightness of the LEDs (DUTY CYLE)
- //By continuously varying the ON time versus the OFF times we get a fade effect.
- PORTB = 0b00011111; //Turn ON all LEDs 01234
- delay_us(time_on);
- PORTB = 0b00000000; //Turn OFF all LEDs 01234
- delay_us(time_off);
- //This is the TimeOut code. Breaks loop.turning off the LEDs.
- if (TIMER_ENABLE)
- {
- fade_inc_count++;
- if (fade_inc_count>(max_value/2))
- {
- fade_inc_count=0;
- cycle_count++;
- if (cycle_count > time_to_off)
- {
- shutdown_and_sleep();
- //does not return
- break;
- }
- }
- }
- if (direction==up)
- {
- //In the UP direction the Time ON increases while the Time OFF decreases.
- //The result is that the LEDs get brighter
- time_on+=rate_of_change;
- time_off-=rate_of_change;
- //When the end of the fade is reached, switch directions
- if (time_on>max_value)
- direction=down;
- }
- else
- {
- //In the DOWN direction the Time ON decreases while the Time OFF increases.
- //The result is that the LEDs get dimmer
- time_on-=rate_of_change;
- time_off+=rate_of_change;
- //When the end of the fade is reached, switch directions
- if (time_on<=min_value)
- direction=up;
- }
- }
- //this code should not be hit, the following is just in case
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_mode();
- while(1); // Ending infinite loop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement