Exemple de test
Fork of AS5600 by
LTC2944.cpp@1:378dc5a174ce, 2017-05-17 (annotated)
- Committer:
- gr66
- Date:
- Wed May 17 18:45:40 2017 +0000
- Revision:
- 1:378dc5a174ce
- Parent:
- AS5600.cpp@0:ec69fa9832e5
Test 2944
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gr66 | 1:378dc5a174ce | 1 | // LTC2944 V1.0 |
gr66 | 1:378dc5a174ce | 2 | // 07/03/2017 GR : |
gr66 | 1:378dc5a174ce | 3 | #include "LTC2944.h" |
gr66 | 1:378dc5a174ce | 4 | //Serial pc(USBTX, USBRX); // I/O terminal PC |
gr66 | 0:ec69fa9832e5 | 5 | |
gr66 | 0:ec69fa9832e5 | 6 | |
gr66 | 0:ec69fa9832e5 | 7 | |
gr66 | 1:378dc5a174ce | 8 | LTC2944::LTC2944(PinName sda, PinName scl) : i2c(sda,scl),pc(USBTX, USBRX) |
gr66 | 1:378dc5a174ce | 9 | { |
gr66 | 1:378dc5a174ce | 10 | address=0xC8; // adresse I2C |
gr66 | 1:378dc5a174ce | 11 | //i2c.frequency(400000); |
gr66 | 1:378dc5a174ce | 12 | } |
gr66 | 1:378dc5a174ce | 13 | void LTC2944::setRsense(float r) |
gr66 | 0:ec69fa9832e5 | 14 | { |
gr66 | 1:378dc5a174ce | 15 | rsense=r; |
gr66 | 1:378dc5a174ce | 16 | } |
gr66 | 1:378dc5a174ce | 17 | // |
gr66 | 1:378dc5a174ce | 18 | void LTC2944::setMode(unsigned char m) |
gr66 | 1:378dc5a174ce | 19 | { |
gr66 | 1:378dc5a174ce | 20 | dataw[0]=0x01; // registre B control |
gr66 | 1:378dc5a174ce | 21 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 22 | i2c.read(address,datar,1); // lecture du registre |
gr66 | 1:378dc5a174ce | 23 | pc.printf("valm1 %x \n",datar[0]); |
gr66 | 1:378dc5a174ce | 24 | dataw[1]=(datar[0]&~0xC0)|(m<<6); |
gr66 | 1:378dc5a174ce | 25 | i2c.write(address,dataw,2); // ecriture |
gr66 | 1:378dc5a174ce | 26 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 27 | i2c.read(address,datar,1); // lecture du registre |
gr66 | 1:378dc5a174ce | 28 | pc.printf("valm2 %x \n",datar[0]); |
gr66 | 0:ec69fa9832e5 | 29 | } |
gr66 | 1:378dc5a174ce | 30 | // |
gr66 | 1:378dc5a174ce | 31 | void LTC2944::setPrescaler(unsigned char p) |
gr66 | 0:ec69fa9832e5 | 32 | { |
gr66 | 1:378dc5a174ce | 33 | dataw[0]=0x01; // registre B control |
gr66 | 1:378dc5a174ce | 34 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 35 | i2c.read(address,datar,1); // lecture du registre |
gr66 | 1:378dc5a174ce | 36 | pc.printf("valp1 %x \n",datar[0]); |
gr66 | 1:378dc5a174ce | 37 | dataw[1]=(datar[0]&~0x38)|(p<<3); |
gr66 | 1:378dc5a174ce | 38 | i2c.write(address,dataw,2); // ecriture |
gr66 | 1:378dc5a174ce | 39 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 40 | i2c.read(address,datar,1); // lecture du registre |
gr66 | 1:378dc5a174ce | 41 | pc.printf("valp2 %x \n",datar[0]); |
gr66 | 1:378dc5a174ce | 42 | } |
gr66 | 1:378dc5a174ce | 43 | // |
gr66 | 1:378dc5a174ce | 44 | void LTC2944::setImin(float imin) |
gr66 | 1:378dc5a174ce | 45 | { |
gr66 | 1:378dc5a174ce | 46 | dataw[0]=0x12; // registre B control |
gr66 | 1:378dc5a174ce | 47 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 48 | i2c.read(address,datar,2); // lecture du registre |
gr66 | 1:378dc5a174ce | 49 | pc.printf("valImin1 %x %x \n",datar[0],datar[1]); |
gr66 | 1:378dc5a174ce | 50 | short val=(short)((imin*rsense*32767/64)+32767); |
gr66 | 1:378dc5a174ce | 51 | dataw[1]=(char)(val>>8); |
gr66 | 1:378dc5a174ce | 52 | dataw[2]=(char)(val&0x00FF); |
gr66 | 1:378dc5a174ce | 53 | i2c.write(address,dataw,3); // ecriture |
gr66 | 1:378dc5a174ce | 54 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 55 | i2c.read(address,datar,2); // lecture du registre |
gr66 | 1:378dc5a174ce | 56 | pc.printf("valmin2 %x %x \n",datar[0],datar[1]); |
gr66 | 0:ec69fa9832e5 | 57 | } |
gr66 | 0:ec69fa9832e5 | 58 | |
gr66 | 1:378dc5a174ce | 59 | void LTC2944::setImax(float imin) |
gr66 | 0:ec69fa9832e5 | 60 | { |
gr66 | 1:378dc5a174ce | 61 | dataw[0]=0x10; // registre B control |
gr66 | 1:378dc5a174ce | 62 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 63 | i2c.read(address,datar,2); // lecture du registre |
gr66 | 1:378dc5a174ce | 64 | pc.printf("valImax1 %x %x \n",datar[0],datar[1]); |
gr66 | 1:378dc5a174ce | 65 | short val=(short)((imin*rsense*32767/64)+32767); |
gr66 | 1:378dc5a174ce | 66 | dataw[1]=(char)(val>>8); |
gr66 | 1:378dc5a174ce | 67 | dataw[2]=(char)(val&0x00FF); |
gr66 | 1:378dc5a174ce | 68 | i2c.write(address,dataw,3); // ecriture |
gr66 | 1:378dc5a174ce | 69 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 70 | i2c.read(address,datar,2); // lecture du registre |
gr66 | 1:378dc5a174ce | 71 | pc.printf("valmax2 %x %x \n",datar[0],datar[1]); |
gr66 | 1:378dc5a174ce | 72 | } |
gr66 | 1:378dc5a174ce | 73 | // |
gr66 | 1:378dc5a174ce | 74 | float LTC2944::getVoltage(void) |
gr66 | 1:378dc5a174ce | 75 | { |
gr66 | 1:378dc5a174ce | 76 | dataw[0]=8; // adresse de la tension |
gr66 | 1:378dc5a174ce | 77 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 78 | i2c.read(address,datar,2); // lecture du registre de deux octets |
gr66 | 1:378dc5a174ce | 79 | return((((unsigned short)(datar[0])<<8)+datar[1])*70.8/0xFFFF); |
gr66 | 0:ec69fa9832e5 | 80 | } |
gr66 | 0:ec69fa9832e5 | 81 | |
gr66 | 1:378dc5a174ce | 82 | float LTC2944::getCurrent(void) |
gr66 | 0:ec69fa9832e5 | 83 | { |
gr66 | 1:378dc5a174ce | 84 | dataw[0]=0xE; // adresse de la tension |
gr66 | 1:378dc5a174ce | 85 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 86 | i2c.read(address,datar,2); // lecture du registre de deux octets |
gr66 | 1:378dc5a174ce | 87 | unsigned short value=((unsigned short)(datar[0])<<8)|(unsigned short)datar[1]; |
gr66 | 1:378dc5a174ce | 88 | return(((float)(value)-32767.0)*64./rsense/32767.0); |
gr66 | 0:ec69fa9832e5 | 89 | } |
gr66 | 0:ec69fa9832e5 | 90 | |
gr66 | 1:378dc5a174ce | 91 | unsigned short LTC2944::getAccumulatedCharge(void) |
gr66 | 0:ec69fa9832e5 | 92 | { |
gr66 | 1:378dc5a174ce | 93 | dataw[0]=0x2; // adresse de la tension |
gr66 | 1:378dc5a174ce | 94 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 95 | i2c.read(address,datar,2); // lecture du registre de deux octets |
gr66 | 1:378dc5a174ce | 96 | return(((unsigned short)(datar[0])<<8)|(unsigned short)datar[1]); |
gr66 | 0:ec69fa9832e5 | 97 | |
gr66 | 1:378dc5a174ce | 98 | } |
gr66 | 1:378dc5a174ce | 99 | unsigned char LTC2944::getStatus(void) |
gr66 | 0:ec69fa9832e5 | 100 | { |
gr66 | 1:378dc5a174ce | 101 | dataw[0]=0x0; // adresse status |
gr66 | 1:378dc5a174ce | 102 | i2c.write(address,dataw,1,1); // positionnement de l'adresse |
gr66 | 1:378dc5a174ce | 103 | i2c.read(address,datar,1); // lecture du registre de deux octets |
gr66 | 1:378dc5a174ce | 104 | return(datar[0]); |
gr66 | 1:378dc5a174ce | 105 | |
gr66 | 0:ec69fa9832e5 | 106 | } |
gr66 | 1:378dc5a174ce | 107 | unsigned char LTC2944::alertResponse(void) |
gr66 | 1:378dc5a174ce | 108 | { |
gr66 | 1:378dc5a174ce | 109 | i2c.read(0x18,datar,1); // lecture du registre de deux octets |
gr66 | 0:ec69fa9832e5 | 110 | |
gr66 | 0:ec69fa9832e5 | 111 | |
gr66 | 1:378dc5a174ce | 112 | return( datar[0]); |
gr66 | 0:ec69fa9832e5 | 113 | } |