commit 48a8327281fb61723248c504edcb0d63d9cc82ed Author: James Devine Date: Thu Dec 26 15:44:53 2013 -0800 diff --git a/arduinopowerdemo.pde b/arduinopowerdemo.pde new file mode 100644 index 0000000..1000bf3 --- /dev/null +++ b/arduinopowerdemo.pde @@ -0,0 +1,231 @@ +//registers on ADE7753 +#define WAVEFORM 0x01 +#define AENERGY 0x02 +#define RAENERGY 0x03 +#define LAENERGY 0x04 +#define VAENERGY 0x05 +#define LVAENERGY 0x06 +#define LVARENERGY 0x07 +#define MODE 0x09 +#define IRQEN 0x0A +#define STATUS 0x0B +#define RSTSTATUS 0x0C +#define CH1OS 0x0D +#define CH2OS 0x0E +#define GAIN 0x0F +#define PHCAL 0x10 +#define APOS 0x11 +#define WGAIN 0x12 +#define WDIV 0x12 +#define CFNUM 0x14 +#define CFDEN 0x15 +#define IRMS 0x16 +#define VRMS 0x17 +#define IRMSOS 0x18 +#define VRMSOS 0x19 +#define VAGAIN 0x1A +#define VADIV 0x1B +#define LINECYC 0x1C +#define ZXTOUT 0x1D +#define SAGCYC 0x1E +#define SAGLVL 0x1F +#define IPKLVL 0x20 +#define VPKLVL 0x21 +#define IPEAK 0x22 +#define RSTIPEAK 0x23 +#define VPEAK 0x24 +#define RSTVPEAK 0x25 +#define TEMP 0x26 +#define PERIOD 0x27 +#define TMODE 0x3D +#define CHKSUM 0x3E +#define DIEREV 0x3F + +#define DATAOUT 11//MOSI +#define DATAIN 12//MISO +#define SPICLOCK 13//sck +#define SLAVESELECT 10//ss + +//opcodes +#define WREN 6 +#define WRDI 4 +#define RDSR 5 +#define WRSR 1 +#define READ 3 +#define WRITE 2 + +//SPCR = (1<"); + //Serial.println(EEPROM_address, HEX); + //set write mode + byte make_write_cmd = B10000000; + byte this_write = B00000000; + EEPROM_address = EEPROM_address|make_write_cmd; + digitalWrite(SLAVESELECT,LOW); + spi_transfer((char)(EEPROM_address)); //send address + + //here there should be a t7 delay, however long that is + for (int i = 0; i>(8*((bytes_to_write-1)-i))); + //Serial.println(this_write, HEX); + spi_transfer((char)(this_write)); //send data byte + } + digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer +} + +long read_eeprom(int EEPROM_address, int bytes_to_read) +{ + //Serial.print("Multi-read to addr>"); + //Serial.print(EEPROM_address, HEX); + //Serial.println(" Data starts:"); + long data = 0; + byte reader_buf = 0; + digitalWrite(SLAVESELECT,LOW); + spi_transfer((char)(EEPROM_address)); //send LSByte address + for (int i = 1; i <= bytes_to_read; i++){ + reader_buf = spi_transfer(0xFF); //get data byte + //Serial.println(i); + //Serial.println(reader_buf, BIN); + + data = data|reader_buf; + if (i< bytes_to_read) { + data = data<<8; + } + } + //Serial.print("completed. data was>"); + //Serial.println(data, BIN); + digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer + return data; +} + +void printout(long printme) +{ + Serial.print(printme, DEC); + Serial.print(","); + Serial.println(printme, HEX); +} + +void loop() +{ + + delay(1000); + Serial.println("STATUS CHECK "); + eeprom_output_data = read_eeprom(STATUS,2); + printout(eeprom_output_data); + + Serial.println("MODE CHECK "); + eeprom_output_data = read_eeprom(MODE,2); + printout(eeprom_output_data); + + Serial.println("LINECYC CHECK "); + eeprom_output_data = read_eeprom(LINECYC,2); + printout(eeprom_output_data); + + Serial.println("SAGCYC CHECK "); + eeprom_output_data = read_eeprom(SAGCYC,2); + printout(eeprom_output_data); + + Serial.println("CFNUM CHECK "); + eeprom_output_data = read_eeprom(CFNUM,2); + printout(eeprom_output_data); + + Serial.println("IRMS CHECK "); + eeprom_output_data = read_eeprom(IRMS,4); + printout(eeprom_output_data); + + Serial.println("VRMS CHECK "); + eeprom_output_data = read_eeprom(CFNUM,4); + printout(eeprom_output_data); + + Serial.println("CFNUM CHECK "); + eeprom_output_data = read_eeprom(CFNUM,2); + printout(eeprom_output_data); + + Serial.println("AENERGY CHECK "); + eeprom_output_data = read_eeprom(AENERGY,4); + printout(eeprom_output_data); + + Serial.println("IPEAK CHECK "); + eeprom_output_data = read_eeprom(IPEAK,4); + printout(eeprom_output_data); + + Serial.println("VPEAK CHECK "); + eeprom_output_data = read_eeprom(VPEAK,4); + printout(eeprom_output_data); +} + +