example program how to us the MCP4728 class and DevInterface classes

Dependencies:   mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces

Committer:
wbeaumont
Date:
Wed Jan 13 19:12:45 2016 +0000
Revision:
1:d175631a5803
Parent:
0:5bc0f4bd5aa0
Child:
2:8ce5a2128381
Child:
3:e12e21dcd16a
added some comments

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 1:d175631a5803 7 #define MCP4728EXAMPLEVER "1.10 "
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 0:5bc0f4bd5aa0 30 #include "mcp4728.h"
wbeaumont 0:5bc0f4bd5aa0 31
wbeaumont 0:5bc0f4bd5aa0 32 MBEDI2CInterface mbedi2c( SDA, SCL);
wbeaumont 0:5bc0f4bd5aa0 33 MBEDI2CInterface* mbedi2cp= &mbedi2c ;
wbeaumont 0:5bc0f4bd5aa0 34 I2CInterface* i2cdev= mbedi2cp;
wbeaumont 0:5bc0f4bd5aa0 35 const float Vdd=5.0;
wbeaumont 0:5bc0f4bd5aa0 36
wbeaumont 0:5bc0f4bd5aa0 37 Serial pc(USBTX, USBRX);
wbeaumont 0:5bc0f4bd5aa0 38
wbeaumont 0:5bc0f4bd5aa0 39 int main(void) {
wbeaumont 0:5bc0f4bd5aa0 40
wbeaumont 0:5bc0f4bd5aa0 41 // get the version of getVersion
wbeaumont 0:5bc0f4bd5aa0 42 getVersion gv;
wbeaumont 1:d175631a5803 43 printf("MCP4728 example program version %s, compile date %s time %s\n\r",MCP4728EXAMPLEVER,__DATE__,__TIME__);
wbeaumont 0:5bc0f4bd5aa0 44 printf("getVersion :%s\n\r",gv.getversioninfo());
wbeaumont 0:5bc0f4bd5aa0 45
wbeaumont 0:5bc0f4bd5aa0 46 MCP4728 dac( i2cdev ,0, Vdd); // assuming the address of the MCP4728 is set to 0 factory default
wbeaumont 0:5bc0f4bd5aa0 47 printf("MCP4728 :%s\n\r",dac.getversioninfo());
wbeaumont 0:5bc0f4bd5aa0 48 wait(4);
wbeaumont 0:5bc0f4bd5aa0 49 float voltage=0;
wbeaumont 0:5bc0f4bd5aa0 50 int cnt=0;
wbeaumont 1:d175631a5803 51 while(cnt < 4096){
wbeaumont 0:5bc0f4bd5aa0 52 // first set the 4 channels
wbeaumont 0:5bc0f4bd5aa0 53 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 0:5bc0f4bd5aa0 54 if ( dac.setDACvalue(cnt,cc) )
wbeaumont 0:5bc0f4bd5aa0 55 printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
wbeaumont 0:5bc0f4bd5aa0 56 }
wbeaumont 0:5bc0f4bd5aa0 57 printf("set DAC value to %d result in",cnt);
wbeaumont 0:5bc0f4bd5aa0 58 // no else read anyway even if set fails
wbeaumont 1:d175631a5803 59 if(dac.update()) printf("\n\rfailed to readback channel info \n\r");
wbeaumont 1:d175631a5803 60 else {
wbeaumont 1:d175631a5803 61 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 1:d175631a5803 62 (void)dac.getVoltage(voltage,cc);//no need to test done with updat
wbeaumont 0:5bc0f4bd5aa0 63 printf(" CH%d %f[V]",cc,voltage);
wbeaumont 1:d175631a5803 64 }
wbeaumont 1:d175631a5803 65 printf("\n\r");
wbeaumont 1:d175631a5803 66 }
wbeaumont 0:5bc0f4bd5aa0 67 cnt++;
wbeaumont 0:5bc0f4bd5aa0 68 cnt=cnt % 4096;
wbeaumont 0:5bc0f4bd5aa0 69 wait_ms(200);
wbeaumont 0:5bc0f4bd5aa0 70
wbeaumont 1:d175631a5803 71 }
wbeaumont 1:d175631a5803 72 // now the same with the DAC interface
wbeaumont 1:d175631a5803 73 DACInterface* di = &dac;
wbeaumont 1:d175631a5803 74 cnt=0;
wbeaumont 1:d175631a5803 75 while(1){
wbeaumont 1:d175631a5803 76 // first set the 4 channels
wbeaumont 1:d175631a5803 77 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 1:d175631a5803 78 if ( di->setDACvalue(cnt,cc) )
wbeaumont 1:d175631a5803 79 printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
wbeaumont 1:d175631a5803 80 }
wbeaumont 1:d175631a5803 81 printf("set DAC value to %d result in",cnt);
wbeaumont 1:d175631a5803 82 // no else read anyway even if set fails
wbeaumont 1:d175631a5803 83 if(di->update()) printf("\n\rfailed to readback channel info \n\r");
wbeaumont 1:d175631a5803 84 else {
wbeaumont 1:d175631a5803 85 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 1:d175631a5803 86 (void)di->getVoltage(voltage,cc);// no need to test done with update
wbeaumont 1:d175631a5803 87 printf(" CH%d %f[V]",cc,voltage);
wbeaumont 1:d175631a5803 88 }
wbeaumont 1:d175631a5803 89 printf("\n\r");
wbeaumont 1:d175631a5803 90 }
wbeaumont 1:d175631a5803 91
wbeaumont 1:d175631a5803 92 cnt++;
wbeaumont 1:d175631a5803 93 cnt=cnt % 4096;
wbeaumont 1:d175631a5803 94 wait_ms(200);
wbeaumont 1:d175631a5803 95
wbeaumont 1:d175631a5803 96 }
wbeaumont 1:d175631a5803 97
wbeaumont 1:d175631a5803 98
wbeaumont 0:5bc0f4bd5aa0 99 // never reach this
wbeaumont 0:5bc0f4bd5aa0 100 return 1;
wbeaumont 0:5bc0f4bd5aa0 101 }