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

Some basic code questions

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



Joined: 28 Sep 2003
Posts: 9
Location: AR, USA

View user's profile Send private message Yahoo Messenger

Some basic code questions
PostPosted: Fri Feb 04, 2005 2:44 pm     Reply with quote

Hi,

I have some questions below:

Question #1: Is it ok if I declare like the codes below? I will use these values as constants. Or I have to use them as variables to get a float values?

Code:
#define Vref 2.1
#define R_load 0.18
#define Rs 0.036
#define Ts 1e-5
#define L 1e-4

Question #2: Do I need to do right-shift to get the right value of ADC? The sense value of ADC is 2.5V.

Code:
Vsense_adc_read = read_adc(); //10-bit ADC
Vsense_adc_read = Vsense_adc_read>>6; //right-shift
iVsense = (int32)((Vsense_adc_read)*(iADCgain));

Question #3: Is this right?

A = 2.5;
A = A>>13;
B = 3.4;
B = B>>13;
C = (int32)(A*B)>>26;

does it mean that C = 8? What if C is a float variable and the last line of code is: C = (A*B)>>26, then C = 8.5?

I'm new in this.

Thanks alot
Ken.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 04, 2005 5:47 pm     Reply with quote

Quote:
Question #1: Is it ok if I declare like the codes below? I will use these values as constants.
Code:
#define Vref 2.1
#define R_load 0.18
etc.

Yes, that will work. However, normally constants are put in upper case.
Here's one of the many C style guides on the net, which says that:
http://www.cs.uiowa.edu/~jones/syssoft/style.html#caps

Quote:
Question #2: Do I need to do right-shift to get the right value of ADC? The sense value of ADC is 2.5V.

You shouldn't have to do a right-shift if you have a modern version
of the compiler and it's working properly for your PIC, and if you
have declared: #device adc=10

There's a chart in the CCS manual, in the read_adc() section,
which shows this.

Quote:
Question #3: Is this right?
A = 2.5;
etc.

This particular question, I don't want to do.
Is this some sort of class puzzle ?
kkobraa



Joined: 28 Sep 2003
Posts: 9
Location: AR, USA

View user's profile Send private message Yahoo Messenger

PostPosted: Fri Feb 04, 2005 6:58 pm     Reply with quote

Hi,

Thanks for you reply.
Let me state question #3 again.

Question #3:

Code:

A=2.5; // A is a constant
B = 3.4 // B is a constant
A1 = A*1024; // A1 = A*2^10
B1 = B*1024; // B1 = B*2^10
C1 = int32(A1*B1)>>20; // C1 is int32 variable
C2 = (A1*B1)>>20; //C2 is float variable

The question is: C1=8 and C2=8.5?

The reason I do this is because I don't want to use float variables. I need ROM for other purposes and save time.

Thanks again and any suggest would help.

Ken.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Feb 05, 2005 3:59 am     Reply with quote

I'm still not completely sure what you want, maybe because of the
way you're writing it.

You didn't show your variable declarations.

What are the data types for A, B, A1, B1, C1 and C2 ?

You said "I don't want to use float variables".

If none of those variables listed above are floats, you'll never
get a value such as 8.5 loaded into them. It will be 8 or 9 or
some other integer.

It's tough to know, but I think you may be asking if the
compiler pre-processor will do floating point math, and then
convert the result to an integer and stuff it into your integer
variable ? Example:
Code:

#define MY_CONST  1024 * 3.4

void main()
{
int32 result;

result = MY_CONST;

while(1);

The answer is yes. The variable 'result' is set = 3481.

It is NOT set = to 3481.6, because it's declared as a int32.
So it can only be a whole integer value, with no fractional part.
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