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
- Committer:
- csmk18112
- Date:
- 2022-08-09
- Revision:
- 0:ae8082690b32
- Child:
- 3:18847ea9afbe
File content as of revision 0:ae8082690b32:
#include "mbed.h" #include "HEPTA_EPS.h" #define MAG 0x13<<1 Serial pc(USBTX, USBRX); I2C i2c(p28,p27); DigitalOut pin(p26); float magnet[3]; char cmd[2]={0}; const double dt = 1; char send[1], get[1]; int data[8]={0}; char temp; int main() { pin=1; i2c.frequency(100000); printf("magnet setting\r\n"); cmd[0]=0x4B; cmd[1]=0x01; i2c.write(MAG,cmd,2); wait(0.1); cmd[0]=0x4C; cmd[1]=0x00; i2c.write(MAG,cmd,2); cmd[0]=0x4E; cmd[1]=0x84; i2c.write(MAG,cmd,2); cmd[0]=0x51; cmd[1]=0x04; i2c.write(MAG,cmd,2); cmd[0]=0x52; cmd[1]=0x16; i2c.write(MAG,cmd,2); cmd[0]=0x00; i2c.write(MAG,cmd,1,1); i2c.read(MAG,cmd,1); printf("read:0x%02x\r\n",cmd[0]); while(1) { for(int i=0;i<8;i++){ send[0]=(char)(0x42+i); i2c.write(MAG,send,1); i2c.read(MAG,get,1); temp=get[0]; data[i]=temp; } for(int i=0;i<3;i++){ if(i!=2)magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 3; else magnet[i]=(int16_t)(((int16_t)data[i*2+1]<<8) | data[i*2]) >> 1; if(i==2 && magnet[i]>16383)magnet[i]-=32768; else if(i!=2 && magnet[i]>4095)magnet[i]-=8092; } pc.printf("mx = %2.4f, my = %2.4f, mz = %2.4f\r\n\n",magnet[0],magnet[1],magnet[2]); wait(dt); } }