Test for mike

Dependencies:   mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces

Committer:
wbeaumont
Date:
Fri Jan 13 12:33:44 2017 +0000
Revision:
3:e12e21dcd16a
Parent:
1:d175631a5803
Child:
6:d1e569f370ac
sync commit.

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 3:e12e21dcd16a 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 3:e12e21dcd16a 74 i2cresult =mpc4728.readaddress(&oldaddr, &eeaddr, &regaddr);
wbeaumont 3:e12e21dcd16a 75 if ( i2cresult ) printf("failed to read (new) address error : %d \n", i2cresult);
wbeaumont 3:e12e21dcd16a 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 3:e12e21dcd16a 77 }
wbeaumont 3:e12e21dcd16a 78 }
wbeaumont 3:e12e21dcd16a 79 else {
wbeaumont 3:e12e21dcd16a 80 printf( " new address %d is not valid reset the up to restart the program \n", (int)newaddr );
wbeaumont 3:e12e21dcd16a 81 }
wbeaumont 3:e12e21dcd16a 82 }
wbeaumont 3:e12e21dcd16a 83 } // if add < 16
wbeaumont 3:e12e21dcd16a 84 printf("give test addr ");
wbeaumont 3:e12e21dcd16a 85 scanf("%d",&addr);
wbeaumont 3:e12e21dcd16a 86
wbeaumont 3:e12e21dcd16a 87 MCP4728 dac( i2cdev ,addr, Vdd); // assuming the address of the MCP4728 is set to 0 factory default
wbeaumont 3:e12e21dcd16a 88 printf("\n\raddr %d MCP4728 :%s\n\r",addr,dac.getversioninfo());
wbeaumont 3:e12e21dcd16a 89 wait(1);
wbeaumont 0:5bc0f4bd5aa0 90 float voltage=0;
wbeaumont 0:5bc0f4bd5aa0 91 int cnt=0;
wbeaumont 1:d175631a5803 92 while(cnt < 4096){
wbeaumont 0:5bc0f4bd5aa0 93 // first set the 4 channels
wbeaumont 0:5bc0f4bd5aa0 94 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 0:5bc0f4bd5aa0 95 if ( dac.setDACvalue(cnt,cc) )
wbeaumont 0:5bc0f4bd5aa0 96 printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
wbeaumont 0:5bc0f4bd5aa0 97 }
wbeaumont 3:e12e21dcd16a 98 printf("set to %d result ",cnt);
wbeaumont 0:5bc0f4bd5aa0 99 // no else read anyway even if set fails
wbeaumont 1:d175631a5803 100 if(dac.update()) printf("\n\rfailed to readback channel info \n\r");
wbeaumont 1:d175631a5803 101 else {
wbeaumont 1:d175631a5803 102 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 1:d175631a5803 103 (void)dac.getVoltage(voltage,cc);//no need to test done with updat
wbeaumont 0:5bc0f4bd5aa0 104 printf(" CH%d %f[V]",cc,voltage);
wbeaumont 1:d175631a5803 105 }
wbeaumont 1:d175631a5803 106 printf("\n\r");
wbeaumont 1:d175631a5803 107 }
wbeaumont 0:5bc0f4bd5aa0 108 cnt++;
wbeaumont 0:5bc0f4bd5aa0 109 cnt=cnt % 4096;
wbeaumont 3:e12e21dcd16a 110 wait_ms(1200);
wbeaumont 0:5bc0f4bd5aa0 111
wbeaumont 1:d175631a5803 112 }
wbeaumont 3:e12e21dcd16a 113
wbeaumont 1:d175631a5803 114 // now the same with the DAC interface
wbeaumont 1:d175631a5803 115 DACInterface* di = &dac;
wbeaumont 1:d175631a5803 116 cnt=0;
wbeaumont 1:d175631a5803 117 while(1){
wbeaumont 1:d175631a5803 118 // first set the 4 channels
wbeaumont 1:d175631a5803 119 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 1:d175631a5803 120 if ( di->setDACvalue(cnt,cc) )
wbeaumont 1:d175631a5803 121 printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
wbeaumont 1:d175631a5803 122 }
wbeaumont 1:d175631a5803 123 printf("set DAC value to %d result in",cnt);
wbeaumont 1:d175631a5803 124 // no else read anyway even if set fails
wbeaumont 1:d175631a5803 125 if(di->update()) printf("\n\rfailed to readback channel info \n\r");
wbeaumont 1:d175631a5803 126 else {
wbeaumont 1:d175631a5803 127 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 1:d175631a5803 128 (void)di->getVoltage(voltage,cc);// no need to test done with update
wbeaumont 1:d175631a5803 129 printf(" CH%d %f[V]",cc,voltage);
wbeaumont 1:d175631a5803 130 }
wbeaumont 1:d175631a5803 131 printf("\n\r");
wbeaumont 1:d175631a5803 132 }
wbeaumont 1:d175631a5803 133
wbeaumont 1:d175631a5803 134 cnt++;
wbeaumont 1:d175631a5803 135 cnt=cnt % 4096;
wbeaumont 1:d175631a5803 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 }