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