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@0:ae8082690b32, 2022-08-09 (annotated)
- Committer:
- csmk18112
- Date:
- Tue Aug 09 02:42:29 2022 +0000
- Revision:
- 0:ae8082690b32
- Child:
- 3:18847ea9afbe
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 | 0:ae8082690b32 | 3 | #define MAG 0x13<<1 |
csmk18112 | 0:ae8082690b32 | 4 | Serial pc(USBTX, USBRX); |
csmk18112 | 0:ae8082690b32 | 5 | I2C i2c(p28,p27); |
csmk18112 | 0:ae8082690b32 | 6 | DigitalOut pin(p26); |
csmk18112 | 0:ae8082690b32 | 7 | |
csmk18112 | 0:ae8082690b32 | 8 | float magnet[3]; |
csmk18112 | 0:ae8082690b32 | 9 | char cmd[2]={0}; |
csmk18112 | 0:ae8082690b32 | 10 | const double dt = 1; |
csmk18112 | 0:ae8082690b32 | 11 | char send[1], get[1]; |
csmk18112 | 0:ae8082690b32 | 12 | int data[8]={0}; |
csmk18112 | 0:ae8082690b32 | 13 | char temp; |
csmk18112 | 0:ae8082690b32 | 14 | |
csmk18112 | 0:ae8082690b32 | 15 | int main() { |
csmk18112 | 0:ae8082690b32 | 16 | pin=1; |
csmk18112 | 0:ae8082690b32 | 17 | i2c.frequency(100000); |
csmk18112 | 0:ae8082690b32 | 18 | printf("magnet setting\r\n"); |
csmk18112 | 0:ae8082690b32 | 19 | cmd[0]=0x4B; |
csmk18112 | 0:ae8082690b32 | 20 | cmd[1]=0x01; |
csmk18112 | 0:ae8082690b32 | 21 | i2c.write(MAG,cmd,2); wait(0.1); |
csmk18112 | 0:ae8082690b32 | 22 | cmd[0]=0x4C; |
csmk18112 | 0:ae8082690b32 | 23 | cmd[1]=0x00; |
csmk18112 | 0:ae8082690b32 | 24 | i2c.write(MAG,cmd,2); |
csmk18112 | 0:ae8082690b32 | 25 | cmd[0]=0x4E; |
csmk18112 | 0:ae8082690b32 | 26 | cmd[1]=0x84; |
csmk18112 | 0:ae8082690b32 | 27 | i2c.write(MAG,cmd,2); |
csmk18112 | 0:ae8082690b32 | 28 | cmd[0]=0x51; |
csmk18112 | 0:ae8082690b32 | 29 | cmd[1]=0x04; |
csmk18112 | 0:ae8082690b32 | 30 | i2c.write(MAG,cmd,2); |
csmk18112 | 0:ae8082690b32 | 31 | cmd[0]=0x52; |
csmk18112 | 0:ae8082690b32 | 32 | cmd[1]=0x16; |
csmk18112 | 0:ae8082690b32 | 33 | i2c.write(MAG,cmd,2); |
csmk18112 | 0:ae8082690b32 | 34 | cmd[0]=0x00; |
csmk18112 | 0:ae8082690b32 | 35 | i2c.write(MAG,cmd,1,1); |
csmk18112 | 0:ae8082690b32 | 36 | i2c.read(MAG,cmd,1); |
csmk18112 | 0:ae8082690b32 | 37 | printf("read:0x%02x\r\n",cmd[0]); |
csmk18112 | 0:ae8082690b32 | 38 | |
csmk18112 | 0:ae8082690b32 | 39 | while(1) { |
csmk18112 | 0:ae8082690b32 | 40 | for(int i=0;i<8;i++){ |
csmk18112 | 0:ae8082690b32 | 41 | send[0]=(char)(0x42+i); |
csmk18112 | 0:ae8082690b32 | 42 | i2c.write(MAG,send,1); |
csmk18112 | 0:ae8082690b32 | 43 | i2c.read(MAG,get,1); |
csmk18112 | 0:ae8082690b32 | 44 | temp=get[0]; |
csmk18112 | 0:ae8082690b32 | 45 | data[i]=temp; |
csmk18112 | 0:ae8082690b32 | 46 | } |
csmk18112 | 0:ae8082690b32 | 47 | for(int i=0;i<3;i++){ |
csmk18112 | 0:ae8082690b32 | 48 | if(i!=2)magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 3; |
csmk18112 | 0:ae8082690b32 | 49 | else magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 1; |
csmk18112 | 0:ae8082690b32 | 50 | if(i==2 && magnet[i]>16383)magnet[i]-=32768; |
csmk18112 | 0:ae8082690b32 | 51 | else if(i!=2 && magnet[i]>4095)magnet[i]-=8092; |
csmk18112 | 0:ae8082690b32 | 52 | } |
csmk18112 | 0:ae8082690b32 | 53 | pc.printf("mx = %2.4f, my = %2.4f, mz = %2.4f\r\n\n",magnet[0],magnet[1],magnet[2]); |
csmk18112 | 0:ae8082690b32 | 54 | wait(dt); |
csmk18112 | 0:ae8082690b32 | 55 | } |
csmk18112 | 0:ae8082690b32 | 56 | } |
csmk18112 | 0:ae8082690b32 | 57 | |
csmk18112 | 0:ae8082690b32 | 58 |