Rev 2953 Rev 2978
1 //#include "C:\Users\Honza\Documents\pic\I2C_HUB\main.h" -  
2   -  
3 #include "main.h" 1 #include "main.h"
4 //I2CHUB 2 //I2CHUB
5 //ADRESA pro èteni 0xE1 3 //ADRESA pro èteni 0xE1
6 //pro zápis 0xE0 4 //pro zápis 0xE0
7   5  
8   6  
9 #define EEPROM_SDA PIN_C4 7 #define EEPROM_SDA PIN_C4
10 #define EEPROM_SCL PIN_C3 8 #define EEPROM_SCL PIN_C3
11 #use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL) 9 #use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)
12 #use rs232(baud=9600,parity=N,xmit=PIN_C7,rcv=PIN_C6,bits=8) //rcv TXD xmit RXD 10 #use rs232(baud=9600,parity=N,xmit=PIN_C7,rcv=PIN_C6,bits=8) //rcv TXD xmit RXD
13   11  
14 void setHUB(int d) //vstupuje 1-8 12 void setHUB(int d) //vstupuje 1-8
15 //hodnota udava jaky port bude pripojen na master I2C 13 //hodnota udava jaky port bude pripojen na master I2C
16 //zde je nastaveno propojeni jen jedne brany, je mozne propojit i vice bran naraz 14 //zde je nastaveno propojeni jen jedne brany, je mozne propojit i vice bran naraz
17 { 15 {
18 switch (d) { 16 switch (d) {
19   17  
20 case 1: d=0x01; 18 case 1: d=0x01;
21   19  
22 break; 20 break;
23   21  
24 case 2: d=0x02; 22 case 2: d=0x02;
25   23  
26 break; 24 break;
27 25
28 case 3: d=0x04; 26 case 3: d=0x04;
29   27  
30 break; 28 break;
31   29  
32 case 4: d=0x08; 30 case 4: d=0x08;
33   31  
34 break; 32 break;
35 case 5: d=0x10; 33 case 5: d=0x10;
36   34  
37 break; 35 break;
38   36  
39 case 6: d=0x20; 37 case 6: d=0x20;
40   38  
41 break; 39 break;
42 case 7: d=0x40; 40 case 7: d=0x40;
43   41  
44 break; 42 break;
45   43  
46 case 8: d=0x80; 44 case 8: d=0x80;
47   45  
48 break; 46 break;
49 47
50 default:printf("chyba"); 48 default:printf("chyba");
51   49  
52 break; } 50 break; }
53 51
54 i2c_start(); 52 i2c_start();
55 I2C_Write(0xE0); 53 I2C_Write(0xE0);
56 54
57 I2C_write(d); 55 I2C_write(d);
58 i2c_stop(); 56 i2c_stop();
59 -  
60 -  
61 -  
62   57  
63 } 58 }
64   59  
65   60  
66 void main() 61 void main()
67 { 62 {
68   63  
69 setup_adc_ports(NO_ANALOGS|VSS_VDD); 64 setup_adc_ports(NO_ANALOGS|VSS_VDD);
70 setup_adc(ADC_CLOCK_DIV_2); 65 setup_adc(ADC_CLOCK_DIV_2);
71 setup_spi(SPI_SS_DISABLED); 66 setup_spi(SPI_SS_DISABLED);
72 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 67 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
73 setup_timer_1(T1_DISABLED); 68 setup_timer_1(T1_DISABLED);
74 setup_timer_2(T2_DISABLED,0,1); 69 setup_timer_2(T2_DISABLED,0,1);
75 setup_ccp1(CCP_OFF); 70 setup_ccp1(CCP_OFF);
76 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard 71 setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
77   72  
78 printf("I2CHUB \r\n",); 73 printf("I2CHUB \r\n",);
79 printf("(c)MLAB 2013 JACHO \r\n",); 74 printf("(c)MLAB 2013 JACHO \r\n",);
80 unsigned int8 INTEP,INTEP1, NAP,NAP1, PROUD,PROUD1,KAPZ,KAPZ1,KAPC,KAPC1, STAV,STAV1; -  
81   -  
82   -  
83   -  
84 int16 data4; -  
85 int16 data5; -  
86 int16 data6; -  
87 int16 data7; -  
88 int16 data8; -  
89 int16 data9; -  
90   -  
91 byte XH; -  
92   -  
93   -  
94   -  
95   -  
96   75  
97 while(TRUE) 76 while(TRUE)
98 { 77 {
99   -  
100 -  
101   -  
102   -  
103 //akcelerometr -  
104 int1 ack; -  
105 i2c_start(); // If the write command is acknowledged, -  
106 ack = i2c_write(0xE1);//hen the device is ready. 78 setHUB(3); //pripoji prislusnou branu
107 i2c_stop(); -  
108 ack=!ack; -  
109 -  
110   -  
111 -  
112 delay_ms(1000); 79 delay_ms(1000);
113   -  
114   -  
115 //printf("Stav: %d (procenta)\r\n", ack); -  
116   -  
117 printf("Stavx: %X(procenta)\r\n", XH); -  
118   -  
119   -  
120   -  
121   -  
122 setHUB(3); -  
123   -  
124   -  
125   -  
126   -  
127   -  
128 -  
129 i2c_start(); -  
130 I2C_Write(0xAA); -  
131 I2C_write(0x0c); -  
132 -  
133 i2c_start(); -  
134 I2C_Write(0xAB); -  
135 INTEP=i2c_read(0); -  
136 i2c_stop(); -  
137 -  
138 i2c_start(); -  
139 I2C_Write(0xAA); -  
140 I2C_write(0x0d); -  
141 i2c_start(); -  
142 I2C_Write(0xAB); -  
143 INTEP1=i2c_read(0); -  
144 i2c_stop(); -  
145 -  
146 i2c_start(); -  
147 I2C_Write(0xAA); -  
148 I2C_write(0x08); -  
149 i2c_stop(); -  
150 i2c_start(); -  
151 I2C_Write(0xAB); -  
152 NAP=i2c_read(0); -  
153 i2c_stop(); -  
154   -  
155 i2c_start(); -  
156 I2C_Write(0xAA); -  
157 I2C_write(0x09); -  
158 i2c_stop(); -  
159 i2c_start(); -  
160 I2C_Write(0xAB); -  
161 NAP1=i2c_read(0); -  
162 i2c_stop(); -  
163 -  
164 i2c_start(); -  
165 I2C_Write(0xAA); -  
166 I2C_write(0x0a); -  
167 i2c_stop(); -  
168 i2c_start(); -  
169 I2C_Write(0xAB); -  
170 PROUD=i2c_read(0); -  
171 i2c_stop(); -  
172   -  
173 i2c_start(); -  
174 I2C_Write(0xAA); -  
175 I2C_write(0x0b); -  
176 i2c_stop(); -  
177 i2c_start(); -  
178 I2C_Write(0xAB); -  
179 PROUD1=i2c_read(0); -  
180 i2c_stop(); -  
181 -  
182 -  
183 i2c_start(); -  
184 I2C_Write(0xAA); -  
185 I2C_write(0x04); -  
186 i2c_stop(); -  
187 i2c_start(); -  
188 I2C_Write(0xAB); -  
189 KAPZ=i2c_read(0); -  
190 i2c_stop(); -  
191   -  
192 i2c_start(); -  
193 I2C_Write(0xAA); -  
194 I2C_write(0x05); -  
195 i2c_stop(); -  
196 i2c_start(); -  
197 I2C_Write(0xAB); -  
198 KAPZ1=i2c_read(0); -  
199 i2c_stop(); -  
200 -  
201 -  
202 i2c_start(); -  
203 I2C_Write(0xAA); -  
204 I2C_write(0x06); -  
205 i2c_stop(); -  
206 i2c_start(); -  
207 I2C_Write(0xAB); -  
208 KAPC=i2c_read(0); -  
209 i2c_stop(); -  
210   -  
211 i2c_start(); -  
212 I2C_Write(0xAA); -  
213 I2C_write(0x07); -  
214 i2c_stop(); -  
215 i2c_start(); -  
216 I2C_Write(0xAB); -  
217 KAPC1=i2c_read(0); -  
218 i2c_stop(); -  
219 -  
220 i2c_start(); -  
221 I2C_Write(0xAA); -  
222 I2C_write(0x02); -  
223 i2c_stop(); -  
224 i2c_start(); -  
225 I2C_Write(0xAB); -  
226 STAV=i2c_read(0); -  
227 i2c_stop(); -  
228   -  
229 i2c_start(); -  
230 I2C_Write(0xAA); -  
231 I2C_write(0x03); -  
232 i2c_stop(); -  
233 i2c_start(); -  
234 I2C_Write(0xAB); -  
235 STAV1=i2c_read(0); -  
236 i2c_stop(); -  
237 -  
238   -  
239 data4 = (((unsigned int16) INTEP1 << 8) + INTEP ); -  
240 data4 = (data4/10)-273; -  
241 data5 = (((unsigned int16) NAP1 << 8) + NAP ); -  
242 data6 = (((unsigned int16) PROUD1 << 8) + PROUD ); -  
243 data7 = (((unsigned int16) KAPZ1 << 8) + KAPZ ); -  
244 data8 = (((unsigned int16) KAPC1 << 8) + KAPC ); -  
245 data9 = (((unsigned int16) STAV1 << 8) + STAV ); -  
246   -  
247 -  
248 -  
249   -  
250   -  
251 printf("Teplota uvnitr io: %Ld C\r\n", data4); -  
252 printf("Napeti clanku: %Ld mV \r\n", data5); -  
253 printf("Proud: %Ld mA \r\n", data6); -  
254 printf("Kapacita zbytek: %Ld mAh\r\n", data7); -  
255 printf("Kapacita max: %Ld mAh\r\n", data8); -  
256 printf("Stav: %Ld (procenta)\r\n", data9); -  
257   -  
258   -  
259 delay_ms (2000); -  
260 -  
261 -  
262 -  
263 } 80 }
264 } 81 }
265   82