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