Rev Author Line No. Line
1045 jacho 1 #include "C:\Users\Honza\Documents\pic\uhel_sl\main.h"
2  
3  
4  
5 #define PIN_SDA PIN_B0
6 #define PIN_SCL PIN_B1
7 #use i2c(master, sda=PIN_SDA, scl=PIN_SCL)
8 #use rs232(baud=9600,parity=N,xmit=PIN_B3,rcv=PIN_B2,bits=8) //rcv TXD xmit RXD
9 #include <math.h>
10  
11 #define AK_W 0x38 //adresa akcelerometru zápis
12 #define AK_R 0x39 //adresa akcelerometru ètení
13 #define AK_XH 0x01 //osa X LSB
14 #define AK_XL 0x02 //osa X MSB
15 #define AK_YH 0x03 //osa Y LSB
16 #define AK_YL 0x04 //osa Y MSB
17 #define AK_ZH 0x05 //osa Z LSB
18 #define AK_ZL 0x06 //osa Z MSB
19  
20  
21 //pøipojení motorù
22 //AIN1 - pro vysku slunce
23 #define AIN1 PIN_D0
24 #define AIN2 PIN_D1
25 //motor A -cerveny vodic na AOUT2
26 //motor A -modry vodic na Aout1
27  
28 signed int16 X, Y, Z,AX, AY, AZ;
29  
30 void setAK (void) //nastaveni akcelerometru
31 {
32 i2c_start();
33 I2C_Write(AK_W);
34 I2C_write(0x2A);
35 I2C_write(0x01); //nastaví aktivní stav
36  
37 i2c_stop();
38 }
39  
40 int16 akcele (int8 H, int8 L) //vycitani hodnot z akcelerometru
41 {
42 unsigned int8 XL=0,XH=0;
43 signed int16 x;
44  
45 i2c_start();
46 I2C_Write(AK_W);
47 I2C_write(H);
48 i2c_start();
49 I2C_Write(AK_R);
50 XH=i2c_read(0);
51 i2c_stop();
52  
53 i2c_start();
54 I2C_Write(AK_W);
55 I2C_write(L);
56 i2c_start();
57 I2C_Write(AK_R);
58 XL=i2c_read(0);
59 i2c_stop();
60  
61 x = (((unsigned int16) XH << 8) + XL ); //prevod na 16bit hodnotu
62 x=x/4;
63  
64 return x;
65 }
66  
67 float vyska_sl (void) //vypocet aktualni vysky panelu
68 {
69 X= akcele (AK_XH, AK_XL);
70 Y= akcele (AK_YH, AK_YL);
71 Z= akcele (AK_ZH, AK_ZL);
72  
73 AX=abs(X);
74 AY=abs(Y)+250;
75 AZ=abs(Z)+250;
76  
77 float a, b;
78 a=(float)Y/Z;
79 b=atan(a);
80 b = (b/3.14)*180;
81 b=abs(b);
82  
83 if(((AX>AY) || (AX>AZ))) //indikace prevraceni panelu
84 {
85 printf("Prevracený panel)\r\n", );
86 }
87 else
88 {
89 if(Z==0) //osetreni proti deleni 0
90 {
91 if(Y>0)
92 {
93 b=180;
94 }
95 else
96 {
97 b=0;
98 }
99 }
100 else
101 {
102 if(Z>0)
103 {
104 if(Y>=0)
105 {
106 b=90+b;
107 }
108 else
109 {
110 b=90-b;
111 }
112 }
113 else
114 {
115 if(Y>=0)
116 {
117 b=180-b;
118 }
119 else
120 {
121 b=270+b;
122 }
123 }
124  
125 }
126  
127  
128  
129 }
130 //printf("uhel namìreny %10.2f \r\n", b);
131 return b;
132  
133 }
134  
135 void motorA (int8 H) //pro ovladani prvniho motoru nastaveni vysky
136 {
137 switch(H){
138 case 1: //reverzní chod
139 output_low (AIN2);
140 output_high (AIN1);
141 break;
142  
143 case 2: //dopøedu
144 output_low (AIN1);
145 output_high (AIN2);
146 break;
147  
148 default:
149 output_low (AIN2);
150 output_low (AIN1);
151 }
152 }
153  
154 void uhel (int8 H) //slouzi pro nastaveni nove vysky panelu
155 {
156 float a;
157 int16 b,c;
158  
159 a=vyska_sl();
160 b= (int16) a;
161 //printf("auhel pro mereni: %Ld(procenta)\r\n", b);
162 if(b>350)
163 {
164 b=0;
165 }
166 else
167 {
168  
169 }
170 c=abs(H-b);
171  
172 while(c>2) //maximalni odchylka uhlu, aby nebylo potreba panelem hybat
173 {
174 while(H!=b) //probiha dokud se uhel panelu nerovna zadanemu na cele stupne
175 {
176 //printf("aktualni uhel: %Ld(procenta)\r\n", b);
177 //printf("zadane nastavení: %d(procenta)\r\n", H);
178 if(H>b)
179 {
180 motorA(2);
181 //printf("zmotor2í: \r\n", );
182 }
183 else
184 {
185 motorA(1);
186 }
187  
188 delay_ms (50); //cas sepnuti motoru
189  
190 motorA(3); //vypne motor
191 delay_ms (50); //doma na ustaleni panelu pred merenim
192 a=vyska_sl();
193 b= (int16) a;
194 //printf("auhel pro mereni: %Ld(procenta)\r\n", b);
195 if(b>350) //osetreni pro uhel 0. Zabezpeci ze neprejde stav z 0 na 359 kdy by se solar zacal tocit na druhou stranu
196 {
197 b=0;
198 }
199 else
200 {
201 b=b;
202 }
203 c=abs(H-b);
204 }
205 }
206 motorA(3); //vypne motor
207 printf("Podaøené nastavení: %Ld\r\n", b);
208 }
209  
210  
211  
212  
213 void main()
214 {
215  
216 setup_adc_ports(NO_ANALOGS|VSS_VDD);
217 setup_adc(ADC_CLOCK_DIV_2);
218 setup_spi(SPI_SS_DISABLED);
219 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
220 setup_timer_1(T1_DISABLED);
221 setup_timer_2(T2_DISABLED,0,1);
222 setup_ccp1(CCP_OFF);
223 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
224  
225 setup_adc_ports(PIN_A0); //piny pro A/D RA0
226  
227 printf("Akcelerometr: \r\n",);
228  
229 motorA(3);
230  
231 float a, b;
232 unsigned int value1=10;
233  
234  
235 setAK(); //nastaveni akcelerometru
236  
237 while(TRUE)
238 {
239  
240 X= akcele (AK_XH, AK_XL);
241 Y= akcele (AK_YH, AK_YL);
242 Z= akcele (AK_ZH, AK_ZL);
243  
244 printf("Stav X: %Ld(procenta)\r\n", X);
245 printf("Stav Y: %Ld(procenta)\r\n", Y);
246 printf("Stav Z: %Ld(procenta)\r\n", Z);
247  
248  
249  
250  
251  
252  
253 set_adc_channel(0); //nastavi AD na kanál 0 (RA0)
254 read_adc(ADC_START_ONLY); // Spustime A/D prevod
255 Delay_ms(1);
256 while(!adc_done()); // Cekame na dokonceni prevodu
257 value1=read_adc(); // Precteme hodnotu z prevodniku
258 uhel (value1);
259 printf("zadaný uhel %d \r\n", value1);
260  
261  
262  
263  
264  
265 delay_ms (2000);
266  
267  
268 }
269 }
270