Rev 2743 Rev 2744
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; 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 lcd_init(); -  
43   -  
44 lcd_putc("\fHVPS01A 2013 MLAB\n"); -  
45 Delay_ms(1000); -  
46   -  
47 setup_adc_ports(sAN0|VSS_VDD); 42 setup_adc_ports(sAN0|VSS_VDD);
48 setup_adc(ADC_CLOCK_DIV_32); 43 setup_adc(ADC_CLOCK_DIV_32);
49 setup_spi(SPI_SS_DISABLED); 44 setup_spi(SPI_SS_DISABLED);
50 setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); 45 setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
51 setup_timer_1(T1_DISABLED); 46 setup_timer_1(T1_DISABLED);
52 setup_timer_2(T2_DIV_BY_1,255,1); 47 setup_timer_2(T2_DIV_BY_1,255,1);
53 setup_ccp1(CCP_PWM); 48 setup_ccp1(CCP_PWM);
54 setup_ccp2(CCP_PWM); 49 setup_ccp2(CCP_PWM);
55 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
56 setup_oscillator(OSC_8MHZ); 51 setup_oscillator(OSC_8MHZ);
57   52  
58 set_pwm1_duty(0); 53 set_pwm1_duty(0);
59 set_pwm2_duty(0); 54 set_pwm2_duty(0);
60   55  
61   -  
62 output_high(LED1); 56 output_high(LED1);
63 output_high(LED2); 57 output_high(LED2);
64 output_high(LED3); 58 output_high(LED3);
65 output_high(LED4); 59 output_high(LED4);
-   60 output_low(BEEP);
-   61  
-   62 lcd_init();
66   63  
67 // ext_int_edge( L_TO_H ); // Sets up EXT 64 lcd_putc("\fHVPS01A 2013 MLAB\n");
68 // enable_interrupts(INT_EXT); -  
69 // enable_interrupts(GLOBAL); 65 Delay_ms(1000);
70 66
71 setpoint = 43; -  
72 lcd_putc("\f"); 67 lcd_putc("\f");
73 68
74 while(true) 69 while(true)
75 { 70 {
76 71
77 set_adc_channel(1); 72 set_adc_channel(1);
78 delay_us(100); 73 delay_us(100);
79 napeti = (napeti+read_adc())/2; 74 napeti = (napeti+read_adc())/2;
80   75  
81 if (napeti <= setpoint) 76 if (napeti <= setpoint)
82 { 77 {
83 if (plneni < 300) plneni++; 78 if (plneni < 300) plneni++;
84 output_low(LED2); 79 output_low(LED2);
85 } 80 }
86 else 81 else
87 { 82 {
88 if (plneni > 0) plneni--; 83 if (plneni > 0) plneni--;
89 output_high(LED2); 84 output_high(LED2);
90 } 85 }
91 set_pwm1_duty(plneni); 86 set_pwm1_duty(plneni);
92 set_pwm2_duty(1023-plneni); 87 set_pwm2_duty(1023-plneni);
93 88
94 lcd_gotoxy(1,1); 89 lcd_gotoxy(1,1);
95 printf(lcd_putc,"Actual: %lu V ",napeti); 90 printf(lcd_putc,"Actual: %lu V ",napeti);
96 lcd_gotoxy(1,2); 91 lcd_gotoxy(1,2);
97 printf(lcd_putc,"Set: %lu V ",setpoint); 92 printf(lcd_putc,"Set: %lu V ",setpoint);
98   93  
99   94  
100 if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta. 95 if(button_press==false ) // tlacitka se ctou znovu pouze pokud v redchozim cyklu nebyla zmacknuta.
101 { 96 {
102 if(!input(S1)) 97 if(!input(S1))
103 { 98 {
104 delay_ms(20); 99 delay_ms(20);
105 if(!input(S1)) 100 if(!input(S1))
106 { 101 {
107 button_press=true; 102 button_press=true;
108 sound_beep(100,700); 103 sound_beep(100,700);
109 if(setpoint < 0xfa )setpoint+=5; 104 if(setpoint < 0xfa )setpoint+=5;
110 } 105 }
111 } 106 }
112   107  
113 if(!input(S2)) 108 if(!input(S2))
114 { 109 {
115 delay_ms(20); 110 delay_ms(20);
116 if(!input(S2)) 111 if(!input(S2))
117 { 112 {
118 button_press=true; 113 button_press=true;
119 sound_beep(100,600); 114 sound_beep(100,600);
120 if(setpoint > 0x05 ) setpoint-=5; 115 if(setpoint > 0x05 ) setpoint-=5;
121 } 116 }
122 } 117 }
123   118  
124 if(!input(S3)) 119 if(!input(S3))
125 { 120 {
126 delay_ms(20); 121 delay_ms(20);
127 if(!input(S3)) 122 if(!input(S3))
128 { 123 {
129 button_press=true; 124 button_press=true;
130 sound_beep(100,500); 125 sound_beep(100,500);
131 if(setpoint < 0xff )setpoint++; 126 if(setpoint < 0xff )setpoint++;
132 } 127 }
133 } 128 }
134   129  
135 if(!input(S4)) 130 if(!input(S4))
136 { 131 {
137 delay_ms(20); 132 delay_ms(20);
138 if(!input(S4)) 133 if(!input(S4))
139 { 134 {
140 button_press=true; 135 button_press=true;
141 sound_beep(100,400); 136 sound_beep(100,400);
142 if(setpoint > 0x00 ) setpoint--; 137 if(setpoint > 0x00 ) setpoint--;
143 } 138 }
144 } 139 }
145 } 140 }
146 141
147 if ( input(S1) && input(S2) && input(S3) && input(S4) ) button_press=false; 142 if ( input(S1) && input(S2) && input(S3) && input(S4) ) button_press=false;
148   143  
149 output_toggle(LED1); 144 output_toggle(LED1);
150 delay_ms(10); 145 delay_ms(10);
151 } 146 }
152   147  
153 } 148 }