![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
casandralam
Joined: 26 Jan 2008 Posts: 15
|
Read temperature and send over RS232 right? |
Posted: Sat Jan 26, 2008 4:49 am |
|
|
hi,i am a student doing my project now..
My project is about interface designed circuit to PC..
My first stage is to read temperature using the LM35,an analog sensor and sends it over the RS232.
Here is my code:
I hope u can help me to check whether my code is valid to meet my objective.
I am very appreciate of your help
#include<16F877A.h>
#device*=16
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600,xmit=PIN_B2,rcv=PIN_B1)
void main()
{
int16 value; //set data type for analog value
//defines a 16bits number
int_temp;
setup_port_a(ALL_ANALOGS);
setup_adc(ADC_CLOCK_INTERNAL);
// setup_psp(PSP_DISABLED);
//setup_spi(FALSE);
//setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);
// setup_timer_1(T1_DISABLED);
//setup_timer_2(T2_DISABLED,0,1);
// setup_comparator(NC_NC_NC_NC);
// setup_vref(FALSE);
setup_adc_channel(0); //channel AN0 is adc input port
delay_ms(1000);
do{
value=read_adc();
read_adc(ADC_START_ONLY);
value=read_adc(ADC_READ_ONLY);
delay_ms(1000);
temp=(value*48)/100; //convert to degree Celcius
delay_ms(1000);
printf("%u\r\n",value);
}
while(TRUE)
Thanks for all of your help
:-) |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sat Jan 26, 2008 5:56 am |
|
|
Start with the ADC reading. The line:
On it's own does everything needed to perform a conversion. It starts the ADC, waits for the conversion to complete, and reads the ADC.
When called with the defined constants (START_ONLY etc.), it performs just _part_ of the reading operation. You unfortunately, then perform only two of the required three parts, starting the ADC, and immediately reading it, without waiting for the conversion to complete. As such, your reading will be invalid. Either just use the single line to do the whole thing, or add the line:
Code: |
while (!adc_done());
|
Between starting the ADC, and reading the value.
Secondly, change the ADC clock, to ADC_CLOCK_DIV_8. Read what it says in the data sheet about using the internal RC clock. It'll _work_, but it's accuracy will be poor, if the chip is running faster than 1MHz, unless you use the 'sleep' ability to perform the reading.
Now your conversion. What do you actually want it to send?. The 335, has an output that steps 10mV/degree _kelvin_. Do you want to work in kelvin, or centigrade?. Your ADC input, assuming you are running on a 5v supply, has an input, that steps 4.8828125mV/step. 0C, will be 2.73v, and on the ADC, will read 559. Your current code will return this as 268. Not quite a kelvin result. The problem is that you have rounded '48.8', down to 48, rather than up to 49 (which is closer).
temp=(val*49)/100;
will give a better result.
But your next problem will be that this is already _over_ the maximum that can be stored in an int. Assuming you want to work in centrigrade, and you only want to work with +ve values, then use:
temp=((val-559(*49)/100;
Which will give '20', for 20C at the sensor (with the slight error present in the sensor).
Finally, you are printing 'value', not 'temp'. Value is the int16 reading from the ADC, not the temperature....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
casandralam
Joined: 26 Jan 2008 Posts: 15
|
thanks |
Posted: Sat Jan 26, 2008 11:45 pm |
|
|
thanks you very much..
i understand now.your explanation is excellent
one thing i am not so clear is the ADC internal clock....
why i cannot just write ADC_CLOCK_INTERNAL?But, ADC_CLOCK_DIV_8?
Which data sheet can i refer to?i try to look into my microcontroller datasheet and also ccs manual already,but i cannot find it.. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sun Jan 27, 2008 10:28 am |
|
|
The microcontroller data sheet.
Section 11, on the ADC module.
Table 11.1
Note2, below the table.
The problem is that the RC clock, is asynchronous to the processor clock. As the processor gets faster, the internal processor noise rises, and since the RC oscillator changes in speed significantly with temperature, you will get varying numbers of machine instructions executed in the ADC cycle, and more variability in the noise seen. In fact the RC clock, is the _best_ clock for really accurate result, if you put the processor to sleep for the conversion. However unless this is done, above 1MHz, you should select a clock based on the master oscillator.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Foppie
Joined: 16 Sep 2005 Posts: 138 Location: The Netherlands
|
|
Posted: Mon Jan 28, 2008 2:57 am |
|
|
I might have a small hint.
I don't know your exact design specification. But what I would do in this case is sending the raw value form the adc to the pc and let the pc software handle the conversion. This has in my opinion 2 big advantages.
1. The conversion can be done much 'better' on a PC. You save time on your PIC.
2. The communication is easier when you send in hex format.
The disadvantage is that your PIC doesn't know the temperature.
I hope this helps you. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|