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