free 8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems

 
8051 microcontroller 8051 microcontroller
Forums

Rickey's World :: Discussion Forums :: Project Development :: Project Help
 
<< Previous thread | Next thread >>
very easy c code, need help with
Go to page       >>  
Moderators: Ajay Bhargav, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph, ExperimenterUK, DavesGarage
Author Post
Alex86Fire
Tue May 26 2009, 08:21AM
 User Offline
Registered Member #18823
Joined: Tue May 26 2009, 08:11AM

Posts: 9
Thanked 0 times in 0 posts
Hi everyone, I need help with what I think should be a very simple C code:

I have 2 input signals and 1 output signal
InputSignal1(IS1) = 1 determines my OutputSignal(OS) to be 1
InputSigna 2(IS2) = 0 determines my OS to be 0
Thats about it.
The 2 signals are from 2 water levels and the output signal should command for a water valve to open to fill up my water tank if the water level is below a certain limit and to close when it gets to my upper limit.

I was thinking to use only PortA for both inputs and outputs, something like: PA0, PA1 inputs, PA2 output, but I'm not sure if it's correct and exactly how to do it. If u guys could help me it would be so great. Thanks

Back to top

DavesGarage
Tue May 26 2009, 09:22AM

 User Offline
Registered Member #14254
Joined: Tue Jan 20 2009, 08:14AM

Posts: 620
Thanked 116 times in 112 posts
So, if I understand you correctly, IS1 is a lower level sensor, which means the water level has reached a low level, and the container needs to be filled up again, and IS2 is an upper level sensor, which means the water level has reached a maximum level, and the water needs to be turned off.

If this is the case, you are correct: The software is fairly simple:

You need a flag to indicate that the lower level sensor has been reached. This flag will be used to turn on the water valve. As the water rises, the lower level sensor will no longer be active, but that's okay, because the flag will still be set, so the valve will remain open. As soon as the upper sensor is reached, it will disable the flag. The output will reflect the flag status always: flag on means output valve is on, flag off -> output off...

So what will the software look like? Give it a try and let's have a look at what you have attempted. There are other things to consider, like sensor jitter, but that will come after your first attempt at the software...

you can make a flag like this:

CODE:
unsigned char Flag;


You can define your sensor like this:

CODE:
#define LowLevelSensor     P0^0
#define HiLevelSensor      P0^1
#define SolenoidValve      P1^0
 


The outline for your routine will look like this:

CODE:
void main()
   {
   /* define flag here */
   
   /* initialize everything here */

   /* main loop here - repeats forever*/
   while( 1 )
      {
      /* define output as a function of inputs */
      }
   }
 


Give it a shot, and see just how much you can learn from this!

Hope this helps,


-Dave
"Basic research is what I am doing when I don't know what I am doing"
Back to top

Alex86Fire
Tue May 26 2009, 09:53AM
 User Offline
Registered Member #18823
Joined: Tue May 26 2009, 08:11AM

Posts: 9
Thanked 0 times in 0 posts
ok, here goes:
CODE:

#define LowLevelSensor     PA0
#define HiLevelSensor      PA1
#define SolenoidValve      PA2

void main()
   {
     unsigned char Flag;
   
   /* initialize everything here
       not sure what to initialize except flag*/

   Flag=0;

   while( 1 )
      {if ( LowLevelSensor==1)
            Flag=1;
        if ( HiLevelSensor==0)
            Flag=0;
          SolenoidValve=Flag;    
      }
   }
 


[ Edited Tue May 26 2009, 09:53AM ]
Back to top

DavesGarage
Tue May 26 2009, 10:45AM

 User Offline
Registered Member #14254
Joined: Tue Jan 20 2009, 08:14AM

Posts: 620
Thanked 116 times in 112 posts
Very good!

Now, consider what might happen if there is noise on the input line from the sensors...

There is a technique called "digital filtering" that can keep you from reacting to a false positive.

Reading the input over and over again, and only setting the flag if the input stays constant for a few successive reads can reduce the possibility of reacting to a false positive.

Here is an example:

CODE:
/* example software... */
#define LowLevelSensor     PA0
#define HiLevelSensor      PA1
#define SolenoidValve      PA2

#define DEBOUNCE_COUNT     10

