/*
Copyright 2011 Dustin L. Westaby
----------------------------------------------------------------------
Prop Circuit for Attiny45
----------------------------------------------------------------------
Title: cortana.c
Author: Dustin Westaby
Date Created: 2/13/11
Last Modified: 3/21/11
Purpose: Animated output to LEDs
Circuit Schematic: http://www.flickr.com/photos/dwest2/5448858395/
Compiled with AVR-GCC WinAVR
Revisions List:
2/13/11 Initial Draft and test circuit
2/14/11 Updated Comments, re-arranged structure
3/21/11 Added link to circuit schematic
----------------------------------------------------------------------
Fuses:
----------------------------------------------------------------------
BrownOut Disabled
CKDIV8 (leave default if set)
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 };
//--------------------------------------
// 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--;
}
}
//--------------------------------------
// 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;
//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)
/* 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);
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;
}
}
while(1); // Ending infinite loop (just in case)
}