Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Three-Axis Digital Compass IC HMC5883L

Status
Not open for further replies.
dear Bigdog,
I'm unable to convert the hex data to decimal. can you pls guide me.
I'm interfacing hmc 5883l with at89s52 in Keil U Vision 4.
thanks in advance
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include<reg51.h>
#define addwr 0x3c
#define addrd 0x3d
#define lcd P1
sbit scl=P3^6;
sbit sda=P3^7;
sbit rs=P3^5;
sbit en=P3^4;
unsigned int ch,ch0,ch1,ch2,ch3,ch4,ch5,a,b,c,d,e;
unsigned int b1,c1,d1,z1,z2,z3;
void lcddata();
 
 
void delay (unsigned int time)
{
unsigned int i,j;
for(j=0;j<time;j++)
for(i=0;i<=1275;i++);
} 
 
void start()
{
scl=1;
sda=1;
sda=0;
scl=0;
}
 
void ack()
{
sda=0;
scl=1;
scl=0;
sda=1;
}
 
void nack()
{
scl=1;
sda=1;
scl=0;
sda=0;
}
 
void stop()
{
scl=0;
sda=0;
sda=1;
scl=0; 
}
 
 
void i2c_out_byte(unsigned char o_byte)
{
unsigned int i;
for(i=0;i<8;i++)
      {
      
      if(o_byte & 0x80)
      {
            sda=1;
      }
      else
      {
            sda=0;
      }
      scl=1;
      scl=0;
      o_byte=o_byte<<1;
 
      }
      sda=1;
}
    
         
unsigned int i2c_in_byte(void)
{
unsigned int i_byte,i;
sda=1;
for(i=0;i<8;i++)
 {
    scl=1;
    if(sda==1)
         {
         i_byte=(i_byte<<1)|0x01;
         }
else
         {
         i_byte=(i_byte<<1);
         }
     scl=0;
  }
  return (i_byte);
 
}    
 
 
void lcdcmd(unsigned char ch)
{
 lcd=ch;
 rs=0;
 en=1;
 delay(1);
 en=0;
}
void lcddata(unsigned char ch)
{
 lcd=ch;
 rs=1;
 en=1;
 delay(1);
 en=0;
 
} 
void lcdinit()
{
 lcdcmd(0x38);
 delay(150);
 lcdcmd(0x0e);
 delay(150);
 lcdcmd(0x01);
 delay(150);
 lcdcmd(0x06);
 delay(150);
 }          
void adc_conversion1(unsigned int ch1)
{
      unsigned int kk;
  
        kk=ch1; 
    b1=kk%10;
    c1=kk/10;
    d1=c1%10;
    z1=c1/10;
    z2=z1%10;
    z3=z1/10;
 
    z2=z2|0x30;
    z3=z3|0x30;
    d1=d1|0x30;
    b1=b1|0x30;
//   lcdcmd(0x80);
    lcddata(z2);
    lcddata(z3);
    lcddata(d1);    
    lcddata(b1);    
}
 
void lcd_ser(unsigned char *ptr)
{
while(*ptr)
    {
    lcddata (*ptr++);
    }
}
    
 
 
 
void main()
{
 
lcdinit ();  
lcddata(0x80);
lcd_ser(" compass "); 
 
start();
i2c_out_byte(0x3c);
nack();
i2c_out_byte(0x00);
nack();
i2c_out_byte(0x70);
nack();
stop();
 
start();
i2c_out_byte(0x3c);
nack();
i2c_out_byte(0x01);
nack();
i2c_out_byte(0xA0);
nack();
stop();
 
start();
i2c_out_byte(0x3c);
nack();
i2c_out_byte(0x02);
nack();
i2c_out_byte(0x00);
nack();
stop();   
delay(6); 
    while(1)
    {
 
    lcdcmd(0x01);
 
 
 
//////////////////////////////////////////////reading all registers//////////////////
 
    start();
    i2c_out_byte(0x3d);
    nack();
    i2c_out_byte(0x06);
    nack();
 
    ch=i2c_in_byte();    //reading x msb
    ack();
    ch1=i2c_in_byte();   //reading x lsb
    ack();
    ch2=i2c_in_byte();   //reading y msb
    ack();
    ch3=i2c_in_byte();   //reading y lsb
    ack();
    ch4=i2c_in_byte();   //reading z msb
    ack();
    ch5=i2c_in_byte();   //reading z lsb
    nack();
    stop();
 
            
//////////////////////////////x axis conversion//////////////////
 
    
 
/*  ch=ch&0x00ff;       //conversion part
    ch=ch-0x01;
    ch=~ch;             //
                        //
                        //this is an experiment which i tried for converting the data of different axis
    a=ch&0x0f;          //
    b=ch&0xf0;          //
    c=(b*16)+a;
 
    lcdcmd(0x01);
    lcdcmd(0x80);
    adc_conversion1(c);
 
 
    ch1=ch1-1;
    ch1=~ch1;
 
    a=ch1&0x0f;
    b=ch1&0xf0;
    a=a*16*16;
    b=b>>4;
    b=b*16*16*16;
    d=a+b;
    e=d+c;
    lcdcmd(0xc0);
    adc_conversion1(e); */
 
    ch=(ch<<8)|ch1;
    adc_conversion1(ch);
 
///////////////////////////////////y axis conversion////////////////
 
    ch2=ch2&0x00ff;
    ch2=ch2-0x01;
    ch2=~ch2;
 
 
    a=ch2&0x0f;
    b=ch2&0xf0;
    c=(b*16)+a;
 
//  lcdcmd(0x01);
    lcdcmd(0x85);
    adc_conversion1(c);
 
 
    ch3=ch3-1;
    ch3=~ch3;
 
    a=ch3&0x0f;
    b=ch3&0xf0;
    b=b>>4;
    c=(a*10)+b;
    lcdcmd(0xc5);
    adc_conversion1(c);
 
/////////////////////////////////////////////z axis conversion////////////////
 
    ch4=ch4&0x00ff;
    ch4=ch4-0x01;
    ch4=~ch4;
 
 
    a=ch4&0x0f;
    b=ch4&0xf0;
    c=(b*16)+a;
 
//  lcdcmd(0x01);
    lcdcmd(0x8a);
    adc_conversion1(c);
 
 
    ch5=ch5-1;
    ch5=~ch5;
 
    a=ch5&0x0f;
    b=ch5&0xf0;
    b=b>>4;
    c=(a*10)+b;
    lcdcmd(0xca);
    adc_conversion1(c);
/////////////////////////////////////////sending 0x3c and 0x03 to start from beginning again/////////////
    start();
    i2c_out_byte(0x3c);
    nack();
    i2c_out_byte(0x03);
    nack();
    stop();
 
    delay(200);
    }  
}

 
Last edited by a moderator:

hi bigdog,
did you find any errors in my code??
if yes pls update me so that i can correct them
 

hi,
i'm getting the readings from the hmc5883l sensor. the problem was with the sensor. the program worked with a new sensor.

now i have a doubt. i.e. how should i convert the readings to +ve and -ve values in at89s52. for better understanding i'm typing a example below.

x: 15 y: -12 z: -131
x: 12 y: -15 z: -135
x: 14 y: -18 z: -132
x: 14 y: -16 z: -133
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top