avr automatic turn off power mains using timer

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

****************deleted the old code******************

 

I just wrote another code for atmega16 and i am using 555 timer so tat i dont hav to worry about complex looping and pls let me know if i hav to add anything extra. Thank you

 

#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 1000000UL

int count=0,start_timer,ir_check,timer_op,OutOfTime;

void timer()
{
	PORTD& = (0x10);	//send 1 to 555 timer
	PORTD& = (0x00);	//send 0
	delay_ms(10);		//delay ms for hold time for 555 to detect trigger
	PORTD& = (0x10);	//send 1
	start_timer = 1;
}

void main()
{

		DDRA=0x00;  //port A input for ir sensor values
		DDRD=0xff;	//for a relay which is used to switch from vcc to ground and vice versa
		PORTD& = (0x20);  //trig pin is made high
		while(1)
		{
			ir_check = PINA & (0x07); //to read which ir is read and count is manipulated based on tat
			OutOfTime=PINA & (0x08); // to read if 555 delay of X sec is over and output of is low
			if((ir_check==1)||(ir_check==5)){
				count+=1;  //1 person entered
			}else
			if((ir_check==2)||(ir_check==6)){
				count-=1;	//1 person left
			}
			else
			{
			}
			if((count==0)&&(start_timer==0)){
				timer();  //none inside so start timer 
			} 
			else
			{
				PORTD& = (0x20);//relay on
			}
			
			if(count!=0)
			{
				start_timer=0; //if someone entered then it should not turn off relay
			}
			
			if((OutOfTime==0)&&(count==0)&&(start_timer==1))
			{
				PORTD& = ~(0x20);   //relay off
				start_timer=0;     
			}
			delay_ms(100);
		}
	
}
		
	

 

Last Edited: Tue. Sep 19, 2017 - 08:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Someone appears to have stolen your [Tab] key ?

 

You may want to try again with:

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ya thx :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
PORTA|=(0<<relay);

 

 I don't have any idea what you mean by monitoring sensors while timer is running, but let's start with the quoted line above:  ORing anything with 0 does nothing.  See http://www.avrfreaks.net/forum/t...

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
		while(1)
		{
			while ((TIFR & 0x01) == 0);
			TCNT0 = 0x00;
			TIFR=0x01;			//clear timer1 overflow flag
			count++;
		
			if (count>=38)  //10ms timer
			{
				PORTA|=(0<<relay);
				count=0;
				outOfTime=1;
			}
		
		}

Once you get into that loop how do you ever get out again? It's all very well having a while(1) in a called function but you need some kind of "break;" condition to eventually exit from it. as it stands the while(1) back in main, where you check for outOfTime==1 will never be reached as it can never return from timer().

 

This code has the look of something that has "grown organically" rather than an implementation from design. Software is best written by first planning out what you want to achieve and then just coding from that design. You will already have thought abouut "how do I get out of the infinite while(1)?" back at the design stage so the code implementation then becomes trivial.

 

If you do it the way you appear to here, adding bits as you think of them, you will just tie yourself up in all kinds of knots and it's very unlikely your code will have a good structure.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Another basic error:

	y= PINA & (0x02);
...
    if((x==0)&&(y==1))

y can never be 1.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

oh ya thx, tat was my mistake. the thing i want to achieve is when ther is 0 count the timer starts counting. Now while the timer is running i must monitor if someone entered and if someone did then i must stop timer and again count people like before

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i should check if y==4 right ? or just right shift twice and then compare it with 1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

prostriker wrote:
i should check if y==4 right ?

Think about what you just wrote, in light of

 

sternst wrote:
y= PINA & (0x02);

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are still making a mistake here. Don't just throw random C code at your editor and hope that something sticks!

 

Go back and think about the overall goal. What are the steps required to achieve that goal? Break that down into more and more detail and then you have an exact specification for what the C needs to do. Then write the C to match that requirement/specification.

 

Sure there might be some bits of this existing code you could re-use or perhaps you could just call it a "prototype" where you have tested out some things (like how to read an input or start a timer) but your best solution is going to come from thinking about the overall goal, designing a solution and then implementing that from scratch. You aren't far enough into what you have got so far to have to worry about abandoning it.

 

If you continue to try and patch up your existing solution with string and sealing wax don't be surprised if it falls in a heap later!

 

BTW don't call variables things like a, b, x, y unless those names have a true significance. Sure maybe x and y represent perpendicular sensors so I guess they may be OK names for variables but I doubt 'a' or 'b' are ever going to tell you very much about the information they contain from their names.

 

Oh and don't do this:

#define relay 6

Programmers expect names in lower or mixed case to be C variable names. To differentiate macro defines it is usual to use all upper case so:

#define RELAY 6

a reader will now recognize this as a defined macro name. Also the use of that seems to be in:

			PORTA|=(0<<relay);

You've already been told why that won't do what you think. But also you have:

	DDRA=0x00;  //port A input for ir sensor values

So even if you had used:

			PORTA &= ~(1 << RELAY);

(which is the correct way to set the bit to 0) then it's not going to drive an output low because you have not set bit 6 of DDRA to be an output.

 

As I say you need to design this and then things like setting the right pins as output will just come out of your design and will be easy to code in C.

 

PS forgot to say that white space in C cost nothing (apart from a quick stab of the Space Bar) but helps to make the code easier to read. Get into the habit of putting spaces on each side of C operators like |=, << and so on.

Last Edited: Mon. Sep 18, 2017 - 03:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

prostriker wrote:
i am using 555 timer so tat i dont hav to worry about complex looping

This sounds like the most perfect excuse to stay ignorant for the rest of your life. (Well ment: Prove you are not ignorant / stupd)

 

There is a little bit of complexity and thought needed to let a uC do multiple things seemingly "at once" but you will need this for almost any non-trivial project.

So sit down and learn the (now seemingly complex) task of making your uC do that.

Once you've mastered the technique it is trivial to apply to lots of different projects and simple projects such as tese are the best to learn such things.

 

When I glance at your progam, then "state machines" should probably also be high on list of "to learn" stuff...

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well I had written d code using d internal timer but had a lot of issues as it was my first attempt to code for mc . Ur words triggered me in a positive way :) (honestly). I am having a mini open house expo tis weekend n tat restricted me to not try learn is NW as I am doing many other projects parallel . But I will surely post d code after learning how to do it :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just a few helpful (I hope) thoughts for you.  Figure out how to make a 1-second timer, either entirely with a built-in hardware timer, or with a built-in timer in combination with software counting (e.g. a 10ms hardware timer tick, counted 100 times to give 1 second).  Then figure out how to use that 1 second tick to do your exit delay.  Hint: if you use timer interrupts (and at some point you should learn to use them) you'll need to understand both the 'volatile' keyword, and 'atomic' accesses to data accessed both in your ISR and outside of it.