Program to interact via the mbed with its I2C interface.

Dependencies:   DevInterfaces I2Cinterfaces MCP4725 mbed

Fork of MCP4728test by wimbeaumont Project

Committer:
katrijnverhasselt
Date:
Fri Apr 01 08:40:54 2016 +0000
Revision:
2:b374184cf620
Parent:
1:d175631a5803
The complete class that is not working as it should.

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