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

store string & adc reference help!!!

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



Joined: 30 Nov 2008
Posts: 8

View user's profile Send private message

store string & adc reference help!!!
PostPosted: Wed Dec 03, 2008 9:49 am     Reply with quote

hi, now I'm working for my final year project. i need to measure 2 temperature sensor and 1 pressure sensor and transmit the value measured through MU-1-R transceiver. i have write some code for this project. now i face 2 problem, first is for pressure sensor, because the range of pressure I'm going to measure are very small (from 99kPa to 101kPa) and the voltage output also very small (range from 4.11V to 4.17V), so i hope can measure the 0.001V change from pressure sensor, this should be can done by 12bit adc, but my pic18f4550 cant make 12bit adc. so can i measure pressure sensor with voltage reference but measure temperature sensor for 0-5V?(see my code)or can suggest better opinion?

My second problem is how I can store the string into buffer? Lets say I have 20 loop of data. For first data I store at buffer 1, second data store at buffer and so on.

Maybe my explanation not so detail, but I can explain more if need. Please help me, thanks.


#include <18F4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device ADC=10 // define 10 bit resolution
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, parity=n, bits=8, BRGH1OK, stream=UART)
#use rs232(baud=4800, xmit=PIN_D0, rcv=PIN_D1, parity=n, bits=8, BRGH1OK, stream=GPS)

#include <stdlib.h>

#define buzzer pin_b0
#define sw1 pin_b1

#define temp1_ch 0
#define temp2_ch 1
#define press_ch 4
#define acc_x_ch 5
#define acc_y_ch 6
#define acc_z_ch 7

signed int temp_1,temp_2;
float volt_temp_1, volt_temp_2, volt_press,press,;
int n;

float analog_read(int channel);

void main()
{
set_tris_a(0xff);
set_tris_b(0xff);

for(n=1;n<21;n++)
{

//setup_adc_ports(AN0_TO_AN7|VREF_VREF);
//setup_adc(ADC_CLOCK_DIV_64);

volt_temp_1= analog_read(temp1_ch);
temp_1=((float)volt_temp_1-2.73)*100;

//setup_adc_ports(AN0_TO_AN7);
//setup_adc(ADC_CLOCK_DIV_64);

volt_temp_2= analog_read(temp2_ch);
temp_2=((float)volt_temp_1-2.73)*100;

volt_press=analog_read(press_ch);
press=(((float)volt_press-0.204)*21.7865)+15;

fprintf(UART,"\r\ntransmitting...");

fprintf(UART,"\r\n%2d,%1.3f,%3.2f,%1.2f,%d,%1.2f,%d",n,volt_press,press,volt_temp_1,temp_1,volt_temp_2,temp_2);

//store the string in buffer[n](n from 1 to 20)
//sprintf(buff[n],"\r\n%2d,%1.3f,%3.2f,%1.2f,%d,%1.2f,%d",n,volt_press,press,volt_temp_1,temp_1,volt_temp_2,temp_2);


printf("\r\nFINISH TRANS!!!");

delay_ms(2000);
}

//how to print the string that store in buffer 1 to buffer 20 to uart
//for(n=1;n<21;n++)
//{
//fprintf(UART,buff[n]);
//}

fprintf(UART,"\r\nFINISH ALL!!!");

}
}

float analog_read(int channel){

float tmp = 0;

set_adc_channel(channel); // select channel
delay_ms(10);
tmp = read_adc(); // read into volt reg

return (float)tmp/204.6; //(a/(5/1023))
}
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Wed Dec 03, 2008 10:34 am     Reply with quote

Quote:
but measure temperature sensor for 0-5V
You haven't mentioned the kind of temperature sensor you are using (thermistor, LM35, etc), but since you are using an ADC, I assume it is one of these (or equivalent). Thermistor-based sensors have a non-linear output, while for LM35 sensors the voltage output is linear. For the thermistor you can use a data-table which stores voltage-temperature information, and for the LM35 use a voltage-slope-offset approach.

Quote:
voltage output also very small (range from 4.11V to 4.17V), so i hope can measure the 0.001V change from pressure sensor
The only option is to amplify the voltage. Buffer the signal; use a reference of 4.11 volts (or thereabouts) and a gain of approximately ( 5 / (max_value-min_value) ). You may need an instrumentation amplifier and some additional hardware for this.

Quote:
how i can store the string into buffer
Use an array for this.
Code:
float volt_temp_1[20]
In the loop
Code:
for(n=0;n<20;n++)   [corrected, can believe how I messed that up :oops: ]
use
Code:
volt_temp_1[n]=analog_read(temp1_ch);


A few other things:
Use the 'code' BBCode tags when you post code. It's much more readable that way.

The 'set_tris()' instruction is redundant. The CCS compile handles all direction setting for you automatically (unless you use the directive '#use fast_io()'.

Uncomment the 'setup_adc' instruction. Put it outside the loop. You need just one of these instructions through the entire program.

Rohit


Last edited by Rohit de Sa on Thu Dec 04, 2008 2:31 am; edited 1 time in total
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Wed Dec 03, 2008 10:56 am     Reply with quote

Rohit de Sa wrote:
Quote:

Quote:
voltage output also very small (range from 4.11V to 4.17V), so i hope can measure the 0.001V change from pressure sensor
The only option is to amplify the voltage. Buffer the signal; use a reference of 4.11 volts (or thereabouts) and a gain of approximately ( 5 / (max_value-min_value) ). You may need an instrumentation amplifier and some additional hardware for this.


This is not quite correct. You need to level shift the input voltage and apply gain. Here is a link which talks about the basics: http://www.daycounter.com/Circuits/OpAmp-Level-Shifter/OpAmp-Level-Shifter.phtml

In your case you want the output of the opamp to center around 2.5 volts (for a 5V VDD) with a gain of 5.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

PostPosted: Wed Dec 03, 2008 10:47 pm     Reply with quote

Quote:
use a reference of 4.11 volts (or thereabouts) and a gain of approximately ( 5 / (max_value-min_value) )
Quote:
You need to level shift the input voltage and apply gain
I meant exactly that. Sorry, I should have been a little clearer and more technically correct. My fault.

Rohit
crystal_lattice



Joined: 13 Jun 2006
Posts: 164

View user's profile Send private message

PostPosted: Thu Dec 04, 2008 2:02 am     Reply with quote

your code for the loop is wrong, you will not use the full array size and it will overflow. Use:

Code:

for(n=0;n<20;n++)
     volt_temp_1[n]=analog_read(temp1_ch);


This will give you a range from 0-19, which is valid for the array you declared.
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