Introduction:
This blog is written for SPI communication between ADE7753 IC and Nuvoton NUC200VE3AN 32 bit microcontroller. If you want to know more about SPI communication, I would suggest you to read my another blog NUC200VE3AN SPI basic part 1.(link will post very soon)
Note:
It is recommended that both ADE7753 and NUC200VE3 must be operated at same voltage or you have to use a voltage translator. If you are not getting success, how to use voltage translator then read my another blog ADuM7642 as voltage translator and digital Isolator between two SPI devices
Source Code
Before writing the code I would like to share a brief overview of SPI connection diagram between ADE7753 IC and Nuvoton(Nuc200VE3AN). Here is diagram
Now SPI Driver code for ADE7753 IC is given below in which you will see how to initialize GPIO, SPI0, writing/reading data and UART0 too.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
[php] #include <stdio.h> #include "NUC1xx.h" //this header file will work for both nuc100 series and nuc200 series uint32_t output_data, delay=0; void InitUART(void) { /* Step 1. Enable and Select UART clock source*/ /* Step 1. Enable and Select UART clock source*/ UNLOCKREG(); SYSCLK->PWRCON.XTL12M_EN = 1; LOCKREG(); SYSCLK->APBCLK.UART0_EN = 1;//Enable UART clock SYSCLK->CLKSEL1.UART_S = 0; //Select external 12Mhz for UART clock source SYSCLK->CLKDIV.UART_N = 0; //UART clock source = 12Mhz; /* Step 2. GPIO initial */ SYS->GPBMFP.UART0_RX =1; SYS->GPBMFP.UART0_TX =1; /* Step 3. Set BaudRate */ UART0->BAUD.DIVX_EN = 1; UART0->BAUD.DIVX1 = 1; UART0->BAUD.DIV = 12000000 / 115200 -2; /*Select Operation mode */ UART0->FCR.TFR =1; //Reset Tx FIFO UART0->FCR.RFR =1; //Reset Rx FIFO UART0->FCR.RFITL = 0; //Set Rx Trigger Level -1byte FIFO UART0->LCR.PBE = 0; //Disable parity UART0->LCR.WLS = 3; //8 data bits UART0->LCR.NSB = 0; //Enable 1 Stop bit } void write_to_ADE7753(int ADE7753_address, long write_buffer, int bytes_to_write) { uint8_t make_write_cmd = 0x80; uint8_t this_write = 0x00; ADE7753_address = ADE7753_address|make_write_cmd; SPI0->SSR.SSR = 1; // select Slave SPI0->CNTRL.TX_BIT_LEN = 8 + bytes_to_write*8; //Set length of TX and RX SPI0->TX[0] = ((ADE7753_address)<<(bytes_to_write*8)) + write_buffer; //Store data into TX buffer SPI0->CNTRL.GO_BUSY=1; //Start TX and RX while(SPI0->CNTRL.GO_BUSY); //Check busy SPI0->SSR.SSR =0; // unselect Salave } long read_ADE7753(int ADE7753_address, int bytes_to_read) { long data = 0; int i; uint8_t reader_buf = 0; SPI0->SSR.SSR = 1; // select Slave SPI0->CNTRL.TX_BIT_LEN = 8 ; //Set length of TX and RX SPI0->TX[0] = ADE7753_address; //send MSB Byte address SPI0->CNTRL.GO_BUSY=1; //Start TX and RX while(SPI0->CNTRL.GO_BUSY); //Check busy for (i = 1; i <= bytes_to_read; i++) { SPI0->TX[0] = 0xFF; //get data byte SPI0->CNTRL.GO_BUSY=1; //Start TX and RX while(SPI0->CNTRL.GO_BUSY); //Check busy data |= SPI0->RX[0]& 0xFF; if (i< bytes_to_read) { data = data<<8; } } SPI0->SSR.SSR = 0; return data; } /////////////////main//////////////////////////// int32_t main (void) { InitUART(); InitSPI(); while(1) { for(delay=0; delay< 620000; delay++); // for a small delay write_to_ADE7753(0x09,0x002c,2); //write to mode register to enable temprature measurement for(delay=0; delay< 620000; delay++); output_data= read_ADE7753(0x26,1); // read temperature register on address 0x26 printf(" Temprature register %d , ",output_data); output_data= read_ADE7753(0x17,3); // read VRMS register on address 0x17 printf(" VRMS %dn , ",output_data); for(delay=0; delay< 620000; delay++); }} [/php] |
Note:
You will receive zero if AC input is not connected to ADE7753 IC so first connect AC input then do testing.
Still Have Issues:
Looking forward for your positive response but still if you are facing any problem please leave reply with your issue.
Hi, I was working on this metering IC and came across your code. I cannot seem to get my values right. I suspect that there is something wrong with the circuit. Can you send your circuit diagram?
I have very tight schedule that why it is more difficult and time consuming to draw the whole circuit for you. If you provide me your circuit diagram then it will be much better and fast way to detect the faulted section and providing the correct circuit and all for that particular section.