-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPWM.cpp
89 lines (68 loc) · 1.48 KB
/
PWM.cpp
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
/*
* PWM.cpp
*
* Created: 25/02/2015 9:28:37
* Author: Vinicius Knabben
*
*/
/*
PWM waves generated by Timer 1 (16bits)
Timer1 Fast PWM Mode(14) (see Datasheet)
*/
#include "PWM.h"
/* Setup Timer 1 and I/O pins to generate a PWM Wave */
void PWM_Init(void)
{
/* Set the timer max value */
ICR1 = TOP;
/* Setup PWM to Panel */
set_bit(DDRB,PB1);
OCR1A = TOP-1; // Initiate the compare value
TCCR1A = (1 << COM1A1) | (1 << WGM11);
/* Setup PWM to Servo */
set_bit(ServoPWM_DDR,ServoPWM_pin);
clr_bit(ServoPWM_PORT,ServoPWM_pin);
TIMSK1 = (1 << OCIE1B)| (1 << TOIE1); // Enable interrupts on compare and overflow to change pin state
OCR1B = ServoPos(45);
/* Start timer mode(14) and prescaler */
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10);
}
/* Panel Driver PWM Functions */
void PWM_SetCount(unsigned int count)
{
if(count >= TOP)
count = TOP-1;
OCR1A = count;
}
void PWM_SetVoltage(float v)
{
if(v > 5)
v=5;
OCR1A = v*(40000/5) - 1;
}
/* Servo Control PWM Functions */
void PWM_ServoPos(unsigned int angle)
{
OCR1B = ServoPos(angle);
}
void PWM_ServoAnalogIn(unsigned int input)
{
if(input>1000)
input = 999;
OCR1B = input*40;
}
ISR(TIMER1_COMPB_vect)
{
clr_bit(ServoPWM_PORT,ServoPWM_pin);
}
ISR(TIMER1_OVF_vect)
{
static int cont = 0;
if(cont == 8)
{
set_bit(ServoPWM_PORT,ServoPWM_pin);
cont = 0;
return;
}
cont++;
}