![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test for mike
Dependencies: mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces
main.cpp@4:527490fdcfd8, 2017-01-13 (annotated)
- Committer:
- wbeaumont
- Date:
- Fri Jan 13 12:45:37 2017 +0000
- Revision:
- 4:527490fdcfd8
- Parent:
- 2:8ce5a2128381
- Child:
- 5:18d7ce800a18
revert to MCP4728test , by mistake mixed up with other test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbeaumont | 4:527490fdcfd8 | 1 | /** example program for the use of the MCP4728 class and DACInterface.h classes |
wbeaumont | 1:d175631a5803 | 2 | * |
wbeaumont | 4:527490fdcfd8 | 3 | * V 1.0 : tested on the KL05z but without the MCP4728 connected \ |
wbeaumont | 4:527490fdcfd8 | 4 | * (C) Wim Beaumont Universiteit Antwerpen 2016 |
wbeaumont | 1:d175631a5803 | 5 | */ |
wbeaumont | 1:d175631a5803 | 6 | |
wbeaumont | 4:527490fdcfd8 | 7 | #define MCP4728EXAMPLEVER "2.04" |
wbeaumont | 1:d175631a5803 | 8 | |
wbeaumont | 0:5bc0f4bd5aa0 | 9 | #include "mbed.h" |
wbeaumont | 0:5bc0f4bd5aa0 | 10 | |
wbeaumont | 0:5bc0f4bd5aa0 | 11 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
wbeaumont | 4:527490fdcfd8 | 12 | PinName const SDA = PTE25; |
wbeaumont | 4:527490fdcfd8 | 13 | PinName const SCL = PTE24; |
wbeaumont | 0:5bc0f4bd5aa0 | 14 | #elif defined (TARGET_KL05Z) |
wbeaumont | 0:5bc0f4bd5aa0 | 15 | PinName const SDA = PTB4; |
wbeaumont | 0:5bc0f4bd5aa0 | 16 | PinName const SCL = PTB3; |
wbeaumont | 0:5bc0f4bd5aa0 | 17 | #elif defined (TARGET_K20D50M) |
wbeaumont | 0:5bc0f4bd5aa0 | 18 | PinName const SDA = PTB1; |
wbeaumont | 0:5bc0f4bd5aa0 | 19 | PinName const SCL = PTB0; |
wbeaumont | 0:5bc0f4bd5aa0 | 20 | #else |
wbeaumont | 0:5bc0f4bd5aa0 | 21 | #error TARGET NOT DEFINED |
wbeaumont | 0:5bc0f4bd5aa0 | 22 | #endif |
wbeaumont | 0:5bc0f4bd5aa0 | 23 | |
wbeaumont | 0:5bc0f4bd5aa0 | 24 | |
wbeaumont | 0:5bc0f4bd5aa0 | 25 | #include "I2C.h" |
wbeaumont | 0:5bc0f4bd5aa0 | 26 | #include "I2CInterface.h" |
wbeaumont | 4:527490fdcfd8 | 27 | #include "MBEDI2CInterface.h" |
wbeaumont | 4:527490fdcfd8 | 28 | #include "DACInterface.h" |
wbeaumont | 0:5bc0f4bd5aa0 | 29 | #include "dev_interface_def.h" |
wbeaumont | 4:527490fdcfd8 | 30 | #include "MCP4728setaddr.h" |
wbeaumont | 4:527490fdcfd8 | 31 | #include "mcp4728.h" |
wbeaumont | 0:5bc0f4bd5aa0 | 32 | |
wbeaumont | 0:5bc0f4bd5aa0 | 33 | MBEDI2CInterface mbedi2c( SDA, SCL); |
wbeaumont | 0:5bc0f4bd5aa0 | 34 | MBEDI2CInterface* mbedi2cp= &mbedi2c ; |
wbeaumont | 0:5bc0f4bd5aa0 | 35 | I2CInterface* i2cdev= mbedi2cp; |
wbeaumont | 4:527490fdcfd8 | 36 | const float Vdd=4.5; |
wbeaumont | 4:527490fdcfd8 | 37 | |
wbeaumont | 4:527490fdcfd8 | 38 | DigitalOut LDAC(PTA6); |
wbeaumont | 4:527490fdcfd8 | 39 | DigitalOut CntPin(PTA7); |
wbeaumont | 4:527490fdcfd8 | 40 | |
wbeaumont | 0:5bc0f4bd5aa0 | 41 | |
wbeaumont | 0:5bc0f4bd5aa0 | 42 | Serial pc(USBTX, USBRX); |
wbeaumont | 0:5bc0f4bd5aa0 | 43 | |
wbeaumont | 0:5bc0f4bd5aa0 | 44 | int main(void) { |
wbeaumont | 4:527490fdcfd8 | 45 | |
wbeaumont | 0:5bc0f4bd5aa0 | 46 | // get the version of getVersion |
wbeaumont | 0:5bc0f4bd5aa0 | 47 | getVersion gv; |
wbeaumont | 4:527490fdcfd8 | 48 | int addr; |
wbeaumont | 4:527490fdcfd8 | 49 | printf("MCP4728 example program version %s, compile date %s time %s\n\r",MCP4728EXAMPLEVER,__DATE__,__TIME__); |
wbeaumont | 2:8ce5a2128381 | 50 | printf("getVersion :%s\n\r ",gv.getversioninfo()); |
wbeaumont | 4:527490fdcfd8 | 51 | printf("\n\rMCP4728 set address program date : %s time :%s\n\r",__DATE__,__TIME__); |
wbeaumont | 4:527490fdcfd8 | 52 | printf("give new addr "); |
wbeaumont | 4:527490fdcfd8 | 53 | scanf("%d",&addr); |
wbeaumont | 4:527490fdcfd8 | 54 | if ( addr < 8) { |
wbeaumont | 4:527490fdcfd8 | 55 | LDAC=1; |
wbeaumont | 4:527490fdcfd8 | 56 | char oldaddr, eeaddr, regaddr, newaddr= 2; |
wbeaumont | 4:527490fdcfd8 | 57 | int newaddrint; |
wbeaumont | 4:527490fdcfd8 | 58 | // Set addr part |
wbeaumont | 4:527490fdcfd8 | 59 | MPC4728_address_set mpc4728(PTB0,&LDAC,i2cdev, &CntPin); |
wbeaumont | 4:527490fdcfd8 | 60 | int i2cresult =mpc4728.readaddress(&oldaddr, &eeaddr, ®addr); |
wbeaumont | 4:527490fdcfd8 | 61 | if ( i2cresult ) { |
wbeaumont | 4:527490fdcfd8 | 62 | printf("failed to read current address error code: %d , scl cnt = %d \n", i2cresult,mpc4728.getsclcnt() ); |
wbeaumont | 4:527490fdcfd8 | 63 | } |
wbeaumont | 4:527490fdcfd8 | 64 | else { |
wbeaumont | 4:527490fdcfd8 | 65 | printf( " current address = %d ,eeaddr %d , regaddr %d sclcnt= %d give new address : ", (int) oldaddr,(int) eeaddr, (int)regaddr, mpc4728.getsclcnt()); |
wbeaumont | 4:527490fdcfd8 | 66 | scanf( "%d",&newaddrint); |
wbeaumont | 4:527490fdcfd8 | 67 | printf("\n\r"); |
wbeaumont | 4:527490fdcfd8 | 68 | if ( newaddrint >= 0 && newaddrint <= 8 ) { |
wbeaumont | 4:527490fdcfd8 | 69 | newaddr= (char) newaddrint; |
wbeaumont | 4:527490fdcfd8 | 70 | i2cresult=mpc4728.setaddress(eeaddr,newaddr); |
wbeaumont | 4:527490fdcfd8 | 71 | if ( i2cresult ) printf("failed to set address to %d error : %d , sclcnt= %d \n",newaddr, i2cresult,mpc4728.getsclcnt()); |
wbeaumont | 4:527490fdcfd8 | 72 | else { |
wbeaumont | 4:527490fdcfd8 | 73 | printf("set new address \n"); |
wbeaumont | 4:527490fdcfd8 | 74 | i2cresult =mpc4728.readaddress(&oldaddr, &eeaddr, ®addr); |
wbeaumont | 4:527490fdcfd8 | 75 | if ( i2cresult ) printf("failed to read (new) address error : %d \n", i2cresult); |
wbeaumont | 4:527490fdcfd8 | 76 | 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 | 4:527490fdcfd8 | 77 | } |
wbeaumont | 4:527490fdcfd8 | 78 | } |
wbeaumont | 4:527490fdcfd8 | 79 | else { |
wbeaumont | 4:527490fdcfd8 | 80 | printf( " new address %d is not valid reset the up to restart the program \n", (int)newaddr ); |
wbeaumont | 4:527490fdcfd8 | 81 | } |
wbeaumont | 4:527490fdcfd8 | 82 | } |
wbeaumont | 4:527490fdcfd8 | 83 | } // if add < 16 |
wbeaumont | 4:527490fdcfd8 | 84 | printf("give test addr "); |
wbeaumont | 4:527490fdcfd8 | 85 | scanf("%d",&addr); |
wbeaumont | 4:527490fdcfd8 | 86 | |
wbeaumont | 4:527490fdcfd8 | 87 | MCP4728 dac( i2cdev ,addr, Vdd); // assuming the address of the MCP4728 is set to 0 factory default |
wbeaumont | 4:527490fdcfd8 | 88 | printf("\n\raddr %d MCP4728 :%s\n\r",addr,dac.getversioninfo()); |
wbeaumont | 4:527490fdcfd8 | 89 | wait(1); |
wbeaumont | 4:527490fdcfd8 | 90 | float voltage=0; |
wbeaumont | 0:5bc0f4bd5aa0 | 91 | int cnt=0; |
wbeaumont | 4:527490fdcfd8 | 92 | while(cnt < 4096){ |
wbeaumont | 4:527490fdcfd8 | 93 | // first set the 4 channels |
wbeaumont | 4:527490fdcfd8 | 94 | for ( int cc =0 ; cc <4 ; cc++) { |
wbeaumont | 4:527490fdcfd8 | 95 | if ( dac.setDACvalue(cnt,cc) ) |
wbeaumont | 4:527490fdcfd8 | 96 | printf("failed to set dac value %d for channel %d\n\r",cnt,cc); |
wbeaumont | 4:527490fdcfd8 | 97 | } |
wbeaumont | 4:527490fdcfd8 | 98 | printf("set to %d result ",cnt); |
wbeaumont | 4:527490fdcfd8 | 99 | // no else read anyway even if set fails |
wbeaumont | 4:527490fdcfd8 | 100 | if(dac.update()) printf("\n\rfailed to readback channel info \n\r"); |
wbeaumont | 4:527490fdcfd8 | 101 | else { |
wbeaumont | 4:527490fdcfd8 | 102 | for ( int cc =0 ; cc <4 ; cc++) { |
wbeaumont | 4:527490fdcfd8 | 103 | (void)dac.getVoltage(voltage,cc);//no need to test done with updat |
wbeaumont | 4:527490fdcfd8 | 104 | printf(" CH%d %f[V]",cc,voltage); |
wbeaumont | 4:527490fdcfd8 | 105 | } |
wbeaumont | 4:527490fdcfd8 | 106 | printf("\n\r"); |
wbeaumont | 4:527490fdcfd8 | 107 | } |
wbeaumont | 4:527490fdcfd8 | 108 | cnt++; |
wbeaumont | 4:527490fdcfd8 | 109 | cnt=cnt % 4096; |
wbeaumont | 4:527490fdcfd8 | 110 | wait_ms(1200); |
wbeaumont | 4:527490fdcfd8 | 111 | |
wbeaumont | 4:527490fdcfd8 | 112 | } |
wbeaumont | 4:527490fdcfd8 | 113 | |
wbeaumont | 4:527490fdcfd8 | 114 | // now the same with the DAC interface |
wbeaumont | 4:527490fdcfd8 | 115 | DACInterface* di = &dac; |
wbeaumont | 4:527490fdcfd8 | 116 | cnt=0; |
wbeaumont | 4:527490fdcfd8 | 117 | while(1){ |
wbeaumont | 4:527490fdcfd8 | 118 | // first set the 4 channels |
wbeaumont | 4:527490fdcfd8 | 119 | for ( int cc =0 ; cc <4 ; cc++) { |
wbeaumont | 4:527490fdcfd8 | 120 | if ( di->setDACvalue(cnt,cc) ) |
wbeaumont | 4:527490fdcfd8 | 121 | printf("failed to set dac value %d for channel %d\n\r",cnt,cc); |
wbeaumont | 4:527490fdcfd8 | 122 | } |
wbeaumont | 4:527490fdcfd8 | 123 | printf("set DAC value to %d result in",cnt); |
wbeaumont | 4:527490fdcfd8 | 124 | // no else read anyway even if set fails |
wbeaumont | 4:527490fdcfd8 | 125 | if(di->update()) printf("\n\rfailed to readback channel info \n\r"); |
wbeaumont | 4:527490fdcfd8 | 126 | else { |
wbeaumont | 4:527490fdcfd8 | 127 | for ( int cc =0 ; cc <4 ; cc++) { |
wbeaumont | 4:527490fdcfd8 | 128 | (void)di->getVoltage(voltage,cc);// no need to test done with update |
wbeaumont | 4:527490fdcfd8 | 129 | printf(" CH%d %f[V]",cc,voltage); |
wbeaumont | 4:527490fdcfd8 | 130 | } |
wbeaumont | 4:527490fdcfd8 | 131 | printf("\n\r"); |
wbeaumont | 4:527490fdcfd8 | 132 | } |
wbeaumont | 4:527490fdcfd8 | 133 | |
wbeaumont | 4:527490fdcfd8 | 134 | cnt++; |
wbeaumont | 4:527490fdcfd8 | 135 | cnt=cnt % 4096; |
wbeaumont | 4:527490fdcfd8 | 136 | wait_ms(200); |
wbeaumont | 1:d175631a5803 | 137 | |
wbeaumont | 1:d175631a5803 | 138 | } |
wbeaumont | 1:d175631a5803 | 139 | |
wbeaumont | 1:d175631a5803 | 140 | |
wbeaumont | 0:5bc0f4bd5aa0 | 141 | // never reach this |
wbeaumont | 0:5bc0f4bd5aa0 | 142 | return 1; |
wbeaumont | 0:5bc0f4bd5aa0 | 143 | } |