1 |
#include ".\main.h" |
1 |
#include ".\main.h" |
2 |
|
2 |
|
3 |
#define LED1 PIN_C6 //CHANGE PIN_XX TO YOUR LED PIN NUMBER |
3 |
#define LED1 PIN_C6 //CHANGE PIN_XX TO YOUR LED PIN NUMBER |
4 |
#define LED2 PIN_C5 //CHANGE PIN_XX TO YOUR LED PIN NUMBER |
4 |
#define LED2 PIN_C5 //CHANGE PIN_XX TO YOUR LED PIN NUMBER |
5 |
#define LED3 PIN_C4 //CHANGE PIN_XX TO YOUR LED PIN NUMBER |
5 |
#define LED3 PIN_C4 //CHANGE PIN_XX TO YOUR LED PIN NUMBER |
6 |
#define LED4 PIN_D3 //CHANGE PIN_XX TO YOUR LED PIN NUMBER |
6 |
#define LED4 PIN_D3 //CHANGE PIN_XX TO YOUR LED PIN NUMBER |
7 |
|
7 |
|
8 |
#define S1 PIN_B0 //Raw up |
8 |
#define S1 PIN_B0 //Raw up |
9 |
#define S2 PIN_B1 //Raw down |
9 |
#define S2 PIN_B1 //Raw down |
10 |
#define S3 PIN_D0 //Fine up |
10 |
#define S3 PIN_D0 //Fine up |
11 |
#define S4 PIN_D1 //Fine down |
11 |
#define S4 PIN_D1 //Fine down |
12 |
|
12 |
|
13 |
#define BEEP PIN_D2 //piezo beeper |
13 |
#define BEEP PIN_D2 //piezo beeper |
14 |
|
14 |
|
15 |
#define LCD_ENABLE_PIN PIN_E0 //// |
15 |
#define LCD_ENABLE_PIN PIN_E0 //// |
16 |
#define LCD_RS_PIN PIN_E1 //// |
16 |
#define LCD_RS_PIN PIN_E1 //// |
17 |
#define LCD_RW_PIN PIN_E2 //// |
17 |
#define LCD_RW_PIN PIN_E2 //// |
18 |
#define LCD_DATA4 PIN_D4 //// |
18 |
#define LCD_DATA4 PIN_D4 //// |
19 |
#define LCD_DATA5 PIN_D5 //// |
19 |
#define LCD_DATA5 PIN_D5 //// |
20 |
#define LCD_DATA6 PIN_D6 //// |
20 |
#define LCD_DATA6 PIN_D6 //// |
21 |
#define LCD_DATA7 PIN_D7 |
21 |
#define LCD_DATA7 PIN_D7 |
22 |
#include <lcd.c> |
22 |
#include <lcd.c> |
23 |
|
23 |
|
24 |
#define OUTPUT_ENABLE PIN_B5 |
24 |
#define OUTPUT_ENABLE PIN_B5 |
25 |
|
25 |
|
26 |
|
26 |
|
27 |
//#define MAXDUTY (int16)160 // 50 kHz |
27 |
//#define MAXDUTY (int16)160 // 50 kHz |
28 |
#define MAXDUTY (int16)400 // 20 kHz |
28 |
#define MAXDUTY (int16)400 // 20 kHz |
29 |
//#define MAXDUTY (int16)800 // 10 kHz |
29 |
//#define MAXDUTY (int16)800 // 10 kHz |
30 |
#define HALFDUTY (int16)MAXDUTY/2 |
30 |
#define HALFDUTY (int16)MAXDUTY/2 |
31 |
//#define DIVISOR 199 // 10 kHz |
31 |
//#define DIVISOR 199 // 10 kHz |
32 |
#define DIVISOR 99 // 20 kHz |
32 |
#define DIVISOR 99 // 20 kHz |
33 |
//#define DIVISOR 39 // 50 kHz |
33 |
//#define DIVISOR 39 // 50 kHz |
34 |
|
34 |
|
35 |
#define MAX_OUTPUT 530 |
35 |
#define MAX_OUTPUT 530 |
36 |
#define MIN_OUTPUT 220 |
36 |
#define MIN_OUTPUT 220 |
37 |
#define raw_step 50 |
37 |
#define raw_step 50 |
38 |
#define fine_step 5 |
38 |
#define fine_step 5 |
39 |
|
39 |
|
40 |
|
40 |
|
41 |
|
41 |
|
42 |
/// Linear interpolation parameters |
42 |
/// Linear interpolation parameters |
43 |
// |
43 |
// |
44 |
|
44 |
|
45 |
#define k 0.333961 |
45 |
#define k 0.333961 |
46 |
#define q 206.952 |
46 |
#define q 206.952 |
47 |
|
47 |
|
48 |
|
48 |
|
49 |
unsigned int16 setpoint; |
49 |
unsigned int16 setpoint; |
50 |
unsigned int16 adc_value; |
50 |
unsigned int16 adc_value; |
51 |
int1 output_permit=FALSE; |
51 |
int1 output_permit=FALSE; |
52 |
|
52 |
|
53 |
#INT_AD |
53 |
#INT_AD |
54 |
void adc_handler() { |
54 |
void adc_handler() { |
55 |
|
55 |
|
56 |
adc_value = read_adc(ADC_READ_ONLY); |
56 |
adc_value = read_adc(ADC_READ_ONLY); |
57 |
if(output_permit == TRUE) |
57 |
if(output_permit == TRUE) |
58 |
{ |
58 |
{ |
59 |
if (adc_value <= setpoint) |
59 |
if (adc_value <= setpoint) |
60 |
{ |
60 |
{ |
61 |
set_pwm2_duty((int16)255); // zapnuti menice |
61 |
set_pwm2_duty((int16)255); // zapnuti menice |
62 |
output_low(LED2); |
62 |
output_low(LED2); |
63 |
} |
63 |
} |
64 |
else // vypnuti menice |
64 |
else // vypnuti menice |
65 |
{ |
65 |
{ |
66 |
set_pwm2_duty((int16)MAXDUTY); |
66 |
set_pwm2_duty((int16)MAXDUTY); |
67 |
output_high(LED2); |
67 |
output_high(LED2); |
68 |
} |
68 |
} |
69 |
|
69 |
|
70 |
} |
70 |
} |
71 |
else |
71 |
else |
72 |
{ |
72 |
{ |
73 |
set_pwm2_duty((int16)MAXDUTY); |
73 |
set_pwm2_duty((int16)MAXDUTY); |
74 |
output_high(LED2); |
74 |
output_high(LED2); |
75 |
} |
75 |
} |
76 |
read_adc(ADC_START_ONLY); |
76 |
read_adc(ADC_START_ONLY); |
77 |
} |
77 |
} |
78 |
|
78 |
|
79 |
void sound_beep( unsigned int lenght, int16 frequency) |
79 |
void sound_beep( unsigned int lenght, int16 frequency) |
80 |
{ |
80 |
{ |
81 |
unsigned int i; |
81 |
unsigned int i; |
82 |
|
82 |
|
83 |
for(i=0;i<=lenght;i++) |
83 |
for(i=0;i<=lenght;i++) |
84 |
{ |
84 |
{ |
85 |
output_toggle(BEEP); |
85 |
output_toggle(BEEP); |
86 |
delay_us(1/frequency); |
86 |
delay_us(1/frequency); |
87 |
} |
87 |
} |
88 |
} |
88 |
} |
89 |
|
89 |
|
90 |
void main() |
90 |
void main() |
91 |
{ |
91 |
{ |
92 |
unsigned int1 button_press; // semafor pro cteni tlacitek |
92 |
unsigned int1 button_press; // semafor pro cteni tlacitek |
93 |
float output_voltage; |
93 |
float output_voltage; |
94 |
float voltage_setpoint = 300; |
94 |
float voltage_setpoint = 300; |
95 |
|
95 |
|
96 |
setup_adc_ports(sAN0|VSS_VREF); |
96 |
setup_adc_ports(sAN0|VSS_VREF); |
97 |
setup_adc(ADC_CLOCK_DIV_32); |
97 |
setup_adc(ADC_CLOCK_DIV_32); |
98 |
setup_spi(SPI_SS_DISABLED); |
98 |
setup_spi(SPI_SS_DISABLED); |
99 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
99 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
100 |
setup_timer_1(T1_DISABLED); |
100 |
setup_timer_1(T1_DISABLED); |
101 |
setup_timer_2(T2_DIV_BY_1,DIVISOR,1); |
101 |
setup_timer_2(T2_DIV_BY_1,DIVISOR,1); |
102 |
setup_ccp1(CCP_PWM); |
102 |
setup_ccp1(CCP_PWM); |
103 |
setup_ccp2(CCP_PWM); |
103 |
setup_ccp2(CCP_PWM); |
104 |
set_pwm1_duty(HALFDUTY); |
104 |
set_pwm1_duty(HALFDUTY); |
105 |
set_pwm2_duty(MAXDUTY); |
105 |
set_pwm2_duty(MAXDUTY); |
106 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
106 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
107 |
setup_oscillator(OSC_8MHZ); |
107 |
setup_oscillator(OSC_8MHZ); |
108 |
|
108 |
|
109 |
enable_interrupts(INT_AD); |
109 |
enable_interrupts(INT_AD); |
110 |
enable_interrupts(GLOBAL); |
110 |
enable_interrupts(GLOBAL); |
111 |
|
111 |
|
112 |
output_high(LED1); |
112 |
output_high(LED1); |
113 |
output_high(LED2); |
113 |
output_high(LED2); |
114 |
output_high(LED3); |
114 |
output_high(LED3); |
115 |
output_high(LED4); |
115 |
output_high(LED4); |
116 |
output_low(BEEP); |
116 |
output_low(BEEP); |
117 |
|
117 |
|
118 |
set_pwm2_duty((int16)MAXDUTY); |
118 |
set_pwm2_duty((int16)MAXDUTY); |
119 |
|
119 |
|
120 |
lcd_init(); |
120 |
lcd_init(); |
121 |
set_adc_channel(0); |
121 |
set_adc_channel(0); |
122 |
|
122 |
|
123 |
setpoint =(int16) ((voltage_setpoint - q) / k); |
123 |
setpoint =(int16) ((voltage_setpoint - q) / k); |
124 |
|
124 |
|
125 |
lcd_gotoxy(1,1); |
125 |
lcd_gotoxy(1,1); |
126 |
lcd_putc("HVPS01A"); |
126 |
lcd_putc("HVPS01A"); |
127 |
lcd_gotoxy(1,2); |
127 |
lcd_gotoxy(1,2); |
128 |
lcd_putc(" MLAB.cz"); |
128 |
lcd_putc(" MLAB.cz"); |
129 |
Delay_ms(1000); |
129 |
Delay_ms(1000); |
130 |
|
130 |
|
131 |
lcd_putc("\f"); |
131 |
lcd_putc("\f"); |
132 |
|
132 |
|
133 |
read_adc(ADC_START_ONLY); |
133 |
read_adc(ADC_START_ONLY); |
134 |
Delay_ms(100); |
134 |
Delay_ms(100); |
135 |
|
135 |
|
136 |
while(true) |
136 |
while(true) |
137 |
{ |
137 |
{ |
138 |
if(input(OUTPUT_ENABLE)) output_permit=TRUE; |
138 |
if(input(OUTPUT_ENABLE)) output_permit=TRUE; |
139 |
else output_permit=FALSE; |
139 |
else output_permit=FALSE; |
140 |
|
140 |
|
141 |
output_voltage = adc_value * k + q; // compute absalute measured voltage from adc value |
141 |
output_voltage = adc_value * k + q; // compute absalute measured voltage from adc value |
142 |
|
142 |
|
143 |
lcd_gotoxy(1,1); // output voltage out of range |
143 |
lcd_gotoxy(1,1); // output voltage out of range |
144 |
if (output_voltage > 215) printf(lcd_putc,"%3.0f",output_voltage); |
144 |
if (output_voltage > 215) printf(lcd_putc,"%4.0f",output_voltage); |
145 |
else printf(lcd_putc,"---"); |
145 |
else printf(lcd_putc,"---"); |
146 |
|
146 |
|
147 |
|
147 |
|
148 |
lcd_gotoxy(1,2); |
148 |
lcd_gotoxy(1,2); |
149 |
voltage_setpoint = setpoint * k + q; |
149 |
voltage_setpoint = setpoint * k + q; |
150 |
if(input(OUTPUT_ENABLE))printf(lcd_putc,"Set:%3.0f ", voltage_setpoint); // setpoint print |
150 |
if(input(OUTPUT_ENABLE))printf(lcd_putc,"GIV %4.0f", voltage_setpoint); // setpoint print |
151 |
else printf(lcd_putc,"DISABLED"); |
151 |
else printf(lcd_putc,"DIS %4.0f", voltage_setpoint); // print this information if voltage output is not allowed |
152 |
|
152 |
|
153 |
if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta. |
153 |
if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta. |
154 |
{ |
154 |
{ |
155 |
|
155 |
|
156 |
if(!input(S1)) |
156 |
if(!input(S1)) |
157 |
{ |
157 |
{ |
158 |
delay_ms(20); |
158 |
delay_ms(20); |
159 |
if(!input(S1)) |
159 |
if(!input(S1)) |
160 |
{ |
160 |
{ |
161 |
button_press=true; |
161 |
button_press=true; |
162 |
sound_beep(100,700); |
162 |
sound_beep(100,700); |
163 |
if(voltage_setpoint < (MAX_OUTPUT - raw_step) ) voltage_setpoint += raw_step; |
163 |
if(voltage_setpoint < (MAX_OUTPUT - raw_step) ) voltage_setpoint += raw_step; |
164 |
else voltage_setpoint = MAX_OUTPUT; |
164 |
else voltage_setpoint = MAX_OUTPUT; |
165 |
} |
165 |
} |
166 |
} |
166 |
} |
167 |
|
167 |
|
168 |
if(!input(S2)) |
168 |
if(!input(S2)) |
169 |
{ |
169 |
{ |
170 |
delay_ms(20); |
170 |
delay_ms(20); |
171 |
if(!input(S2)) |
171 |
if(!input(S2)) |
172 |
{ |
172 |
{ |
173 |
button_press=true; |
173 |
button_press=true; |
174 |
sound_beep(100,600); |
174 |
sound_beep(100,600); |
175 |
if(voltage_setpoint > (raw_step + MIN_OUTPUT) ) voltage_setpoint -= raw_step; |
175 |
if(voltage_setpoint > (raw_step + MIN_OUTPUT) ) voltage_setpoint -= raw_step; |
176 |
else voltage_setpoint = MIN_OUTPUT; |
176 |
else voltage_setpoint = MIN_OUTPUT; |
177 |
} |
177 |
} |
178 |
} |
178 |
} |
179 |
|
179 |
|
180 |
if(!input(S3)) |
180 |
if(!input(S3)) |
181 |
{ |
181 |
{ |
182 |
delay_ms(20); |
182 |
delay_ms(20); |
183 |
if(!input(S3)) |
183 |
if(!input(S3)) |
184 |
{ |
184 |
{ |
185 |
button_press=true; |
185 |
button_press=true; |
186 |
sound_beep(100,500); |
186 |
sound_beep(100,500); |
187 |
if(voltage_setpoint < MAX_OUTPUT - fine_step )voltage_setpoint += fine_step ; |
187 |
if(voltage_setpoint < MAX_OUTPUT - fine_step )voltage_setpoint += fine_step ; |
188 |
else voltage_setpoint = MAX_OUTPUT; |
188 |
else voltage_setpoint = MAX_OUTPUT; |
189 |
} |
189 |
} |
190 |
} |
190 |
} |
191 |
|
191 |
|
192 |
if(!input(S4)) |
192 |
if(!input(S4)) |
193 |
{ |
193 |
{ |
194 |
delay_ms(20); |
194 |
delay_ms(20); |
195 |
if(!input(S4)) |
195 |
if(!input(S4)) |
196 |
{ |
196 |
{ |
197 |
button_press=true; |
197 |
button_press=true; |
198 |
sound_beep(100,400); |
198 |
sound_beep(100,400); |
199 |
if(voltage_setpoint > (fine_step + MIN_OUTPUT) ) voltage_setpoint -= fine_step; |
199 |
if(voltage_setpoint > (fine_step + MIN_OUTPUT) ) voltage_setpoint -= fine_step; |
200 |
else voltage_setpoint = MIN_OUTPUT; |
200 |
else voltage_setpoint = MIN_OUTPUT; |
201 |
} |
201 |
} |
202 |
} |
202 |
} |
203 |
|
203 |
|
204 |
setpoint =(int16) ((voltage_setpoint - q) / k); |
204 |
setpoint =(int16) ((voltage_setpoint - q) / k); |
205 |
} |
205 |
} |
206 |
|
206 |
|
207 |
if ( input(S1) && input(S2) && input(S3) && input(S4) ) button_press=false; |
207 |
if ( input(S1) && input(S2) && input(S3) && input(S4) ) button_press=false; |
208 |
output_toggle(LED1); |
208 |
output_toggle(LED1); |
209 |
} |
209 |
} |
210 |
} |
210 |
} |