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