• AVR Freaks

Hot!Need help creating quantizer

Page: < 1234 Showing page 4 of 4
Author
1and0
Access is Denied
  • Total Posts : 9769
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Need help creating quantizer 2019/09/30 07:01:37 (permalink)
0
ric
Could I also suggest now that you avoid single letter variable names like the plague.
They give no hint what the variable is used for, and are impossible to search for.
Make them a bit longer, and more descriptive.

Short variable names are like pronouns in natural languages; i.e. to be used in small context and scope. To me, "i" means "iteration" which is quite meaningful in short loop, and "p" means "pointer" in a local context.
 
ric
Also, 1and0 suggested to ways of calculating the number of elements in an array.
Personally, I think the first version is much easier for someone to look at and see what is happening
len = sizeof array / sizeof array[0];



I agree the first version is much easier to look at, and the second version is more concise. ;)
 
Anyway, may be this:
    int16_t *p_scale;
    uint8_t length;
   
    if (scale < 510) {
        p_scale = major;
        length = sizeof major / sizeof major[0];
    } else {
        p_scale = minor;
        length = sizeof minor / sizeof minor[0];
    }
   
    do {
        if (inputValue <= *p_scale) break;
        p_scale++;
    } while (--length);
   
    PWM1_LoadDutyValue(*p_scale);
#61
didierleplae
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Need help creating quantizer 2019/09/30 12:02:04 (permalink)
0
1and0
 
Anyway, may be this:
    int16_t *p_scale;
    uint8_t length;
   
    if (scale < 510) {
        p_scale = major;
        length = sizeof major / sizeof major[0];
    } else {
        p_scale = minor;
        length = sizeof minor / sizeof minor[0];
    }
   
    do {
        if (inputValue <= *p_scale) break;
        p_scale++;
    } while (--length);
   
    PWM1_LoadDutyValue(*p_scale);




Actually, doing it this way, I can just input the array size instead of using sizeof. Although I have multiple arrays of different size, any given array will stay the same.
This seems to be working quite well now. I'm going to start testing the hardware part.
Thanks so much for all your help!
#62
1and0
Access is Denied
  • Total Posts : 9769
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Need help creating quantizer 2019/09/30 14:16:21 (permalink)
0
didierleplae
Actually, doing it this way, I can just input the array size instead of using sizeof. Although I have multiple arrays of different size, any given array will stay the same.

Are you saying you're going to hard-code the array size instead of using sizeof to calculate it for you, such as,
    length = 36;

?
 
#63
ric
Super Member
  • Total Posts : 23870
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Need help creating quantizer 2019/09/30 14:55:03 (permalink)
0
As above.
It sounds like you are proposing to move from a RELIABLE way of doing it (letting the compiler do it for you),
to a highly unreliable way of assuming the programmer (which may or not be you) to always keep that second variable up to date...
 
FYI, if you're doing this for some supposed efficiency gain, the sizeof() function only works when the compiler is able to evaluate it at compile time. That's why you can't use it on a pointer to an array. There is no efficiency improvement in hard coding an array length rather than using sizeof() to calculate it for you.
post edited by ric - 2019/09/30 14:57:15

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#64
didierleplae
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Need help creating quantizer 2019/09/30 16:27:32 (permalink)
0
1and0
didierleplae
Actually, doing it this way, I can just input the array size instead of using sizeof. Although I have multiple arrays of different size, any given array will stay the same.

Are you saying you're going to hard-code the array size instead of using sizeof to calculate it for you, such as,
    length = 36;

?
 


Yes, exactly
#65
didierleplae
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Need help creating quantizer 2019/09/30 16:29:59 (permalink)
0
ric
As above.
It sounds like you are proposing to move from a RELIABLE way of doing it (letting the compiler do it for you),
to a highly unreliable way of assuming the programmer (which may or not be you) to always keep that second variable up to date...
 
FYI, if you're doing this for some supposed efficiency gain, the sizeof() function only works when the compiler is able to evaluate it at compile time. That's why you can't use it on a pointer to an array. There is no efficiency improvement in hard coding an array length rather than using sizeof() to calculate it for you.


Interesting. That's good to know. 
May be better to keep it with the sizeof() function in case I make some changes and forget to update for my array size. 
post edited by didierleplae - 2019/09/30 16:31:08
#66
1and0
Access is Denied
  • Total Posts : 9769
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Need help creating quantizer 2019/09/30 17:13:09 (permalink)
0
sizeof is an operator, not a function, which is used to compute the size (in bytes) of its operand at compile time. As an operator, the parentheses is not needed if the operand is an expression; if the operand is a type, the parentheses is needed.
 
#67
ric
Super Member
  • Total Posts : 23870
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Need help creating quantizer 2019/09/30 17:18:46 (permalink)
0
Thanks Harry. I did feel uncomfortable calling it a "function", but the term "operator" just wouldn't come into my head as I was typing. :)
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#68
PStechPaul
Super Member
  • Total Posts : 2401
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: Need help creating quantizer 2019/09/30 21:25:57 (permalink)
0
A better and more efficient method for maintaining the size of an array might be:
#define MAJOR_SIZE 36
unsigned int major[MAJOR_SIZE];
for (i=0; i<MAJOR_SIZE; i++)
  element = major[i];


 
