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