View previous topic :: View next topic |
Author |
Message |
kkobraa
Joined: 28 Sep 2003 Posts: 9 Location: AR, USA
|
Some basic code questions |
Posted: Fri Feb 04, 2005 2:44 pm |
|
|
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
|
|
Posted: Fri Feb 04, 2005 5:47 pm |
|
|
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
|
|
Posted: Fri Feb 04, 2005 6:58 pm |
|
|
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
|
|
Posted: Sat Feb 05, 2005 3:59 am |
|
|
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. |
|
|
|