#69
1and0
Access is Denied
  • Total Posts : 9769
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Need help creating quantizer 2019/10/01 01:30:11 (permalink)
0
PStechPaul
A better and more efficient method for maintaining the size of an array might be:
#define MAJOR_SIZE 36
unsigned int major[MAJOR_SIZE];
for (i=0; i<MAJOR_SIZE; i++)
  element = major[i];



Try doing that when you have multiple (10's, 100's, etc.) arrays of different sizes as in OP's case.
 
With the following you don't even have to know the size of the array, because the compiler is taking care of it for you:
#define ARRAYSIZE(arr) ((sizeof(arr)/sizeof(arr[0])) / ((size_t)(!(sizeof(arr)%sizeof(arr[0])))))
unsigned int major[] = { 0,34,68,85,119,153,187,204,238,
                         272,289,323,357,391,408,442,476,493,
                         527,561,595,612,646,680,697,731,765,
                         799,816,850,884,901,935,969,1003,1020
                       };
for (i = 0; i < ARRAYSIZE(major); i++)
    element = major[i];

So is it better or not - this way you don't have the annoyance of specifying the size each time for all the arrays.  Efficiency-wise, regarding memory and speed, it is the SAME!
 
<edit> Added the ARRAYSIZE macro.
post edited by 1and0 - 2019/10/01 09:10:31
#70
NorthGuy
Super Member
  • Total Posts : 5675
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Need help creating quantizer 2019/10/01 06:32:40 (permalink)
0
Or you can use
 
#define ASIZE(array) (sizeof array/sizeof array[0])

#71
1and0
Access is Denied
  • Total Posts : 9769
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Need help creating quantizer 2019/10/01 08:52:50 (permalink)
0
Here's one that causes a divide-by-zero error at compile time for cases (except signed and unsigned char *) where a pointer instead of an array argument is passed:
#define ARRAYSIZE(arr) ((sizeof(arr)/sizeof(arr[0])) / ((size_t)(!(sizeof(arr)%sizeof(arr[0])))))

#72
didierleplae
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Need help creating quantizer 2019/10/02 10:41:56 (permalink)
0
I'm having a little trouble with what I believe is the voltage on ANA0 interfering with my input at ANA4. At the extreme high or lows of the voltage on ANA0 will make my output switch to the next or previous array.
 
I'm wondering if this note in the PIC16F18313 datasheet might hold the answer:

It is recommended that when switching from an ADC channel of a higher voltage to a channel of a lower voltage, the software selects the VSS channel before switching to the channel of the lower voltage. If the ADC does not have a dedicated VSS input channel, the VSS selection (DAC1R<4:0> = b’00000’) through the DAC output channel can be used. If the DAC is in use, a free input channel can be connected to VSS, and can be used in place of the DAC.





 
My question is, how exactly should this be done?
 
This is what I've tried and doesn't seem to work:

ADC_Initialize();
ADC_StartConversion(channel_ANA0);
while(!ADC_IsConversionDone());
inputValue = ADC_GetConversionResult()/64;

ADCON0 = 0xF1;


ADCON0 = 0x11;
ADC_StartConversion(channel_ANA4);
while(!ADC_IsConversionDone());
scale = ADC_GetConversionResult()/64;

 
#73
1and0
Access is Denied
  • Total Posts : 9769
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Need help creating quantizer 2019/10/02 11:39:22 (permalink)
0
didierleplae
I'm having a little trouble with what I believe is the voltage on ANA0 interfering with my input at ANA4. At the extreme high or lows of the voltage on ANA0 will make my output switch to the next or previous array.
 
I'm wondering if this note in the PIC16F18313 datasheet might hold the answer:

It is recommended that when switching from an ADC channel of a higher voltage to a channel of a lower voltage, the software selects the VSS channel before switching to the channel of the lower voltage. If the ADC does not have a dedicated VSS input channel, the VSS selection (DAC1R<4:0> = b’00000’) through the DAC output channel can be used. If the DAC is in use, a free input channel can be connected to VSS, and can be used in place of the DAC.


Read the two-sentence paragraph immediately before what you've quoted above from your PIC datasheet.
#74
didierleplae
Starting Member
  • Total Posts : 43
  • Reward points : 0
  • Joined: 2019/03/31 16:57:50
  • Location: 0
  • Status: offline
Re: Need help creating quantizer 2019/10/05 04:14:45 (permalink)
0
OK, so I need to wait the required acquisition time.
The math seems pretty complicated to figure out, but do you think I can assume the example in the datasheet would be pretty close to my required acquisition time?:
 

TACQ = 2µs + 892ns + (50°C- 25°C)
= 4.62µs

 
Would I then do something like this?
 

 
ADC_Initialize();
 
 
 
ADC_StartConversion(channel_ANA0);
while(!ADC_IsConversionDone());
inputValue = ADC_GetConversionResult()/64;

ADCON0 = 0xF1;
__delay_us(5);

ADCON0 = 0x11;
ADC_StartConversion(channel_ANA4);
while(!ADC_IsConversionDone());
scale = ADC_GetConversionResult()/64;
 

post edited by didierleplae - 2019/10/06 22:33:24
#75
Page: < 1234 Showing page 4 of 4
Jump to:
© 2019 APG vNext Commercial Version 4.5