To scale a 10 bit value to 8 bit, is just taking the 8 highest bits... nothing else, is no needed any math operation.
When the pic read adc, the readed value is always 10 bits, but it is stored in two 8bit registers, ADRESH and ADRESL, depending if you need 10bit or 8 bit value, GCBasic configure ADC to store the lower 8 bits in ADRESL and the two highest bits in ADRESH when 10 bit is needed; if 8 bit is needed the 8 highest bits are stored in ADRESH, 2 lower ones in ADRESL, but only ADRESH is readed and passed to the variable.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have seen it discussed a few times here and there but have a question.
Was wondering about the actual 10 bit value and what is happening in GCB.
Does the ReadAN(x) scale the 10 bit value to an 8 bit when the pic has a 10bit ADc?
ReadAN10(x) returns the full 10 bit value so...
I have a pic that has 10bit ADc.
I only want an 8 bit value scaled across my full voltage etc.
I thought this might work:
Dim result as word
Dim scaled_value as byte
result = ReadAN10(x) (0-1023)
scaled_value = result / 4 (1024 / 4 = 256)
Also would add some code to prevent div by 0 unless GCB handles that already?
Thoughts?
-Mz-
If you just need an 8 bit value just use:
dim value as byte
value = ReadAD(ANx)
GCBasic will just read the highest 8 bit.
To scale a 10 bit value to 8 bit, is just taking the 8 highest bits... nothing else, is no needed any math operation.
When the pic read adc, the readed value is always 10 bits, but it is stored in two 8bit registers, ADRESH and ADRESL, depending if you need 10bit or 8 bit value, GCBasic configure ADC to store the lower 8 bits in ADRESL and the two highest bits in ADRESH when 10 bit is needed; if 8 bit is needed the 8 highest bits are stored in ADRESH, 2 lower ones in ADRESL, but only ADRESH is readed and passed to the variable.
Thanks for the quick feedback.
I just assumed that ReadAD(x) was truncating not justifing left and dropping the low bits (which were the h) off the value.
I also thought of doing that and just rotating the bits left 8 times, etc.
-Mz-