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