lib to set the address of the MCP4728
Dependents: MCP4728setaddrProg mbedSerialInterface_talkback2 MCP4728test mbedSerialInterface_sequencer
MCP4728setaddr.cpp@0:19560a1deb3c, 2016-12-12 (annotated)
- Committer:
- wbeaumont
- Date:
- Mon Dec 12 17:37:03 2016 +0000
- Revision:
- 0:19560a1deb3c
- Child:
- 1:1ba04e54bd3c
update if needed
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:19560a1deb3c | 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 | 0:19560a1deb3c | 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 | 0:19560a1deb3c | 48 | *address=0xFF; |
wbeaumont | 0:19560a1deb3c | 49 | return i2cresult ; |
wbeaumont | 0:19560a1deb3c | 50 | } |
wbeaumont | 0:19560a1deb3c | 51 | i2cresult=i2cd->read(MCP4728_baseaddr, &cmd, 1,false ); |
wbeaumont | 0:19560a1deb3c | 52 | if ( i2cresult) *address=0xFE; |
wbeaumont | 0:19560a1deb3c | 53 | else { |
wbeaumont | 0:19560a1deb3c | 54 | *address=cmd; |
wbeaumont | 0:19560a1deb3c | 55 | *regaddr= cmd >>1; |
wbeaumont | 0:19560a1deb3c | 56 | *regaddr= *regaddr& 0x7; |
wbeaumont | 0:19560a1deb3c | 57 | *eepromaddr = cmd>>5; |
wbeaumont | 0:19560a1deb3c | 58 | *eepromaddr = *eepromaddr & 0x7 ; |
wbeaumont | 0:19560a1deb3c | 59 | } |
wbeaumont | 0:19560a1deb3c | 60 | *LDAC=ldac1; |
wbeaumont | 0:19560a1deb3c | 61 | return i2cresult ; |
wbeaumont | 0:19560a1deb3c | 62 | } |
wbeaumont | 0:19560a1deb3c | 63 | |
wbeaumont | 0:19560a1deb3c | 64 | |
wbeaumont | 0:19560a1deb3c | 65 | int MPC4728_address_set::setaddress(char currentaddress, char newaddress ){ // both address |
wbeaumont | 0:19560a1deb3c | 66 | *LDAC=ldac1; |
wbeaumont | 0:19560a1deb3c | 67 | char oldaddr=0x7 & currentaddress;oldaddr=oldaddr<<1; |
wbeaumont | 0:19560a1deb3c | 68 | char newaddr=0x7 & newaddress; |
wbeaumont | 0:19560a1deb3c | 69 | newaddr= newaddr<<2; |
wbeaumont | 0:19560a1deb3c | 70 | char address=MCP4728_baseaddr | oldaddr;oldaddr=oldaddr<<1; |
wbeaumont | 0:19560a1deb3c | 71 | const char cmdtype= 0x60; // 0x011000000 0x011AAA0 |
wbeaumont | 0:19560a1deb3c | 72 | char data[3] ; |
wbeaumont | 0:19560a1deb3c | 73 | data[0]= cmdtype | oldaddr | 1; //0x011OOO01 |
wbeaumont | 0:19560a1deb3c | 74 | data[1]= cmdtype | newaddr | 2; //0x011NNN10 |
wbeaumont | 0:19560a1deb3c | 75 | data[2]= cmdtype | newaddr | 3; //0x011NNN11 |
wbeaumont | 0:19560a1deb3c | 76 | printf( "cmd %x %x data %x %x %x \n\r", newaddress, newaddr,(int)data[0],(int)data[1],(int)data[2]); |
wbeaumont | 0:19560a1deb3c | 77 | start_scl_cnt(18);//1+ 9+8 faling edges |
wbeaumont | 0:19560a1deb3c | 78 | int i2cresult=i2cd->write(address, data, 3,false ); //3 bytes sent and stop |
wbeaumont | 0:19560a1deb3c | 79 | *LDAC=ldac1; |
wbeaumont | 0:19560a1deb3c | 80 | return i2cresult ; |
wbeaumont | 0:19560a1deb3c | 81 | } |
wbeaumont | 0:19560a1deb3c | 82 | |
wbeaumont | 0:19560a1deb3c | 83 | void MPC4728_address_set::start_scl_cnt(int cnts) { |
wbeaumont | 0:19560a1deb3c | 84 | sclcnt= cnts; |
wbeaumont | 0:19560a1deb3c | 85 | cntin.fall(this, &MPC4728_address_set::count_down); |
wbeaumont | 0:19560a1deb3c | 86 | } |