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 CCS Technical Support

sensor analog and adc coding

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







sensor analog and adc coding
PostPosted: Wed Dec 16, 2009 9:01 am     Reply with quote

Hello, I'm new used in microcontroller.
How can I do the program when the input sensor is analog which is a variable resistance. Then, how can my output relay go active high when input voltage to the PIC is half of maximum voltage input which is 2.5V?

I have tried compile the coding. Can you check the programming below.
Code:

#include<16F877a.h>
#device adc=8
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt,nolvp
#byte porta=5
#byte portB=6
#byte portC=6

void main()
{
int value;
set_tris_B(0);
set_tris_C(0);

setup_port_a(all_analog);
setup_adc(adc_clock_internal);
set_adc_channel(1);

while(1)
{
   value=read_adc();
   portB=value;

if(value=128)
{
output_high(PIN_B0);

output_low(PIN_C0);
}

else
{
output_low(PIN_B0);
output_high(PIN_C0);
}
delay_ms(100);

}

}
Ttelmah
Guest







PostPosted: Wed Dec 16, 2009 9:21 am     Reply with quote

What is the C syntax to _test_ two values for equality - it is _not_ '='......

Best Wishes
pcb0ard
Guest







PostPosted: Wed Dec 16, 2009 10:00 am     Reply with quote

Thanks. I will improve it.

How can I write the value of adc number to the coding.
It is in decimal? Or the value in step on quantisition. Because maximum value in PIC only 5V.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Dec 16, 2009 12:41 pm     Reply with quote

It is not clear if you are sensing a voltage or if you are sensing a resistance. To sense a resistance you will need some hardware, maybe just a pull-up resistor.

This line
Code:
#device adc=8
says that your A/D gives a value from 0 to 255. So a voltage above 2.5V will give a value above 128.

Look up the meanings of the symbols = < > == <= and >=.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Dec 17, 2009 3:41 am     Reply with quote

Your problem will be where you get VREF from. If you take it from VSS the PICs supply then as that drops so will VREF. I assuem you plan on connecting the A2D input to the VSS pin to measure the voltage!
So as the voltage drops on the pic so will the VREF drop and the reading will stay the same until the pic fails.

You will need another supply for VREF which is stable even when VSS drops so that you can measure VSS to see if it drops below a set voltage.

We need to know what your circuit will be for this.
pcb0ard
Guest







PostPosted: Thu Dec 17, 2009 9:11 am     Reply with quote

Hi all. Thanks for your view reply to my posting.
Quote:

You will need another supply for VREF which is stable even when VSS drops so that you can measure VSS to see if it drops below a set voltage.

How can I have stable voltage of Vref to the PIC?
because, as the input it used variable resistor. So, it make the value varied.

For our information, input of this PIC used variable resistor as sensor. And output of the PIC is relay function as switch.
the operation of the circuit, when sensor (variable resistor) reach to minimum value( voltage=2.5V) the output PIC was active high.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Dec 17, 2009 10:18 am     Reply with quote

I thought you were trying to trigger an output when the pic supply dropped below a certain value.

What you are doing is fine. using a pot you will be able to detect when it is at mid position.

As stated you need to use == for your conditional (if) statement
if (value == 128)

But I would use <= or >= as your code will rely on the valule matching exactly 128 when you do the check.

So
if (value <= 128)
Is what I think you want.
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Thu Dec 17, 2009 11:04 am     Reply with quote

Is that a school project?

Since you did not specify a Vref for your A/D by default the processor uses Vdd as the upper limit and Vss as the lower limit. You connect your sensor to a pot and the pot to Vdd and the other end of your sensor to GND. You tweak your pot so that you get 2.5V at the input to the PIC and your should get a A/D count of 127 or 128. Now as the sensor resistance changes the counts will change.
pcb0ard
Guest







PostPosted: Sun Dec 20, 2009 9:50 pm     Reply with quote

Thanks 4 replying.

But, when I was simulate the circuit, why at the port which it is output, can't follow instruction of input adc?
At port input which is adc only show at when value<=128 active high. Not at the output port.

At the below such the coding:
Code:

if(value <=128)
  {
   output_high(PIN_B1);
  }
else
  {
   output_high(PIN_C0);
  }

and while doing the simulation, it trigger on and off at port output.
How to improve the output to hold only just ON and OFF at one time?
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Mon Dec 21, 2009 9:51 am     Reply with quote

pcb0ard wrote:
Thanks 4 replying.

But, when I was simulate the circuit, why at the port which it is output, can't follow instruction of input adc?
At port input which is adc only show at when value<=128 active high. Not at the output port.

At the below such the coding:
Code:

if(value <=128)
  {
   output_high(PIN_B1);
  }
else
  {
   output_high(PIN_C0);
  }

and while doing the simulation, it trigger on and off at port output.
How to improve the output to hold only just ON and OFF at one time?

I think you forgot about what happens to PIN_B1 and PIN_C0 when value is >= 128. Assume that both B1 and C0 are low to start with. For example, let's say value = 100. Then your B1 will be high and C0 will be low. Now the next time around value = 200, now C0 will be high BUT B1 REMAINS HIGH. I think that's your problem.
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