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