2

Dependencies:   mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces

Committer:
wbeaumont
Date:
Wed Jan 13 14:15:21 2016 +0000
Revision:
0:5bc0f4bd5aa0
Child:
1:d175631a5803
inital not tested with MCP4728 , but was tested in the SOLID project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:5bc0f4bd5aa0 1 #include "mbed.h"
wbeaumont 0:5bc0f4bd5aa0 2
wbeaumont 0:5bc0f4bd5aa0 3 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
wbeaumont 0:5bc0f4bd5aa0 4 PinName const SDA = PTE25;
wbeaumont 0:5bc0f4bd5aa0 5 PinName const SCL = PTE24;
wbeaumont 0:5bc0f4bd5aa0 6 #elif defined (TARGET_KL05Z)
wbeaumont 0:5bc0f4bd5aa0 7 PinName const SDA = PTB4;
wbeaumont 0:5bc0f4bd5aa0 8 PinName const SCL = PTB3;
wbeaumont 0:5bc0f4bd5aa0 9 #elif defined (TARGET_K20D50M)
wbeaumont 0:5bc0f4bd5aa0 10 PinName const SDA = PTB1;
wbeaumont 0:5bc0f4bd5aa0 11 PinName const SCL = PTB0;
wbeaumont 0:5bc0f4bd5aa0 12 #else
wbeaumont 0:5bc0f4bd5aa0 13 #error TARGET NOT DEFINED
wbeaumont 0:5bc0f4bd5aa0 14 #endif
wbeaumont 0:5bc0f4bd5aa0 15
wbeaumont 0:5bc0f4bd5aa0 16
wbeaumont 0:5bc0f4bd5aa0 17 #include "I2C.h"
wbeaumont 0:5bc0f4bd5aa0 18 #include "I2CInterface.h"
wbeaumont 0:5bc0f4bd5aa0 19 #include "MBEDI2CInterface.h"
wbeaumont 0:5bc0f4bd5aa0 20 #include "DACInterface.h"
wbeaumont 0:5bc0f4bd5aa0 21 #include "dev_interface_def.h"
wbeaumont 0:5bc0f4bd5aa0 22 #include "mcp4728.h"
wbeaumont 0:5bc0f4bd5aa0 23
wbeaumont 0:5bc0f4bd5aa0 24 MBEDI2CInterface mbedi2c( SDA, SCL);
wbeaumont 0:5bc0f4bd5aa0 25 MBEDI2CInterface* mbedi2cp= &mbedi2c ;
wbeaumont 0:5bc0f4bd5aa0 26 I2CInterface* i2cdev= mbedi2cp;
wbeaumont 0:5bc0f4bd5aa0 27 const float Vdd=5.0;
wbeaumont 0:5bc0f4bd5aa0 28
wbeaumont 0:5bc0f4bd5aa0 29 Serial pc(USBTX, USBRX);
wbeaumont 0:5bc0f4bd5aa0 30
wbeaumont 0:5bc0f4bd5aa0 31 int main(void) {
wbeaumont 0:5bc0f4bd5aa0 32
wbeaumont 0:5bc0f4bd5aa0 33 // get the version of getVersion
wbeaumont 0:5bc0f4bd5aa0 34 getVersion gv;
wbeaumont 0:5bc0f4bd5aa0 35 printf("getVersion :%s\n\r",gv.getversioninfo());
wbeaumont 0:5bc0f4bd5aa0 36
wbeaumont 0:5bc0f4bd5aa0 37 MCP4728 dac( i2cdev ,0, Vdd); // assuming the address of the MCP4728 is set to 0 factory default
wbeaumont 0:5bc0f4bd5aa0 38 printf("MCP4728 :%s\n\r",dac.getversioninfo());
wbeaumont 0:5bc0f4bd5aa0 39 wait(4);
wbeaumont 0:5bc0f4bd5aa0 40 float voltage=0;
wbeaumont 0:5bc0f4bd5aa0 41 int cnt=0;
wbeaumont 0:5bc0f4bd5aa0 42 while(1){
wbeaumont 0:5bc0f4bd5aa0 43 // first set the 4 channels
wbeaumont 0:5bc0f4bd5aa0 44 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 0:5bc0f4bd5aa0 45 if ( dac.setDACvalue(cnt,cc) )
wbeaumont 0:5bc0f4bd5aa0 46 printf("failed to set dac value %d for channel %d\n\r",cnt,cc);
wbeaumont 0:5bc0f4bd5aa0 47 }
wbeaumont 0:5bc0f4bd5aa0 48 printf("set DAC value to %d result in",cnt);
wbeaumont 0:5bc0f4bd5aa0 49 // no else read anyway even if set fails
wbeaumont 0:5bc0f4bd5aa0 50 for ( int cc =0 ; cc <4 ; cc++) {
wbeaumont 0:5bc0f4bd5aa0 51 if ( dac.getVoltage(voltage,cc))
wbeaumont 0:5bc0f4bd5aa0 52 printf("\n\rfailed to readback voltage for channel %d\n\r",cc);
wbeaumont 0:5bc0f4bd5aa0 53 else
wbeaumont 0:5bc0f4bd5aa0 54 printf(" CH%d %f[V]",cc,voltage);
wbeaumont 0:5bc0f4bd5aa0 55 }
wbeaumont 0:5bc0f4bd5aa0 56 printf("\n\r");
wbeaumont 0:5bc0f4bd5aa0 57 cnt++;
wbeaumont 0:5bc0f4bd5aa0 58 cnt=cnt % 4096;
wbeaumont 0:5bc0f4bd5aa0 59 wait_ms(200);
wbeaumont 0:5bc0f4bd5aa0 60
wbeaumont 0:5bc0f4bd5aa0 61 }
wbeaumont 0:5bc0f4bd5aa0 62 // never reach this
wbeaumont 0:5bc0f4bd5aa0 63 return 1;
wbeaumont 0:5bc0f4bd5aa0 64 }