2

Dependencies:   mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces

Committer:
wbeaumont
Date:
Fri Jan 13 14:02:39 2017 +0000
Revision:
6:d1e569f370ac
Parent:
5:18d7ce800a18
Parent:
3:e12e21dcd16a
Child:
7:e649bb2b6187
back the MCP4728test

Who changed what in which revision?

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