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