CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Power Monitoring with ADE7756 or similar.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 14, 2009 4:41 pm     Reply with quote

This Microchip ASM code could be converted to CCS:
http://www.analog.com/static/imported-files/application_notes/317740795RMS_CODE.txt
Converting it to use CCS functions would greatly reduce the number of
lines of code. Here's an example:

I have converted the SPIRX and ENGREAD1 functions to CCS.
I didn't try to improve it very much. They use globals to transfer
data. For simplicity, I just kept it that way. That could be improved.
Compare this code to the ASM code in the link. But remember,
I don't want to do this project for you. This is just an example to
help you to see how to convert the code in a minimal way. You must
do the rest of it, if you want to.
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)

// SPI mode constants used in the setup_spi() function.
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)


#define ADE7756_CS  PIN_B4

#define RSTENERGY_CMD  0x03

int8 insd5;
int8 insd4;
int8 insd3;
int8 insd2;
int8 insd1;

//--------------------------
void SPIRX(int8 spi_command, int8 wordlen)
{
output_low(ADE7756_CS);

spi_write(spi_command);

insd1 = 0;
insd2 = 0;
insd3 = 0;
insd4 = 0;
insd5 = 0;

// This switch-case code intentionally leaves out the break
// statements, because it is intended to "fall through" to
// get all the required bytes (from 1 to 5, depending on
// the parameter passed to the SPIRX() function).
switch(wordlen)
  {
   case 5:
     insd5 = spi_read(0);
   case 4:
     insd4 = spi_read(0);
   case 3:
     insd3 = spi_read(0);
   case 2:
     insd2 = spi_read(0);
   case 1:
     insd1 = spi_read(0);
     break;
   default:
     printf("Error: Incorrect wordlen\n\r");
   
   }

output_high(ADE7756_CS);
}

//---------------------------
void ENGREAD1(void)
{
SPIRX(RSTENERGY_CMD, 5);     // 40 bits = 5 bytes
}

//============================   
void main()
{
// The ADE7756 will be connected to the 3 hardware SPI pins
// on the PIC.   A spare i/o pin is used for the Chip Select.
// The max allowable SCLK is 3 MHz, so a divisor of 4 is used
// with our 4 MHz crystal, to give 1 MHz SCLK.
setup_spi(SPI_MASTER | SPI_MODE_1 | SPI_CLK_DIV_4); 

output_high(ADE7756_CS);
delay_ms(10);

ENGREAD1();

// The result from the ENGREAD1() function is returned in
// 5 global variables.  Display them.
printf("%x %x %x %x %x \n\r" insd5, insd4, insd3, insd2, insd1);

while(1);
}


---

Edit: Fixed a problem where I left ADE7756_CS in a low state
at the end of SPIRX(). Also added code to initialize it to a high
level at the start of main() and do a short delay after that.


Last edited by PCM programmer on Thu Oct 15, 2009 5:30 pm; edited 1 time in total
gomond



Joined: 05 Nov 2008
Posts: 18

View user's profile Send private message

PostPosted: Thu Oct 15, 2009 3:13 am     Reply with quote

Quote:


This Microchip ASM code could be converted to CCS:
http://www.analog.com/static/imported-files/application_notes/317740795RMS_CODE.txt
Converting it to use CCS functions would greatly reduce the number of
lines of code. Here's an example:

I have converted the SPIRX and ENGREAD1 functions to CCS.
I didn't try to improve it very much. They use globals to transfer
data. For simplicity, I just kept it that way. That could be improved.
Compare this code to the ASM code in the link. But remember,
I don't want to do this project for you. This is just an example to
help you to see how to convert the code in a minimal way. You must
do the rest of it, if you want to.


Thanks Heaps PCM Programmer

That is a ripper kick start. I would have never thought that it could be simpilfied like that.

Being a fitter and turner this stuff takes a while to convert to nuts and bolts but with help like this I can at least get my head arround it.

Thanks again.

Greg.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group