RE: [AVR-Chat] Arrays and pointers in GCC
2009-01-12 by subscriptions@aeolusdevelopment.com
David VanHorn Wrote
>Is there a reason that I can't use a char as a pointer in an array of ints?
<snip>
>Started with this code:
Change ADC_Pointer declaration to
unsigned char ADC_Pointer;
Also I'd call it ADC_Index so no one thinks it is actually a pointer
>// Reads all 8 channels even though we only use two.
>ISR(ADC_vect)
>{ // Store in array of locations per channel flag
>
> // Read the channel data MUST BE IN LOW-HIGH order!
> // low byte of ADC_Reading = ADCL
> // high byte of ADC_Reading = ADCH
> ADC_Data[ADC_Pointer] = ADCW; // ADC_Reading;
>
> // Select the next channel
> ADC_Pointer++; //Only I ever increment this!
> if (ADC_Pointer > 7) ADC_Pointer=0;
Change above two lines to (1)
ADC_Pointer = (ADC_Pointer+1)&7u; /* Use only 3 low oder bits as an
index */
If GCC is smart enough it might elide the automatic expansion of type width
since it makes no difference.
Robert
1- This is logically equivalent to what you are doing but very few
compilers are capable of optimizing the if statement to a masking operation.
--------------------------------------------------------------------
mail2web.com - Microsoft® Exchange solutions from a leading provider -
http://link.mail2web.com/Business/Exchange