lib to set the address of the MCP4728

Dependents:   MCP4728setaddrProg mbedSerialInterface_talkback2 MCP4728test mbedSerialInterface_sequencer

Committer:
wbeaumont
Date:
Wed Dec 14 10:58:49 2016 +0000
Revision:
1:1ba04e54bd3c
Parent:
0:19560a1deb3c
bug corrected in transfer old addr to write addr routine

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:19560a1deb3c 1
wbeaumont 0:19560a1deb3c 2 #include "mbed.h"
wbeaumont 0:19560a1deb3c 3
wbeaumont 0:19560a1deb3c 4 #include "MCP4728setaddr.h"
wbeaumont 0:19560a1deb3c 5 #include "I2CInterface.h"
wbeaumont 0:19560a1deb3c 6 //#include "dev_interface_def.h"
wbeaumont 0:19560a1deb3c 7
wbeaumont 0:19560a1deb3c 8
wbeaumont 0:19560a1deb3c 9
wbeaumont 0:19560a1deb3c 10 #define MCP4728_baseaddr 0xC0
wbeaumont 0:19560a1deb3c 11 #define MCP4728_readaddresscmd 0xC
wbeaumont 0:19560a1deb3c 12
wbeaumont 0:19560a1deb3c 13
wbeaumont 0:19560a1deb3c 14 MPC4728_address_set::MPC4728_address_set(PinName sclcntpin, DigitalOut *LDACpin, I2CInterface* i2cdevice, DigitalOut *Cntoutpin , bool ldac_invert ): cntin(sclcntpin) {
wbeaumont 0:19560a1deb3c 15 i2cd=i2cdevice;
wbeaumont 0:19560a1deb3c 16 LDAC=LDACpin;
wbeaumont 0:19560a1deb3c 17 Cntout=Cntoutpin;
wbeaumont 0:19560a1deb3c 18 *Cntoutpin=0;
wbeaumont 0:19560a1deb3c 19 if (ldac_invert ) { ldac1 = 0; ldac0=1;}
wbeaumont 0:19560a1deb3c 20 else { ldac1 = 1; ldac0=0;}
wbeaumont 0:19560a1deb3c 21 *LDAC=ldac1;
wbeaumont 0:19560a1deb3c 22 i2cd->frequency(40000); // interrupt is slow, so set to 40 kHz
wbeaumont 0:19560a1deb3c 23
wbeaumont 0:19560a1deb3c 24 }
wbeaumont 0:19560a1deb3c 25
wbeaumont 0:19560a1deb3c 26 void MPC4728_address_set::count_down( ){
wbeaumont 0:19560a1deb3c 27 sclcnt--;
wbeaumont 0:19560a1deb3c 28 *Cntout=1;
wbeaumont 0:19560a1deb3c 29 *Cntout=0;
wbeaumont 0:19560a1deb3c 30 if ( sclcnt==0){
wbeaumont 0:19560a1deb3c 31 //disable this interrupt
wbeaumont 0:19560a1deb3c 32 int wait=10; while ( wait--);
wbeaumont 0:19560a1deb3c 33 *LDAC=ldac0;
wbeaumont 0:19560a1deb3c 34 }
wbeaumont 0:19560a1deb3c 35 }
wbeaumont 0:19560a1deb3c 36
wbeaumont 0:19560a1deb3c 37
wbeaumont 0:19560a1deb3c 38
wbeaumont 1:1ba04e54bd3c 39 int MPC4728_address_set::readaddress(char& address, char& eepromaddr , char& regaddr ) {
wbeaumont 0:19560a1deb3c 40 *LDAC=ldac1;
wbeaumont 0:19560a1deb3c 41 start_scl_cnt(18);// 1+9+8 faling edges
wbeaumont 1:1ba04e54bd3c 42 address=0x0C; // not allowed value
wbeaumont 0:19560a1deb3c 43 char cmd = MCP4728_readaddresscmd;
wbeaumont 0:19560a1deb3c 44 int i2cresult=i2cd->write(0, &cmd, 1,true ); //1 byte don't send stop
wbeaumont 0:19560a1deb3c 45 if ( i2cresult ) { // <> 0
wbeaumont 0:19560a1deb3c 46 //i2cd->abort_transfer ( );
wbeaumont 0:19560a1deb3c 47 i2cd->stop();
wbeaumont 1:1ba04e54bd3c 48 address=0xFF;
wbeaumont 0:19560a1deb3c 49 return i2cresult ;
wbeaumont 0:19560a1deb3c 50 }
wbeaumont 1:1ba04e54bd3c 51 i2cresult=i2cd->read(MCP4728_baseaddr+4, &cmd, 1,false ); // +4 because it could conflicts with the MCP4725 what is set to 0xC1
wbeaumont 1:1ba04e54bd3c 52 if ( i2cresult) address=0xFE;
wbeaumont 0:19560a1deb3c 53 else {
wbeaumont 1:1ba04e54bd3c 54 address=cmd;
wbeaumont 1:1ba04e54bd3c 55 regaddr= cmd ;
wbeaumont 1:1ba04e54bd3c 56 regaddr = regaddr >>1;
wbeaumont 1:1ba04e54bd3c 57 regaddr= regaddr & 0x7;
wbeaumont 1:1ba04e54bd3c 58 eepromaddr = cmd;
wbeaumont 1:1ba04e54bd3c 59 eepromaddr = eepromaddr >> 5;
wbeaumont 1:1ba04e54bd3c 60 eepromaddr = eepromaddr & 0x7 ;
wbeaumont 0:19560a1deb3c 61 }
wbeaumont 0:19560a1deb3c 62 *LDAC=ldac1;
wbeaumont 0:19560a1deb3c 63 return i2cresult ;
wbeaumont 0:19560a1deb3c 64 }
wbeaumont 0:19560a1deb3c 65
wbeaumont 0:19560a1deb3c 66
wbeaumont 0:19560a1deb3c 67 int MPC4728_address_set::setaddress(char currentaddress, char newaddress ){ // both address
wbeaumont 0:19560a1deb3c 68 *LDAC=ldac1;
wbeaumont 1:1ba04e54bd3c 69 char oldaddr=0x7 & currentaddress;
wbeaumont 0:19560a1deb3c 70 char newaddr=0x7 & newaddress;
wbeaumont 0:19560a1deb3c 71 newaddr= newaddr<<2;
wbeaumont 1:1ba04e54bd3c 72 oldaddr=oldaddr<<1;
wbeaumont 1:1ba04e54bd3c 73 char address=MCP4728_baseaddr | oldaddr;
wbeaumont 1:1ba04e54bd3c 74 oldaddr=oldaddr<<1;
wbeaumont 1:1ba04e54bd3c 75 const char cmdtype= 0x60; // 0x011000000 0x011A AA01
wbeaumont 0:19560a1deb3c 76 char data[3] ;
wbeaumont 0:19560a1deb3c 77 data[0]= cmdtype | oldaddr | 1; //0x011OOO01
wbeaumont 0:19560a1deb3c 78 data[1]= cmdtype | newaddr | 2; //0x011NNN10
wbeaumont 0:19560a1deb3c 79 data[2]= cmdtype | newaddr | 3; //0x011NNN11
wbeaumont 1:1ba04e54bd3c 80 printf( "cmd %x %x %x data %x %x %x \n\r", (int)newaddress, (int)newaddr,(int) oldaddr,(int)data[0],(int)data[1],(int)data[2]);
wbeaumont 0:19560a1deb3c 81 start_scl_cnt(18);//1+ 9+8 faling edges
wbeaumont 0:19560a1deb3c 82 int i2cresult=i2cd->write(address, data, 3,false ); //3 bytes sent and stop
wbeaumont 0:19560a1deb3c 83 *LDAC=ldac1;
wbeaumont 0:19560a1deb3c 84 return i2cresult ;
wbeaumont 0:19560a1deb3c 85 }
wbeaumont 0:19560a1deb3c 86
wbeaumont 0:19560a1deb3c 87 void MPC4728_address_set::start_scl_cnt(int cnts) {
wbeaumont 0:19560a1deb3c 88 sclcnt= cnts;
wbeaumont 0:19560a1deb3c 89 cntin.fall(this, &MPC4728_address_set::count_down);
wbeaumont 0:19560a1deb3c 90 }