CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

could I just use 6 bit A/D in CCS or with PIC18F458?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SOS_help
Guest







could I just use 6 bit A/D in CCS or with PIC18F458?
PostPosted: Fri Jul 30, 2004 2:31 pm     Reply with quote

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?
PostPosted: Fri Jul 30, 2004 2:38 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jul 30, 2004 3:17 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jul 30, 2004 6:33 pm     Reply with quote

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







PostPosted: Fri Jul 30, 2004 7:17 pm     Reply with quote

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







PostPosted: Sat Jul 31, 2004 2:45 am     Reply with quote

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








PostPosted: Sat Jul 31, 2004 6:35 am     Reply with quote

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
PostPosted: Sat Jul 31, 2004 7:28 pm     Reply with quote

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,
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group