![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
program to set the MCP4728 address for the SOLID amplifier board
Dependencies: DevInterfaces I2Cinterfaces MCP4728setaddr mbed
main.cpp@1:7e052cf2f2ee, 2016-12-14 (annotated)
- Committer:
- wbeaumont
- Date:
- Wed Dec 14 10:59:43 2016 +0000
- Revision:
- 1:7e052cf2f2ee
- Parent:
- 0:d97e8b3802b3
corrected bug in old address
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbeaumont | 0:d97e8b3802b3 | 1 | #include "mbed.h" |
wbeaumont | 0:d97e8b3802b3 | 2 | #include "I2C.h" |
wbeaumont | 0:d97e8b3802b3 | 3 | #define TARGET TARGET_KL05Z |
wbeaumont | 0:d97e8b3802b3 | 4 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
wbeaumont | 0:d97e8b3802b3 | 5 | PinName const SDA = PTE25; |
wbeaumont | 0:d97e8b3802b3 | 6 | PinName const SCL = PTE24; |
wbeaumont | 0:d97e8b3802b3 | 7 | #elif defined (TARGET_KL05Z) |
wbeaumont | 0:d97e8b3802b3 | 8 | PinName const SDA = PTB4; |
wbeaumont | 0:d97e8b3802b3 | 9 | PinName const SCL = PTB3; |
wbeaumont | 0:d97e8b3802b3 | 10 | #elif defined (TARGET_K20D50M) |
wbeaumont | 0:d97e8b3802b3 | 11 | PinName const SDA = PTB1; |
wbeaumont | 0:d97e8b3802b3 | 12 | PinName const SCL = PTB0; |
wbeaumont | 0:d97e8b3802b3 | 13 | #else |
wbeaumont | 0:d97e8b3802b3 | 14 | #error TARGET NOT DEFINED |
wbeaumont | 0:d97e8b3802b3 | 15 | #endif |
wbeaumont | 0:d97e8b3802b3 | 16 | |
wbeaumont | 0:d97e8b3802b3 | 17 | |
wbeaumont | 0:d97e8b3802b3 | 18 | |
wbeaumont | 1:7e052cf2f2ee | 19 | #include "I2CInterface.h" |
wbeaumont | 1:7e052cf2f2ee | 20 | #include "MBEDI2CInterface.h" |
wbeaumont | 1:7e052cf2f2ee | 21 | #include "MCP4728setaddr.h" |
wbeaumont | 0:d97e8b3802b3 | 22 | |
wbeaumont | 0:d97e8b3802b3 | 23 | MBEDI2CInterface mbedi2c( SDA, SCL); |
wbeaumont | 0:d97e8b3802b3 | 24 | |
wbeaumont | 0:d97e8b3802b3 | 25 | MBEDI2CInterface* mbedi2cp= &mbedi2c ; |
wbeaumont | 0:d97e8b3802b3 | 26 | |
wbeaumont | 0:d97e8b3802b3 | 27 | I2CInterface* i2cdev= mbedi2cp; |
wbeaumont | 0:d97e8b3802b3 | 28 | |
wbeaumont | 0:d97e8b3802b3 | 29 | |
wbeaumont | 0:d97e8b3802b3 | 30 | DigitalOut LDAC(PTA6); |
wbeaumont | 0:d97e8b3802b3 | 31 | DigitalOut CntPin(PTA7); |
wbeaumont | 0:d97e8b3802b3 | 32 | |
wbeaumont | 0:d97e8b3802b3 | 33 | |
wbeaumont | 0:d97e8b3802b3 | 34 | I2CInterface* i2cif; |
wbeaumont | 0:d97e8b3802b3 | 35 | |
wbeaumont | 0:d97e8b3802b3 | 36 | |
wbeaumont | 0:d97e8b3802b3 | 37 | int main(void){ |
wbeaumont | 0:d97e8b3802b3 | 38 | printf("\n\rMCP4728 set address program date : %s time :%s\n\r",__DATE__,__TIME__); |
wbeaumont | 0:d97e8b3802b3 | 39 | |
wbeaumont | 0:d97e8b3802b3 | 40 | LDAC=1; |
wbeaumont | 1:7e052cf2f2ee | 41 | char oldaddr, eeaddr=33, regaddr=34, newaddr= 2; |
wbeaumont | 0:d97e8b3802b3 | 42 | int newaddrint; |
wbeaumont | 1:7e052cf2f2ee | 43 | MPC4728_address_set mpc4728(PTB0,&LDAC,i2cdev, &CntPin, true); |
wbeaumont | 1:7e052cf2f2ee | 44 | int i2cresult =mpc4728.readaddress(oldaddr, eeaddr, regaddr); |
wbeaumont | 0:d97e8b3802b3 | 45 | if ( i2cresult ) { |
wbeaumont | 0:d97e8b3802b3 | 46 | printf("failed to read current address error code: %d , scl cnt = %d \n", i2cresult,mpc4728.getsclcnt() ); |
wbeaumont | 0:d97e8b3802b3 | 47 | } |
wbeaumont | 0:d97e8b3802b3 | 48 | else { |
wbeaumont | 1:7e052cf2f2ee | 49 | printf( " current address = %d 0X%x ,eeaddr %d , regaddr %d sclcnt= %d give new address : ", (int) oldaddr,(int) oldaddr,(int) eeaddr, (int)regaddr, mpc4728.getsclcnt()); |
wbeaumont | 0:d97e8b3802b3 | 50 | scanf( "%d",&newaddrint); |
wbeaumont | 1:7e052cf2f2ee | 51 | printf("new addr %d \n\r", newaddrint ); |
wbeaumont | 0:d97e8b3802b3 | 52 | if ( newaddrint >= 0 && newaddrint <= 8 ) { |
wbeaumont | 0:d97e8b3802b3 | 53 | newaddr= (char) newaddrint; |
wbeaumont | 1:7e052cf2f2ee | 54 | i2cresult=mpc4728.setaddress(eeaddr,newaddr); |
wbeaumont | 0:d97e8b3802b3 | 55 | if ( i2cresult ) printf("failed to set address to %d error : %d , sclcnt= %d \n",newaddr, i2cresult,mpc4728.getsclcnt()); |
wbeaumont | 0:d97e8b3802b3 | 56 | else { |
wbeaumont | 0:d97e8b3802b3 | 57 | printf("set new address \n"); |
wbeaumont | 1:7e052cf2f2ee | 58 | i2cresult =mpc4728.readaddress(oldaddr, eeaddr, regaddr); |
wbeaumont | 0:d97e8b3802b3 | 59 | if ( i2cresult ) printf("failed to read (new) address error : %d \n", i2cresult); |
wbeaumont | 0:d97e8b3802b3 | 60 | else printf( "new address = %d ,eeaddr %d , regaddr %d sclcnt= %d \n\r", (int) oldaddr,(int) eeaddr, (int)regaddr, mpc4728.getsclcnt()); printf( " new address = %d \n", (int) oldaddr); |
wbeaumont | 0:d97e8b3802b3 | 61 | } |
wbeaumont | 0:d97e8b3802b3 | 62 | } |
wbeaumont | 0:d97e8b3802b3 | 63 | else { |
wbeaumont | 0:d97e8b3802b3 | 64 | printf( " new address %d is not valid reset the up to restart the program \n", (int)newaddr ); |
wbeaumont | 0:d97e8b3802b3 | 65 | } |
wbeaumont | 0:d97e8b3802b3 | 66 | } |
wbeaumont | 0:d97e8b3802b3 | 67 | printf("restart !!!! \n\r"); |
wbeaumont | 0:d97e8b3802b3 | 68 | while (1); |
wbeaumont | 0:d97e8b3802b3 | 69 | //return 0; // never reach this |
wbeaumont | 0:d97e8b3802b3 | 70 | }; |
wbeaumont | 0:d97e8b3802b3 | 71 |