Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@3:18847ea9afbe, 2022-08-09 (annotated)
- Committer:
- csmk18112
- Date:
- Tue Aug 09 06:26:00 2022 +0000
- Revision:
- 3:18847ea9afbe
- Parent:
- 0:ae8082690b32
- Child:
- 4:e3238cb56cb7
mako
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
csmk18112 | 0:ae8082690b32 | 1 | #include "mbed.h" |
csmk18112 | 0:ae8082690b32 | 2 | #include "HEPTA_EPS.h" |
csmk18112 | 3:18847ea9afbe | 3 | #define MAG 0x13<<1 //addr_compus |
csmk18112 | 3:18847ea9afbe | 4 | |
csmk18112 | 0:ae8082690b32 | 5 | Serial pc(USBTX, USBRX); |
csmk18112 | 3:18847ea9afbe | 6 | HEPTA_EPS eps(p16,p26); |
csmk18112 | 0:ae8082690b32 | 7 | I2C i2c(p28,p27); |
csmk18112 | 0:ae8082690b32 | 8 | |
csmk18112 | 0:ae8082690b32 | 9 | float magnet[3]; |
csmk18112 | 0:ae8082690b32 | 10 | char cmd[2]={0}; |
csmk18112 | 0:ae8082690b32 | 11 | char send[1], get[1]; |
csmk18112 | 0:ae8082690b32 | 12 | int data[8]={0}; |
csmk18112 | 3:18847ea9afbe | 13 | char temp; //temperature |
csmk18112 | 0:ae8082690b32 | 14 | |
csmk18112 | 0:ae8082690b32 | 15 | int main() { |
csmk18112 | 0:ae8082690b32 | 16 | i2c.frequency(100000); |
csmk18112 | 3:18847ea9afbe | 17 | pc.printf("magnet setting\r\n"); |
csmk18112 | 0:ae8082690b32 | 18 | cmd[0]=0x4B; |
csmk18112 | 0:ae8082690b32 | 19 | cmd[1]=0x01; |
csmk18112 | 3:18847ea9afbe | 20 | i2c.write(MAG,cmd,2); |
csmk18112 | 0:ae8082690b32 | 21 | cmd[0]=0x4C; |
csmk18112 | 0:ae8082690b32 | 22 | cmd[1]=0x00; |
csmk18112 | 0:ae8082690b32 | 23 | i2c.write(MAG,cmd,2); |
csmk18112 | 0:ae8082690b32 | 24 | cmd[0]=0x4E; |
csmk18112 | 0:ae8082690b32 | 25 | cmd[1]=0x84; |
csmk18112 | 0:ae8082690b32 | 26 | i2c.write(MAG,cmd,2); |
csmk18112 | 0:ae8082690b32 | 27 | cmd[0]=0x51; |
csmk18112 | 0:ae8082690b32 | 28 | cmd[1]=0x04; |
csmk18112 | 0:ae8082690b32 | 29 | i2c.write(MAG,cmd,2); |
csmk18112 | 0:ae8082690b32 | 30 | cmd[0]=0x52; |
csmk18112 | 0:ae8082690b32 | 31 | cmd[1]=0x16; |
csmk18112 | 0:ae8082690b32 | 32 | i2c.write(MAG,cmd,2); |
csmk18112 | 0:ae8082690b32 | 33 | cmd[0]=0x00; |
csmk18112 | 0:ae8082690b32 | 34 | i2c.write(MAG,cmd,1,1); |
csmk18112 | 0:ae8082690b32 | 35 | i2c.read(MAG,cmd,1); |
csmk18112 | 0:ae8082690b32 | 36 | while(1) { |
csmk18112 | 0:ae8082690b32 | 37 | for(int i=0;i<8;i++){ |
csmk18112 | 0:ae8082690b32 | 38 | send[0]=(char)(0x42+i); |
csmk18112 | 0:ae8082690b32 | 39 | i2c.write(MAG,send,1); |
csmk18112 | 0:ae8082690b32 | 40 | i2c.read(MAG,get,1); |
csmk18112 | 0:ae8082690b32 | 41 | temp=get[0]; |
csmk18112 | 0:ae8082690b32 | 42 | data[i]=temp; |
csmk18112 | 0:ae8082690b32 | 43 | } |
csmk18112 | 0:ae8082690b32 | 44 | for(int i=0;i<3;i++){ |
csmk18112 | 0:ae8082690b32 | 45 | if(i!=2)magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 3; |
csmk18112 | 0:ae8082690b32 | 46 | else magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 1; |
csmk18112 | 0:ae8082690b32 | 47 | if(i==2 && magnet[i]>16383)magnet[i]-=32768; |
csmk18112 | 0:ae8082690b32 | 48 | else if(i!=2 && magnet[i]>4095)magnet[i]-=8092; |
csmk18112 | 0:ae8082690b32 | 49 | } |
csmk18112 | 0:ae8082690b32 | 50 | pc.printf("mx = %2.4f, my = %2.4f, mz = %2.4f\r\n\n",magnet[0],magnet[1],magnet[2]); |
csmk18112 | 3:18847ea9afbe | 51 | wait(1); |
csmk18112 | 0:ae8082690b32 | 52 | } |
csmk18112 | 3:18847ea9afbe | 53 | } |