Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / testing / robot_routine_reg_test / lights.c @ 13

History | View | Annotate | Download (2.33 KB)

1
/*
2
lights.c
3
Controls led_user (small green LED) and the ORB (big light)
4

5
author: CMU Robotics Club, Colony Project
6
*/
7

    
8
//#include "lights.h"
9
#include "firefly+_lib.h"
10

    
11
// the below comment may not be correct
12
// Initializes timer2 to count every 0.5 us, overflowing every 16.32 ms (because we set the counter to start at 0x8000)
13
void orb_init() 
14
{
15

    
16
        //enable the data direction register on the 3 orb LEDs
17
        DDRE |= _BV(REDLED) | _BV(GREENLED) | _BV(BLUELED);
18
        
19
        TCCR3A = 0xA9;  // COM## set to noninvert, WGM 1:0 set to 1
20
        TCCR3B = 0x0C;  // High bits to 0, WGM 3:2 set to 1, prescaler to 8
21

    
22
// clear Output Compare Registers
23
        OCR3AH=0x00;
24
        OCR3AL=0x00;
25
        OCR3BH=0x00;
26
        OCR3BL=0x00;
27
        OCR3CH=0x00;
28
        OCR3CL=0x00;
29
}
30

    
31
// Sets the red, green and blue elements of the LED with 8-bit resolution
32
void orb_set(unsigned int red_led, unsigned int green_led, unsigned int blue_led)
33
{        
34
        OCR3AL = red_led;
35
        OCR3BL = green_led;
36
        OCR3CL = blue_led;
37
}
38

    
39
void orb_set_color(int col)
40
{
41
 int red, green, blue;
42

    
43
 red = ((col & 0xE0) >> 5) * 36;
44
 green = ((col & 0x1C) >> 2) * 36;
45
 blue = (col & 0x03) * 85;
46

    
47
 orb_set(red, green, blue);
48
}
49

    
50
// Disables the timer1 interrupt, disabling the Orb's color fading capabilities
51
// You can still turn the red, green, and blue leds on and off with set_orb_dio
52
void orb_disable()
53
{
54
        TCCR3A = 0x01;  // COM## disconnected, WGM 1:0 set to 1
55
}
56

    
57
// Enables the timer1 interrupt, enabling the Orb's color fading capabilities
58
void orb_enable()
59
{
60
        TCCR3A = 0xA9;  // COM## set to noninvert, WGM 1:0 set to 1
61
}
62

    
63

    
64
/*
65
This function turns each Orb element on or off
66
Useful for using the Orb for debugging purposes without
67
the overhead of the software PWM routine used for color fading.
68
*/
69
void orb_set_dio(int red, int green, int blue) 
70
{
71
        if(red == 0)
72
                PORTE &= (0xFF - _BV(REDLED));
73
        else
74
                PORTE |= _BV(REDLED);
75
        
76
        
77
        if(green == 0)
78
                PORTE &= (0xFF - _BV(GREENLED));
79
        else
80
                PORTE |= _BV(GREENLED);
81
                
82
        
83
        if(blue == 0)
84
                PORTE &= (0xFF - _BV(BLUELED));
85
        else
86
                PORTE |= _BV(BLUELED);
87
}
88

    
89

    
90
/////////////////////////////////////////
91
///////////    user LED   ///////////////
92
/////////////////////////////////////////
93
void led_init( void )
94
{
95
        DDRG |= _BV(USERLED);
96
}
97

    
98
void led_user(int value) 
99
{
100
        if(value == 0) 
101
                PORTG &= (0xFF - _BV(USERLED)); 
102
        else
103
                PORTG |= _BV(USERLED);
104
}