2

Dependencies:   mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces

Committer:
wbeaumont
Date:
Fri Jan 13 14:02:39 2017 +0000
Revision:
6:d1e569f370ac
back the MCP4728test

Who changed what in which revision?

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