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 |
|
3308 |
kaklik |
37 |
unsigned int16 setpoint=43; |
|
|
38 |
unsigned int16 adc_value; |
|
|
39 |
int1 output_permit=FALSE; |
|
|
40 |
|
|
|
41 |
#INT_AD |
|
|
42 |
void adc_handler() { |
|
|
43 |
|
3309 |
kaklik |
44 |
adc_value = read_adc(ADC_READ_ONLY); |
3308 |
kaklik |
45 |
if(output_permit == TRUE) |
|
|
46 |
{ |
|
|
47 |
if (adc_value <= setpoint) |
|
|
48 |
{ |
|
|
49 |
set_pwm2_duty((int16)255); // zapnuti menice |
|
|
50 |
output_low(LED2); |
|
|
51 |
} |
|
|
52 |
else // vypnuti menice |
|
|
53 |
{ |
|
|
54 |
set_pwm2_duty((int16)MAXDUTY); |
|
|
55 |
output_high(LED2); |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
} |
|
|
59 |
else |
|
|
60 |
{ |
|
|
61 |
set_pwm2_duty((int16)MAXDUTY); |
|
|
62 |
output_high(LED2); |
|
|
63 |
} |
3310 |
kaklik |
64 |
read_adc(ADC_START_ONLY); |
3308 |
kaklik |
65 |
} |
|
|
66 |
|
2743 |
kaklik |
67 |
void sound_beep( unsigned int lenght, int16 frequency) |
2600 |
kaklik |
68 |
{ |
2743 |
kaklik |
69 |
unsigned int i; |
|
|
70 |
|
|
|
71 |
for(i=0;i<=lenght;i++) |
|
|
72 |
{ |
|
|
73 |
output_toggle(BEEP); |
|
|
74 |
delay_us(1/frequency); |
|
|
75 |
} |
2600 |
kaklik |
76 |
} |
|
|
77 |
|
2596 |
kaklik |
78 |
void main() |
3308 |
kaklik |
79 |
{ |
2743 |
kaklik |
80 |
unsigned int1 button_press; // semafor pro cteni tlacitek |
2596 |
kaklik |
81 |
|
3302 |
kaklik |
82 |
setup_adc_ports(sAN0|VSS_VREF); |
3305 |
kaklik |
83 |
setup_adc(ADC_CLOCK_DIV_32); |
2596 |
kaklik |
84 |
setup_spi(SPI_SS_DISABLED); |
|
|
85 |
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); |
|
|
86 |
setup_timer_1(T1_DISABLED); |
3296 |
kaklik |
87 |
setup_timer_2(T2_DIV_BY_1,DIVISOR,1); |
3299 |
kaklik |
88 |
setup_ccp1(CCP_PWM); |
|
|
89 |
setup_ccp2(CCP_PWM); |
3296 |
kaklik |
90 |
set_pwm1_duty(HALFDUTY); |
|
|
91 |
set_pwm2_duty(MAXDUTY); |
2596 |
kaklik |
92 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
|
|
93 |
setup_oscillator(OSC_8MHZ); |
|
|
94 |
|
3308 |
kaklik |
95 |
enable_interrupts(INT_AD); |
|
|
96 |
enable_interrupts(GLOBAL); |
3305 |
kaklik |
97 |
|
2600 |
kaklik |
98 |
output_high(LED1); |
|
|
99 |
output_high(LED2); |
|
|
100 |
output_high(LED3); |
|
|
101 |
output_high(LED4); |
2744 |
kaklik |
102 |
output_low(BEEP); |
2600 |
kaklik |
103 |
|
3309 |
kaklik |
104 |
set_pwm2_duty((int16)MAXDUTY); |
|
|
105 |
|
2744 |
kaklik |
106 |
lcd_init(); |
3296 |
kaklik |
107 |
set_adc_channel(0); |
3021 |
kaklik |
108 |
|
|
|
109 |
lcd_gotoxy(1,1); |
|
|
110 |
lcd_putc("HVPS01A"); |
|
|
111 |
lcd_gotoxy(1,2); |
|
|
112 |
lcd_putc(" MLAB.cz"); |
2744 |
kaklik |
113 |
Delay_ms(1000); |
2596 |
kaklik |
114 |
|
2743 |
kaklik |
115 |
lcd_putc("\f"); |
3296 |
kaklik |
116 |
|
3311 |
kaklik |
117 |
read_adc(ADC_START_ONLY); |
2596 |
kaklik |
118 |
|
|
|
119 |
while(true) |
|
|
120 |
{ |
3308 |
kaklik |
121 |
if(input(OUTPUT_ENABLE)) output_permit=TRUE; |
|
|
122 |
else output_permit=FALSE; |
2743 |
kaklik |
123 |
|
|
|
124 |
lcd_gotoxy(1,1); |
3308 |
kaklik |
125 |
printf(lcd_putc,"%4lu",adc_value); |
2743 |
kaklik |
126 |
lcd_gotoxy(1,2); |
2596 |
kaklik |
127 |
|
3023 |
kaklik |
128 |
if(input(OUTPUT_ENABLE))printf(lcd_putc,"Set:%lu "setpoint); |
|
|
129 |
else printf(lcd_putc,"DISABLED"); |
|
|
130 |
|
2743 |
kaklik |
131 |
if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta. |
|
|
132 |
{ |
|
|
133 |
if(!input(S1)) |
|
|
134 |
{ |
|
|
135 |
delay_ms(20); |
|
|
136 |
if(!input(S1)) |
|
|
137 |
{ |
|
|
138 |
button_press=true; |
|
|
139 |
sound_beep(100,700); |
3302 |
kaklik |
140 |
if(setpoint < (MAX_OUTPUT-5) )setpoint+=5; |
2743 |
kaklik |
141 |
} |
|
|
142 |
} |
|
|
143 |
|
|
|
144 |
if(!input(S2)) |
|
|
145 |
{ |
|
|
146 |
delay_ms(20); |
|
|
147 |
if(!input(S2)) |
|
|
148 |
{ |
|
|
149 |
button_press=true; |
|
|
150 |
sound_beep(100,600); |
|
|
151 |
if(setpoint > 0x05 ) setpoint-=5; |
|
|
152 |
} |
|
|
153 |
} |
|
|
154 |
|
|
|
155 |
if(!input(S3)) |
|
|
156 |
{ |
|
|
157 |
delay_ms(20); |
|
|
158 |
if(!input(S3)) |
|
|
159 |
{ |
|
|
160 |
button_press=true; |
|
|
161 |
sound_beep(100,500); |
3302 |
kaklik |
162 |
if(setpoint < MAX_OUTPUT )setpoint++; |
2743 |
kaklik |
163 |
} |
|
|
164 |
} |
|
|
165 |
|
|
|
166 |
if(!input(S4)) |
|
|
167 |
{ |
|
|
168 |
delay_ms(20); |
|
|
169 |
if(!input(S4)) |
|
|
170 |
{ |
|
|
171 |
button_press=true; |
|
|
172 |
sound_beep(100,400); |
|
|
173 |
if(setpoint > 0x00 ) setpoint--; |
|
|
174 |
} |
|
|
175 |
} |
|
|
176 |
} |
|
|
177 |
|
|
|
178 |
if ( input(S1) && input(S2) && input(S3) && input(S4) ) button_press=false; |
2600 |
kaklik |
179 |
output_toggle(LED1); |
2596 |
kaklik |
180 |
} |
|
|
181 |
|
|
|
182 |
} |