-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path5_Analog_Output_PWM.c
133 lines (99 loc) · 3.47 KB
/
5_Analog_Output_PWM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
TITLE : Analog Output using PWM
DATE : 2019/11/12
AUTHOR : e-Yantra Team
AIM: To decrease the brightness of Red Led and simultaneously increase the brightness of Green Led,
then to decrease the brightness of Green Led and simultaneously increase the brightness of Blue Led,
then to decrease the brightness of Blue Led and simultaneously increase the brightness of Red Led
and repeat this cyclically.
*/
#define F_CPU 16000000UL // Define Crystal Frequency of eYFi-Mega Board
#include <avr/io.h> // Standard AVR IO Library
#include <util/delay.h> // Standard AVR Delay Library
#include <avr/interrupt.h> // Standard AVR Interrupt Library
#define PIN_LED_RED PH3
#define PIN_LED_GREEN PH5
#define PIN_LED_BLUE PH4
void led_init(void){
DDRH |= (1 << PIN_LED_RED) | (1 << PIN_LED_GREEN) | (1 << PIN_LED_BLUE);
PORTH |= (1 << PIN_LED_RED) | (1 << PIN_LED_GREEN) | (1 << PIN_LED_BLUE);
}
void led_redOn(void){
PORTH &= ~(1 << PIN_LED_RED);
}
void led_redOff(void){
PORTH |= (1 << PIN_LED_RED);
}
void led_greenOn(void){
PORTH &= ~(1 << PIN_LED_GREEN);
}
void led_greenOff(void){
PORTH |= (1 << PIN_LED_GREEN);
}
void led_blueOn(void){
PORTH &= ~(1 << PIN_LED_BLUE);
}
void led_blueOff(void){
PORTH |= (1 << PIN_LED_BLUE);
}
// Timer 4 initialized in PWM mode for brightness control
// Prescale:256
// PWM 8bit fast, TOP=0x00FF
// Timer Frequency:225.000Hz
void timer4_init()
{
cli(); //disable all interrupts
TCCR4B = 0x00; //Stop
TCNT4H = 0xFF; //Counter higher 8-bit value to which OCR5xH value is compared with
TCNT4L = 0x00; //Counter lower 8-bit value to which OCR5xH value is compared with
OCR4AH = 0x00; //Output compare register high value for Red Led
OCR4AL = 0xFF; //Output compare register low value for Red Led
OCR4BH = 0x00; //Output compare register high value for Blue Led
OCR4BL = 0xFF; //Output compare register low value for Blue Led
OCR4CH = 0x00; //Output compare register high value for Green Led
OCR4CL = 0xFF; //Output compare register low value for Green Led
// Clear OC4A, OC4B & OC4C on compare match (set output to low level)
TCCR4A |= (1 << COM4A1) | (1 << COM4B1) | (1 << COM4C1);
TCCR4A &= ~((1 << COM4A0) | (1 << COM4B0) | (1 << COM4C0));
// FAST PWM 8-bit Mode
TCCR4A |= (1 << WGM40);
TCCR4A &= ~(1 << WGM41);
TCCR4B |= (1 << WGM42);
// Set Prescalar to 64
TCCR4B |= (1 << CS41) | (1 << CS40);
TCCR4B &= ~(1 << CS42);
sei(); //re-enable interrupts
}
// Function for brightness control of all 3 LEDs
void brightness (unsigned char red_led, unsigned char green_led, unsigned char blue_led){
OCR4AL = 255 - (unsigned char)red_led; // active low thats why subtracting by 255
OCR4CL = 255 - (unsigned char)green_led;
OCR4BL = 255 - (unsigned char)blue_led;
}
//use this function to initialize all devices
void init_devices (void) {
led_init();
timer4_init();
}
//Main Function
int main(){
init_devices();
int step = 0;
while(1){
// decrease Red and increase Green, Blue off
for (step = 0; step < 256; step++){
brightness(255 - step, 0 + step, 0);
_delay_ms(10);
}
// Red off, decrease Green and increase Blue
for (step = 0; step < 256; step++){
brightness(0, 255 - step, 0 + step);
_delay_ms(10);
}
// increase Red, Green off and decrease Blue
for (step = 0; step < 256; step++){
brightness(0 + step, 0, 255 - step);
_delay_ms(10);
}
}
}