Rev 2744 Rev 3021
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 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5 8 #define S1 PIN_B0 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
9 #define S2 PIN_B1 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5 9 #define S2 PIN_B1 //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
10 #define S3 PIN_D0 //CHANGE PIN_XX TO YOUR LED PIN NUMBER 10 #define S3 PIN_D0 //CHANGE PIN_XX TO YOUR LED PIN NUMBER
11 #define S4 PIN_D1 //CHANGE PIN_XX TO YOUR LED PIN NUMBER 11 #define S4 PIN_D1 //CHANGE PIN_XX TO YOUR LED PIN NUMBER
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 void sound_beep( unsigned int lenght, int16 frequency) 24 void sound_beep( unsigned int lenght, int16 frequency)
25 { 25 {
26 unsigned int i; 26 unsigned int i;
27 27
28 for(i=0;i<=lenght;i++) 28 for(i=0;i<=lenght;i++)
29 { 29 {
30 output_toggle(BEEP); 30 output_toggle(BEEP);
31 delay_us(1/frequency); 31 delay_us(1/frequency);
32 } 32 }
33 } 33 }
34   34  
35 void main() 35 void main()
36 { 36 {
37 unsigned int16 setpoint=43; 37 unsigned int16 setpoint=43;
38 unsigned int16 napeti; 38 unsigned int16 napeti;
39 unsigned int16 plneni=0; 39 unsigned int16 plneni=0;
40 unsigned int1 button_press; // semafor pro cteni tlacitek 40 unsigned int1 button_press; // semafor pro cteni tlacitek
41   41  
42 setup_adc_ports(sAN0|VSS_VDD); 42 setup_adc_ports(sAN0|VSS_VDD);
43 setup_adc(ADC_CLOCK_DIV_32); 43 setup_adc(ADC_CLOCK_DIV_32);
44 setup_spi(SPI_SS_DISABLED); 44 setup_spi(SPI_SS_DISABLED);
45 setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); 45 setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
46 setup_timer_1(T1_DISABLED); 46 setup_timer_1(T1_DISABLED);
47 setup_timer_2(T2_DIV_BY_1,255,1); 47 setup_timer_2(T2_DIV_BY_1,255,1);
48 setup_ccp1(CCP_PWM); 48 setup_ccp1(CCP_PWM);
49 setup_ccp2(CCP_PWM); 49 setup_ccp2(CCP_PWM);
50 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard 50 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
51 setup_oscillator(OSC_8MHZ); 51 setup_oscillator(OSC_8MHZ);
52   52  
53 set_pwm1_duty(0); 53 set_pwm1_duty(0);
54 set_pwm2_duty(0); 54 set_pwm2_duty(0);
55   55  
56 output_high(LED1); 56 output_high(LED1);
57 output_high(LED2); 57 output_high(LED2);
58 output_high(LED3); 58 output_high(LED3);
59 output_high(LED4); 59 output_high(LED4);
60 output_low(BEEP); 60 output_low(BEEP);
61   61  
62 lcd_init(); 62 lcd_init();
63   63
-   64 lcd_gotoxy(1,1);
64 lcd_putc("\fHVPS01A 2013 MLAB\n"); 65 lcd_putc("HVPS01A");
-   66 lcd_gotoxy(1,2);
-   67 lcd_putc(" MLAB.cz");
65 Delay_ms(1000); 68 Delay_ms(1000);
66 69
67 lcd_putc("\f"); 70 lcd_putc("\f");
68 71
69 while(true) 72 while(true)
70 { 73 {
71 74
72 set_adc_channel(1); 75 set_adc_channel(1);
73 delay_us(100); 76 delay_us(100);
74 napeti = (napeti+read_adc())/2; 77 napeti = (napeti+read_adc())/2;
75   78  
76 if (napeti <= setpoint) 79 if (napeti <= setpoint)
77 { 80 {
78 if (plneni < 300) plneni++; 81 if (plneni < 300) plneni++;
79 output_low(LED2); 82 output_low(LED2);
80 } 83 }
81 else 84 else
82 { 85 {
83 if (plneni > 0) plneni--; 86 if (plneni > 0) plneni--;
84 output_high(LED2); 87 output_high(LED2);
85 } 88 }
86 set_pwm1_duty(plneni); 89 set_pwm1_duty(plneni);
87 set_pwm2_duty(1023-plneni); 90 set_pwm2_duty(1023-plneni);
88 91
89 lcd_gotoxy(1,1); 92 lcd_gotoxy(1,1);
90 printf(lcd_putc,"Actual: %lu V ",napeti); 93 printf(lcd_putc,"%lu",napeti);
91 lcd_gotoxy(1,2); 94 lcd_gotoxy(1,2);
92 printf(lcd_putc,"Set: %lu V ",setpoint); 95 printf(lcd_putc,"Set:%lu"setpoint);
93   -  
94   96  
95 if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta. 97 if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta.
96 { 98 {
97 if(!input(S1)) 99 if(!input(S1))
98 { 100 {
99 delay_ms(20); 101 delay_ms(20);
100 if(!input(S1)) 102 if(!input(S1))
101 { 103 {
102 button_press=true; 104 button_press=true;
103 sound_beep(100,700); 105 sound_beep(100,700);
104 if(setpoint < 0xfa )setpoint+=5; 106 if(setpoint < (1023-5) )setpoint+=5;
105 } 107 }
106 } 108 }
107   109  
108 if(!input(S2)) 110 if(!input(S2))
109 { 111 {
110 delay_ms(20); 112 delay_ms(20);
111 if(!input(S2)) 113 if(!input(S2))
112 { 114 {
113 button_press=true; 115 button_press=true;
114 sound_beep(100,600); 116 sound_beep(100,600);
115 if(setpoint > 0x05 ) setpoint-=5; 117 if(setpoint > 0x05 ) setpoint-=5;
116 } 118 }
117 } 119 }
118   120  
119 if(!input(S3)) 121 if(!input(S3))
120 { 122 {
121 delay_ms(20); 123 delay_ms(20);
122 if(!input(S3)) 124 if(!input(S3))
123 { 125 {
124 button_press=true; 126 button_press=true;
125 sound_beep(100,500); 127 sound_beep(100,500);
126 if(setpoint < 0xff )setpoint++; 128 if(setpoint < 1023 )setpoint++;
127 } 129 }
128 } 130 }
129   131  
130 if(!input(S4)) 132 if(!input(S4))
131 { 133 {
132 delay_ms(20); 134 delay_ms(20);
133 if(!input(S4)) 135 if(!input(S4))
134 { 136 {
135 button_press=true; 137 button_press=true;
136 sound_beep(100,400); 138 sound_beep(100,400);
137 if(setpoint > 0x00 ) setpoint--; 139 if(setpoint > 0x00 ) setpoint--;
138 } 140 }
139 } 141 }
140 } 142 }
141 143
142 if ( input(S1) && input(S2) && input(S3) && input(S4) ) button_press=false; 144 if ( input(S1) && input(S2) && input(S3) && input(S4) ) button_press=false;
143   145  
144 output_toggle(LED1); 146 output_toggle(LED1);
145 delay_ms(10); 147 delay_ms(10);
146 } 148 }
147   149  
148 } 150 }