example program how to us the MCP4728 class and DevInterface classes
Dependencies: mbed DevInterfaces MCP4728 MCP4728setaddr I2Cinterfaces
main.cpp
- Committer:
- wbeaumont
- Date:
- 2017-01-13
- Revision:
- 2:8ce5a2128381
- Parent:
- 1:d175631a5803
- Child:
- 4:527490fdcfd8
File content as of revision 2:8ce5a2128381:
/** example program for the use of the AT30TSE7xx class * * 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 */ #define AT30TSE753EXAMPLEVER "0.40" #include "mbed.h" #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) PinName const SDA = PTE0; PinName const SCL = PTE1; #elif defined (TARGET_KL05Z) PinName const SDA = PTB4; PinName const SCL = PTB3; #elif defined (TARGET_K20D50M) PinName const SDA = PTB1; PinName const SCL = PTB0; #else #error TARGET NOT DEFINED #endif #include "I2C.h" #include "I2CInterface.h" #include "MBEDI2CInterface.h" #include "dev_interface_def.h" #include "AT30TSE75x.h" MBEDI2CInterface mbedi2c( SDA, SCL); MBEDI2CInterface* mbedi2cp= &mbedi2c ; I2CInterface* i2cdev= mbedi2cp; 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__); 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); 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); } // never reach this return 1; }