-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
229 lines (212 loc) · 5.43 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* main.c
*
* Created on: 26.4.2014
* Author: Ville
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "globaldef.h"
#include "serial.h"
#include "pid.h"
/* Pause feature */
static volatile bool m_paused = false;
#define pause_led_on() do{PORTB |= (1 << 5);}while(0)
#define pause_led_off() do{PORTB &= ~(1 << 5);}while(0)
/* Clear pin on compare match */
#define output_disable() do{TCCR2A &= ~(1 << COM2A1);}while(0)
#define output_enable() do{TCCR2A |= (1 << COM2A1);}while(0)
/* PWM/PID values */
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define adc_ref_mv 5000u
#define adc_max 1024u
#define adc_to_mv(adc) adc == 0 ? 0 : \
((uint32_t)((uint32_t)(adc) * adc_ref_mv) / adc_max)
#define pwm_max_volt 5000u
#define pwm_max_value 255u
#define mv_to_pwm(mv) mv == 0 ? 0 : \
((uint32_t)((uint32_t)(mv) * pwm_max_value) / pwm_max_volt)
#define pwm_fb_enable() do{ADCSRA |= (1 << ADIE); ADCSRA |= (1 << ADSC);}while(0)
#define pwm_fb_disable() do{ADCSRA &= ~(1 << ADIE);}while(0)
/* Target voltage in millivolts */
static volatile int16_t m_pwm_target_value = 0;
static volatile uint8_t m_pwm_set_value = 0;
static volatile uint8_t m_print_buf[256];
static pidData_t m_pid;
void serve_serial(void)
{
uint8_t pwm_set_latch = 0, len = 0;
int16_t ch = serial_read();
if(ch >= '0' && ch <= '5')
{
int16_t new_target = ch - '0';
if(new_target != m_pwm_target_value)
{
__disable_irq();
pid_Reset_Integrator(&m_pid);
m_pwm_target_value = new_target * 1000u;
__enable_irq();
}
}
/* For some mysterious reason INT0 starts oscillating on
* rising edge, when PORTD 2 is driven (serial TX) */
__disable_irq();
pwm_set_latch = m_pwm_set_value;
__enable_irq();
len = snprintf((char*)m_print_buf,
sizeof(m_print_buf),
"target:%u\t"
"pwm_out:%u\n",
m_pwm_target_value,
pwm_set_latch);
serial_write((void*)m_print_buf, len);
}
void init(void)
{
/* GPIO
* PORTD :
* 7: OUT (GPI0)
* 6: OUT (GPIO)
* 5: OUT (GPIO)
* 4: OUT (GPIO)
* 3: OUT (GPIO)
* 2: IN (INT0)
* 1: OUT (UART TXD)
* 0: IN (UART RXD) */
PORTD = 0;
DDRD = 0b11111010;
/* PORTB :
* 7: IN (XTAL)
* 6: IN (XTAL)
* 5: OUT (LED0)
* 4: OUT (GPIO)
* 3: OUT (PWM)
* 2: OUT (GPIO)
* 1: OUT (GPIO)
* 0: OUT (GPIO)
*/
PORTB = 0;
DDRB = 0b00111111;
/* PORTC :
* 7: NC
* 6: IN (RST)
* 5: IN (ADC5)
* 4: IN (ADC4)
* 3: IN (ADC3)
* 2: IN (ADC2)
* 1: IN (ADC1)
* 0: IN (ADC0)
*/
PORTC = 0;
DDRC = 0b00000000;
/* INT0 Pin:
* Falling edge triggers interrupt */
EIFR |= 0x01;
EICRA = (1 << ISC01);
EIMSK = 0x01;
pause_led_off();
/* TIMER2 (Fast PWM)
* Reset timer count
* Set PWM to 0
* Enable COMPA interrupt
* Start timer: No prescaler */
output_disable();
TCNT2 = 0;
OCR2A = 0;
TIFR2 |= (1 << OCF2A);
TIMSK2 |= ((1 << OCIE2A) | (1 << TOIE2));
TCCR2A = ((1 << WGM21) | (1 << WGM20));
TCCR2B = ((1 << CS20));
/* ADC
* Set AD prescaler to 128
* PWM LPF feedback is connected to ADC channel 0
* ADC free running mode (triggered by TIM2 ofv)
* Measures the output voltage of PWM LPF
* Interrupt for conversion ready
* ISR runs PID, which sets value for CC2A */
pwm_fb_disable();
ADMUX = (1 << REFS0);
ADCSRA = ((1 << ADEN) |
(1 << ADPS2) |
(1 << ADPS1) |
(1 << ADPS0));
ADCSRB = 0;
DIDR0 = (1 << ADC0D);
ADCSRA |= (1 << ADSC);
ADCSRA |= (1 << ADSC);
}
#define K_P 1.0
#define K_I 0.3
#define K_D 1.0
int main(void)
{
/* Initialize everything before enabling output */
init();
serial_init();
pid_Init(K_P * SCALING_FACTOR,
K_I * SCALING_FACTOR,
K_D * SCALING_FACTOR,
&m_pid);
/* Finally enable output */
pwm_fb_enable();
output_enable();
__enable_irq();
while(1)
{
serve_serial();
}
return 0;
}
ISR(ADC_vect)
{
uint8_t pwm = 0;
uint16_t ad_mv;
int16_t pid_out;
/* Value is right adjusted */
ad_mv = ADC;
ad_mv = adc_to_mv(ad_mv);
pid_out = pid_Controller(m_pwm_target_value,
ad_mv,
&m_pid);
if(pid_out < 0)
{
/* Can't drive negative values */
pid_out = 0;
}
/* No reason to set over maximum voltage */
pid_out = min(pid_out, pwm_max_volt);
pwm = mv_to_pwm(pid_out);
m_pwm_set_value = pwm;
/* Start new conversion */
ADCSRA |= (1 << ADSC);
}
ISR(INT0_vect)
{
PORTB ^= 0x01;
if(m_paused)
{
/* Resume */
pause_led_off();
output_enable();
pwm_fb_enable();
}
else
{
/* Pause */
pause_led_on();
pwm_fb_disable();
output_disable();
}
m_paused = !m_paused;
}
EMPTY_INTERRUPT(TIMER2_COMPA_vect);
ISR(TIMER2_OVF_vect)
{
/* Set new PWM period when timer count resets */
OCR2A = m_pwm_set_value;
}