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