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