2
Dependencies: mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces
Diff: main.cpp
- Revision:
- 4:527490fdcfd8
- Parent:
- 2:8ce5a2128381
- Child:
- 5:18d7ce800a18
diff -r 8ce5a2128381 -r 527490fdcfd8 main.cpp --- a/main.cpp Fri Jan 13 12:28:09 2017 +0000 +++ b/main.cpp Fri Jan 13 12:45:37 2017 +0000 @@ -1,19 +1,16 @@ -/** example program for the use of the AT30TSE7xx class +/** example program for the use of the MCP4728 class and DACInterface.h classes * - * V 0.4 : tested on the KL25z - * tested eeprom read/ write full page / single byte - * temperature read 12 bit resolution - * fix chip configuration. - * (C) Wim Beaumont Universiteit Antwerpen 2017 + * V 1.0 : tested on the KL05z but without the MCP4728 connected \ + * (C) Wim Beaumont Universiteit Antwerpen 2016 */ -#define AT30TSE753EXAMPLEVER "0.40" +#define MCP4728EXAMPLEVER "2.04" #include "mbed.h" #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) - PinName const SDA = PTE0; - PinName const SCL = PTE1; + PinName const SDA = PTE25; + PinName const SCL = PTE24; #elif defined (TARGET_KL05Z) PinName const SDA = PTB4; PinName const SCL = PTB3; @@ -27,74 +24,116 @@ #include "I2C.h" #include "I2CInterface.h" -#include "MBEDI2CInterface.h" +#include "MBEDI2CInterface.h" +#include "DACInterface.h" #include "dev_interface_def.h" -#include "AT30TSE75x.h" - +#include "MCP4728setaddr.h" +#include "mcp4728.h" MBEDI2CInterface mbedi2c( SDA, SCL); MBEDI2CInterface* mbedi2cp= &mbedi2c ; I2CInterface* i2cdev= mbedi2cp; +const float Vdd=4.5; + +DigitalOut LDAC(PTA6); +DigitalOut CntPin(PTA7); + Serial pc(USBTX, USBRX); -void print_buf_hex( char *data, int length){ - int nr; - char *ptr=data; - for ( int lc=0; lc < length ; lc++){ - nr= (int) *(ptr++); - printf( "%02x ",nr); - } - printf("\n\r"); -} - - int main(void) { - char str1[16] = "SOLID Temp"; - char write_eprom= 'n'; + // get the version of getVersion getVersion gv; - int addr=0; - printf("AT30TSE752 example program version %s, compile date %s time %s\n\r",AT30TSE753EXAMPLEVER,__DATE__,__TIME__); + int addr; + printf("MCP4728 example program version %s, compile date %s time %s\n\r",MCP4728EXAMPLEVER,__DATE__,__TIME__); printf("getVersion :%s\n\r ",gv.getversioninfo()); - printf("%s\n\r",str1); - printf("give new addr "); scanf("%d", &addr); - if ( addr < 0 || addr > 15) addr=0; - fflush(stdin); - - AT30TSE75x tid( i2cdev ,addr); - printf ( "AT30SE75x version :%s\n\r ",tid.getversioninfo()); - printf( "Taddr %x , Eaddr %x subaddr %d\n\r ", tid.getTaddr(),tid.getEaddr(), addr); - + printf("\n\rMCP4728 set address program date : %s time :%s\n\r",__DATE__,__TIME__); + printf("give new addr "); + scanf("%d",&addr); + if ( addr < 8) { + LDAC=1; + char oldaddr, eeaddr, regaddr, newaddr= 2; + int newaddrint; + // Set addr part + MPC4728_address_set mpc4728(PTB0,&LDAC,i2cdev, &CntPin); + int i2cresult =mpc4728.readaddress(&oldaddr, &eeaddr, ®addr); + if ( i2cresult ) { + printf("failed to read current address error code: %d , scl cnt = %d \n", i2cresult,mpc4728.getsclcnt() ); + } + else { + printf( " current address = %d ,eeaddr %d , regaddr %d sclcnt= %d give new address : ", (int) oldaddr,(int) eeaddr, (int)regaddr, mpc4728.getsclcnt()); + scanf( "%d",&newaddrint); + printf("\n\r"); + if ( newaddrint >= 0 && newaddrint <= 8 ) { + newaddr= (char) newaddrint; + i2cresult=mpc4728.setaddress(eeaddr,newaddr); + if ( i2cresult ) printf("failed to set address to %d error : %d , sclcnt= %d \n",newaddr, i2cresult,mpc4728.getsclcnt()); + else { + printf("set new address \n"); + i2cresult =mpc4728.readaddress(&oldaddr, &eeaddr, ®addr); + if ( i2cresult ) printf("failed to read (new) address error : %d \n", i2cresult); + 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); + } + } + else { + printf( " new address %d is not valid reset the up to restart the program \n", (int)newaddr ); + } + } + } // if add < 16 + printf("give test addr "); + scanf("%d",&addr); + + MCP4728 dac( i2cdev ,addr, Vdd); // assuming the address of the MCP4728 is set to 0 factory default + printf("\n\raddr %d MCP4728 :%s\n\r",addr,dac.getversioninfo()); + wait(1); + float voltage=0; int cnt=0; - int i2cerr; - i2cerr=tid.set_config(); - if( i2cerr) printf("config not set %d \n\r", i2cerr); - int configrd= tid.read_config( 0, &i2cerr); - printf( " config %x I2cerr %d \n\r", configrd,i2cerr ); - printf("write to eprom ?"); scanf("%c",&write_eprom); - int pagenr=0;write_eprom = 'y'; - if(write_eprom == 'y'){ - printf("\n\rgive pagenr"); - scanf("%d",&pagenr); - i2cerr= tid.write_eeprompage(str1, 16, 0, (uint8_t) pagenr); - if(i2cerr) printf("eeprom write error %d\n\r",i2cerr); - } - wait_ms(400); // give some time to write - while(1) { - i2cerr=tid.read_eeprompage(str1, 16, 0, (uint8_t) pagenr); - if(i2cerr) printf("eeprom read error %d \n\r",i2cerr); - str1[15]='\0'; //make sure it ends - printf("eeprom content page %d :\n\r %s\n\r",pagenr,str1); - //print_buf_hex( str1,16); - char singlechar; - i2cerr=tid.read_eeprombyte(singlechar, 3, (uint8_t) pagenr); - if(i2cerr) printf("eeprom read error %d \n\r",i2cerr); - printf("third char is : %c\n\r",singlechar); - wait_ms(1000); - float Tmp= tid.get_temperature(&i2cerr); - printf ("%d:Temp = %f I2cerr %d \n\r",cnt++, Tmp, i2cerr); - + while(cnt < 4096){ + // first set the 4 channels + for ( int cc =0 ; cc <4 ; cc++) { + if ( dac.setDACvalue(cnt,cc) ) + printf("failed to set dac value %d for channel %d\n\r",cnt,cc); + } + printf("set to %d result ",cnt); + // no else read anyway even if set fails + if(dac.update()) printf("\n\rfailed to readback channel info \n\r"); + else { + for ( int cc =0 ; cc <4 ; cc++) { + (void)dac.getVoltage(voltage,cc);//no need to test done with updat + printf(" CH%d %f[V]",cc,voltage); + } + printf("\n\r"); + } + cnt++; + cnt=cnt % 4096; + wait_ms(1200); + + } + + // now the same with the DAC interface + DACInterface* di = &dac; + cnt=0; + while(1){ + // first set the 4 channels + for ( int cc =0 ; cc <4 ; cc++) { + if ( di->setDACvalue(cnt,cc) ) + printf("failed to set dac value %d for channel %d\n\r",cnt,cc); + } + printf("set DAC value to %d result in",cnt); + // no else read anyway even if set fails + if(di->update()) printf("\n\rfailed to readback channel info \n\r"); + else { + for ( int cc =0 ; cc <4 ; cc++) { + (void)di->getVoltage(voltage,cc);// no need to test done with update + printf(" CH%d %f[V]",cc,voltage); + } + printf("\n\r"); + } + + cnt++; + cnt=cnt % 4096; + wait_ms(200); }