void main()
   {
   unsigned char Flag;
   unsigned char CounterLow;
   unsigned char CounterHi;

   /* initialize everything here              */
   /* not sure what to initialize except flag */
   Flag        = 0;
   CounterLow  = 0;
   CounterHi   = 0;

   /* repeat forever... */
   while( 1 )
      {
      /* check low level sensor */
      if( LowLevelSensor == 1 )
         {
         if( CounterLow < DEBOUNCE_COUNT )
            CounterLow++;
         }
      else
         {
         CounterLow = 0;
         }

      /* check high level sensor */
      if( HiLevelSensor == 1 )
         {
         if( CounterHi < DEBOUNCE_COUNT )
            CounterHi++;
         }
      else
         {
         CounterHi = 0;
         }

      /* decide how to set flag... */
      if( CounterLow == DEBOUNCE_COUNT )
         Flag = 1;

      if( CounterHi == DEBOUNCE_COUNT )
         Flag = 0;

      /* set the valve based on the flag... */
      SolenoidValve = Flag;
      }
   }
 


-Dave
"Basic research is what I am doing when I don't know what I am doing"
Back to top

Alex86Fire
Tue May 26 2009, 10:50AM
 User Offline
Registered Member #18823
Joined: Tue May 26 2009, 08:11AM

Posts: 9
Thanked 0 times in 0 posts
Thanks for the help!

I already filtered the noise with the electronics behind the sensor, you think a double noise checker would be safer or it can work without it?

P.S.

All I have to connect to the atmega16 is the 3 pins and GND right?
Back to top

DavesGarage
Tue May 26 2009, 10:55AM

 User Offline
Registered Member #14254
Joined: Tue Jan 20 2009, 08:14AM

Posts: 620
Thanked 116 times in 112 posts
If you have a filter built into your electronics, then the digital filtering probably isn't necessary. I always provide a method within my code just in case I want to implement it later on, or if during debugging, I find I have a noisy system.

You can play around with it by changing the debounce value. If you are tight on code space, then I would probably leave it out.

Regarding the connection, as long as the voltage levels and current levels are right, you only need to connect the sensors. My guess is, though, that driving the solenoid from the processor isn't such a good idea. Calculate the current draw from the solenoid coil, and you'll probably come up with the same conclusion. Besides, there's things like back EMF when you open the solenoid ( coil field collapses ) that can damage a micro very quickly. I always isolate loads like this with a driver of some kind.

Good luck,


-Dave
"Basic research is what I am doing when I don't know what I am doing"
Back to top

Alex86Fire
Tue May 26 2009, 11:01AM
 User Offline
Registered Member #18823
Joined: Tue May 26 2009, 08:11AM

Posts: 9
Thanked 0 times in 0 posts
The voltage for the relay is 24 from another circuit and I use it to command an electro valve (supplied at 220) for filling up the tank.
Back to top

DavesGarage
Tue May 26 2009, 11:04AM

 User Offline
Registered Member #14254
Joined: Tue Jan 20 2009, 08:14AM

Posts: 620
Thanked 116 times in 112 posts
Yeah, and what is the voltage level for your microcontroller? And what is the current sinking capability of your port pin? Check the data sheet for these values, and you'll probably find that your micro is out-matched by the solenoid coil load by a significant amount...

My suggestion is go with a transistor to turn the solenoid on and off, or maybe an opto-isolator / driver...



-Dave
"Basic research is what I am doing when I don't know what I am doing"
Back to top

Alex86Fire
Tue May 26 2009, 11:08AM
 User Offline
Registered Member #18823
Joined: Tue May 26 2009, 08:11AM

Posts: 9
Thanked 0 times in 0 posts
Yea, my microcontroller is supplied at 5V and I indeed have a tranzistor that comands the relay .

So in conclusion I only need to connect the 3 pins? I don't know why but I thought that ground is supposed to be common for all the circuit, that's why I wanted to connect the microcontroler ground with the sensor cirucit ground.
Back to top

DavesGarage
Tue May 26 2009, 11:27AM

 User Offline
Registered Member #14254
Joined: Tue Jan 20 2009, 08:14AM

Posts: 620
Thanked 116 times in 112 posts
Yeah, if your circuit looks like this then you'll need to connect the grounds together...





-Dave
"Basic research is what I am doing when I don't know what I am doing"
Back to top

Go to page       >>   

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System

8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems