View previous topic :: View next topic |
Author |
Message |
SOS_help Guest
|
could I just use 6 bit A/D in CCS or with PIC18F458? |
Posted: Fri Jul 30, 2004 2:31 pm |
|
|
for some reason, I don't have to use 8 or 10 bit A/D , I just need 6 bit. how I can do it in CCS with PIC18F458? or I have to change pic chip to 6 bit pic? thanks |
|
|
Ttelmah Guest
|
Re: could I just use 6 bit A/D in CCS or with PIC18F458? |
Posted: Fri Jul 30, 2004 2:38 pm |
|
|
SOS_help wrote: | for some reason, I don't have to use 8 or 10 bit A/D , I just need 6 bit. how I can do it in CCS with PIC18F458? or I have to change pic chip to 6 bit pic? thanks |
By default, the AD, will return 8bits, if you don't add the confguration to the device statement. This is the lowest resolution mode that is 'native' to the hardware.
Just use:
val=read_adc()>>2;
This will give the most significant 6 bits, at the cost of only two machine cycles for the shift.
It is worth also being aware, that if you use the chip like this, you can sample earlier after changing an ADC channel, than is otherwise the case.
There is no PIC with a 6bit ADC.
Best Wishes |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Jul 30, 2004 3:17 pm |
|
|
I might do it like this:
val = read_adc() & 0x3F;
This will strip the two highest bits from the value. I believe the >> would change the value by actually moving the bits to a different location in the word.
Ronald |
|
|
languer
Joined: 09 Jan 2004 Posts: 144 Location: USA
|
|
Posted: Fri Jul 30, 2004 6:33 pm |
|
|
Using Ttelmah's approach, you get the 6-MSBs (which is what you want),
Code: |
.................... val = read_adc() >> 2;
005F: BSF 1F.1
0060: BTFSC 1F.1
0061: GOTO 060
0062: RRF 1E,W
0063: MOVWF 22
0064: RRF 22,F
0065: MOVLW 3F
0066: ANDWF 22,F
|
Using rnielsen's approach, you get the 6-LSBs (which I do not think is what you want),
Code: |
.................... val = read_adc() & 0x3F;
0067: BSF 1F.1
0068: BTFSC 1F.1
0069: GOTO 068
006A: MOVF 1E,W
006B: ANDLW 3F
006C: MOVWF 22
|
|
|
|
Sherpa Doug Guest
|
|
Posted: Fri Jul 30, 2004 7:17 pm |
|
|
MSB or LSB depends on how big your input signal is. If 256 is 5V then the 6 MSBs will range from zero to 5V. The 6 LSBs range from zero to 1.25V. If your signal is small you don't have to amplify it as much using the LSBs. However the LSBs may give you a little more noise.
Sherpa Doug |
|
|
Ttelmah Guest
|
|
Posted: Sat Jul 31, 2004 2:45 am |
|
|
rnielsen wrote: | I might do it like this:
val = read_adc() & 0x3F;
This will strip the two highest bits from the value. I believe the >> would change the value by actually moving the bits to a different location in the word.
Ronald |
I am throwing away to two 'bottom' bits of the data. Your approach throws away the two top bits, giving an ADC, covering 1/4 the reference voltage range.
It is also worth considering:
val=(read(adc)+2)>>2;
Which gives more symmetrical behaviour.
Best Wishes |
|
|
Guest
|
|
Posted: Sat Jul 31, 2004 6:35 am |
|
|
Generally when I work the ADC I try to make sure the MSB in the data is the MSB of the ADC. That is I assume the data is value between 0.0 1.0 where zero maps 0x00 and 1.0 mapps to 0xFF.
For example if I was doing a 6 bit ADC then I would
value=ADCresult & 0xFC; //6 bit result.
Now if I wanted to read the voltage output as 0.0-5.0 volts with one decimal place. You could do this by multiplying the value by 50.
int16 volts;
volts=value*50;
//volts now has the value of 0-0x31CE
volts=volts>>8; //get the upper word (note faster methods than shift)
//at this point volts is 0-0x31 or 0-49
Now what happens is the volts will be a value between 0-50 which maps to the voltage 0.0-5.0...
Doing the ADC this way is nice in that 6 or 8 bit ADC it does not matter. Even if you have 10 bit ADC then the only thing is the variables need to be 16 bit and the ADC needs to be left justified.
Trampas. |
|
|
SOS_help Guest
|
thanks all reply |
Posted: Sat Jul 31, 2004 7:28 pm |
|
|
I can't try the methods you guys metioned here at home, but I am wondering if I do so, 5V input still matches to 64(6 bit)?or just reduces the input voltage range?
thanks again, |
|
|
|