View previous topic :: View next topic |
Author |
Message |
aeroboy
Joined: 26 Apr 2009 Posts: 9
|
How to read 10 bits in an analog conversion? |
Posted: Fri May 22, 2009 9:06 pm |
|
|
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
|
|
Posted: Fri May 22, 2009 9:18 pm |
|
|
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
|
|
Posted: Sat May 23, 2009 1:43 pm |
|
|
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
|
|
Posted: Sat May 23, 2009 10:53 pm |
|
|
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
|
|
Posted: Mon Jun 01, 2009 5:38 pm |
|
|
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
|
|
Posted: Mon Jun 01, 2009 5:47 pm |
|
|
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). |
|
|
|