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

How to read 10 bits in an analog conversion?

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



Joined: 26 Apr 2009
Posts: 9

View user's profile Send private message

How to read 10 bits in an analog conversion?
PostPosted: Fri May 22, 2009 9:06 pm     Reply with quote

Hello,

I have the next code, I am reading an analog value every 10 ms, then I am sending that value via serial, but I can only send 8 bits, these 8 bits cover the 5 volt range, I think I am only sending the ADRESH register, how do I do to send the ADRESL register too? I thought about defining ADRESL but it is in the bank 1 and I do not know how to change banks in CCS. I know I will have to send 2 values separately.

Code:

#include <16F877A.h>
#include <stdlib.h>

#use delay(clock=20000000)
#use rs232(baud=57600, xmit=PIN_C6, rcv=PIN_C7)
#byte PORTB=0x06 //Define PORTB in memory location
#byte PORTC=0x07 //Define PORTC in memory location
#byte PORTD=0x08 //Define PORTD in memory location
#byte TXREG=0x19 //Define TXREG in memory location
#byte RCREG=0x1A //Define RCREG in memory location

unsigned long value;

void main(){
int a;
a=1;

set_tris_a(0xFF); // Port A all input
setup_port_a(ALL_ANALOG); // Port A all analog

while(a==1){
set_adc_channel(0); // Set channel 0
setup_adc(ADC_CLOCK_INTERNAL); // Turn on
delay_us(20); // Wait 20 us
value=read_adc(); // Read convertion
setup_adc(ADC_OFF); // Turn off converter
TXREG=value; // Send value
delay_ms(10);
  }
}


Thanks.
--Luis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 22, 2009 9:18 pm     Reply with quote

Note the #device statement in this program:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168

Also you are missing your #fuses statement.

Other notes:
1. There is no requirement to turn off the ADC.
2. You will get better accuracy from the ADC by using the oscillator
clock divisor instead of the "internal" adc clock. For 20 MHz, the divisor
value is 32. See the .h file for your PIC to get the correct constant.
3. It's far better to use the putc() function to write to the UART,
because putc() checks to see if the transmitter is ready for a new
character before writing to it.
4. You can use printf to send a 16-bit value (as ASCII) with the UART.
5. You don't have to set the TRIS. The compiler will do it for you, if
necessary.
aeroboy



Joined: 26 Apr 2009
Posts: 9

View user's profile Send private message

PostPosted: Sat May 23, 2009 1:43 pm     Reply with quote

Hello,

Thanks, the next code puts the max value for a ten bit variable. But then that means that I will be sending 4 ASCII characters, right?

Code:

#include <16F877.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

void main
{
float value;

value=1023;
printf("%4.0f\n",value);

while(1);
}


Also I was wondering if I can keep the ADC turned on even if I change the channel, eg,

Code:
set_adc_channel(0);


to

Code:
set_adc_channel(1);


Thanks.
--Luis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 23, 2009 10:53 pm     Reply with quote

Quote:
that means that I will be sending 4 ASCII characters, right?

Yes, you're sending 4 ascii characters.

Quote:

Also I was wondering if I can keep the ADC turned on even if I change the channel.

You don't ever have to turn off the ADC. Turn it on and leave it on.
(In nearly all applications).
aeroboy



Joined: 26 Apr 2009
Posts: 9

View user's profile Send private message

PostPosted: Mon Jun 01, 2009 5:38 pm     Reply with quote

Hello,

This is working, now I was wondering if someone knows how much time it takes to send and receive data via serial, I mean, how much time it takes to execute the printf function.

Thanks.
--Luis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 01, 2009 5:47 pm     Reply with quote

Assuming a baud rate of 9600 baud, the first two characters that you
send to the UART will have no delay (to the program), but after those two,
it will take about 1 ms per character. This assumes a string of several
characters are sent with one printf statement. So to send 10 characters
it will take about 8 ms.

The statements above apply to the hardware UART. It also assumes
that you start with an idle UART (it's not in the process of sending
anything).
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