1045 |
jacho |
1 |
//For AKCE |
|
|
2 |
//ADRESA pro èteni 0x39 |
|
|
3 |
//pro zápis 0x38 |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
#include "C:\Users\Honza\Documents\pic\I2C_akcelerometr\main.h" |
|
|
8 |
#define EEPROM_SDA PIN_B0 |
|
|
9 |
#define EEPROM_SCL PIN_B1 |
|
|
10 |
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL) |
|
|
11 |
#use rs232(baud=9600,parity=N,xmit=PIN_B3,rcv=PIN_B2,bits=8) //rcv TXD xmit RXD |
|
|
12 |
#include <math.h> |
|
|
13 |
|
|
|
14 |
void main() |
|
|
15 |
{ |
|
|
16 |
|
|
|
17 |
setup_adc_ports(NO_ANALOGS|VSS_VDD); |
|
|
18 |
setup_adc(ADC_CLOCK_DIV_2); |
|
|
19 |
setup_spi(SPI_SS_DISABLED); |
|
|
20 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
21 |
setup_timer_1(T1_DISABLED); |
|
|
22 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
23 |
setup_ccp1(CCP_OFF); |
|
|
24 |
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard |
|
|
25 |
|
|
|
26 |
printf("Simple Thermomether \r\n",); |
|
|
27 |
|
|
|
28 |
int16 data1, data2, data3; |
|
|
29 |
signed int16 X, Y, Z; |
|
|
30 |
float a, b; |
|
|
31 |
|
|
|
32 |
unsigned int8 XL,XH,YL,YH,ZL,ZH; |
|
|
33 |
|
|
|
34 |
|
|
|
35 |
i2c_start(); |
|
|
36 |
I2C_Write(0x38); |
|
|
37 |
I2C_write(0x2A); |
|
|
38 |
|
|
|
39 |
I2C_write(0x01); |
|
|
40 |
|
|
|
41 |
i2c_stop(); |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
while(TRUE) |
|
|
46 |
{ |
|
|
47 |
|
|
|
48 |
data1=0; |
|
|
49 |
data2=0 ; |
|
|
50 |
|
|
|
51 |
|
|
|
52 |
//akcelerometr |
|
|
53 |
int1 ack; |
|
|
54 |
i2c_start(); // If the write command is acknowledged, |
|
|
55 |
ack = i2c_write(0x39);//hen the device is ready. |
|
|
56 |
i2c_stop(); |
|
|
57 |
ack=!ack; |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
i2c_stop(); |
|
|
62 |
|
|
|
63 |
i2c_start(); |
|
|
64 |
I2C_Write(0x38); |
|
|
65 |
I2C_write(0x01); |
|
|
66 |
// i2c_stop(); |
|
|
67 |
i2c_start(); |
|
|
68 |
I2C_Write(0x39); |
|
|
69 |
XH=i2c_read(0); |
|
|
70 |
i2c_stop(); |
|
|
71 |
|
|
|
72 |
i2c_start(); |
|
|
73 |
I2C_Write(0x38); |
|
|
74 |
I2C_write(0x02); |
|
|
75 |
// i2c_stop(); |
|
|
76 |
i2c_start(); |
|
|
77 |
I2C_Write(0x39); |
|
|
78 |
XL=i2c_read(0); |
|
|
79 |
i2c_stop(); |
|
|
80 |
|
|
|
81 |
i2c_start(); |
|
|
82 |
I2C_Write(0x38); |
|
|
83 |
I2C_write(0x03); |
|
|
84 |
// i2c_stop(); |
|
|
85 |
i2c_start(); |
|
|
86 |
I2C_Write(0x39); |
|
|
87 |
YH=i2c_read(0); |
|
|
88 |
i2c_stop(); |
|
|
89 |
|
|
|
90 |
i2c_start(); |
|
|
91 |
I2C_Write(0x38); |
|
|
92 |
I2C_write(0x04); |
|
|
93 |
// i2c_stop(); |
|
|
94 |
i2c_start(); |
|
|
95 |
I2C_Write(0x39); |
|
|
96 |
YL=i2c_read(0); |
|
|
97 |
i2c_stop(); |
|
|
98 |
|
|
|
99 |
i2c_start(); |
|
|
100 |
I2C_Write(0x38); |
|
|
101 |
I2C_write(0x05); |
|
|
102 |
// i2c_stop(); |
|
|
103 |
i2c_start(); |
|
|
104 |
I2C_Write(0x39); |
|
|
105 |
ZH=i2c_read(0); |
|
|
106 |
i2c_stop(); |
|
|
107 |
|
|
|
108 |
i2c_start(); |
|
|
109 |
I2C_Write(0x38); |
|
|
110 |
I2C_write(0x06); |
|
|
111 |
// i2c_stop(); |
|
|
112 |
i2c_start(); |
|
|
113 |
I2C_Write(0x39); |
|
|
114 |
ZL=i2c_read(0); |
|
|
115 |
i2c_stop(); |
|
|
116 |
|
|
|
117 |
i2c_start(); |
|
|
118 |
I2C_Write(0x38); |
|
|
119 |
I2C_write(0x00); |
|
|
120 |
// i2c_stop(); |
|
|
121 |
i2c_start(); |
|
|
122 |
I2C_Write(0x39); |
|
|
123 |
ZL=i2c_read(0); |
|
|
124 |
i2c_stop(); |
|
|
125 |
|
|
|
126 |
printf("Stav: %ud(procenta)\r\n", XL); |
|
|
127 |
printf("Stav: %ud(procenta)\r\n", XH); |
|
|
128 |
printf("Stav: %ud(procenta)\r\n", YL); |
|
|
129 |
printf("Stav: %ud(procenta)\r\n", YH); |
|
|
130 |
printf("Stav: %ud(procenta)\r\n", ZL); |
|
|
131 |
printf("Stav: %ud(procenta)\r\n", ZH); |
|
|
132 |
|
|
|
133 |
|
|
|
134 |
X = (((unsigned int16) XH << 8) + XL ); |
|
|
135 |
Y = (((unsigned int16) YH << 8) + YL); |
|
|
136 |
Z = (((unsigned int16) ZH << 8) + ZL); |
|
|
137 |
|
|
|
138 |
X=X/4; |
|
|
139 |
Y=Y/4; |
|
|
140 |
Z=Z/4; |
|
|
141 |
//X = X>>2; |
|
|
142 |
//Y = Y>>2; |
|
|
143 |
//Z = Z>>2; |
|
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
printf("Stav: %d (procenta)\r\n", ack); |
|
|
148 |
|
|
|
149 |
printf("Stavx: %Ld(procenta)\r\n", X); |
|
|
150 |
printf("Stavy: %Ld(procenta)\r\n", Y); |
|
|
151 |
printf("Stavz: %Ld(procenta)\r\n", Z); |
|
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
a=(float)Y/Z; |
|
|
156 |
printf("y/z %10.2f \r\n", a); |
|
|
157 |
b=atan(a); |
|
|
158 |
printf("atan %10.2f \r\n", b); |
|
|
159 |
b = (b/3.14)*180; |
|
|
160 |
b=abs(b); |
|
|
161 |
b=90-b; |
|
|
162 |
printf("uhel %10.2f \r\n", b); |
|
|
163 |
|
|
|
164 |
delay_ms (2000); |
|
|
165 |
|
|
|
166 |
} |
|
|
167 |
} |